Skip to content

Commit

Permalink
refactor(core): move block collection to affine and implement as doc (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Saul-Mirone committed Jan 4, 2025
1 parent 4cb186d commit dcf4993
Show file tree
Hide file tree
Showing 39 changed files with 595 additions and 192 deletions.
2 changes: 1 addition & 1 deletion blocksuite/affine/block-data-view/src/data-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ export class BlockQueryDataSource extends DataSourceBase {
type ?? propertyPresets.multiSelectPropertyConfig.type
].create(this.newColumnName());

const id = doc.generateBlockId();
const id = doc.collection.idGenerator();
if (this.block.columns.some(v => v.id === id)) {
return id;
}
Expand Down
2 changes: 1 addition & 1 deletion blocksuite/affine/block-data-view/src/data-view-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class DataViewBlockModel extends BlockModel<Props> {
}

duplicateView(id: string): string {
const newId = this.doc.generateBlockId();
const newId = this.doc.collection.idGenerator();
this.doc.transact(() => {
const index = this.views.findIndex(v => v.id === id);
const view = this.views[index];
Expand Down
2 changes: 1 addition & 1 deletion blocksuite/affine/block-database/src/data-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ export const databaseViewInitTemplate = (
const rowId = model.doc.addBlock(
'affine:paragraph',
{
text: new model.doc.Text(`Task ${i + 1}`),
text: new Text(`Task ${i + 1}`),
},
model.id
);
Expand Down
4 changes: 2 additions & 2 deletions blocksuite/affine/block-database/src/utils/block-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export function addProperty(
id?: string;
}
): string {
const id = column.id ?? model.doc.generateBlockId();
const id = column.id ?? model.doc.collection.idGenerator();
if (model.columns.some(v => v.id === id)) {
return id;
}
Expand Down Expand Up @@ -101,7 +101,7 @@ export function deleteView(model: DatabaseBlockModel, id: string) {
}

export function duplicateView(model: DatabaseBlockModel, id: string): string {
const newId = model.doc.generateBlockId();
const newId = model.doc.collection.idGenerator();
model.doc.transact(() => {
const index = model.views.findIndex(v => v.id === id);
const view = model.views[index];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
type DraftModel,
type Query,
Slice,
Text,
} from '@blocksuite/store';
import { render, type TemplateResult } from 'lit';

Expand Down Expand Up @@ -407,7 +408,7 @@ export function createLinkedDocFromSlice(
const linkedDoc = doc.collection.createDoc({});
linkedDoc.load(() => {
const rootId = linkedDoc.addBlock('affine:page', {
title: new doc.Text(docTitle),
title: new Text(docTitle),
});
linkedDoc.addBlock('affine:surface', {}, rootId);
const noteId = linkedDoc.addBlock('affine:note', {}, rootId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
referenceToNode,
} from '@blocksuite/affine-shared/utils';
import { Bound, throttle } from '@blocksuite/global/utils';
import { Text } from '@blocksuite/store';
import { computed } from '@preact/signals-core';
import { html, nothing } from 'lit';
import { property, queryAsync, state } from 'lit/decorators.js';
Expand Down Expand Up @@ -171,7 +172,7 @@ export class EmbedLinkedDocBlockComponent extends EmbedBlockComponent<EmbedLinke
...this.referenceInfo$.peek(),
},
});
const text = new doc.Text(yText);
const text = new Text(yText);

doc.addBlock(
'affine:paragraph',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
BlockViewType,
type GetDocOptions,
type Query,
Text,
} from '@blocksuite/store';
import { computed } from '@preact/signals-core';
import { html, type PropertyValues } from 'lit';
Expand Down Expand Up @@ -280,7 +281,7 @@ export class EmbedSyncedDocBlockComponent extends EmbedBlockComponent<EmbedSynce
...this.referenceInfo,
},
});
const text = new doc.Text(yText);
const text = new Text(yText);

doc.addBlock(
'affine:paragraph',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { focusTextModel } from '@blocksuite/affine-components/rich-text';
import { getLastNoteBlock } from '@blocksuite/affine-shared/utils';
import type { Command } from '@blocksuite/block-std';
import { Text } from '@blocksuite/store';

/**
* Append a paragraph block at the end of the whole page.
Expand All @@ -19,11 +20,7 @@ export const appendParagraphCommand: Command<
if (!noteId) {
noteId = doc.addBlock('affine:note', {}, doc.root.id);
}
const id = doc.addBlock(
'affine:paragraph',
{ text: new doc.Text(text) },
noteId
);
const id = doc.addBlock('affine:paragraph', { text: new Text(text) }, noteId);

focusTextModel(std, id, text.length);
next();
Expand Down
3 changes: 2 additions & 1 deletion blocksuite/affine/shared/src/utils/model/doc.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Workspace } from '@blocksuite/store';
import { Text } from '@blocksuite/store';

export function createDefaultDoc(
collection: Workspace,
Expand All @@ -9,7 +10,7 @@ export function createDefaultDoc(
doc.load();
const title = options.title ?? '';
const rootId = doc.addBlock('affine:page', {
title: new doc.Text(title),
title: new Text(title),
});
collection.meta.setDocMeta(doc.id, {
title,
Expand Down
12 changes: 6 additions & 6 deletions blocksuite/blocks/src/__tests__/database/database.unit.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
} from '@blocksuite/affine-model';
import { propertyModelPresets } from '@blocksuite/data-view/property-pure-presets';
import type { BlockModel, Blocks } from '@blocksuite/store';
import { Schema } from '@blocksuite/store';
import { Schema, Text } from '@blocksuite/store';
import {
createAutoIncrementIdGenerator,
TestWorkspace,
Expand Down Expand Up @@ -71,7 +71,7 @@ describe('DatabaseManager', () => {
doc = createTestDoc();

rootId = doc.addBlock('affine:page', {
title: new doc.Text('database test'),
title: new Text('database test'),
});
noteBlockId = doc.addBlock('affine:note', {}, rootId);

Expand Down Expand Up @@ -114,14 +114,14 @@ describe('DatabaseManager', () => {
p1 = doc.addBlock(
'affine:paragraph',
{
text: new doc.Text('text1'),
text: new Text('text1'),
},
databaseBlockId
);
p2 = doc.addBlock(
'affine:paragraph',
{
text: new doc.Text('text2'),
text: new Text('text2'),
},
databaseBlockId
);
Expand Down Expand Up @@ -173,7 +173,7 @@ describe('DatabaseManager', () => {
const modelId = doc.addBlock(
'affine:paragraph',
{
text: new doc.Text('paragraph'),
text: new Text('paragraph'),
},
noteBlockId
);
Expand Down Expand Up @@ -201,7 +201,7 @@ describe('DatabaseManager', () => {
const newRowId = doc.addBlock(
'affine:paragraph',
{
text: new doc.Text('text3'),
text: new Text('text3'),
},
databaseBlockId
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { DocModeProvider } from '@blocksuite/affine-shared/services';
import { getBlockProps } from '@blocksuite/affine-shared/utils';
import type { EditorHost } from '@blocksuite/block-std';
import { GfxBlockElementModel } from '@blocksuite/block-std/gfx';
import { type BlockModel, type Blocks } from '@blocksuite/store';
import { type BlockModel, type Blocks, Text } from '@blocksuite/store';

import {
getElementProps,
Expand Down Expand Up @@ -43,7 +43,7 @@ export function createLinkedDocFromNote(
const linkedDoc = doc.collection.createDoc({});
linkedDoc.load(() => {
const rootId = linkedDoc.addBlock('affine:page', {
title: new doc.Text(docTitle),
title: new Text(docTitle),
});
linkedDoc.addBlock('affine:surface', {}, rootId);
const blockProps = getBlockProps(note);
Expand Down Expand Up @@ -74,7 +74,7 @@ export function createLinkedDocFromEdgelessElements(
const linkedDoc = host.doc.collection.createDoc({});
linkedDoc.load(() => {
const rootId = linkedDoc.addBlock('affine:page', {
title: new host.doc.Text(docTitle),
title: new Text(docTitle),
});
const surfaceId = linkedDoc.addBlock('affine:surface', {}, rootId);
const surface = getSurfaceBlock(linkedDoc);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ import {
} from '@blocksuite/affine-shared/services';
import { getHostName, referenceToNode } from '@blocksuite/affine-shared/utils';
import { type BlockStdScope, WidgetComponent } from '@blocksuite/block-std';
import { type BlockModel } from '@blocksuite/store';
import { type BlockModel, Text } from '@blocksuite/store';
import { autoUpdate, computePosition, flip, offset } from '@floating-ui/dom';
import { html, nothing, type TemplateResult } from 'lit';
import { query, state } from 'lit/decorators.js';
Expand Down Expand Up @@ -619,7 +619,7 @@ export class EmbedCardToolbar extends WidgetComponent<
const insert = title || caption || url;
yText.insert(0, insert);
yText.format(0, insert.length, { link: url });
const text = new doc.Text(yText);
const text = new Text(yText);
doc.addBlock(
'affine:paragraph',
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -709,9 +709,7 @@ export const defaultSlashMenuConfig: SlashMenuConfig = {
model.flavour as never,
{
type: (model as ParagraphBlockModel).type,
text: new rootComponent.doc.Text(
model.text.toDelta() as DeltaInsert[]
),
text: new Text(model.text.toDelta() as DeltaInsert[]),
// @ts-expect-error FIXME: ts error
checked: model.checked,
},
Expand Down
15 changes: 8 additions & 7 deletions blocksuite/framework/store/src/__tests__/collection.unit.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { applyUpdate, encodeStateAsUpdate } from 'yjs';
import { COLLECTION_VERSION, PAGE_VERSION } from '../consts.js';
import type { BlockModel, Blocks, BlockSchemaType } from '../index.js';
import { Schema } from '../index.js';
import { Text } from '../reactive/text.js';
import type { DocMeta } from '../store/workspace.js';
import { TestWorkspace } from '../test/test-workspace.js';
import { createAutoIncrementIdGenerator } from '../utils/id-generator.js';
Expand Down Expand Up @@ -166,7 +167,7 @@ describe('basic', () => {

doc.load(() => {
const rootId = doc.addBlock('affine:page', {
title: new doc.Text(),
title: new Text(),
});
expect(rootAddedCallback).toBeCalledTimes(1);

Expand All @@ -186,7 +187,7 @@ describe('basic', () => {
});
doc.load(() => {
doc.addBlock('affine:page', {
title: new doc.Text(),
title: new Text(),
});
});
{
Expand Down Expand Up @@ -245,7 +246,7 @@ describe('addBlock', () => {
it('can add single model', () => {
const doc = createTestDoc();
doc.addBlock('affine:page', {
title: new doc.Text(),
title: new Text(),
});

assert.deepEqual(serializCollection(doc.rootDoc).spaces[spaceId].blocks, {
Expand All @@ -264,7 +265,7 @@ describe('addBlock', () => {

it('can add model with props', () => {
const doc = createTestDoc();
doc.addBlock('affine:page', { title: new doc.Text('hello') });
doc.addBlock('affine:page', { title: new Text('hello') });

assert.deepEqual(serializCollection(doc.rootDoc).spaces[spaceId].blocks, {
'0': {
Expand All @@ -283,7 +284,7 @@ describe('addBlock', () => {
it('can add multi models', () => {
const doc = createTestDoc();
const rootId = doc.addBlock('affine:page', {
title: new doc.Text(),
title: new Text(),
});
const noteId = doc.addBlock('affine:note', {}, rootId);
doc.addBlock('affine:paragraph', {}, noteId);
Expand Down Expand Up @@ -344,7 +345,7 @@ describe('addBlock', () => {

queueMicrotask(() =>
doc.addBlock('affine:page', {
title: new doc.Text(),
title: new Text(),
})
);
const blockId = await waitOnce(doc.slots.rootAdded);
Expand Down Expand Up @@ -389,7 +390,7 @@ describe('addBlock', () => {
assert.equal(collection.docs.size, 2);

doc0.addBlock('affine:page', {
title: new doc0.Text(),
title: new Text(),
});
collection.removeDoc(doc0.id);

Expand Down
18 changes: 5 additions & 13 deletions blocksuite/framework/store/src/store/doc/doc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import { signal } from '@preact/signals-core';
import type { BlockModel, Schema } from '../../schema/index.js';
import type { DraftModel } from '../../transformer/index.js';
import { syncBlockProps } from '../../utils/utils.js';
import type { BlockProps, Doc } from '../workspace.js';
import type { BlockOptions } from './block/index.js';
import { Block } from './block/index.js';
import type { BlockCollection, BlockProps } from './block-collection.js';
import { DocCRUD } from './crud.js';
import { type Query, runQuery } from './query.js';

type DocOptions = {
schema: Schema;
blockCollection: BlockCollection;
blockCollection: Doc;
readonly?: boolean;
query?: Query;
};
Expand All @@ -23,7 +23,7 @@ export class Blocks {
runQuery(this._query, block);
};

protected readonly _blockCollection: BlockCollection;
protected readonly _blockCollection: Doc;

protected readonly _blocks = signal<Record<string, Block>>({});

Expand All @@ -40,7 +40,7 @@ export class Blocks {

protected readonly _schema: Schema;

readonly slots: BlockCollection['slots'] & {
readonly slots: Doc['slots'] & {
/** This is always triggered after `doc.load` is called. */
ready: Slot;
/**
Expand Down Expand Up @@ -179,10 +179,6 @@ export class Blocks {
return this._blockCollection.collection;
}

get generateBlockId() {
return this._blockCollection.generateBlockId.bind(this._blockCollection);
}

get history() {
return this._blockCollection.history;
}
Expand Down Expand Up @@ -250,10 +246,6 @@ export class Blocks {
return this._blockCollection.spaceDoc;
}

get Text() {
return this._blockCollection.Text;
}

get transact() {
return this._blockCollection.transact.bind(this._blockCollection);
}
Expand Down Expand Up @@ -432,7 +424,7 @@ export class Blocks {
);
}

const id = blockProps.id ?? this._blockCollection.generateBlockId();
const id = blockProps.id ?? this._blockCollection.collection.idGenerator();

this.transact(() => {
this._crud.addBlock(
Expand Down
1 change: 0 additions & 1 deletion blocksuite/framework/store/src/store/doc/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export * from './block/index.js';
export * from './block-collection.js';
export * from './consts.js';
export * from './doc.js';
export * from './query.js';
1 change: 0 additions & 1 deletion blocksuite/framework/store/src/store/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export type * from './doc/block-collection.js';
export * from './doc/index.js';
export * from './meta.js';
export * from './workspace.js';
Loading

0 comments on commit dcf4993

Please sign in to comment.