mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-27 10:52:40 +08:00
refactor(editor): remove block models global type (#10086)
This commit is contained in:
@@ -12,7 +12,7 @@ import type {
|
||||
TransformerSlots,
|
||||
} from '@blocksuite/store';
|
||||
|
||||
import { matchFlavours } from '../../utils';
|
||||
import { matchModels } from '../../utils';
|
||||
|
||||
const handlePoint = (
|
||||
point: TextRangePoint,
|
||||
@@ -20,7 +20,7 @@ const handlePoint = (
|
||||
model: DraftModel
|
||||
) => {
|
||||
const { index, length } = point;
|
||||
if (matchFlavours(model, [RootBlockModel])) {
|
||||
if (matchModels(model, [RootBlockModel])) {
|
||||
if (length === 0) return;
|
||||
(snapshot.props.title as Record<string, unknown>).delta =
|
||||
model.title.sliceToDelta(index, length + index);
|
||||
|
||||
@@ -35,7 +35,7 @@ import {
|
||||
TelemetryProvider,
|
||||
} from '../../services';
|
||||
import type { AffineTextAttributes } from '../../types';
|
||||
import { matchFlavours, referenceToNode } from '../../utils';
|
||||
import { matchModels, referenceToNode } from '../../utils';
|
||||
|
||||
function findLastMatchingNode(
|
||||
root: BlockSnapshot[],
|
||||
@@ -293,7 +293,7 @@ class PasteTr {
|
||||
return;
|
||||
}
|
||||
if (!cursorModel.text) {
|
||||
if (matchFlavours(cursorModel, [ImageBlockModel])) {
|
||||
if (matchModels(cursorModel, [ImageBlockModel])) {
|
||||
const selection = this.std.selection.create(ImageSelection, {
|
||||
blockId: target.blockId,
|
||||
});
|
||||
@@ -358,7 +358,7 @@ class PasteTr {
|
||||
if (
|
||||
this.firstSnapshot !== this.lastSnapshot &&
|
||||
this.lastSnapshot.props.text &&
|
||||
!matchFlavours(this.pointState.model, [CodeBlockModel])
|
||||
!matchModels(this.pointState.model, [CodeBlockModel])
|
||||
) {
|
||||
const text = fromJSON(this.lastSnapshot.props.text) as Text;
|
||||
const doc = new Y.Doc();
|
||||
|
||||
@@ -1,8 +1,12 @@
|
||||
import type { BlockComponent, Command } from '@blocksuite/block-std';
|
||||
import type {
|
||||
BlockComponent,
|
||||
BlockStdScope,
|
||||
Command,
|
||||
} from '@blocksuite/block-std';
|
||||
|
||||
import { getNextContentBlock } from '../../utils/index.js';
|
||||
|
||||
function getNextBlock(std: BlockSuite.Std, path: string) {
|
||||
function getNextBlock(std: BlockStdScope, path: string) {
|
||||
const view = std.view;
|
||||
const model = std.store.getBlock(path)?.model;
|
||||
if (!model) return null;
|
||||
|
||||
@@ -1,8 +1,12 @@
|
||||
import type { BlockComponent, Command } from '@blocksuite/block-std';
|
||||
import type {
|
||||
BlockComponent,
|
||||
BlockStdScope,
|
||||
Command,
|
||||
} from '@blocksuite/block-std';
|
||||
|
||||
import { getPrevContentBlock } from '../../utils/index.js';
|
||||
|
||||
function getPrevBlock(std: BlockSuite.Std, path: string) {
|
||||
function getPrevBlock(std: BlockStdScope, path: string) {
|
||||
const view = std.view;
|
||||
|
||||
const model = std.store.getBlock(path)?.model;
|
||||
|
||||
@@ -52,10 +52,7 @@ export type EmbedOptions = {
|
||||
export type IndentContext = {
|
||||
blockId: string;
|
||||
inlineIndex: number;
|
||||
flavour: Extract<
|
||||
keyof BlockSuite.BlockModels,
|
||||
'affine:paragraph' | 'affine:list'
|
||||
>;
|
||||
flavour: string;
|
||||
type: 'indent' | 'dedent';
|
||||
};
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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
|
||||
) {
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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(() => {
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user