From 92c1d9392f4107bdad8d803b2f6ce4891fd25f87 Mon Sep 17 00:00:00 2001 From: Himself65 Date: Thu, 2 Feb 2023 12:44:11 -0600 Subject: [PATCH] refactor(store): remove `page` field in Text (#952) --- .../page-block/default/default-page-block.ts | 2 +- packages/playground/src/data/index.ts | 6 +-- .../src/__tests__/workspace.unit.spec.ts | 2 - packages/store/src/text-adapter.ts | 40 +++++++++++++------ packages/store/src/utils/utils.ts | 1 + packages/store/src/workspace/page.ts | 3 +- tests/basic.spec.ts | 4 +- tests/link.spec.ts | 2 +- 8 files changed, 37 insertions(+), 23 deletions(-) diff --git a/packages/blocks/src/page-block/default/default-page-block.ts b/packages/blocks/src/page-block/default/default-page-block.ts index beaacebf9306..83efe1f42c9f 100644 --- a/packages/blocks/src/page-block/default/default-page-block.ts +++ b/packages/blocks/src/page-block/default/default-page-block.ts @@ -204,7 +204,7 @@ export class DefaultPageBlockComponent const defaultFrame = model.children[0]; const props = { flavour: 'affine:paragraph', - text: new Text(page, contentRight), + text: new Text(contentRight), }; const newFirstParagraphId = page.addBlock(props, defaultFrame, 0); page.updateBlock(model, { title: contentLeft }); diff --git a/packages/playground/src/data/index.ts b/packages/playground/src/data/index.ts index 84be4c067828..b8963d58a95a 100644 --- a/packages/playground/src/data/index.ts +++ b/packages/playground/src/data/index.ts @@ -42,7 +42,7 @@ export function heavy(workspace: Workspace) { page.addBlockByFlavour( 'affine:paragraph', { - text: new Text(page, 'Hello, world! ' + i), + text: new Text('Hello, world! ' + i), }, frameId ); @@ -126,14 +126,14 @@ export function database(workspace: Workspace) { const p1 = page.addBlockByFlavour( 'affine:paragraph', { - text: new page.Text(page, 'text1'), + text: new page.Text('text1'), }, databaseId ); const p2 = page.addBlockByFlavour( 'affine:paragraph', { - text: new page.Text(page, 'text2'), + text: new page.Text('text2'), }, databaseId ); diff --git a/packages/store/src/__tests__/workspace.unit.spec.ts b/packages/store/src/__tests__/workspace.unit.spec.ts index 882dc7320af1..fd396014784d 100644 --- a/packages/store/src/__tests__/workspace.unit.spec.ts +++ b/packages/store/src/__tests__/workspace.unit.spec.ts @@ -420,14 +420,12 @@ describe.concurrent('workspace.search works', async () => { page.addBlockByFlavour('affine:paragraph', { text: new page.Text( - page, '英特尔第13代酷睿i7-1370P移动处理器现身Geekbench,14核心和5GHz' ), }); page.addBlockByFlavour('affine:paragraph', { text: new page.Text( - page, '索尼考虑移植《GT赛车7》,又一PlayStation独占IP登陆PC平台' ), }); diff --git a/packages/store/src/text-adapter.ts b/packages/store/src/text-adapter.ts index 8f015f672cfa..e8d59c09d71d 100644 --- a/packages/store/src/text-adapter.ts +++ b/packages/store/src/text-adapter.ts @@ -113,14 +113,16 @@ declare module 'yjs' { } export class Text { - private _space: Space; private _yText: Y.Text; + /** + * @internal + */ + public delayedJobs: (() => void)[] = []; // TODO toggle transact by options private _shouldTransact = true; - constructor(space: Space, input: Y.Text | string) { - this._space = space; + constructor(input: Y.Text | string) { if (typeof input === 'string') { this._yText = new Y.Text(input); } else { @@ -128,9 +130,18 @@ export class Text { } } - static fromDelta(space: Space, delta: DeltaOperation[]) { - const result = new Text(space, ''); - result.applyDelta(delta); + /** + * @internal + */ + public doDelayedJobs() { + this.delayedJobs.forEach(cb => cb()); + this.delayedJobs = []; + } + + static fromDelta(delta: DeltaOperation[]) { + const result = new Text(''); + // In the first time, yDoc does not exist. + result.delayedJobs.push(() => result.applyDelta(delta)); return result; } @@ -139,16 +150,21 @@ export class Text { } private _transact(callback: () => void) { - if (this._space.awarenessStore.isReadonly(this._space)) { - console.error('cannot modify data in readonly mode'); - return; + if (this._shouldTransact) { + const doc = this._yText.doc; + if (!doc) { + throw new Error('cannot find doc'); + } + doc.transact(() => { + callback(); + }, doc.clientID); + } else { + callback(); } - const { _space, _shouldTransact } = this; - _shouldTransact ? _space.transact(callback) : callback(); } clone() { - return new Text(this._space, this._yText.clone()); + return new Text(this._yText.clone()); } split(index: number, length: number): [PrelimText, PrelimText] { diff --git a/packages/store/src/utils/utils.ts b/packages/store/src/utils/utils.ts index 1f376870c547..48a7e6319070 100644 --- a/packages/store/src/utils/utils.ts +++ b/packages/store/src/utils/utils.ts @@ -89,6 +89,7 @@ export function trySyncTextProp( if (text instanceof Text) { // @ts-ignore yBlock.set('prop:text', text._yText); + text.doDelayedJobs(); return; } diff --git a/packages/store/src/workspace/page.ts b/packages/store/src/workspace/page.ts index 90d5b484b385..0fcb0676ea15 100644 --- a/packages/store/src/workspace/page.ts +++ b/packages/store/src/workspace/page.ts @@ -715,8 +715,7 @@ export class Page extends Space { } const yText = yBlock.get('prop:text') as Y.Text; - const text = new Text(this, yText); - model.text = text; + model.text = new Text(yText); if (model.flavour === 'affine:page') { model.tags = yBlock.get('meta:tags') as Y.Map>; model.tagSchema = yBlock.get('meta:tagSchema') as Y.Map; diff --git a/tests/basic.spec.ts b/tests/basic.spec.ts index 501cb914303f..2c8fb449ddac 100644 --- a/tests/basic.spec.ts +++ b/tests/basic.spec.ts @@ -49,7 +49,7 @@ test('basic init with external text', async ({ page }) => { const pageId = page.addBlockByFlavour('affine:page', { title: 'hello' }); const frame = page.addBlockByFlavour('affine:frame', {}, pageId); - const text = new page.Text(page, 'world'); + const text = new page.Text('world'); page.addBlockByFlavour('affine:paragraph', { text }, frame); const delta = [ @@ -59,7 +59,7 @@ test('basic init with external text', async ({ page }) => { page.addBlock( { flavour: 'affine:paragraph', - text: page.Text.fromDelta(page, delta), + text: page.Text.fromDelta(delta), }, frame ); diff --git a/tests/link.spec.ts b/tests/link.spec.ts index b23516caf2c4..b7a2604a87c4 100644 --- a/tests/link.spec.ts +++ b/tests/link.spec.ts @@ -102,7 +102,7 @@ async function createLinkBlock(page: Page, str: string, link: string) { }); const frameId = page.addBlock({ flavour: 'affine:frame' }, pageId); - const text = page.Text.fromDelta(page, [ + const text = page.Text.fromDelta([ { insert: 'Hello' }, { insert: str, attributes: { link } }, ]);