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

@@ -5,7 +5,7 @@ import type {
GroupElementModel,
NoteBlockModel,
} from '@blocksuite/blocks';
import { type BlockModel, type Blocks } from '@blocksuite/store';
import type { BlockModel, Store } from '@blocksuite/store';
import { beforeEach, describe, expect, test } from 'vitest';
import * as Y from 'yjs';
@@ -19,7 +19,7 @@ import { setupEditor } from '../utils/setup.js';
let service!: EdgelessRootBlockComponent['service'];
const addNote = (doc: Blocks, props: Record<string, unknown> = {}) => {
const addNote = (doc: Store, props: Record<string, unknown> = {}) => {
return _addNote(doc, {
index: service.layer.generateIndex(),
...props,

View File

@@ -3,11 +3,11 @@ import type {
PageRootBlockComponent,
SurfaceBlockComponent,
} from '@blocksuite/blocks';
import type { Blocks } from '@blocksuite/store';
import type { Store } from '@blocksuite/store';
import type { AffineEditorContainer } from '../../index.js';
export function getSurface(doc: Blocks, editor: AffineEditorContainer) {
export function getSurface(doc: Store, editor: AffineEditorContainer) {
const surfaceModel = doc.getBlockByFlavour('affine:surface');
return editor.host!.view.getBlock(
@@ -16,17 +16,17 @@ export function getSurface(doc: Blocks, editor: AffineEditorContainer) {
}
export function getDocRootBlock(
doc: Blocks,
doc: Store,
editor: AffineEditorContainer,
mode: 'page'
): PageRootBlockComponent;
export function getDocRootBlock(
doc: Blocks,
doc: Store,
editor: AffineEditorContainer,
mode: 'edgeless'
): EdgelessRootBlockComponent;
export function getDocRootBlock(
doc: Blocks,
doc: Store,
editor: AffineEditorContainer,
_?: 'edgeless' | 'page'
) {
@@ -35,7 +35,7 @@ export function getDocRootBlock(
| PageRootBlockComponent;
}
export function addNote(doc: Blocks, props: Record<string, any> = {}) {
export function addNote(doc: Store, props: Record<string, any> = {}) {
const noteId = doc.addBlock(
'affine:note',
{

View File

@@ -1,5 +1,5 @@
import { effects as blocksEffects } from '@blocksuite/blocks/effects';
import type { Blocks, Job } from '@blocksuite/store';
import type { Job, Store } from '@blocksuite/store';
import { effects } from '../../effects.js';
@@ -59,7 +59,7 @@ async function createEditor(collection: TestWorkspace, mode: DocMode = 'page') {
const app = document.createElement('div');
const blockCollection = collection.docs.values().next().value;
assertExists(blockCollection, 'Need to create a doc first');
const doc = blockCollection.getBlocks();
const doc = blockCollection.getStore();
const editor = new AffineEditorContainer();
editor.doc = doc;
editor.mode = mode;
@@ -109,12 +109,12 @@ export function cleanup() {
declare global {
const editor: AffineEditorContainer;
const doc: Blocks;
const doc: Store;
const collection: TestWorkspace;
const job: Job;
interface Window {
editor: AffineEditorContainer;
doc: Blocks;
doc: Store;
job: Job;
collection: TestWorkspace;
}

View File

@@ -1,7 +1,7 @@
import { BlockStdScope, ShadowlessElement } from '@blocksuite/block-std';
import { EdgelessEditorBlockSpecs, ThemeProvider } from '@blocksuite/blocks';
import { SignalWatcher, WithDisposable } 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 { property, state } from 'lit/decorators.js';
import { guard } from 'lit/directives/guard.js';
@@ -48,9 +48,8 @@ export class EdgelessEditor extends SignalWatcher(
this._disposables.add(
this.doc.slots.rootAdded.on(() => this.requestUpdate())
);
const store = new Store({ blocks: this.doc });
this.std = new BlockStdScope({
store,
store: this.doc,
extensions: this.specs,
});
}
@@ -78,16 +77,15 @@ export class EdgelessEditor extends SignalWatcher(
) {
super.willUpdate(changedProperties);
if (changedProperties.has('doc')) {
const store = new Store({ blocks: this.doc });
this.std = new BlockStdScope({
store,
store: this.doc,
extensions: this.specs,
});
}
}
@property({ attribute: false })
accessor doc!: Blocks;
accessor doc!: Store;
@property({ attribute: false })
accessor editor!: TemplateResult;

View File

@@ -9,9 +9,8 @@ import {
import { SignalWatcher, Slot, WithDisposable } from '@blocksuite/global/utils';
import {
type BlockModel,
type Blocks,
type ExtensionType,
Store,
type Store,
} from '@blocksuite/store';
import { computed, signal } from '@preact/signals-core';
import { css, html } from 'lit';
@@ -89,7 +88,7 @@ export class AffineEditorContainer
}
`;
private readonly _doc = signal<Blocks>();
private readonly _doc = signal<Store>();
private readonly _edgelessSpecs = signal<ExtensionType[]>(
EdgelessEditorBlockSpecs
@@ -105,13 +104,9 @@ export class AffineEditorContainer
: this._edgelessSpecs.value
);
private readonly _store = computed(() => {
return new Store({ blocks: this.doc });
});
private readonly _std = computed(() => {
return new BlockStdScope({
store: this._store.value,
store: this.doc,
extensions: this._specs.value,
});
});
@@ -128,10 +123,10 @@ export class AffineEditorContainer
};
get doc() {
return this._doc.value as Blocks;
return this._doc.value as Store;
}
set doc(doc: Blocks) {
set doc(doc: Store) {
this._doc.value = doc;
}

View File

@@ -5,7 +5,7 @@ import {
} from '@blocksuite/block-std';
import { PageEditorBlockSpecs, ThemeProvider } from '@blocksuite/blocks';
import { noop, SignalWatcher, WithDisposable } from '@blocksuite/global/utils';
import { type Blocks, Store } from '@blocksuite/store';
import type { Store } from '@blocksuite/store';
import { css, html, nothing } from 'lit';
import { property, state } from 'lit/decorators.js';
import { guard } from 'lit/directives/guard.js';
@@ -59,9 +59,8 @@ export class PageEditor extends SignalWatcher(
this._disposables.add(
this.doc.slots.rootAdded.on(() => this.requestUpdate())
);
const store = new Store({ blocks: this.doc });
this.std = new BlockStdScope({
store,
store: this.doc,
extensions: this.specs,
});
}
@@ -94,16 +93,15 @@ export class PageEditor extends SignalWatcher(
) {
super.willUpdate(changedProperties);
if (changedProperties.has('doc')) {
const store = new Store({ blocks: this.doc });
this.std = new BlockStdScope({
store,
store: this.doc,
extensions: this.specs,
});
}
}
@property({ attribute: false })
accessor doc!: Blocks;
accessor doc!: Store;
@property({ type: Boolean })
accessor hasViewport = true;

View File

@@ -2,7 +2,7 @@ import type { EditorHost } from '@blocksuite/block-std';
import { ShadowlessElement } from '@blocksuite/block-std';
import type { RichText, RootBlockModel } from '@blocksuite/blocks';
import { assertExists, WithDisposable } from '@blocksuite/global/utils';
import type { Blocks } from '@blocksuite/store';
import type { Store } from '@blocksuite/store';
import { css, html } from 'lit';
import { property, query, state } from 'lit/decorators.js';
@@ -175,7 +175,7 @@ export class DocTitle extends WithDisposable(ShadowlessElement) {
private accessor _richTextElement!: RichText;
@property({ attribute: false })
accessor doc!: Blocks;
accessor doc!: Store;
}
export function getDocTitleByEditorHost(

View File

@@ -13,7 +13,7 @@ import {
SignalWatcher,
WithDisposable,
} from '@blocksuite/global/utils';
import type { Blocks } from '@blocksuite/store';
import type { Store } from '@blocksuite/store';
import { css, html, nothing, type PropertyValues } from 'lit';
import { property, query, state } from 'lit/decorators.js';
import { keyed } from 'lit/directives/keyed.js';
@@ -326,7 +326,7 @@ export class FramePanelBody extends SignalWatcher(
}
}
private _setDocDisposables(doc: Blocks) {
private _setDocDisposables(doc: Store) {
this._clearDocDisposables();
this._docDisposables = new DisposableGroup();
this._docDisposables.add(

View File

@@ -13,7 +13,7 @@ import {
SignalWatcher,
WithDisposable,
} from '@blocksuite/global/utils';
import type { Blocks } from '@blocksuite/store';
import type { Store } from '@blocksuite/store';
import { effect, signal } from '@preact/signals-core';
import { css, html, LitElement, nothing, type PropertyValues } from 'lit';
import { property, query, state } from 'lit/decorators.js';
@@ -698,7 +698,7 @@ export class OutlinePanelBody extends SignalWatcher(
private accessor _selected: string[] = [];
@property({ attribute: false })
accessor doc!: Blocks;
accessor doc!: Store;
@property({ attribute: false })
accessor domHost!: Document | HTMLElement;

View File

@@ -7,7 +7,7 @@ import {
once,
} from '@blocksuite/blocks';
import { SignalWatcher, WithDisposable } from '@blocksuite/global/utils';
import type { BlockModel, Blocks } from '@blocksuite/store';
import type { BlockModel, Store } from '@blocksuite/store';
import { baseTheme } from '@toeverything/theme';
import { css, html, LitElement, unsafeCSS } from 'lit';
import { property, query, state } from 'lit/decorators.js';
@@ -393,7 +393,7 @@ export class OutlineNoteCard extends SignalWatcher(WithDisposable(LitElement)) {
accessor activeHeadingId: string | null = null;
@property({ attribute: false })
accessor doc!: Blocks;
accessor doc!: Store;
@property({ attribute: false })
accessor editorMode: 'page' | 'edgeless' = 'page';

View File

@@ -1,5 +1,5 @@
import { on, once } from '@blocksuite/blocks';
import type { Blocks } from '@blocksuite/store';
import type { Store } from '@blocksuite/store';
import type { OutlinePanelBody } from '../body/outline-panel-body.js';
import type { OutlineNoteCard } from '../card/outline-card.js';
@@ -15,7 +15,7 @@ export function startDragging(options: {
document: Document;
host: Document | HTMLElement;
container: OutlinePanelBody;
doc: Blocks;
doc: Store;
}) {
const {
document,

View File

@@ -5,7 +5,7 @@ import {
type ParagraphBlockModel,
type RootBlockModel,
} from '@blocksuite/blocks';
import type { BlockModel, Blocks } from '@blocksuite/store';
import type { BlockModel, Store } from '@blocksuite/store';
import { headingKeys } from '../config.js';
@@ -22,7 +22,7 @@ type OutlineNoteItem = {
};
export function getNotesFromDoc(
doc: Blocks,
doc: Store,
modes: NoteDisplayMode[]
): OutlineNoteItem[] {
const rootModel = doc.root;
@@ -74,7 +74,7 @@ export function getHeadingBlocksFromNote(
}
export function getHeadingBlocksFromDoc(
doc: Blocks,
doc: Store,
modes: NoteDisplayMode[],
ignoreEmpty = false
) {