feat(editor): merge store and blocks (#9591)

This commit is contained in:
Saul-Mirone
2025-01-08 13:01:19 +00:00
parent a0cba55a5b
commit 5842d45ab1
124 changed files with 362 additions and 414 deletions

View File

@@ -11,7 +11,7 @@ import type { EditorHost } from '@blocksuite/block-std';
import { DataSourceBase, type PropertyMetaConfig } from '@blocksuite/data-view';
import { propertyPresets } from '@blocksuite/data-view/property-presets';
import { assertExists, Slot } from '@blocksuite/global/utils';
import type { Block, Blocks } from '@blocksuite/store';
import type { Block, Store } from '@blocksuite/store';
import type { BlockMeta } from './block-meta/base.js';
import { blockMetaMap } from './block-meta/index.js';
@@ -73,14 +73,14 @@ export class BlockQueryDataSource extends DataSourceBase {
this.columnMetaMap.set(property.metaConfig.type, property.metaConfig);
}
for (const collection of this.workspace.docs.values()) {
for (const block of Object.values(collection.getBlocks().blocks.peek())) {
for (const block of Object.values(collection.getStore().blocks.peek())) {
if (this.meta.selector(block)) {
this.blockMap.set(block.id, block);
}
}
}
this.workspace.docs.forEach(doc => {
this.listenToDoc(doc.getBlocks());
this.listenToDoc(doc.getStore());
});
this.workspace.slots.docCreated.on(id => {
const doc = this.workspace.getDoc(id);
@@ -140,7 +140,7 @@ export class BlockQueryDataSource extends DataSourceBase {
return this.block.columns.find(v => v.id === id);
}
listenToDoc(doc: Blocks) {
listenToDoc(doc: Store) {
this.docDisposeMap.set(
doc.id,
doc.slots.blockUpdated.on(v => {

View File

@@ -4,7 +4,7 @@ import {
} from '@blocksuite/affine-model';
import { BlockService } from '@blocksuite/block-std';
import { viewPresets } from '@blocksuite/data-view/view-presets';
import type { BlockModel, Blocks } from '@blocksuite/store';
import type { BlockModel, Store } from '@blocksuite/store';
import {
databaseViewAddView,
@@ -36,7 +36,7 @@ export class DatabaseBlockService extends BlockService {
viewPresets = viewPresets;
initDatabaseBlock(
doc: Blocks,
doc: Store,
model: BlockModel,
databaseId: string,
viewType: string,

View File

@@ -11,12 +11,11 @@ import { BlockStdScope } from '@blocksuite/block-std';
import { assertExists } from '@blocksuite/global/utils';
import {
type BlockModel,
type Blocks,
type BlockSnapshot,
type DraftModel,
type Query,
Slice,
Store,
type Store,
Text,
} from '@blocksuite/store';
import { render, type TemplateResult } from 'lit';
@@ -196,11 +195,10 @@ async function renderNoteContent(
mode: 'strict',
match: ids.map(id => ({ id, viewType: 'display' })),
};
const previewDoc = doc.doc.getBlocks({ query });
const previewDoc = doc.doc.getStore({ query });
const previewSpec = SpecProvider.getInstance().getSpec('page:preview');
const store = new Store({ blocks: previewDoc });
const previewStd = new BlockStdScope({
store,
store: previewDoc,
extensions: previewSpec.value,
});
const previewTemplate = previewStd.render();
@@ -222,7 +220,7 @@ function filterTextModel(model: BlockModel) {
return false;
}
export function getNotesFromDoc(doc: Blocks) {
export function getNotesFromDoc(doc: Store) {
const notes = doc.root?.children.filter(
child =>
matchFlavours(child, ['affine:note']) &&
@@ -236,7 +234,7 @@ export function getNotesFromDoc(doc: Blocks) {
return notes;
}
export function isEmptyDoc(doc: Blocks | null, mode: DocMode) {
export function isEmptyDoc(doc: Store | null, mode: DocMode) {
if (!doc) {
return true;
}
@@ -268,7 +266,7 @@ export function isEmptyNote(note: BlockModel) {
/**
* Gets the document content with a max length.
*/
export function getDocContentWithMaxLength(doc: Blocks, maxlength = 500) {
export function getDocContentWithMaxLength(doc: Store, maxlength = 500) {
const notes = getNotesFromDoc(doc);
if (!notes) return;
@@ -328,7 +326,7 @@ export function promptDocTitle(std: BlockStdScope, autofill?: string) {
});
}
export function notifyDocCreated(std: BlockStdScope, doc: Blocks) {
export function notifyDocCreated(std: BlockStdScope, doc: Store) {
const notification = std.getOptional(NotificationProvider);
if (!notification) return;
@@ -367,7 +365,7 @@ export function notifyDocCreated(std: BlockStdScope, doc: Blocks) {
export async function convertSelectedBlocksToLinkedDoc(
std: BlockStdScope,
doc: Blocks,
doc: Store,
selectedModels: DraftModel[] | Promise<DraftModel[]>,
docTitle?: string
) {
@@ -401,7 +399,7 @@ export async function convertSelectedBlocksToLinkedDoc(
export function createLinkedDocFromSlice(
std: BlockStdScope,
doc: Blocks,
doc: Store,
snapshots: BlockSnapshot[],
docTitle?: string
) {

View File

@@ -10,7 +10,6 @@ import {
} from '@blocksuite/affine-shared/services';
import { BlockSelection, BlockStdScope } from '@blocksuite/block-std';
import { Bound } from '@blocksuite/global/utils';
import { Store } from '@blocksuite/store';
import { html, nothing } from 'lit';
import { choose } from 'lit/directives/choose.js';
import { classMap } from 'lit/directives/class-map.js';
@@ -31,8 +30,6 @@ export class EmbedEdgelessSyncedDocBlockComponent extends toEdgelessEmbedBlock(
return html`${nothing}`;
}
const store = new Store({ blocks: syncedDoc });
let containerStyleMap = styleMap({
position: 'relative',
width: '100%',
@@ -70,7 +67,7 @@ export class EmbedEdgelessSyncedDocBlockComponent extends toEdgelessEmbedBlock(
() => html`
<div class="affine-page-viewport" data-theme=${appTheme}>
${new BlockStdScope({
store,
store: syncedDoc,
extensions: this._buildPreviewSpec('page:preview'),
}).render()}
</div>
@@ -81,7 +78,7 @@ export class EmbedEdgelessSyncedDocBlockComponent extends toEdgelessEmbedBlock(
() => html`
<div class="affine-edgeless-viewport" data-theme=${edgelessTheme}>
${new BlockStdScope({
store,
store: syncedDoc,
extensions: this._buildPreviewSpec('edgeless:preview'),
}).render()}
</div>

View File

@@ -26,12 +26,7 @@ import {
} from '@blocksuite/block-std';
import { GfxControllerIdentifier } from '@blocksuite/block-std/gfx';
import { assertExists, Bound, getCommonBound } from '@blocksuite/global/utils';
import {
type GetBlocksOptions,
type Query,
Store,
Text,
} from '@blocksuite/store';
import { type GetBlocksOptions, type Query, Text } from '@blocksuite/store';
import { computed } from '@preact/signals-core';
import { html, nothing, type PropertyValues } from 'lit';
import { query, state } from 'lit/decorators.js';
@@ -156,8 +151,6 @@ export class EmbedSyncedDocBlockComponent extends EmbedBlockComponent<EmbedSynce
return html`${nothing}`;
}
const store = new Store({ blocks: syncedDoc });
if (isPageMode) {
this.dataset.pageMode = '';
}
@@ -186,7 +179,7 @@ export class EmbedSyncedDocBlockComponent extends EmbedBlockComponent<EmbedSynce
() => html`
<div class="affine-page-viewport" data-theme=${appTheme}>
${new BlockStdScope({
store,
store: syncedDoc,
extensions: this._buildPreviewSpec('page:preview'),
}).render()}
</div>
@@ -197,7 +190,7 @@ export class EmbedSyncedDocBlockComponent extends EmbedBlockComponent<EmbedSynce
() => html`
<div class="affine-edgeless-viewport" data-theme=${edgelessTheme}>
${new BlockStdScope({
store,
store: syncedDoc,
extensions: this._buildPreviewSpec('edgeless:preview'),
}).render()}
</div>

View File

@@ -3,7 +3,7 @@ import {
getNextContinuousNumberedLists,
matchFlavours,
} from '@blocksuite/affine-shared/utils';
import type { BlockModel, Blocks } from '@blocksuite/store';
import type { BlockModel, Store } from '@blocksuite/store';
/**
* correct target is a numbered list, which is divided into two steps:
@@ -12,7 +12,7 @@ import type { BlockModel, Blocks } from '@blocksuite/store';
* 2. find continuous lists starting from the target list and keep their order continuous
*/
export function correctNumberedListsOrderToPrev(
doc: Blocks,
doc: Store,
modelOrId: BlockModel | string,
transact = true
) {
@@ -58,7 +58,7 @@ export function correctNumberedListsOrderToPrev(
}
}
export function correctListOrder(doc: Blocks, model: ListBlockModel) {
export function correctListOrder(doc: Store, model: ListBlockModel) {
// old numbered list has no order
if (model.type === 'numbered' && !Number.isInteger(model.order)) {
correctNumberedListsOrderToPrev(doc, model, false);

View File

@@ -18,7 +18,7 @@ import {
ShadowlessElement,
} from '@blocksuite/block-std';
import { deserializeXYWH, WithDisposable } from '@blocksuite/global/utils';
import { type BlockModel, type Query, Store } from '@blocksuite/store';
import { type BlockModel, type Query } from '@blocksuite/store';
import { css, nothing } from 'lit';
import { property } from 'lit/decorators.js';
import { styleMap } from 'lit/directives/style-map.js';
@@ -114,14 +114,13 @@ export class SurfaceRefNotePortal extends WithDisposable(ShadowlessElement) {
console.error('Query is not set before rendering note preview');
return nothing;
}
const doc = this.model.doc.doc.getBlocks({
const doc = this.model.doc.doc.getStore({
query: this.query,
readonly: true,
});
const store = new Store({ blocks: doc });
const previewSpec = SpecProvider.getInstance().getSpec('page:preview');
return new BlockStdScope({
store,
store: doc,
extensions: previewSpec.value.slice(),
}).render();
}

View File

@@ -47,7 +47,7 @@ import {
DisposableGroup,
type SerializedXYWH,
} from '@blocksuite/global/utils';
import { type Blocks, Store } from '@blocksuite/store';
import { type Store } from '@blocksuite/store';
import { css, html, nothing, type TemplateResult } from 'lit';
import { query, state } from 'lit/decorators.js';
import { styleMap } from 'lit/directives/style-map.js';
@@ -240,7 +240,7 @@ export class SurfaceRefBlockComponent extends BlockComponent<SurfaceRefBlockMode
}
`;
private _previewDoc: Blocks | null = null;
private _previewDoc: Store | null = null;
private readonly _previewSpec =
SpecProvider.getInstance().getSpec('edgeless:preview');
@@ -339,7 +339,7 @@ export class SurfaceRefBlockComponent extends BlockComponent<SurfaceRefBlockMode
}
const doc = [...this.std.workspace.docs.values()]
.map(doc => doc.getBlocks())
.map(doc => doc.getStore())
.find(
doc =>
doc.getBlock(this.model.reference) ||
@@ -546,9 +546,8 @@ export class SurfaceRefBlockComponent extends BlockComponent<SurfaceRefBlockMode
if (!this._viewportEditor) {
if (this._previewDoc) {
const store = new Store({ blocks: this._previewDoc });
this._viewportEditor = new BlockStdScope({
store,
store: this._previewDoc,
extensions: _previewSpec,
}).render();
} else {

View File

@@ -1,8 +1,8 @@
import type { Blocks } from '@blocksuite/store';
import type { Store } from '@blocksuite/store';
import type { SurfaceBlockModel } from '../surface-model';
export function getSurfaceBlock(doc: Blocks) {
export function getSurfaceBlock(doc: Store) {
const blocks = doc.getBlocksByFlavour('affine:surface');
return blocks.length !== 0 ? (blocks[0].model as SurfaceBlockModel) : null;
}

View File

@@ -8,7 +8,7 @@ import {
stdContext,
} from '@blocksuite/block-std';
import { WithDisposable } from '@blocksuite/global/utils';
import type { BlockModel, Blocks } from '@blocksuite/store';
import type { BlockModel, Store } from '@blocksuite/store';
import { Text } from '@blocksuite/store';
import { consume } from '@lit/context';
import { css, html, nothing } from 'lit';
@@ -161,7 +161,7 @@ export class BlockCaptionEditor<
accessor display = false;
@consume({ context: docContext })
accessor doc!: Blocks;
accessor doc!: Store;
@query('.block-caption-editor')
accessor input!: HTMLInputElement;

View File

@@ -26,7 +26,7 @@ import {
ZERO_WIDTH_NON_JOINER,
ZERO_WIDTH_SPACE,
} from '@blocksuite/inline';
import type { Blocks, DocMeta } from '@blocksuite/store';
import type { DocMeta, Store } from '@blocksuite/store';
import { css, html, nothing } from 'lit';
import { property, state } from 'lit/decorators.js';
import { choose } from 'lit/directives/choose.js';
@@ -73,7 +73,7 @@ export class AffineReference extends WithDisposable(ShadowlessElement) {
}
`;
private readonly _updateRefMeta = (doc: Blocks) => {
private readonly _updateRefMeta = (doc: Store) => {
const refAttribute = this.delta.attributes?.reference;
if (!refAttribute) {
return;

View File

@@ -1,9 +1,9 @@
import type { BlockStdScope, EditorHost } from '@blocksuite/block-std';
import type { GfxModel } from '@blocksuite/block-std/gfx';
import type { BlockModel, Blocks } from '@blocksuite/store';
import type { BlockModel, Store } from '@blocksuite/store';
export abstract class MenuContext {
abstract get doc(): Blocks;
abstract get doc(): Store;
get firstElement(): GfxModel | null {
return null;

View File

@@ -11,7 +11,7 @@ import {
LinkedPageIcon,
PageIcon,
} from '@blocksuite/icons/lit';
import type { Blocks } from '@blocksuite/store';
import type { Store } from '@blocksuite/store';
import { computed, type Signal, signal } from '@preact/signals-core';
import type { TemplateResult } from 'lit';
@@ -70,9 +70,9 @@ export class DocDisplayMetaService
readonly disposables: Disposable[] = [];
readonly iconMap = new WeakMap<Blocks, Signal<TemplateResult>>();
readonly iconMap = new WeakMap<Store, Signal<TemplateResult>>();
readonly titleMap = new WeakMap<Blocks, Signal<string>>();
readonly titleMap = new WeakMap<Store, Signal<string>>();
static override setup(di: Container) {
di.addImpl(DocDisplayMetaProvider, this, [StdIdentifier]);

View File

@@ -1,4 +1,4 @@
import type { BlockModel, Blocks, DraftModel } from '@blocksuite/store';
import type { BlockModel, DraftModel, Store } from '@blocksuite/store';
import { minimatch } from 'minimatch';
export function matchFlavours<Key extends (keyof BlockSuite.BlockModels)[]>(
@@ -14,7 +14,7 @@ export function matchFlavours<Key extends (keyof BlockSuite.BlockModels)[]>(
}
export function isInsideBlockByFlavour(
doc: Blocks,
doc: Store,
block: BlockModel | string,
flavour: string
): boolean {

View File

@@ -1,6 +1,6 @@
import { type NoteBlockModel, NoteDisplayMode } from '@blocksuite/affine-model';
import type { BlockComponent, EditorHost } from '@blocksuite/block-std';
import type { BlockModel, Blocks } from '@blocksuite/store';
import type { BlockModel, Store } from '@blocksuite/store';
import { matchFlavours } from './checker.js';
@@ -41,7 +41,7 @@ export function findNoteBlockModel(model: BlockModel) {
) as NoteBlockModel | null;
}
export function getLastNoteBlock(doc: Blocks) {
export function getLastNoteBlock(doc: Store) {
let note: NoteBlockModel | null = null;
if (!doc.root) return null;
const { children } = doc.root;

View File

@@ -1,6 +1,6 @@
import type { ListBlockModel } from '@blocksuite/affine-model';
import type { BlockStdScope } from '@blocksuite/block-std';
import type { BlockModel, Blocks } from '@blocksuite/store';
import type { BlockModel, Store } from '@blocksuite/store';
import { matchFlavours } from './checker.js';
@@ -9,7 +9,7 @@ import { matchFlavours } from './checker.js';
* typically used for updating list numbers. The result not contains the list passed in.
*/
export function getNextContinuousNumberedLists(
doc: Blocks,
doc: Store,
modelOrId: BlockModel | string
): ListBlockModel[] {
const model =

View File

@@ -1,9 +1,9 @@
import type { BlockModel, Blocks, Text } from '@blocksuite/store';
import type { BlockModel, Store, Text } from '@blocksuite/store';
export function transformModel(
model: BlockModel,
flavour: BlockSuite.Flavour,
props?: Parameters<Blocks['addBlock']>[1]
props?: Parameters<Store['addBlock']>[1]
) {
const doc = model.doc;
const parent = doc.getParent(model);

View File

@@ -5,7 +5,7 @@ import {
type DndEventState,
} from '@blocksuite/block-std';
import { Point } from '@blocksuite/global/utils';
import { type BlockViewType, type Query, Store } from '@blocksuite/store';
import { type BlockViewType, type Query } from '@blocksuite/store';
import { DragPreview } from '../components/drag-preview.js';
import type { AffineDragHandleWidget } from '../drag-handle.js';
@@ -79,10 +79,9 @@ export class PreviewHelper {
const query = this._calculateQuery(selectedIds);
const doc = this.widget.doc.doc.getBlocks({ query });
const store = this.widget.doc.doc.getStore({ query });
const previewSpec = SpecProvider.getInstance().getSpec('page:preview');
const store = new Store({ blocks: doc });
const previewStd = new BlockStdScope({
store,
extensions: previewSpec.value,