mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-08-02 18:09:58 +08:00
refactor(editor): reduce dependency to doc collection (#9492)
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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>);
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user