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

View File

@@ -30,6 +30,7 @@
"date-fns": "^4.0.0",
"lit": "^3.2.0",
"minimatch": "^10.0.1",
"yjs": "^13.6.21",
"zod": "^3.23.8"
},
"exports": {

View File

@@ -66,7 +66,7 @@ export class NoteRenderer
},
} satisfies AffineTextAttributes as BaseTextAttributes
);
collection.setDocMeta(note.id, { title: rowContent });
collection.meta.setDocMeta(note.id, { title: rowContent });
if (note.root) {
(note.root as RootBlockModel).title.insert(rowContent ?? '', 0);
note.root.children

View File

@@ -28,6 +28,7 @@
"@toeverything/theme": "^1.1.3",
"lit": "^3.2.0",
"minimatch": "^10.0.1",
"yjs": "^13.6.21",
"zod": "^3.23.8"
},
"exports": {

View File

@@ -22,13 +22,13 @@ import {
referenceToNode,
} from '@blocksuite/affine-shared/utils';
import { Bound, throttle } from '@blocksuite/global/utils';
import { DocCollection } from '@blocksuite/store';
import { computed } from '@preact/signals-core';
import { html, nothing } from 'lit';
import { property, queryAsync, state } from 'lit/decorators.js';
import { classMap } from 'lit/directives/class-map.js';
import { styleMap } from 'lit/directives/style-map.js';
import { when } from 'lit/directives/when.js';
import * as Y from 'yjs';
import { EmbedBlockComponent } from '../common/embed-block-element.js';
import {
@@ -163,7 +163,7 @@ export class EmbedLinkedDocBlockComponent extends EmbedBlockComponent<EmbedLinke
}
const index = parent.children.indexOf(this.model);
const yText = new DocCollection.Y.Text();
const yText = new Y.Text();
yText.insert(0, REFERENCE_NODE);
yText.format(0, REFERENCE_NODE.length, {
reference: {

View File

@@ -27,7 +27,6 @@ import { GfxControllerIdentifier } from '@blocksuite/block-std/gfx';
import { assertExists, Bound, getCommonBound } from '@blocksuite/global/utils';
import {
BlockViewType,
DocCollection,
type GetDocOptions,
type Query,
} from '@blocksuite/store';
@@ -38,6 +37,7 @@ import { choose } from 'lit/directives/choose.js';
import { classMap } from 'lit/directives/class-map.js';
import { guard } from 'lit/directives/guard.js';
import { type StyleInfo, styleMap } from 'lit/directives/style-map.js';
import * as Y from 'yjs';
import { EmbedBlockComponent } from '../common/embed-block-element.js';
import { isEmptyDoc } from '../common/render-linked-doc.js';
@@ -272,7 +272,7 @@ export class EmbedSyncedDocBlockComponent extends EmbedBlockComponent<EmbedSynce
assertExists(parent);
const index = parent.children.indexOf(this.model);
const yText = new DocCollection.Y.Text();
const yText = new Y.Text();
yText.insert(0, REFERENCE_NODE);
yText.format(0, REFERENCE_NODE.length, {
reference: {

View File

@@ -27,6 +27,7 @@
"lit": "^3.2.0",
"lodash.chunk": "^4.2.0",
"nanoid": "^5.0.7",
"yjs": "^13.6.21",
"zod": "^3.23.8"
},
"devDependencies": {

View File

@@ -10,7 +10,7 @@ import {
rotatePoints,
} from '@blocksuite/global/utils';
import { deltaInsertsToChunks } from '@blocksuite/inline';
import type { Y } from '@blocksuite/store';
import type * as Y from 'yjs';
import {
getFontFacesByFontFamily,

View File

@@ -2,7 +2,8 @@ import type { ConnectorElementModel } 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';
import { defineBlockSchema, DocCollection } from '@blocksuite/store';
import { defineBlockSchema } from '@blocksuite/store';
import * as Y from 'yjs';
import { elementsCtorMap } from './element-model/index.js';
import { SurfaceBlockTransformer } from './surface-transformer.js';
@@ -12,7 +13,7 @@ import { groupRelationWatcher } from './watchers/group.js';
export const SurfaceBlockSchema = defineBlockSchema({
flavour: 'affine:surface',
props: (internalPrimitives): SurfaceBlockProps => ({
elements: internalPrimitives.Boxed(new DocCollection.Y.Map()),
elements: internalPrimitives.Boxed(new Y.Map()),
}),
metadata: {
version: 5,

View File

@@ -3,9 +3,9 @@ import type {
FromSnapshotPayload,
SnapshotNode,
ToSnapshotPayload,
Y,
} from '@blocksuite/store';
import { BaseBlockTransformer, DocCollection } from '@blocksuite/store';
import { BaseBlockTransformer } from '@blocksuite/store';
import * as Y from 'yjs';
const SURFACE_TEXT_UNIQ_IDENTIFIER = 'affine:surface:text';
// Used for group children field
@@ -24,11 +24,11 @@ export class SurfaceBlockTransformer extends BaseBlockTransformer<SurfaceBlockPr
private _fromJSON(value: unknown): unknown {
if (value instanceof Object) {
if (Reflect.has(value, SURFACE_TEXT_UNIQ_IDENTIFIER)) {
const yText = new DocCollection.Y.Text();
const yText = new Y.Text();
yText.applyDelta(Reflect.get(value, 'delta'));
return yText;
} else if (Reflect.has(value, SURFACE_YMAP_UNIQ_IDENTIFIER)) {
const yMap = new DocCollection.Y.Map();
const yMap = new Y.Map();
const json = Reflect.get(value, 'json') as Record<string, unknown>;
Object.entries(json).forEach(([key, value]) => {
yMap.set(key, value);
@@ -40,12 +40,12 @@ export class SurfaceBlockTransformer extends BaseBlockTransformer<SurfaceBlockPr
}
private _toJSON(value: unknown): unknown {
if (value instanceof DocCollection.Y.Text) {
if (value instanceof Y.Text) {
return {
[SURFACE_TEXT_UNIQ_IDENTIFIER]: true,
delta: value.toDelta(),
};
} else if (value instanceof DocCollection.Y.Map) {
} else if (value instanceof Y.Map) {
return {
[SURFACE_YMAP_UNIQ_IDENTIFIER]: true,
json: value.toJSON(),
@@ -55,7 +55,7 @@ export class SurfaceBlockTransformer extends BaseBlockTransformer<SurfaceBlockPr
}
elementFromJSON(element: Record<string, unknown>) {
const yMap = new DocCollection.Y.Map();
const yMap = new Y.Map();
Object.entries(element).forEach(([key, value]) => {
yMap.set(key, this._fromJSON(value));
});
@@ -71,7 +71,7 @@ export class SurfaceBlockTransformer extends BaseBlockTransformer<SurfaceBlockPr
string,
unknown
>;
const yMap = new DocCollection.Y.Map<Y.Map<unknown>>();
const yMap = new Y.Map<Y.Map<unknown>>();
Object.entries(elementsJSON).forEach(([key, value]) => {
const element = this.elementFromJSON(value as Record<string, unknown>);

View File

@@ -14,7 +14,7 @@ import {
type SurfaceBlockModel,
} from '@blocksuite/block-std/gfx';
import { assertType, isEqual, type IVec, last } from '@blocksuite/global/utils';
import { DocCollection } from '@blocksuite/store';
import * as Y from 'yjs';
import { fitContent } from '../../renderer/elements/shape/utils.js';
import { layout } from './layout.js';
@@ -360,7 +360,7 @@ export function createFromTree(
layoutType: LayoutType,
surface: SurfaceBlockModel
) {
const children = new DocCollection.Y.Map();
const children = new Y.Map();
const traverse = (subtree: MindmapNode, parent?: string) => {
const value: NodeDetail = {
...subtree.detail,

View File

@@ -31,6 +31,7 @@
"lit-html": "^3.2.1",
"lodash.clonedeep": "^4.5.0",
"shiki": "^1.12.0",
"yjs": "^13.6.21",
"zod": "^3.23.8"
},
"exports": {

View File

@@ -16,7 +16,7 @@ import {
KEYBOARD_ALLOW_DEFAULT,
type KeyboardBindingContext,
} from '@blocksuite/inline';
import type { Y } from '@blocksuite/store';
import type * as Y from 'yjs';
import { z, type ZodObject, type ZodTypeAny } from 'zod';
import { MarkdownMatcherIdentifier } from './markdown-matcher.js';

View File

@@ -6,7 +6,7 @@ import type {
InlineRange,
KeyboardBindingHandler,
} from '@blocksuite/inline';
import type { Y } from '@blocksuite/store';
import type * as Y from 'yjs';
import type { ZodTypeAny } from 'zod';
export type InlineSpecs<

View File

@@ -4,12 +4,11 @@ import { unsafeCSSVar } from '@blocksuite/affine-shared/theme';
import { type BlockStdScope, ShadowlessElement } from '@blocksuite/block-std';
import { noop, SignalWatcher, WithDisposable } from '@blocksuite/global/utils';
import { DoneIcon } from '@blocksuite/icons/lit';
import type { Y } from '@blocksuite/store';
import { DocCollection } from '@blocksuite/store';
import { effect, type Signal, signal } from '@preact/signals-core';
import { css, html } from 'lit';
import { property } from 'lit/decorators.js';
import { codeToTokensBase, type ThemedToken } from 'shiki';
import * as Y from 'yjs';
import { InlineManagerExtension } from '../../../../extension/index.js';
import { LatexEditorUnitSpecExtension } from '../../affine-inline-specs.js';
@@ -111,7 +110,7 @@ export class LatexEditorMenu extends SignalWatcher(
override connectedCallback(): void {
super.connectedCallback();
const doc = new DocCollection.Y.Doc();
const doc = new Y.Doc();
this.yText = doc.getText('latex');
this.yText.insert(0, this.latexSignal.value);

View File

@@ -11,12 +11,12 @@ import {
type KeyboardBindingContext,
type VLine,
} from '@blocksuite/inline';
import type { Y } from '@blocksuite/store';
import { DocCollection, Text } from '@blocksuite/store';
import { Text } from '@blocksuite/store';
import { effect } from '@preact/signals-core';
import { css, html, type TemplateResult } from 'lit';
import { property, query } from 'lit/decorators.js';
import { classMap } from 'lit/directives/class-map.js';
import * as Y from 'yjs';
import { z } from 'zod';
import { onVBeforeinput, onVCompositionEnd } from './hooks.js';
@@ -289,7 +289,7 @@ export class RichText extends WithDisposable(ShadowlessElement) {
}
if (!this.undoManager) {
this.undoManager = new DocCollection.Y.UndoManager(this._yText, {
this.undoManager = new Y.UndoManager(this._yText, {
trackedOrigins: new Set([this._yText.doc.clientID]),
});
}

View File

@@ -26,6 +26,7 @@
"@toeverything/theme": "^1.1.3",
"date-fns": "^4.0.0",
"lit": "^3.2.0",
"yjs": "^13.6.21",
"zod": "^3.23.8"
},
"exports": {

View File

@@ -1,9 +1,10 @@
import { ShadowlessElement } from '@blocksuite/block-std';
import { assertEquals } from '@blocksuite/global/utils';
import { DocCollection, type Text } from '@blocksuite/store';
import { type Text } from '@blocksuite/store';
import { css, html } from 'lit';
import { state } from 'lit/decorators.js';
import { createRef, ref } from 'lit/directives/ref.js';
import * as Y from 'yjs';
import { t } from '../../../../core/index.js';
import type { TableAreaSelection } from '../../types.js';
@@ -98,7 +99,7 @@ export function fillSelectionWithFocusCellData(
curCellText.clear();
curCellText.applyDelta(delta);
} else {
const newText = new DocCollection.Y.Text();
const newText = new Y.Text();
newText.applyDelta(delta);
curCell.valueSet(newText);
}

View File

@@ -18,6 +18,7 @@
"@blocksuite/inline": "workspace:*",
"@blocksuite/store": "workspace:*",
"fractional-indexing": "^3.2.0",
"yjs": "^13.6.21",
"zod": "^3.23.8"
},
"exports": {

View File

@@ -14,7 +14,7 @@ export class RootBlockModel extends BlockModel<RootBlockProps> {
if (model instanceof RootBlockModel) {
const newDocMeta = this.doc.collection.meta.getDocMeta(model.doc.id);
if (!newDocMeta || newDocMeta.title !== model.title.toString()) {
this.doc.collection.setDocMeta(model.doc.id, {
this.doc.collection.meta.setDocMeta(model.doc.id, {
title: model.title.toString(),
});
}

View File

@@ -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;

View File

@@ -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 {

View File

@@ -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()

View File

@@ -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;

View File

@@ -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;

View File

@@ -44,6 +44,7 @@
"remark-parse": "^11.0.0",
"remark-stringify": "^11.0.0",
"unified": "^11.0.5",
"yjs": "^13.6.21",
"zod": "^3.23.8"
},
"exports": {

View File

@@ -17,12 +17,12 @@ import {
type BlockModel,
type BlockSnapshot,
type DeltaOperation,
DocCollection,
fromJSON,
type JobMiddleware,
type SliceSnapshot,
type Text,
} from '@blocksuite/store';
import * as Y from 'yjs';
import { REFERENCE_NODE } from '../../consts';
import {
@@ -357,7 +357,7 @@ class PasteTr {
!matchFlavours(this.pointState.model, ['affine:code'])
) {
const text = fromJSON(this.lastSnapshot.props.text) as Text;
const doc = new DocCollection.Y.Doc();
const doc = new Y.Doc();
const temp = doc.getMap('temp');
temp.set('text', text.yText);
this.lastIndex = text.length;

View File

@@ -6,10 +6,10 @@ import {
DisposableGroup,
Slot,
} from '@blocksuite/global/utils';
import { DocCollection } from '@blocksuite/store';
import { computed, type Signal, signal } from '@preact/signals-core';
import clonedeep from 'lodash.clonedeep';
import mergeWith from 'lodash.mergewith';
import * as Y from 'yjs';
import { z } from 'zod';
import { makeDeepOptional, NodePropsSchema } from '../utils/index.js';
@@ -63,9 +63,9 @@ function isSessionProp(key: string): key is keyof SessionProps {
function customizer(_target: unknown, source: unknown) {
if (
ColorSchema.safeParse(source).success ||
source instanceof DocCollection.Y.Text ||
source instanceof DocCollection.Y.Array ||
source instanceof DocCollection.Y.Map
source instanceof Y.Text ||
source instanceof Y.Array ||
source instanceof Y.Map
) {
return source;
}

View File

@@ -11,7 +11,7 @@ export function createDefaultDoc(
const rootId = doc.addBlock('affine:page', {
title: new doc.Text(title),
});
collection.setDocMeta(doc.id, {
collection.meta.setDocMeta(doc.id, {
title,
});