refactor(editor): remove global types in edgeless (#10092)

Closes: [BS-2553](https://linear.app/affine-design/issue/BS-2553/remove-global-types-in-edgeless)
This commit is contained in:
Saul-Mirone
2025-02-11 12:09:44 +00:00
parent 3062bd0771
commit dbf0f9dc20
45 changed files with 126 additions and 329 deletions
@@ -1,3 +1,4 @@
import type { SurfaceElementModelMap } from '@blocksuite/affine-model';
import { EditPropsStore } from '@blocksuite/affine-shared/services';
import { type BlockStdScope, StdIdentifier } from '@blocksuite/block-std';
import {
@@ -62,13 +63,13 @@ export class EdgelessCRUDExtension extends Extension {
};
addBlock = (
flavour: BlockSuite.EdgelessModelKeys | string,
flavour: string,
props: Record<string, unknown>,
parentId?: string | BlockModel,
parentIndex?: number
) => {
const gfx = this.std.get(GfxControllerIdentifier);
const key = getLastPropsKey(flavour as BlockSuite.EdgelessModelKeys, props);
const key = getLastPropsKey(flavour, props);
if (key) {
props = this.std.get(EditPropsStore).applyLastProps(key, props);
}
@@ -94,7 +95,7 @@ export class EdgelessCRUDExtension extends Extension {
}
const gfx = this.std.get(GfxControllerIdentifier);
const key = getLastPropsKey(type as BlockSuite.EdgelessModelKeys, props);
const key = getLastPropsKey(type, props);
if (key) {
props = this.std.get(EditPropsStore).applyLastProps(key, props) as T;
}
@@ -117,10 +118,10 @@ export class EdgelessCRUDExtension extends Extension {
const element = this._surface.getElementById(id);
if (element) {
const key = getLastPropsKey(
element.type as BlockSuite.EdgelessModelKeys,
{ ...element.yMap.toJSON(), ...props }
);
const key = getLastPropsKey(element.type, {
...element.yMap.toJSON(),
...props,
});
key && this.std.get(EditPropsStore).recordLastProps(key, props);
this._surface.updateElement(id, props);
return;
@@ -128,10 +129,10 @@ export class EdgelessCRUDExtension extends Extension {
const block = this.std.store.getBlockById(id);
if (block) {
const key = getLastPropsKey(
block.flavour as BlockSuite.EdgelessModelKeys,
{ ...block.yBlock.toJSON(), ...props }
);
const key = getLastPropsKey(block.flavour, {
...block.yBlock.toJSON(),
...props,
});
key && this.std.get(EditPropsStore).recordLastProps(key, props);
this.std.store.updateBlock(block, props);
}
@@ -142,17 +143,13 @@ export class EdgelessCRUDExtension extends Extension {
if (!surface) {
return null;
}
const el =
surface.getElementById(id) ??
(this.std.store.getBlockById(
id
) as BlockSuite.EdgelessBlockModelType | null);
return el;
const el = surface.getElementById(id) ?? this.std.store.getBlockById(id);
return el as GfxModel | null;
}
getElementsByType<K extends keyof BlockSuite.SurfaceElementModelMap>(
getElementsByType<K extends keyof SurfaceElementModelMap>(
type: K
): BlockSuite.SurfaceElementModelMap[K][] {
): SurfaceElementModelMap[K][] {
if (!this._surface) {
return [];
}
@@ -7,7 +7,11 @@ import {
type LocalConnectorElementModel,
} from '@blocksuite/affine-model';
import { ThemeProvider } from '@blocksuite/affine-shared/services';
import type { GfxController, GfxModel } from '@blocksuite/block-std/gfx';
import type {
GfxController,
GfxLocalElementModel,
GfxModel,
} from '@blocksuite/block-std/gfx';
import type { IBound, IVec, IVec3 } from '@blocksuite/global/utils';
import {
almostEqual,
@@ -70,9 +74,7 @@ export const ConnectorEndpointLocationsOnTriangle: IVec[] = [
[0.25, 0.5],
];
export function isConnectorWithLabel(
model: GfxModel | BlockSuite.SurfaceLocalModel
) {
export function isConnectorWithLabel(model: GfxModel | GfxLocalElementModel) {
return model instanceof ConnectorElementModel && model.hasLabel();
}
@@ -1,6 +1,7 @@
import type { GfxPrimitiveElementModel } from '@blocksuite/block-std/gfx';
import type { IBound } from '@blocksuite/global/utils';
import type { RoughCanvas, SurfaceElementModel } from '../../index.js';
import type { RoughCanvas } from '../../index.js';
import type { CanvasRenderer } from '../canvas-renderer.js';
import { brush } from './brush/index.js';
import { connector } from './connector/index.js';
@@ -11,7 +12,7 @@ import { text } from './text/index.js';
export { normalizeShapeBound } from './shape/utils.js';
export type ElementRenderer<
T extends BlockSuite.SurfaceElementModel = SurfaceElementModel,
T extends GfxPrimitiveElementModel = GfxPrimitiveElementModel,
> = (
model: T,
ctx: CanvasRenderingContext2D,
@@ -1,4 +1,7 @@
import type { ConnectorElementModel } from '@blocksuite/affine-model';
import type {
ConnectorElementModel,
SurfaceElementModelMap,
} from '@blocksuite/affine-model';
import type { SurfaceBlockProps } from '@blocksuite/block-std/gfx';
import { SurfaceBlockModel as BaseSurfaceModel } from '@blocksuite/block-std/gfx';
import { DisposableGroup } from '@blocksuite/global/utils';
@@ -55,11 +58,9 @@ export class SurfaceBlockModel extends BaseSurfaceModel {
);
}
override getElementsByType<K extends keyof BlockSuite.SurfaceElementModelMap>(
override getElementsByType<K extends keyof SurfaceElementModelMap>(
type: K
): BlockSuite.SurfaceElementModelMap[K][] {
return super.getElementsByType(
type
) as BlockSuite.SurfaceElementModelMap[K][];
): SurfaceElementModelMap[K][] {
return super.getElementsByType(type) as SurfaceElementModelMap[K][];
}
}
@@ -10,7 +10,10 @@ import { TelemetryProvider } from '@blocksuite/affine-shared/services';
import type { NoteChildrenFlavour } from '@blocksuite/affine-shared/types';
import { handleNativeRangeAtPoint } from '@blocksuite/affine-shared/utils';
import type { BlockStdScope } from '@blocksuite/block-std';
import { GfxControllerIdentifier } from '@blocksuite/block-std/gfx';
import {
type GfxBlockElementModel,
GfxControllerIdentifier,
} from '@blocksuite/block-std/gfx';
import {
type IPoint,
type Point,
@@ -117,7 +120,7 @@ export function addNote(
const blocks =
(doc.root?.children.filter(
child => child.flavour === 'affine:note'
) as BlockSuite.EdgelessBlockModelType[]) ?? [];
) as GfxBlockElementModel[]) ?? [];
const element = blocks.find(b => b.id === noteId);
if (element) {
gfx.selection.set({
@@ -8,7 +8,7 @@ import { NodePropsSchema } from '@blocksuite/affine-shared/utils';
const LastPropsSchema = NodePropsSchema;
export function getLastPropsKey(
modelType: BlockSuite.EdgelessModelKeys,
modelType: string,
modelProps: Partial<LastProps[LastPropsKey]>
): LastPropsKey | null {
if (modelType === 'shape') {