Skip to content

Commit

Permalink
refactor(store): remove page field in Text (#952)
Browse files Browse the repository at this point in the history
  • Loading branch information
himself65 authored Feb 2, 2023
1 parent a24de2e commit 92c1d93
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down
6 changes: 3 additions & 3 deletions packages/playground/src/data/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
Expand Down Expand Up @@ -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
);
Expand Down
2 changes: 0 additions & 2 deletions packages/store/src/__tests__/workspace.unit.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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平台'
),
});
Expand Down
40 changes: 28 additions & 12 deletions packages/store/src/text-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,24 +113,35 @@ 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 {
this._yText = input;
}
}

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

Expand All @@ -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] {
Expand Down
1 change: 1 addition & 0 deletions packages/store/src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ export function trySyncTextProp(
if (text instanceof Text) {
// @ts-ignore
yBlock.set('prop:text', text._yText);
text.doDelayedJobs();
return;
}

Expand Down
3 changes: 1 addition & 2 deletions packages/store/src/workspace/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -715,8 +715,7 @@ export class Page extends Space<PageData> {
}

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<Y.Map<unknown>>;
model.tagSchema = yBlock.get('meta:tagSchema') as Y.Map<unknown>;
Expand Down
4 changes: 2 additions & 2 deletions tests/basic.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand All @@ -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
);
Expand Down
2 changes: 1 addition & 1 deletion tests/link.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 } },
]);
Expand Down

2 comments on commit 92c1d93

@vercel
Copy link

@vercel vercel bot commented on 92c1d93 Feb 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

blocksuite-react – ./packages/react/examples/next

blocksuite-react-toeverything.vercel.app
blocksuite-react.vercel.app
blocksuite-react-git-master-toeverything.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 92c1d93 Feb 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

blocksuite – ./packages/playground

blocksuite-toeverything.vercel.app
blocksuite-five.vercel.app
blocksuite-git-master-toeverything.vercel.app

Please sign in to comment.