Skip to content

Commit

Permalink
feat(editor): merge store and blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
Saul-Mirone committed Jan 8, 2025
1 parent a0cba55 commit ffb4ebf
Show file tree
Hide file tree
Showing 117 changed files with 341 additions and 393 deletions.
4 changes: 2 additions & 2 deletions blocksuite/affine/block-data-view/src/data-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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 => {
Expand Down
4 changes: 2 additions & 2 deletions blocksuite/affine/block-database/src/database-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -36,7 +36,7 @@ export class DatabaseBlockService extends BlockService {
viewPresets = viewPresets;

initDatabaseBlock(
doc: Blocks,
doc: Store,
model: BlockModel,
databaseId: string,
viewType: string,
Expand Down
18 changes: 8 additions & 10 deletions blocksuite/affine/block-embed/src/common/render-linked-doc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -198,9 +197,8 @@ async function renderNoteContent(
};
const previewDoc = doc.doc.getBlocks({ 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();
Expand All @@ -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']) &&
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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
) {
Expand Down Expand Up @@ -401,7 +399,7 @@ export async function convertSelectedBlocksToLinkedDoc(

export function createLinkedDocFromSlice(
std: BlockStdScope,
doc: Blocks,
doc: Store,
snapshots: BlockSnapshot[],
docTitle?: string
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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%',
Expand Down Expand Up @@ -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>
Expand All @@ -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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -156,8 +151,6 @@ export class EmbedSyncedDocBlockComponent extends EmbedBlockComponent<EmbedSynce
return html`${nothing}`;
}

const store = new Store({ blocks: syncedDoc });

if (isPageMode) {
this.dataset.pageMode = '';
}
Expand Down Expand Up @@ -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>
Expand All @@ -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>
Expand Down
6 changes: 3 additions & 3 deletions blocksuite/affine/block-list/src/commands/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
) {
Expand Down Expand Up @@ -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);
Expand Down
5 changes: 2 additions & 3 deletions blocksuite/affine/block-surface-ref/src/portal/note.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -118,10 +118,9 @@ export class SurfaceRefNotePortal extends WithDisposable(ShadowlessElement) {
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();
}
Expand Down
7 changes: 3 additions & 4 deletions blocksuite/affine/block-surface-ref/src/surface-ref-block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
4 changes: 2 additions & 2 deletions blocksuite/affine/components/src/caption/block-caption.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions blocksuite/affine/components/src/toolbar/menu-context.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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]);
Expand Down
4 changes: 2 additions & 2 deletions blocksuite/affine/shared/src/utils/model/checker.ts
Original file line number Diff line number Diff line change
@@ -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)[]>(
Expand All @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions blocksuite/affine/shared/src/utils/model/getter.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions blocksuite/affine/shared/src/utils/model/list.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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 =
Expand Down
Loading

0 comments on commit ffb4ebf

Please sign in to comment.