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,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, {