refactor(editor): remove block models global type (#10086)

This commit is contained in:
Saul-Mirone
2025-02-11 11:00:57 +00:00
parent a725df6ebe
commit 39eb8625d6
157 changed files with 402 additions and 621 deletions
@@ -6,7 +6,7 @@ import {
ParagraphBlockModel,
type RootBlockModel,
} from '@blocksuite/affine-model';
import { matchFlavours } from '@blocksuite/affine-shared/utils';
import { matchModels } from '@blocksuite/affine-shared/utils';
import { ShadowlessElement } from '@blocksuite/block-std';
import { WithDisposable } from '@blocksuite/global/utils';
import type { Store } from '@blocksuite/store';
@@ -72,7 +72,7 @@ export class DocTitle extends WithDisposable(ShadowlessElement) {
private _getOrCreateFirstPageVisibleNote() {
const note = this._rootModel.children.find(
(child): child is NoteBlockModel =>
matchFlavours(child, [NoteBlockModel]) &&
matchModels(child, [NoteBlockModel]) &&
child.displayMode !== NoteDisplayMode.EdgelessOnly
);
if (note) return note;
@@ -105,7 +105,7 @@ export class DocTitle extends WithDisposable(ShadowlessElement) {
const note = this._getOrCreateFirstPageVisibleNote();
const firstText = note?.children.find(block =>
matchFlavours(block, [
matchModels(block, [
ParagraphBlockModel,
ListBlockModel,
CodeBlockModel,
@@ -4,7 +4,7 @@ import {
type DropTarget,
getClosestBlockComponentByPoint,
isInsidePageEditor,
matchFlavours,
matchModels,
} from '@blocksuite/affine-shared/utils';
import {
type BlockComponent,
@@ -59,7 +59,7 @@ export class FileDropExtension extends LifeCycleWatcher {
const model = element.model;
const parent = this.std.store.getParent(model);
if (!matchFlavours(parent, [SurfaceBlockModel])) {
if (!matchModels(parent, [SurfaceBlockModel])) {
const point = this.point$.value;
target = point && calcDropTarget(point, model, element);
}
@@ -75,7 +75,7 @@ export class FileDropExtension extends LifeCycleWatcher {
if (!rootModel) return null;
let lastNote = rootModel.children[rootModel.children.length - 1];
if (!lastNote || !matchFlavours(lastNote, [NoteBlockModel])) {
if (!lastNote || !matchModels(lastNote, [NoteBlockModel])) {
const newNoteId = this.doc.addBlock('affine:note', {}, rootModel.id);
const newNote = this.doc.getBlock(newNoteId)?.model;
if (!newNote) return null;
@@ -21,7 +21,7 @@ import {
* which are also used for registering hotkeys for converting block flavours.
*/
export interface TextConversionConfig {
flavour: BlockSuite.Flavour;
flavour: string;
type?: string;
name: string;
description?: string;
@@ -2,7 +2,7 @@ import { DatabaseBlockModel } from '@blocksuite/affine-model';
import {
asyncGetBlockComponent,
getCurrentNativeRange,
matchFlavours,
matchModels,
} from '@blocksuite/affine-shared/utils';
import {
type BlockStdScope,
@@ -42,7 +42,7 @@ export function getInlineEditorByModel(
typeof model === 'string'
? editorHost.std.store.getBlock(model)?.model
: model;
if (!blockModel || matchFlavours(blockModel, [DatabaseBlockModel])) {
if (!blockModel || matchModels(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,5 +1,5 @@
import { RootBlockModel } from '@blocksuite/affine-model';
import { matchFlavours } from '@blocksuite/affine-shared/utils';
import { matchModels } from '@blocksuite/affine-shared/utils';
import { type Command, TextSelection } from '@blocksuite/block-std';
import type { Text } from '@blocksuite/store';
@@ -25,7 +25,7 @@ export const deleteTextCommand: Command<{
if (!fromElement) return;
let fromText: Text | undefined;
if (matchFlavours(fromElement.model, [RootBlockModel])) {
if (matchModels(fromElement.model, [RootBlockModel])) {
fromText = fromElement.model.title;
} else {
fromText = fromElement.model.text;
@@ -28,10 +28,7 @@ export const formatBlockCommand: Command<{
.chain()
.pipe(getSelectedBlocksCommand, {
blockSelections,
filter: el =>
FORMAT_BLOCK_SUPPORT_FLAVOURS.includes(
el.model.flavour as BlockSuite.Flavour
),
filter: el => FORMAT_BLOCK_SUPPORT_FLAVOURS.includes(el.model.flavour),
types: ['block'],
})
.pipe((ctx, next) => {
@@ -31,9 +31,7 @@ export const formatNativeCommand: Command<{
.filter(el => {
const block = el.closest<BlockComponent>(`[${BLOCK_ID_ATTR}]`);
if (block) {
return FORMAT_NATIVE_SUPPORT_FLAVOURS.includes(
block.model.flavour as BlockSuite.Flavour
);
return FORMAT_NATIVE_SUPPORT_FLAVOURS.includes(block.model.flavour);
}
return false;
})
@@ -22,10 +22,7 @@ export const formatTextCommand: Command<{
.chain()
.pipe(getSelectedBlocksCommand, {
textSelection,
filter: el =>
FORMAT_TEXT_SUPPORT_FLAVOURS.includes(
el.model.flavour as BlockSuite.Flavour
),
filter: el => FORMAT_TEXT_SUPPORT_FLAVOURS.includes(el.model.flavour),
types: ['text'],
})
.pipe((ctx, next) => {
@@ -1,9 +1,6 @@
import { CodeBlockModel } from '@blocksuite/affine-model';
import { BRACKET_PAIRS } from '@blocksuite/affine-shared/consts';
import {
createDefaultDoc,
matchFlavours,
} from '@blocksuite/affine-shared/utils';
import { createDefaultDoc, matchModels } from '@blocksuite/affine-shared/utils';
import {
type BlockStdScope,
TextSelection,
@@ -29,7 +26,7 @@ export const bracketKeymap = (
if (!textSelection) return;
const model = doc.getBlock(textSelection.from.blockId)?.model;
if (!model) return;
if (!matchFlavours(model, [CodeBlockModel])) return;
if (!matchModels(model, [CodeBlockModel])) return;
const inlineEditor = getInlineEditorByModel(
std.host,
textSelection.from.blockId
@@ -56,7 +53,7 @@ export const bracketKeymap = (
const model = doc.getBlock(textSelection.from.blockId)?.model;
if (!model) return;
const isCodeBlock = matchFlavours(model, [CodeBlockModel]);
const isCodeBlock = matchModels(model, [CodeBlockModel]);
// When selection is collapsed, only trigger auto complete in code block
if (textSelection.isCollapsed() && !isCodeBlock) return;
if (!textSelection.isInSameBlock()) return;
@@ -2,7 +2,7 @@ import {
DividerBlockModel,
ParagraphBlockModel,
} from '@blocksuite/affine-model';
import { matchFlavours } from '@blocksuite/affine-shared/utils';
import { matchModels } from '@blocksuite/affine-shared/utils';
import type { BlockStdScope } from '@blocksuite/block-std';
import type { BlockModel } from '@blocksuite/store';
@@ -16,8 +16,8 @@ export function toDivider(
) {
const { store: doc } = std;
if (
matchFlavours(model, [DividerBlockModel]) ||
(matchFlavours(model, [ParagraphBlockModel]) && model.type === 'quote')
matchModels(model, [DividerBlockModel]) ||
(matchModels(model, [ParagraphBlockModel]) && model.type === 'quote')
) {
return;
}
@@ -3,7 +3,7 @@ import {
type ListType,
ParagraphBlockModel,
} from '@blocksuite/affine-model';
import { matchFlavours, toNumberedList } from '@blocksuite/affine-shared/utils';
import { matchModels, toNumberedList } from '@blocksuite/affine-shared/utils';
import type { BlockStdScope } from '@blocksuite/block-std';
import type { BlockModel } from '@blocksuite/store';
@@ -17,7 +17,7 @@ export function toList(
prefix: string,
otherProperties?: Partial<ListProps>
) {
if (!matchFlavours(model, [ParagraphBlockModel])) {
if (!matchModels(model, [ParagraphBlockModel])) {
return;
}
const { store: doc } = std;
@@ -1,8 +1,5 @@
import { CodeBlockModel, ParagraphBlockModel } from '@blocksuite/affine-model';
import {
isMarkdownPrefix,
matchFlavours,
} from '@blocksuite/affine-shared/utils';
import { isMarkdownPrefix, matchModels } from '@blocksuite/affine-shared/utils';
import { type BlockStdScope, TextSelection } from '@blocksuite/block-std';
import { getInlineEditorByModel } from '../dom.js';
@@ -32,10 +29,10 @@ export function markdownInput(
const prefixText = getPrefixText(inline);
if (!isMarkdownPrefix(prefixText)) return;
const isParagraph = matchFlavours(model, [ParagraphBlockModel]);
const isParagraph = matchModels(model, [ParagraphBlockModel]);
const isHeading = isParagraph && model.type.startsWith('h');
const isParagraphQuoteBlock = isParagraph && model.type === 'quote';
const isCodeBlock = matchFlavours(model, [CodeBlockModel]);
const isCodeBlock = matchModels(model, [CodeBlockModel]);
if (isHeading || isParagraphQuoteBlock || isCodeBlock) return;
const lineInfo = inline.getLine(range.index);
@@ -2,7 +2,7 @@ import {
ParagraphBlockModel,
type ParagraphType,
} from '@blocksuite/affine-model';
import { matchFlavours } from '@blocksuite/affine-shared/utils';
import { matchModels } from '@blocksuite/affine-shared/utils';
import type { BlockStdScope } from '@blocksuite/block-std';
import type { BlockModel } from '@blocksuite/store';
@@ -16,7 +16,7 @@ export function toParagraph(
prefix: string
) {
const { store: doc } = std;
if (!matchFlavours(model, [ParagraphBlockModel])) {
if (!matchModels(model, [ParagraphBlockModel])) {
const parent = doc.getParent(model);
if (!parent) return;
@@ -36,7 +36,7 @@ export function toParagraph(
return id;
}
if (matchFlavours(model, [ParagraphBlockModel]) && model.type !== type) {
if (matchModels(model, [ParagraphBlockModel]) && model.type !== type) {
beforeConvert(std, model, prefix.length);
doc.updateBlock(model, { type });
@@ -1,5 +1,5 @@
import { ParagraphBlockModel } from '@blocksuite/affine-model';
import { matchFlavours } from '@blocksuite/affine-shared/utils';
import { matchModels } from '@blocksuite/affine-shared/utils';
import type { BlockStdScope } from '@blocksuite/block-std';
import type { BlockModel } from '@blocksuite/store';
@@ -11,7 +11,7 @@ export function toCode(
prefixText: string,
language: string | null
) {
if (matchFlavours(model, [ParagraphBlockModel]) && model.type === 'quote') {
if (matchModels(model, [ParagraphBlockModel]) && model.type === 'quote') {
return;
}