mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-08-09 13:15:51 +08:00
205cd7a86d
Closes: BS-2946
50 lines
1.3 KiB
TypeScript
50 lines
1.3 KiB
TypeScript
import {
|
|
ParagraphBlockModel,
|
|
type ParagraphType,
|
|
} from '@blocksuite/affine-model';
|
|
import { matchModels } from '@blocksuite/affine-shared/utils';
|
|
import type { BlockStdScope } from '@blocksuite/std';
|
|
import type { BlockModel } from '@blocksuite/store';
|
|
|
|
import { focusTextModel } from '../dom.js';
|
|
import { beforeConvert } from './utils.js';
|
|
|
|
export function toParagraph(
|
|
std: BlockStdScope,
|
|
model: BlockModel,
|
|
type: ParagraphType,
|
|
prefix: string
|
|
) {
|
|
const { store: doc } = std;
|
|
if (!matchModels(model, [ParagraphBlockModel])) {
|
|
const parent = doc.getParent(model);
|
|
if (!parent) return;
|
|
|
|
const index = parent.children.indexOf(model);
|
|
|
|
beforeConvert(std, model, prefix.length);
|
|
|
|
const blockProps = {
|
|
type: type,
|
|
text: model.text?.clone(),
|
|
children: model.children,
|
|
};
|
|
doc.deleteBlock(model, { deleteChildren: false });
|
|
const id = doc.addBlock('affine:paragraph', blockProps, parent, index);
|
|
|
|
focusTextModel(std, id);
|
|
return id;
|
|
}
|
|
|
|
if (matchModels(model, [ParagraphBlockModel]) && model.props.type !== type) {
|
|
beforeConvert(std, model, prefix.length);
|
|
|
|
doc.updateBlock(model, { type });
|
|
|
|
focusTextModel(std, model.id);
|
|
}
|
|
|
|
// If the model is already a paragraph with the same type, do nothing
|
|
return model.id;
|
|
}
|