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,6 +1,9 @@
import {
type NoteBlockModel,
CodeBlockModel,
ListBlockModel,
NoteBlockModel,
NoteDisplayMode,
ParagraphBlockModel,
type RootBlockModel,
} from '@blocksuite/affine-model';
import { matchFlavours } from '@blocksuite/affine-shared/utils';
@@ -69,7 +72,7 @@ export class DocTitle extends WithDisposable(ShadowlessElement) {
private _getOrCreateFirstPageVisibleNote() {
const note = this._rootModel.children.find(
(child): child is NoteBlockModel =>
matchFlavours(child, ['affine:note']) &&
matchFlavours(child, [NoteBlockModel]) &&
child.displayMode !== NoteDisplayMode.EdgelessOnly
);
if (note) return note;
@@ -102,7 +105,11 @@ export class DocTitle extends WithDisposable(ShadowlessElement) {
const note = this._getOrCreateFirstPageVisibleNote();
const firstText = note?.children.find(block =>
matchFlavours(block, ['affine:paragraph', 'affine:list', 'affine:code'])
matchFlavours(block, [
ParagraphBlockModel,
ListBlockModel,
CodeBlockModel,
])
);
if (firstText) {
if (this._std) focusTextModel(this._std, firstText.id);
@@ -1,3 +1,4 @@
import { NoteBlockModel } from '@blocksuite/affine-model';
import {
calcDropTarget,
type DropTarget,
@@ -11,6 +12,7 @@ import {
type EditorHost,
LifeCycleWatcher,
} from '@blocksuite/block-std';
import { SurfaceBlockModel } from '@blocksuite/block-std/gfx';
import { createIdentifier } from '@blocksuite/global/di';
import type { IVec } from '@blocksuite/global/utils';
import { Point, throttle } from '@blocksuite/global/utils';
@@ -57,7 +59,7 @@ export class FileDropExtension extends LifeCycleWatcher {
const model = element.model;
const parent = this.std.store.getParent(model);
if (!matchFlavours(parent, ['affine:surface' as BlockSuite.Flavour])) {
if (!matchFlavours(parent, [SurfaceBlockModel])) {
const point = this.point$.value;
target = point && calcDropTarget(point, model, element);
}
@@ -73,7 +75,7 @@ export class FileDropExtension extends LifeCycleWatcher {
if (!rootModel) return null;
let lastNote = rootModel.children[rootModel.children.length - 1];
if (!lastNote || !matchFlavours(lastNote, ['affine:note'])) {
if (!lastNote || !matchFlavours(lastNote, [NoteBlockModel])) {
const newNoteId = this.doc.addBlock('affine:note', {}, rootModel.id);
const newNote = this.doc.getBlock(newNoteId)?.model;
if (!newNote) return null;
@@ -1,3 +1,4 @@
import { DatabaseBlockModel } from '@blocksuite/affine-model';
import {
asyncGetBlockComponent,
getCurrentNativeRange,
@@ -41,7 +42,7 @@ export function getInlineEditorByModel(
typeof model === 'string'
? editorHost.std.store.getBlock(model)?.model
: model;
if (!blockModel || matchFlavours(blockModel, ['affine:database'])) {
if (!blockModel || matchFlavours(blockModel, [DatabaseBlockModel])) {
// Not support database model since it's may be have multiple inline editor instances.
// Support to enter the editing state through the Enter key in the database.
return null;
@@ -1,3 +1,4 @@
import { RootBlockModel } from '@blocksuite/affine-model';
import { matchFlavours } from '@blocksuite/affine-shared/utils';
import { type Command, TextSelection } from '@blocksuite/block-std';
import type { Text } from '@blocksuite/store';
@@ -24,7 +25,7 @@ export const deleteTextCommand: Command<{
if (!fromElement) return;
let fromText: Text | undefined;
if (matchFlavours(fromElement.model, ['affine:page'])) {
if (matchFlavours(fromElement.model, [RootBlockModel])) {
fromText = fromElement.model.title;
} else {
fromText = fromElement.model.text;
@@ -1,3 +1,4 @@
import { CodeBlockModel } from '@blocksuite/affine-model';
import { BRACKET_PAIRS } from '@blocksuite/affine-shared/consts';
import {
createDefaultDoc,
@@ -28,7 +29,7 @@ export const bracketKeymap = (
if (!textSelection) return;
const model = doc.getBlock(textSelection.from.blockId)?.model;
if (!model) return;
if (!matchFlavours(model, ['affine:code'])) return;
if (!matchFlavours(model, [CodeBlockModel])) return;
const inlineEditor = getInlineEditorByModel(
std.host,
textSelection.from.blockId
@@ -55,7 +56,7 @@ export const bracketKeymap = (
const model = doc.getBlock(textSelection.from.blockId)?.model;
if (!model) return;
const isCodeBlock = matchFlavours(model, ['affine:code']);
const isCodeBlock = matchFlavours(model, [CodeBlockModel]);
// When selection is collapsed, only trigger auto complete in code block
if (textSelection.isCollapsed() && !isCodeBlock) return;
if (!textSelection.isInSameBlock()) return;
@@ -1,3 +1,7 @@
import {
DividerBlockModel,
ParagraphBlockModel,
} from '@blocksuite/affine-model';
import { matchFlavours } from '@blocksuite/affine-shared/utils';
import type { BlockStdScope } from '@blocksuite/block-std';
import type { BlockModel } from '@blocksuite/store';
@@ -12,8 +16,8 @@ export function toDivider(
) {
const { store: doc } = std;
if (
matchFlavours(model, ['affine:divider']) ||
(matchFlavours(model, ['affine:paragraph']) && model.type === 'quote')
matchFlavours(model, [DividerBlockModel]) ||
(matchFlavours(model, [ParagraphBlockModel]) && model.type === 'quote')
) {
return;
}
@@ -1,4 +1,8 @@
import type { ListProps, ListType } from '@blocksuite/affine-model';
import {
type ListProps,
type ListType,
ParagraphBlockModel,
} from '@blocksuite/affine-model';
import { matchFlavours, toNumberedList } from '@blocksuite/affine-shared/utils';
import type { BlockStdScope } from '@blocksuite/block-std';
import type { BlockModel } from '@blocksuite/store';
@@ -13,7 +17,7 @@ export function toList(
prefix: string,
otherProperties?: Partial<ListProps>
) {
if (!matchFlavours(model, ['affine:paragraph'])) {
if (!matchFlavours(model, [ParagraphBlockModel])) {
return;
}
const { store: doc } = std;
@@ -1,3 +1,4 @@
import { CodeBlockModel, ParagraphBlockModel } from '@blocksuite/affine-model';
import {
isMarkdownPrefix,
matchFlavours,
@@ -31,10 +32,10 @@ export function markdownInput(
const prefixText = getPrefixText(inline);
if (!isMarkdownPrefix(prefixText)) return;
const isParagraph = matchFlavours(model, ['affine:paragraph']);
const isParagraph = matchFlavours(model, [ParagraphBlockModel]);
const isHeading = isParagraph && model.type.startsWith('h');
const isParagraphQuoteBlock = isParagraph && model.type === 'quote';
const isCodeBlock = matchFlavours(model, ['affine:code']);
const isCodeBlock = matchFlavours(model, [CodeBlockModel]);
if (isHeading || isParagraphQuoteBlock || isCodeBlock) return;
const lineInfo = inline.getLine(range.index);
@@ -1,4 +1,7 @@
import type { ParagraphType } from '@blocksuite/affine-model';
import {
ParagraphBlockModel,
type ParagraphType,
} from '@blocksuite/affine-model';
import { matchFlavours } from '@blocksuite/affine-shared/utils';
import type { BlockStdScope } from '@blocksuite/block-std';
import type { BlockModel } from '@blocksuite/store';
@@ -13,7 +16,7 @@ export function toParagraph(
prefix: string
) {
const { store: doc } = std;
if (!matchFlavours(model, ['affine:paragraph'])) {
if (!matchFlavours(model, [ParagraphBlockModel])) {
const parent = doc.getParent(model);
if (!parent) return;
@@ -33,7 +36,7 @@ export function toParagraph(
return id;
}
if (matchFlavours(model, ['affine:paragraph']) && model.type !== type) {
if (matchFlavours(model, [ParagraphBlockModel]) && model.type !== type) {
beforeConvert(std, model, prefix.length);
doc.updateBlock(model, { type });
@@ -1,3 +1,4 @@
import { ParagraphBlockModel } from '@blocksuite/affine-model';
import { matchFlavours } from '@blocksuite/affine-shared/utils';
import type { BlockStdScope } from '@blocksuite/block-std';
import type { BlockModel } from '@blocksuite/store';
@@ -10,7 +11,7 @@ export function toCode(
prefixText: string,
language: string | null
) {
if (matchFlavours(model, ['affine:paragraph']) && model.type === 'quote') {
if (matchFlavours(model, [ParagraphBlockModel]) && model.type === 'quote') {
return;
}