Files
AFFiNE-Mirror/blocksuite/affine/block-paragraph/src/commands/append-paragraph.ts
T

31 lines
783 B
TypeScript

import { focusTextModel } from '@blocksuite/affine-components/rich-text';
import { getLastNoteBlock } from '@blocksuite/affine-shared/utils';
import type { Command } from '@blocksuite/block-std';
import { Text } from '@blocksuite/store';
/**
* Append a paragraph block at the end of the whole page.
*/
export const appendParagraphCommand: Command<{ text?: string }> = (
ctx,
next
) => {
const { std, text = '' } = ctx;
const { store } = std;
if (!store.root) return;
const note = getLastNoteBlock(store);
let noteId = note?.id;
if (!noteId) {
noteId = store.addBlock('affine:note', {}, store.root.id);
}
const id = store.addBlock(
'affine:paragraph',
{ text: new Text(text) },
noteId
);
focusTextModel(std, id, text.length);
next();
};