refactor(editor): reduce dependency to doc collection (#9492)

This commit is contained in:
Saul-Mirone
2025-01-03 01:59:25 +00:00
parent eb15b3cb39
commit 8b6c81f76d
70 changed files with 185 additions and 210 deletions
@@ -23,7 +23,7 @@ import {
polyLineNearestPoint,
Vec,
} from '@blocksuite/global/utils';
import { DocCollection, type Y } from '@blocksuite/store';
import * as Y from 'yjs';
import {
CONNECTOR_LABEL_MAX_WIDTH,
@@ -126,8 +126,8 @@ export class ConnectorElementModel extends GfxPrimitiveElementModel<ConnectorEle
}
static override propsToY(props: ConnectorElementProps) {
if (props.text && !(props.text instanceof DocCollection.Y.Text)) {
props.text = new DocCollection.Y.Text(props.text);
if (props.text && !(props.text instanceof Y.Text)) {
props.text = new Y.Text(props.text);
}
return props;
@@ -12,8 +12,7 @@ import {
} from '@blocksuite/block-std/gfx';
import type { IVec, PointLocation } from '@blocksuite/global/utils';
import { Bound, keys, linePolygonIntersects } from '@blocksuite/global/utils';
import type { Y } from '@blocksuite/store';
import { DocCollection } from '@blocksuite/store';
import * as Y from 'yjs';
type GroupElementProps = BaseElementProps & {
children: Y.Map<boolean>;
@@ -37,12 +36,12 @@ export class GroupElementModel extends GfxGroupLikeElementModel<GroupElementProp
}
static override propsToY(props: Record<string, unknown>) {
if ('title' in props && !(props.title instanceof DocCollection.Y.Text)) {
props.title = new DocCollection.Y.Text(props.title as string);
if ('title' in props && !(props.title instanceof Y.Text)) {
props.title = new Y.Text(props.title as string);
}
if (props.children && !(props.children instanceof DocCollection.Y.Map)) {
const children = new DocCollection.Y.Map() as Y.Map<boolean>;
if (props.children && !(props.children instanceof Y.Map)) {
const children = new Y.Map() as Y.Map<boolean>;
keys(props.children).forEach(key => {
children.set(key as string, true);
@@ -112,13 +111,13 @@ export class GroupElementModel extends GfxGroupLikeElementModel<GroupElementProp
}
)
@field()
accessor children: Y.Map<boolean> = new DocCollection.Y.Map<boolean>();
accessor children: Y.Map<boolean> = new Y.Map<boolean>();
@local()
accessor showTitle: boolean = true;
@field()
accessor title: Y.Text = new DocCollection.Y.Text();
accessor title: Y.Text = new Y.Text();
}
declare global {
@@ -20,8 +20,8 @@ import {
noop,
pick,
} from '@blocksuite/global/utils';
import { DocCollection, type Y } from '@blocksuite/store';
import { generateKeyBetween } from 'fractional-indexing';
import * as Y from 'yjs';
import { z } from 'zod';
import { ConnectorMode } from '../../consts/connector.js';
@@ -183,9 +183,9 @@ export class MindmapElementModel extends GfxGroupLikeElementModel<MindmapElement
if (
props.children &&
!isNodeType(props.children as Record<string, unknown>) &&
!(props.children instanceof DocCollection.Y.Map)
!(props.children instanceof Y.Map)
) {
const children: Y.Map<NodeDetail> = new DocCollection.Y.Map();
const children: Y.Map<NodeDetail> = new Y.Map();
keys(props.children).forEach(key => {
const detail = pick<Record<string, unknown>, keyof NodeDetail>(
@@ -284,9 +284,7 @@ export class MindmapElementModel extends GfxGroupLikeElementModel<MindmapElement
throw new Error(`Parent node ${parent} not found`);
}
props['text'] = new DocCollection.Y.Text(
(props['text'] as string) ?? 'New node'
);
props['text'] = new Y.Text((props['text'] as string) ?? 'New node');
const type = (props.type as string) ?? 'shape';
let id: string;
@@ -919,12 +917,12 @@ export class MindmapElementModel extends GfxGroupLikeElementModel<MindmapElement
}
@convert((initialValue, instance) => {
if (!(initialValue instanceof DocCollection.Y.Map)) {
if (!(initialValue instanceof Y.Map)) {
nodeSchema.parse(initialValue);
assertType<NodeType>(initialValue);
const map: Y.Map<NodeDetail> = new DocCollection.Y.Map();
const map: Y.Map<NodeDetail> = new Y.Map();
const surface = instance.surface;
const doc = surface.doc;
const recursive = (
@@ -967,7 +965,7 @@ export class MindmapElementModel extends GfxGroupLikeElementModel<MindmapElement
// since this model package is imported by playwright
@observe(observeChildren)
@field()
accessor children: Y.Map<NodeDetail> = new DocCollection.Y.Map();
accessor children: Y.Map<NodeDetail> = new Y.Map();
@watch(watchLayoutType)
@field()
@@ -16,7 +16,7 @@ import type {
PointLocation,
SerializedXYWH,
} from '@blocksuite/global/utils';
import { DocCollection, type Y } from '@blocksuite/store';
import * as Y from 'yjs';
import {
DEFAULT_ROUGHNESS,
@@ -68,8 +68,8 @@ export class ShapeElementModel extends GfxPrimitiveElementModel<ShapeProps> {
}
static override propsToY(props: ShapeProps) {
if (props.text && !(props.text instanceof DocCollection.Y.Text)) {
props.text = new DocCollection.Y.Text(props.text);
if (props.text && !(props.text instanceof Y.Text)) {
props.text = new Y.Text(props.text);
}
return props;
@@ -8,7 +8,7 @@ import {
pointInPolygon,
polygonNearestPoint,
} from '@blocksuite/global/utils';
import { DocCollection, type Y } from '@blocksuite/store';
import * as Y from 'yjs';
import {
FontFamily,
@@ -31,8 +31,8 @@ export class TextElementModel extends GfxPrimitiveElementModel<TextElementProps>
}
static override propsToY(props: Record<string, unknown>) {
if (props.text && !(props.text instanceof DocCollection.Y.Text)) {
props.text = new DocCollection.Y.Text(props.text as string);
if (props.text && !(props.text instanceof Y.Text)) {
props.text = new Y.Text(props.text as string);
}
return props;
@@ -82,7 +82,7 @@ export class TextElementModel extends GfxPrimitiveElementModel<TextElementProps>
accessor rotate: number = 0;
@field()
accessor text: Y.Text = new DocCollection.Y.Text();
accessor text: Y.Text = new Y.Text();
@field()
accessor textAlign: TextAlign = TextAlign.Center;