feat(editor): type safe draft model and transformer (#10486)

This commit is contained in:
Saul-Mirone
2025-02-27 09:19:49 +00:00
parent 272d41e32d
commit 4c736bc190
15 changed files with 125 additions and 48 deletions
@@ -1,4 +1,4 @@
import { RootBlockModel } from '@blocksuite/affine-model';
import type { RootBlockModel } from '@blocksuite/affine-model';
import {
type BlockStdScope,
type EditorHost,
@@ -12,7 +12,9 @@ import type {
TransformerSlots,
} from '@blocksuite/store';
import { matchModels } from '../../utils';
const isRootDraftModel = (
model: DraftModel
): model is DraftModel<RootBlockModel> => model.flavour === 'affine:root';
const handlePoint = (
point: TextRangePoint,
@@ -20,7 +22,7 @@ const handlePoint = (
model: DraftModel
) => {
const { index, length } = point;
if (matchModels(model, [RootBlockModel])) {
if (isRootDraftModel(model)) {
if (length === 0) return;
(snapshot.props.title as Record<string, unknown>).delta =
model.title.sliceToDelta(index, length + index);
@@ -11,7 +11,7 @@ type ModelList<T> =
export function matchModels<
const Model extends ConstructorType<BlockModel>[],
U extends ModelList<Model>[number] = ModelList<Model>[number],
>(model: unknown, expected: Model): model is U {
>(model: BlockModel | null, expected: Model): model is U {
return (
!!model && expected.some(expectedModel => model instanceof expectedModel)
);