Files
AFFiNE-Mirror/blocksuite/affine/blocks/note/src/quick-action.ts
T
L-Sun 9dbdd4b7ba 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图标也更换一下)
2025-04-23 05:56:23 +00:00

67 lines
1.7 KiB
TypeScript

import {
convertSelectedBlocksToLinkedDoc,
getTitleFromSelectedModels,
notifyDocCreated,
promptDocTitle,
} from '@blocksuite/affine-block-embed';
import {
draftSelectedModelsCommand,
getSelectedModelsCommand,
} from '@blocksuite/affine-shared/commands';
import type { BlockStdScope } from '@blocksuite/std';
import { toDraftModel } from '@blocksuite/store';
export interface QuickActionConfig {
id: string;
hotkey?: string;
showWhen: (std: BlockStdScope) => boolean;
action: (std: BlockStdScope) => void;
}
export const quickActionConfig: QuickActionConfig[] = [
{
id: 'convert-to-linked-doc',
hotkey: `Mod-Shift-l`,
showWhen: std => {
const [_, ctx] = std.command.exec(getSelectedModelsCommand, {
types: ['block'],
});
const { selectedModels } = ctx;
return !!selectedModels && selectedModels.length > 0;
},
action: std => {
const [_, ctx] = std.command
.chain()
.pipe(getSelectedModelsCommand, {
types: ['block'],
mode: 'flat',
})
.pipe(draftSelectedModelsCommand)
.run();
const { selectedModels, draftedModels } = ctx;
if (!selectedModels) return;
if (!selectedModels.length || !draftedModels) return;
std.selection.clear();
const doc = std.store;
const autofill = getTitleFromSelectedModels(
selectedModels.map(toDraftModel)
);
promptDocTitle(std, autofill)
.then(title => {
if (title === null) return;
convertSelectedBlocksToLinkedDoc(
std,
doc,
draftedModels,
title
).catch(console.error);
notifyDocCreated(std);
})
.catch(console.error);
},
},
];