refactor(editor): remove global types in model (#10082)

Closes: [BS-2249](https://linear.app/affine-design/issue/BS-2249/remove-global-types-in-model)

```ts
// before
matchFlavours(model, ['affine:page']);
// after
matchFlavours(model, [PageBlockModel]);
```
This commit is contained in:
Saul-Mirone
2025-02-11 08:18:57 +00:00
parent 64bb6c5a71
commit 652865c7cf
97 changed files with 492 additions and 323 deletions
@@ -1,3 +1,4 @@
import { ParagraphBlockModel } from '@blocksuite/affine-model';
import type { IndentContext } from '@blocksuite/affine-shared/types';
import {
calculateCollapsedSiblings,
@@ -34,7 +35,7 @@ export const canDedentParagraphCommand: Command<
}
const model = store.getBlock(blockId)?.model;
if (!model || !matchFlavours(model, ['affine:paragraph'])) {
if (!model || !matchFlavours(model, [ParagraphBlockModel])) {
return;
}
@@ -88,7 +89,7 @@ export const dedentParagraphCommand: Command<{
store.captureSync();
if (
matchFlavours(model, ['affine:paragraph']) &&
matchFlavours(model, [ParagraphBlockModel]) &&
model.type.startsWith('h') &&
model.collapsed
) {
@@ -1,4 +1,4 @@
import type { ListBlockModel } from '@blocksuite/affine-model';
import { ListBlockModel, ParagraphBlockModel } from '@blocksuite/affine-model';
import type { IndentContext } from '@blocksuite/affine-shared/types';
import {
calculateCollapsedSiblings,
@@ -37,7 +37,7 @@ export const canIndentParagraphCommand: Command<
}
const model = std.store.getBlock(blockId)?.model;
if (!model || !matchFlavours(model, ['affine:paragraph'])) {
if (!model || !matchFlavours(model, [ParagraphBlockModel])) {
return;
}
@@ -95,7 +95,7 @@ export const indentParagraphCommand: Command<{
const nearestHeading = getNearestHeadingBefore(model);
if (
nearestHeading &&
matchFlavours(nearestHeading, ['affine:paragraph']) &&
matchFlavours(nearestHeading, [ParagraphBlockModel]) &&
nearestHeading.collapsed
) {
store.updateBlock(nearestHeading, {
@@ -105,7 +105,7 @@ export const indentParagraphCommand: Command<{
}
if (
matchFlavours(model, ['affine:paragraph']) &&
matchFlavours(model, [ParagraphBlockModel]) &&
model.type.startsWith('h') &&
model.collapsed
) {
@@ -124,7 +124,7 @@ export const indentParagraphCommand: Command<{
const nearestHeading = getNearestHeadingBefore(model);
if (
nearestHeading &&
matchFlavours(nearestHeading, ['affine:paragraph']) &&
matchFlavours(nearestHeading, [ParagraphBlockModel]) &&
nearestHeading.collapsed
) {
store.updateBlock(nearestHeading, {
@@ -135,12 +135,12 @@ export const indentParagraphCommand: Command<{
// update collapsed state of affine list
if (
matchFlavours(previousSibling, ['affine:list']) &&
matchFlavours(previousSibling, [ListBlockModel]) &&
previousSibling.collapsed
) {
store.updateBlock(previousSibling, {
collapsed: false,
} as Partial<ListBlockModel>);
});
}
const textSelection = selection.find(TextSelection);
@@ -2,6 +2,7 @@ import {
focusTextModel,
getInlineEditorByModel,
} from '@blocksuite/affine-components/rich-text';
import { ParagraphBlockModel } from '@blocksuite/affine-model';
import { matchFlavours } from '@blocksuite/affine-shared/utils';
import { type Command, TextSelection } from '@blocksuite/block-std';
@@ -23,7 +24,7 @@ export const splitParagraphCommand: Command<
if (!blockId) return;
const model = store.getBlock(blockId)?.model;
if (!model || !matchFlavours(model, ['affine:paragraph'])) return;
if (!model || !matchFlavours(model, [ParagraphBlockModel])) return;
const inlineEditor = getInlineEditorByModel(host, model);
const range = inlineEditor?.getInlineRange();
@@ -4,7 +4,10 @@ import {
markdownInput,
textKeymap,
} from '@blocksuite/affine-components/rich-text';
import { ParagraphBlockSchema } from '@blocksuite/affine-model';
import {
ParagraphBlockModel,
ParagraphBlockSchema,
} from '@blocksuite/affine-model';
import {
calculateCollapsedSiblings,
matchFlavours,
@@ -37,7 +40,7 @@ export const ParagraphKeymapExtension = KeymapExtension(
const { store } = std;
const model = store.getBlock(text.from.blockId)?.model;
if (!model || !matchFlavours(model, ['affine:paragraph'])) return;
if (!model || !matchFlavours(model, [ParagraphBlockModel])) return;
const event = ctx.get('keyboardState').raw;
event.preventDefault();
@@ -68,7 +71,7 @@ export const ParagraphKeymapExtension = KeymapExtension(
const text = std.selection.find(TextSelection);
if (!text) return;
const model = store.getBlock(text.from.blockId)?.model;
if (!model || !matchFlavours(model, ['affine:paragraph'])) return;
if (!model || !matchFlavours(model, [ParagraphBlockModel])) return;
const inlineEditor = getInlineEditorByModel(
std.host,
text.from.blockId
@@ -95,7 +98,7 @@ export const ParagraphKeymapExtension = KeymapExtension(
const text = std.selection.find(TextSelection);
if (!text) return;
const model = store.getBlock(text.from.blockId)?.model;
if (!model || !matchFlavours(model, ['affine:paragraph'])) return;
if (!model || !matchFlavours(model, [ParagraphBlockModel])) return;
const inlineEditor = getInlineEditorByModel(
std.host,
text.from.blockId
@@ -1,4 +1,14 @@
import { EMBED_BLOCK_FLAVOUR_LIST } from '@blocksuite/affine-shared/consts';
import {
AttachmentBlockModel,
BookmarkBlockModel,
CodeBlockModel,
DatabaseBlockModel,
DividerBlockModel,
ImageBlockModel,
ListBlockModel,
ParagraphBlockModel,
} from '@blocksuite/affine-model';
import { EMBED_BLOCK_MODEL_LIST } from '@blocksuite/affine-shared/consts';
import {
getNextContentBlock,
matchFlavours,
@@ -15,31 +25,32 @@ export function forwardDelete(std: BlockStdScope) {
if (!text) return;
const isCollapsed = text.isCollapsed();
const model = store.getBlock(text.from.blockId)?.model;
if (!model || !matchFlavours(model, ['affine:paragraph'])) return;
if (!model || !matchFlavours(model, [ParagraphBlockModel])) return;
const isEnd = isCollapsed && text.from.index === model.text.length;
if (!isEnd) return;
const parent = store.getParent(model);
if (!parent) return;
const nextSibling = store.getNext(model);
const ignoreForwardDeleteFlavourList: BlockSuite.Flavour[] = [
'affine:attachment',
'affine:bookmark',
'affine:database',
'affine:code',
'affine:image',
'affine:divider',
...EMBED_BLOCK_FLAVOUR_LIST,
];
if (matchFlavours(nextSibling, ignoreForwardDeleteFlavourList)) {
if (
matchFlavours(nextSibling, [
AttachmentBlockModel,
BookmarkBlockModel,
DatabaseBlockModel,
CodeBlockModel,
ImageBlockModel,
DividerBlockModel,
...EMBED_BLOCK_MODEL_LIST,
] as const)
) {
std.selection.setGroup('note', [
std.selection.create(BlockSelection, { blockId: nextSibling.id }),
]);
return true;
}
if (nextSibling?.text) {
if (matchFlavours(nextSibling, [ParagraphBlockModel, ListBlockModel])) {
model.text.join(nextSibling.text);
if (nextSibling.children) {
const parent = store.getParent(nextSibling);
@@ -2,8 +2,19 @@ import {
asyncSetInlineRange,
focusTextModel,
} from '@blocksuite/affine-components/rich-text';
import type { RootBlockModel } from '@blocksuite/affine-model';
import { EMBED_BLOCK_FLAVOUR_LIST } from '@blocksuite/affine-shared/consts';
import {
AttachmentBlockModel,
BookmarkBlockModel,
CodeBlockModel,
DatabaseBlockModel,
DividerBlockModel,
EdgelessTextBlockModel,
ImageBlockModel,
ListBlockModel,
ParagraphBlockModel,
type RootBlockModel,
} from '@blocksuite/affine-model';
import { EMBED_BLOCK_MODEL_LIST } from '@blocksuite/affine-shared/consts';
import type { ExtendedModel } from '@blocksuite/affine-shared/types';
import {
focusTitle,
@@ -33,7 +44,7 @@ export function mergeWithPrev(editorHost: EditorHost, model: BlockModel) {
const parent = doc.getParent(model);
if (!parent) return false;
if (matchFlavours(parent, ['affine:edgeless-text'])) {
if (matchFlavours(parent, [EdgelessTextBlockModel])) {
return true;
}
@@ -42,7 +53,7 @@ export function mergeWithPrev(editorHost: EditorHost, model: BlockModel) {
return handleNoPreviousSibling(editorHost, model);
}
if (matchFlavours(prevBlock, ['affine:paragraph', 'affine:list'])) {
if (matchFlavours(prevBlock, [ParagraphBlockModel, ListBlockModel])) {
const modelIndex = parent.children.indexOf(model);
if (
(modelIndex === -1 || modelIndex === parent.children.length - 1) &&
@@ -64,12 +75,12 @@ export function mergeWithPrev(editorHost: EditorHost, model: BlockModel) {
if (
matchFlavours(prevBlock, [
'affine:attachment',
'affine:bookmark',
'affine:code',
'affine:image',
'affine:divider',
...EMBED_BLOCK_FLAVOUR_LIST,
AttachmentBlockModel,
BookmarkBlockModel,
CodeBlockModel,
ImageBlockModel,
DividerBlockModel,
...EMBED_BLOCK_MODEL_LIST,
])
) {
const selection = editorHost.selection.create(BlockSelection, {
@@ -86,7 +97,7 @@ export function mergeWithPrev(editorHost: EditorHost, model: BlockModel) {
return true;
}
if (matchFlavours(parent, ['affine:database'])) {
if (matchFlavours(parent, [DatabaseBlockModel])) {
doc.deleteBlock(model);
focusTextModel(editorHost.std, prevBlock.id, prevBlock.text?.yText.length);
return true;
@@ -104,7 +115,7 @@ function handleNoPreviousSibling(editorHost: EditorHost, model: ExtendedModel) {
// Probably no title, e.g. in edgeless mode
if (!titleEditor) {
if (
matchFlavours(parent, ['affine:edgeless-text']) ||
matchFlavours(parent, [EdgelessTextBlockModel]) ||
model.children.length > 0
) {
doc.deleteBlock(model, {