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
@@ -3,6 +3,11 @@ import {
focusTextModel,
onModelTextUpdated,
} from '@blocksuite/affine-components/rich-text';
import {
CodeBlockModel,
ListBlockModel,
ParagraphBlockModel,
} from '@blocksuite/affine-model';
import {
getBlockSelectionsCommand,
getSelectedBlocksCommand,
@@ -196,9 +201,9 @@ export const updateBlockType: Command<
blockModels.forEach(model => {
if (
!matchFlavours(model, [
'affine:paragraph',
'affine:list',
'affine:code',
ParagraphBlockModel,
ListBlockModel,
CodeBlockModel,
])
) {
return;
@@ -1,4 +1,4 @@
import { NoteDisplayMode } from '@blocksuite/affine-model';
import { NoteBlockModel, NoteDisplayMode } from '@blocksuite/affine-model';
import { matchFlavours } from '@blocksuite/affine-shared/utils';
import type { Command } from '@blocksuite/block-std';
@@ -10,7 +10,7 @@ export const changeNoteDisplayMode: Command<{
const { std, noteId, mode, stopCapture } = ctx;
const noteBlockModel = std.store.getBlock(noteId)?.model;
if (!noteBlockModel || !matchFlavours(noteBlockModel, ['affine:note']))
if (!noteBlockModel || !matchFlavours(noteBlockModel, [NoteBlockModel]))
return;
const currentMode = noteBlockModel.displayMode;
@@ -27,7 +27,7 @@ export const changeNoteDisplayMode: Command<{
const parent = std.store.getParent(noteBlockModel);
if (parent) {
const notes = parent.children.filter(child =>
matchFlavours(child, ['affine:note'])
matchFlavours(child, [NoteBlockModel])
);
const firstEdgelessOnlyNote = notes.find(
note => note.displayMode === NoteDisplayMode.EdgelessOnly
@@ -1,3 +1,4 @@
import { NoteBlockModel } from '@blocksuite/affine-model';
import { matchFlavours } from '@blocksuite/affine-shared/utils';
import type { Command } from '@blocksuite/block-std';
@@ -20,7 +21,7 @@ export const dedentBlockToRoot: Command<{
let parent = store.getParent(model);
let changed = false;
while (parent && !matchFlavours(parent, ['affine:note'])) {
while (parent && !matchFlavours(parent, [NoteBlockModel])) {
if (!changed) {
if (stopCapture) store.captureSync();
changed = true;
@@ -1,3 +1,4 @@
import { ParagraphBlockModel } from '@blocksuite/affine-model';
import {
calculateCollapsedSiblings,
matchFlavours,
@@ -45,7 +46,7 @@ export const dedentBlock: Command<{
if (stopCapture) store.captureSync();
if (
matchFlavours(model, ['affine:paragraph']) &&
matchFlavours(model, [ParagraphBlockModel]) &&
model.type.startsWith('h') &&
model.collapsed
) {
@@ -1,3 +1,4 @@
import { NoteBlockModel } from '@blocksuite/affine-model';
import { matchFlavours } from '@blocksuite/affine-shared/utils';
import { type Command, TextSelection } from '@blocksuite/block-std';
@@ -30,7 +31,7 @@ export const dedentBlocksToRoot: Command<{
for (let i = blockIds.length - 1; i >= 0; i--) {
const model = blockIds[i];
const parent = store.getParent(model);
if (parent && !matchFlavours(parent, ['affine:note'])) {
if (parent && !matchFlavours(parent, [NoteBlockModel])) {
std.command.exec(dedentBlockToRoot, {
blockId: model,
stopCapture: false,
@@ -1,3 +1,4 @@
import { ParagraphBlockModel } from '@blocksuite/affine-model';
import {
calculateCollapsedSiblings,
matchFlavours,
@@ -57,7 +58,7 @@ export const dedentBlocks: Command<{
const model = store.getBlock(id)?.model;
if (!model) return;
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 {
calculateCollapsedSiblings,
matchFlavours,
@@ -50,7 +50,7 @@ export const indentBlock: Command<{
if (stopCapture) store.captureSync();
if (
matchFlavours(model, ['affine:paragraph']) &&
matchFlavours(model, [ParagraphBlockModel]) &&
model.type.startsWith('h') &&
model.collapsed
) {
@@ -62,12 +62,12 @@ export const indentBlock: 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>);
});
}
return next();
@@ -1,3 +1,4 @@
import { ParagraphBlockModel } from '@blocksuite/affine-model';
import {
calculateCollapsedSiblings,
getNearestHeadingBefore,
@@ -58,7 +59,7 @@ export const indentBlocks: Command<{
const model = store.getBlock(id)?.model;
if (!model) return;
if (
matchFlavours(model, ['affine:paragraph']) &&
matchFlavours(model, [ParagraphBlockModel]) &&
model.type.startsWith('h') &&
model.collapsed
) {
@@ -83,12 +84,10 @@ export const indentBlocks: Command<{
const nearestHeading = getNearestHeadingBefore(firstModel);
if (
nearestHeading &&
matchFlavours(nearestHeading, ['affine:paragraph']) &&
matchFlavours(nearestHeading, [ParagraphBlockModel]) &&
nearestHeading.collapsed
) {
store.updateBlock(nearestHeading, {
collapsed: false,
});
store.updateBlock(nearestHeading, { collapsed: false });
}
}
@@ -106,12 +105,10 @@ export const indentBlocks: Command<{
const nearestHeading = getNearestHeadingBefore(firstModel);
if (
nearestHeading &&
matchFlavours(nearestHeading, ['affine:paragraph']) &&
matchFlavours(nearestHeading, [ParagraphBlockModel]) &&
nearestHeading.collapsed
) {
store.updateBlock(nearestHeading, {
collapsed: false,
});
store.updateBlock(nearestHeading, { collapsed: false });
}
}
@@ -1,6 +1,8 @@
import {
DefaultTheme,
ListBlockModel,
NoteBlockModel,
ParagraphBlockModel,
StrokeStyle,
} from '@blocksuite/affine-model';
import { ThemeProvider } from '@blocksuite/affine-shared/services';
@@ -98,10 +100,10 @@ export class EdgelessNoteBackground extends SignalWatcher(
if (
(!nearestModel.text ||
!matchFlavours(nearestModel, ['affine:paragraph', 'affine:list'])) &&
!matchFlavours(nearestModel, [ParagraphBlockModel, ListBlockModel])) &&
(!siblingModel ||
!siblingModel.text ||
!matchFlavours(siblingModel, ['affine:paragraph', 'affine:list']))
!matchFlavours(siblingModel, [ParagraphBlockModel, ListBlockModel]))
) {
const [pId] = this.doc.addSiblingBlocks(
nearestModel,
@@ -1,5 +1,11 @@
import { textConversionConfigs } from '@blocksuite/affine-components/rich-text';
import { NoteBlockSchema } from '@blocksuite/affine-model';
import {
CodeBlockModel,
ListBlockModel,
NoteBlockModel,
NoteBlockSchema,
ParagraphBlockModel,
} from '@blocksuite/affine-model';
import {
getBlockSelectionsCommand,
getNextBlockCommand,
@@ -149,7 +155,7 @@ export class NoteBlockService extends BlockService {
const doc = this._std.store;
let parent = doc.getBlock(blockId)?.model ?? null;
while (parent) {
if (matchFlavours(parent, [NoteBlockSchema.model.flavour])) {
if (matchFlavours(parent, [NoteBlockModel])) {
return parent;
}
parent = doc.getParent(parent);
@@ -189,9 +195,9 @@ export class NoteBlockService extends BlockService {
if (
!matchFlavours(nextBlock.model, [
'affine:paragraph',
'affine:list',
'affine:code',
ParagraphBlockModel,
ListBlockModel,
CodeBlockModel,
])
) {
this._std.command.exec(selectBlock, {
@@ -225,9 +231,9 @@ export class NoteBlockService extends BlockService {
event.preventDefault();
if (
matchFlavours(nextBlock.model, [
'affine:paragraph',
'affine:list',
'affine:code',
ParagraphBlockModel,
ListBlockModel,
CodeBlockModel,
])
) {
this._std.command.exec(focusBlockStart, {
@@ -279,9 +285,9 @@ export class NoteBlockService extends BlockService {
if (
!matchFlavours(prevBlock.model, [
'affine:paragraph',
'affine:list',
'affine:code',
ParagraphBlockModel,
ListBlockModel,
CodeBlockModel,
])
) {
this._std.command.exec(selectBlock, {
@@ -313,9 +319,9 @@ export class NoteBlockService extends BlockService {
if (
matchFlavours(prevBlock.model, [
'affine:paragraph',
'affine:list',
'affine:code',
ParagraphBlockModel,
ListBlockModel,
CodeBlockModel,
])
) {
event.preventDefault();
+2 -2
View File
@@ -1,4 +1,4 @@
import { type NoteBlockModel, NoteDisplayMode } from '@blocksuite/affine-model';
import { NoteBlockModel, NoteDisplayMode } from '@blocksuite/affine-model';
import { FeatureFlagService } from '@blocksuite/affine-shared/services';
import { matchFlavours } from '@blocksuite/affine-shared/utils';
import type { BlockStdScope } from '@blocksuite/block-std';
@@ -11,7 +11,7 @@ export function isPageBlock(std: BlockStdScope, note: NoteBlockModel) {
std.get(FeatureFlagService).getFlag('enable_page_block') &&
note.parent?.children.find(
child =>
matchFlavours(child, ['affine:note']) &&
matchFlavours(child, [NoteBlockModel]) &&
child.displayMode !== NoteDisplayMode.EdgelessOnly
) === note
);