mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-08-23 13:02:21 +08:00
1f45cc5dec
**Directory Structure Changes** - Renamed multiple block-related directories by removing the "block-" prefix: - `block-attachment` → `attachment` - `block-bookmark` → `bookmark` - `block-callout` → `callout` - `block-code` → `code` - `block-data-view` → `data-view` - `block-database` → `database` - `block-divider` → `divider` - `block-edgeless-text` → `edgeless-text` - `block-embed` → `embed`
57 lines
1.2 KiB
TypeScript
57 lines
1.2 KiB
TypeScript
import { focusTextModel } from '@blocksuite/affine-rich-text';
|
|
import { type Command, TextSelection } from '@blocksuite/std';
|
|
|
|
/**
|
|
* Add a paragraph next to the current block.
|
|
*/
|
|
export const addParagraphCommand: Command<
|
|
{
|
|
blockId?: string;
|
|
},
|
|
{
|
|
paragraphConvertedId: string;
|
|
}
|
|
> = (ctx, next) => {
|
|
const { std } = ctx;
|
|
const { store, selection } = std;
|
|
store.captureSync();
|
|
|
|
let blockId = ctx.blockId;
|
|
if (!blockId) {
|
|
const text = selection.find(TextSelection);
|
|
blockId = text?.blockId;
|
|
}
|
|
if (!blockId) return;
|
|
|
|
const model = store.getBlock(blockId)?.model;
|
|
if (!model) return;
|
|
|
|
let id: string;
|
|
if (model.children.length > 0) {
|
|
// before:
|
|
// aaa|
|
|
// bbb
|
|
//
|
|
// after:
|
|
// aaa
|
|
// |
|
|
// bbb
|
|
id = store.addBlock('affine:paragraph', {}, model, 0);
|
|
} else {
|
|
const parent = store.getParent(model);
|
|
if (!parent) return;
|
|
const index = parent.children.indexOf(model);
|
|
if (index < 0) return;
|
|
// before:
|
|
// aaa|
|
|
//
|
|
// after:
|
|
// aaa
|
|
// |
|
|
id = store.addBlock('affine:paragraph', {}, parent, index + 1);
|
|
}
|
|
|
|
focusTextModel(std, id);
|
|
return next({ paragraphConvertedId: id });
|
|
};
|