refactor(editor): add helper function for undo notification (#11903)

### What Changes
- Refactors the `notify` function call with undo button. It is called `notifyWithUndo`, which can  associate the visibility of the `NotificationCard` with the top undo item in history stack, such as undo by shortcut `Cmd+Z`.
- change icon of the "Insert into page" button. Close [BS-3267](https://linear.app/affine-design/issue/BS-3267/frame和group的insert-into-page图标也更换一下)
This commit is contained in:
L-Sun
2025-04-23 02:43:56 +00:00
committed by L-Sun
parent 27ff9ab9f4
commit 9dbdd4b7ba
14 changed files with 136 additions and 157 deletions

View File

@@ -1,52 +1,22 @@
import { NotificationProvider } from '@blocksuite/affine-shared/services';
import { type BlockStdScope, EditorLifeCycleExtension } from '@blocksuite/std';
import { type BlockStdScope } from '@blocksuite/std';
import { toast } from '../toast/toast.js';
function notify(std: BlockStdScope, title: string, message: string) {
const notification = std.getOptional(NotificationProvider);
const { store: doc, host } = std;
const { host } = std;
if (!notification) {
toast(host, title);
return;
}
const abortController = new AbortController();
const clear = () => {
doc.history.off('stack-item-added', addHandler);
doc.history.off('stack-item-popped', popHandler);
disposable.unsubscribe();
};
const closeNotify = () => {
abortController.abort();
clear();
};
// edit or undo or switch doc, close notify toast
const addHandler = doc.history.on('stack-item-added', closeNotify);
const popHandler = doc.history.on('stack-item-popped', closeNotify);
const disposable = host.std
.get(EditorLifeCycleExtension)
.slots.unmounted.subscribe(closeNotify);
notification.notify({
notification.notifyWithUndoAction({
title,
message,
accent: 'info',
duration: 10 * 1000,
actions: [
{
key: 'undo',
label: 'Undo',
onClick: () => {
doc.undo();
clear();
},
},
],
abort: abortController.signal,
onClose: clear,
});
}