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

View File

@@ -1,7 +1,7 @@
import { ParagraphBlockModel } from '@blocksuite/affine-model';
import type { BlockModel } from '@blocksuite/store';
import { matchFlavours } from '../model/checker.js';
import { matchModels } from '../model/checker.js';
export function calculateCollapsedSiblings(
model: ParagraphBlockModel
@@ -15,7 +15,7 @@ export function calculateCollapsedSiblings(
const collapsedEdgeIndex = children.findIndex((child, i) => {
if (
i > index &&
matchFlavours(child, [ParagraphBlockModel]) &&
matchModels(child, [ParagraphBlockModel]) &&
child.type.startsWith('h')
) {
const modelLevel = parseInt(model.type.slice(1));
@@ -46,7 +46,7 @@ export function getNearestHeadingBefore(
for (let i = index - 1; i >= 0; i--) {
const sibling = parent.children[i];
if (
matchFlavours(sibling, [ParagraphBlockModel]) &&
matchModels(sibling, [ParagraphBlockModel]) &&
sibling.type.startsWith('h')
) {
return sibling;

View File

@@ -8,7 +8,7 @@ import {
getClosestBlockComponentByElement,
getRectByBlockComponent,
} from '../dom/index.js';
import { matchFlavours } from '../model/index.js';
import { matchModels } from '../model/index.js';
import { getDropRectByPoint } from './get-drop-rect-by-point.js';
import { DropFlags, type DropPlacement, type DropTarget } from './types.js';
@@ -56,7 +56,7 @@ export function calcDropTarget(
.every(m => children.includes(m.flavour));
}
if (!shouldAppendToDatabase && !matchFlavours(model, [DatabaseBlockModel])) {
if (!shouldAppendToDatabase && !matchModels(model, [DatabaseBlockModel])) {
const databaseBlockComponent =
element.closest<BlockComponent>('affine-database');
if (databaseBlockComponent) {
@@ -150,7 +150,7 @@ export function calcDropTarget(
const hasChild = (element as BlockComponent).childBlocks.length;
if (
allowSublist &&
matchFlavours(model, [ListBlockModel]) &&
matchModels(model, [ListBlockModel]) &&
!hasChild &&
point.x > domRect.x + BLOCK_CHILDREN_CONTAINER_PADDING_LEFT
) {

View File

@@ -4,7 +4,7 @@ import type { Point } from '@blocksuite/global/utils';
import type { BlockModel } from '@blocksuite/store';
import { getRectByBlockComponent } from '../dom/index.js';
import { matchFlavours } from '../model/index.js';
import { matchModels } from '../model/index.js';
import { DropFlags } from './types.js';
const ATTR_SELECTOR = `[${BLOCK_ID_ATTR}]`;
@@ -25,7 +25,7 @@ export function getDropRectByPoint(
flag: DropFlags.Normal,
};
const isDatabase = matchFlavours(model, [DatabaseBlockModel]);
const isDatabase = matchModels(model, [DatabaseBlockModel]);
if (isDatabase) {
const table = getDatabaseBlockTableElement(element);

View File

@@ -6,7 +6,7 @@ import type { BlockModel } from '@blocksuite/store';
import { BLOCK_CHILDREN_CONTAINER_PADDING_LEFT } from '../../consts/index.js';
import { clamp } from '../math.js';
import { matchFlavours } from '../model/checker.js';
import { matchModels } from '../model/checker.js';
const ATTR_SELECTOR = `[${BLOCK_ID_ATTR}]`;
@@ -36,7 +36,7 @@ function hasBlockId(element: Element): element is BlockComponent {
* Returns `true` if element is default/edgeless page or note.
*/
function isRootOrNoteOrSurface(element: BlockComponent) {
return matchFlavours(element.model, [
return matchModels(element.model, [
RootBlockModel,
NoteBlockModel,
SurfaceBlockModel,

View File

@@ -1,12 +1,12 @@
import { FrameBlockModel, GroupElementModel } from '@blocksuite/affine-model';
import type { GfxBlockElementModel } from '@blocksuite/block-std/gfx';
import type { GfxBlockElementModel, GfxModel } from '@blocksuite/block-std/gfx';
import {
deserializeXYWH,
getQuadBoundWithRotation,
} from '@blocksuite/global/utils';
import type { BlockModel } from '@blocksuite/store';
export function getSelectedRect(selected: BlockSuite.EdgelessModel[]): DOMRect {
export function getSelectedRect(selected: GfxModel[]): DOMRect {
if (selected.length === 0) {
return new DOMRect();
}
@@ -57,8 +57,8 @@ export function getSelectedRect(selected: BlockSuite.EdgelessModel[]): DOMRect {
);
}
export function getElementsWithoutGroup(elements: BlockSuite.EdgelessModel[]) {
const set = new Set<BlockSuite.EdgelessModel>();
export function getElementsWithoutGroup(elements: GfxModel[]) {
const set = new Set<GfxModel>();
elements.forEach(element => {
if (element instanceof GroupElementModel) {
@@ -73,7 +73,7 @@ export function getElementsWithoutGroup(elements: BlockSuite.EdgelessModel[]) {
}
export function isTopLevelBlock(
selectable: BlockModel | BlockSuite.EdgelessModel | null
selectable: BlockModel | GfxModel | null
): selectable is GfxBlockElementModel {
return !!selectable && 'flavour' in selectable;
}

View File

@@ -8,7 +8,7 @@ type ModelList<T> =
: never
: never;
export function matchFlavours<
export function matchModels<
const Model extends ConstructorType<BlockModel>[],
U extends ModelList<Model>[number] = ModelList<Model>[number],
>(model: unknown, expected: Model): model is U {

View File

@@ -3,7 +3,7 @@ import type { EditorHost } from '@blocksuite/block-std';
import type { BlockModel } from '@blocksuite/store';
import { DocModeProvider } from '../../services/doc-mode-service.js';
import { matchFlavours } from './checker.js';
import { matchModels } from './checker.js';
/**
*
@@ -74,7 +74,7 @@ export function getPrevContentBlock(
const prev = getPrev(model);
if (prev) {
if (prev.role === 'content' && !matchFlavours(prev, [FrameBlockModel])) {
if (prev.role === 'content' && !matchModels(prev, [FrameBlockModel])) {
return prev;
}

View File

@@ -2,7 +2,7 @@ import { NoteBlockModel, NoteDisplayMode } from '@blocksuite/affine-model';
import type { BlockComponent, EditorHost } from '@blocksuite/block-std';
import type { BlockModel, Store } from '@blocksuite/store';
import { matchFlavours } from './checker.js';
import { matchModels } from './checker.js';
export function findAncestorModel(
model: BlockModel,
@@ -37,7 +37,7 @@ export async function asyncGetBlockComponent(
export function findNoteBlockModel(model: BlockModel) {
return findAncestorModel(model, m =>
matchFlavours(m, [NoteBlockModel])
matchModels(m, [NoteBlockModel])
) as NoteBlockModel | null;
}
@@ -48,7 +48,7 @@ export function getLastNoteBlock(doc: Store) {
for (let i = children.length - 1; i >= 0; i--) {
const child = children[i];
if (
matchFlavours(child, [NoteBlockModel]) &&
matchModels(child, [NoteBlockModel]) &&
child.displayMode !== NoteDisplayMode.EdgelessOnly
) {
note = child as NoteBlockModel;

View File

@@ -2,7 +2,7 @@ import { ListBlockModel } from '@blocksuite/affine-model';
import type { BlockStdScope } from '@blocksuite/block-std';
import type { BlockModel, Store } from '@blocksuite/store';
import { matchFlavours } from './checker.js';
import { matchModels } from './checker.js';
/**
* Pass in a list model, and this function will look forward to find continuous sibling numbered lists,
@@ -23,7 +23,7 @@ export function getNextContinuousNumberedLists(
const firstNotNumberedListIndex = parent.children.findIndex(
(model, i) =>
i > modelIndex &&
(!matchFlavours(model, [ListBlockModel]) || model.type !== 'numbered')
(!matchModels(model, [ListBlockModel]) || model.type !== 'numbered')
);
const newContinuousLists = parent.children.slice(
modelIndex + 1,
@@ -31,8 +31,7 @@ export function getNextContinuousNumberedLists(
);
if (
!newContinuousLists.every(
model =>
matchFlavours(model, [ListBlockModel]) && model.type === 'numbered'
model => matchModels(model, [ListBlockModel]) && model.type === 'numbered'
)
)
return [];
@@ -56,7 +55,7 @@ export function toNumberedList(
// if there is a numbered list before, the order continues from the previous list
if (
prevSibling &&
matchFlavours(prevSibling, [ListBlockModel]) &&
matchModels(prevSibling, [ListBlockModel]) &&
prevSibling.type === 'numbered'
) {
doc.transact(() => {

View File

@@ -2,7 +2,7 @@ import type { BlockModel, Store, Text } from '@blocksuite/store';
export function transformModel(
model: BlockModel,
flavour: BlockSuite.Flavour,
flavour: string,
props?: Parameters<Store['addBlock']>[1]
) {
const doc = model.doc;