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

@@ -41,8 +41,5 @@ declare global {
interface EdgelessBlockModelMap {
'affine:embed-ai-chat': AIChatBlockModel;
}
interface BlockModels {
'affine:embed-ai-chat': AIChatBlockModel;
}
}
}

View File

@@ -36,7 +36,7 @@ import { reportResponse } from '../utils/action-reporter';
import { insertBelow, replace } from '../utils/editor-actions';
import { BlockIcon, CreateIcon, InsertBelowIcon, ReplaceIcon } from './icons';
const { matchFlavours } = BlocksUtils;
const { matchModels } = BlocksUtils;
type Selections = {
text?: TextSelection;
@@ -232,7 +232,7 @@ const REPLACE_SELECTION = {
if (currentTextSelection) {
const { doc } = host;
const block = doc.getBlock(currentTextSelection.blockId);
if (matchFlavours(block?.model ?? null, [ParagraphBlockModel])) {
if (matchModels(block?.model ?? null, [ParagraphBlockModel])) {
block?.model.text?.replace(
currentTextSelection.from.index,
currentTextSelection.from.length,

View File

@@ -6,7 +6,7 @@ import {
getSelectedModelsCommand,
ImageBlockModel,
ListBlockModel,
matchFlavours,
matchModels,
ParagraphBlockModel,
} from '@blocksuite/affine/blocks';
@@ -107,7 +107,7 @@ const textBlockShowWhen = (chain: Chain<InitCommandCtx>) => {
if (!selectedModels || selectedModels.length === 0) return false;
return selectedModels.some(model =>
matchFlavours(model, [ParagraphBlockModel, ListBlockModel])
matchModels(model, [ParagraphBlockModel, ListBlockModel])
);
};
@@ -121,7 +121,7 @@ const codeBlockShowWhen = (chain: Chain<InitCommandCtx>) => {
if (!selectedModels || selectedModels.length > 1) return false;
const model = selectedModels[0];
return matchFlavours(model, [CodeBlockModel]);
return matchModels(model, [CodeBlockModel]);
};
const imageBlockShowWhen = (chain: Chain<InitCommandCtx>) => {
@@ -134,7 +134,7 @@ const imageBlockShowWhen = (chain: Chain<InitCommandCtx>) => {
if (!selectedModels || selectedModels.length > 1) return false;
const model = selectedModels[0];
return matchFlavours(model, [ImageBlockModel]);
return matchModels(model, [ImageBlockModel]);
};
const EditAIGroup: AIItemGroupConfig = {
@@ -282,7 +282,7 @@ const GenerateWithAIGroup: AIItemGroupConfig = {
return selectedModels.every(
model =>
matchFlavours(model, [ParagraphBlockModel, ListBlockModel]) &&
matchModels(model, [ParagraphBlockModel, ListBlockModel]) &&
!model.type.startsWith('h')
);
},

View File

@@ -1,5 +1,8 @@
import type { EditorHost } from '@blocksuite/affine/block-std';
import { GfxControllerIdentifier } from '@blocksuite/affine/block-std/gfx';
import {
GfxControllerIdentifier,
type GfxModel,
} from '@blocksuite/affine/block-std/gfx';
import type {
AffineAIPanelWidget,
AIError,
@@ -72,7 +75,7 @@ async function getContentFromHubBlockModel(
export async function getContentFromSelected(
host: EditorHost,
selected: BlockSuite.EdgelessModel[]
selected: GfxModel[]
) {
type RemoveUndefinedKey<T, K extends keyof T> = T & {
[P in K]-?: Exclude<T[P], undefined>;
@@ -486,7 +489,7 @@ export function noteWithCodeBlockShowWen(
return (
selected[0] instanceof NoteBlockModel &&
selected[0].children.length === 1 &&
BlocksUtils.matchFlavours(selected[0].children[0], [CodeBlockModel])
BlocksUtils.matchModels(selected[0].children[0], [CodeBlockModel])
);
}

View File

@@ -6,7 +6,7 @@ import {
type AIItemConfig,
ImageBlockModel,
isInsideEdgelessEditor,
matchFlavours,
matchModels,
NoteBlockModel,
NoteDisplayMode,
} from '@blocksuite/affine/blocks';
@@ -116,7 +116,7 @@ function createNewNote(host: EditorHost): AIItemConfig {
// set the viewport to show the new note block and original note block
const newNote = doc.getBlock(noteBlockId)?.model;
if (!newNote || !matchFlavours(newNote, [NoteBlockModel])) return;
if (!newNote || !matchModels(newNote, [NoteBlockModel])) return;
const newNoteBound = Bound.deserialize(newNote.xywh);
const bounds = [bound, newNoteBound];
service.gfx.fitToScreen({

View File

@@ -1,10 +1,11 @@
import type { GfxModel } from '@blocksuite/affine/block-std/gfx';
import type { MindmapStyle } from '@blocksuite/affine/blocks';
import type { SerializedXYWH } from '@blocksuite/affine/global/utils';
import type { TemplateImage } from '../slides/template';
export interface ContextValue {
selectedElements?: BlockSuite.EdgelessModel[];
selectedElements?: GfxModel[];
content?: string;
// make it real
width?: number;

View File

@@ -1,9 +1,10 @@
import type { BlockComponent, EditorHost } from '@blocksuite/affine/block-std';
import type { GfxModel } from '@blocksuite/affine/block-std/gfx';
import {
AFFINE_EDGELESS_COPILOT_WIDGET,
type EdgelessCopilotWidget,
type EdgelessRootService,
matchFlavours,
matchModels,
MindmapElementModel,
NoteBlockModel,
RootBlockModel,
@@ -32,13 +33,13 @@ export function mindMapToMarkdown(mindmap: MindmapElementModel) {
return markdownStr;
}
export function isMindMapRoot(ele: BlockSuite.EdgelessModel) {
export function isMindMapRoot(ele: GfxModel) {
const group = ele?.group;
return group instanceof MindmapElementModel && group.tree.element === ele;
}
export function isMindmapChild(ele: BlockSuite.EdgelessModel) {
export function isMindmapChild(ele: GfxModel) {
return ele?.group instanceof MindmapElementModel && !isMindMapRoot(ele);
}
@@ -65,10 +66,10 @@ export function getEdgelessCopilotWidget(
export function findNoteBlockModel(blockElement: BlockComponent) {
let curBlock = blockElement;
while (curBlock) {
if (matchFlavours(curBlock.model, [NoteBlockModel])) {
if (matchModels(curBlock.model, [NoteBlockModel])) {
return curBlock.model;
}
if (matchFlavours(curBlock.model, [RootBlockModel, SurfaceBlockModel])) {
if (matchModels(curBlock.model, [RootBlockModel, SurfaceBlockModel])) {
return null;
}
if (!curBlock.parentComponent) {

View File

@@ -164,7 +164,7 @@ export async function extractMarkdownFromDoc(
const blockModels = getNoteBlockModels(doc);
const textModels = blockModels.filter(
model =>
!BlocksUtils.matchFlavours(model, [ImageBlockModel, DatabaseBlockModel])
!BlocksUtils.matchModels(model, [ImageBlockModel, DatabaseBlockModel])
);
const drafts = textModels.map(toDraftModel);
const slice = Slice.fromModels(doc, drafts);

View File

@@ -134,7 +134,7 @@ export async function getTextContentFromBlockModels(
// Currently only filter out images and databases
const selectedTextModels = models.filter(
model =>
!BlocksUtils.matchFlavours(model, [ImageBlockModel, DatabaseBlockModel])
!BlocksUtils.matchModels(model, [ImageBlockModel, DatabaseBlockModel])
);
const drafts = selectedTextModels.map(toDraftModel);
drafts.forEach(draft => traverse(draft, drafts));
@@ -286,9 +286,7 @@ export const getSelectedNoteAnchor = (host: EditorHost, id: string) => {
return host.querySelector(`affine-edgeless-note[data-block-id="${id}"]`);
};
export function getCopilotSelectedElems(
host: EditorHost
): BlockSuite.EdgelessModel[] {
export function getCopilotSelectedElems(host: EditorHost): GfxModel[] {
const service = getService(host);
const copilotWidget = getEdgelessCopilotWidget(host);

View File

@@ -10,7 +10,7 @@ import { useI18n } from '@affine/i18n';
import { track } from '@affine/track';
import { GfxControllerIdentifier } from '@blocksuite/affine/block-std/gfx';
import {
matchFlavours,
matchModels,
NoteBlockModel,
NoteDisplayMode,
} from '@blocksuite/affine/blocks';
@@ -176,7 +176,7 @@ export const EdgelessNoteHeader = ({ note }: { note: NoteBlockModel }) => {
const isFirstVisibleNote =
note.parent?.children.find(
child =>
matchFlavours(child, [NoteBlockModel]) &&
matchModels(child, [NoteBlockModel]) &&
child.displayMode === NoteDisplayMode.DocAndEdgeless
) === note;