Skip to content

Commit

Permalink
refactor(editor): reorg code structure of store package (#9525)
Browse files Browse the repository at this point in the history
  • Loading branch information
Saul-Mirone committed Jan 5, 2025
1 parent 1180e9b commit 3d168ba
Show file tree
Hide file tree
Showing 55 changed files with 619 additions and 636 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
type BlockModel,
type Blocks,
type BlockSnapshot,
BlockViewType,
type DraftModel,
type Query,
Slice,
Expand Down Expand Up @@ -194,7 +193,7 @@ async function renderNoteContent(
});
const query: Query = {
mode: 'strict',
match: ids.map(id => ({ id, viewType: BlockViewType.Display })),
match: ids.map(id => ({ id, viewType: 'display' })),
};
const previewDoc = doc.doc.getBlocks({ query });
const previewSpec = SpecProvider.getInstance().getSpec('page:preview');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,7 @@ import {
} from '@blocksuite/block-std';
import { GfxControllerIdentifier } from '@blocksuite/block-std/gfx';
import { assertExists, Bound, getCommonBound } from '@blocksuite/global/utils';
import {
BlockViewType,
type GetBlocksOptions,
type Query,
Text,
} from '@blocksuite/store';
import { type GetBlocksOptions, type Query, Text } from '@blocksuite/store';
import { computed } from '@preact/signals-core';
import { html, type PropertyValues } from 'lit';
import { query, state } from 'lit/decorators.js';
Expand Down Expand Up @@ -106,7 +101,7 @@ export class EmbedSyncedDocBlockComponent extends EmbedBlockComponent<EmbedSynce
props: {
displayMode: NoteDisplayMode.EdgelessOnly,
},
viewType: BlockViewType.Hidden,
viewType: 'hidden',
},
],
};
Expand Down
4 changes: 2 additions & 2 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, BlockViewType, type Query } 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 @@ -48,7 +48,7 @@ export class SurfaceRefNotePortal extends WithDisposable(ShadowlessElement) {
mode: 'include',
match: Array.from(ancestors).map(id => ({
id,
viewType: BlockViewType.Display,
viewType: 'display',
})),
};
this.query = query;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
type DndEventState,
} from '@blocksuite/block-std';
import { Point } from '@blocksuite/global/utils';
import { BlockViewType, type Query } from '@blocksuite/store';
import type { BlockViewType, Query } from '@blocksuite/store';

import { DragPreview } from '../components/drag-preview.js';
import type { AffineDragHandleWidget } from '../drag-handle.js';
Expand All @@ -24,7 +24,7 @@ export class PreviewHelper {
const ids: Array<{ id: string; viewType: BlockViewType }> = selectedIds.map(
id => ({
id,
viewType: BlockViewType.Display,
viewType: 'display',
})
);

Expand All @@ -33,7 +33,7 @@ export class PreviewHelper {
let parent: string | null = block;
do {
if (!selectedIds.includes(parent)) {
ids.push({ viewType: BlockViewType.Bypass, id: parent });
ids.push({ viewType: 'bypass', id: parent });
}
parent = this.widget.doc.getParent(parent)?.id ?? null;
} while (parent && !ids.map(({ id }) => id).includes(parent));
Expand All @@ -43,7 +43,7 @@ export class PreviewHelper {
const addChildren = (id: string) => {
const children = this.widget.doc.getBlock(id)?.model.children ?? [];
children.forEach(child => {
ids.push({ viewType: BlockViewType.Display, id: child.id });
ids.push({ viewType: 'display', id: child.id });
addChildren(child.id);
});
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
DisposableGroup,
WithDisposable,
} from '@blocksuite/global/utils';
import { type Blocks, BlockViewType, type Query } from '@blocksuite/store';
import { type Blocks, type Query } from '@blocksuite/store';
import { css, html, nothing, type PropertyValues } from 'lit';
import { property, query, state } from 'lit/decorators.js';
import { styleMap } from 'lit/directives/style-map.js';
Expand Down Expand Up @@ -71,7 +71,7 @@ export class FramePreview extends WithDisposable(ShadowlessElement) {
match: [
{
flavour: 'affine:frame',
viewType: BlockViewType.Hidden,
viewType: 'hidden',
},
],
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BlockSuiteError, ErrorCode } from '@blocksuite/global/exceptions';
import { SignalWatcher, WithDisposable } from '@blocksuite/global/utils';
import { type BlockModel, Blocks, BlockViewType } from '@blocksuite/store';
import { type BlockModel, Blocks, type BlockViewType } from '@blocksuite/store';
import { consume, provide } from '@lit/context';
import { computed } from '@preact/signals-core';
import { nothing, type TemplateResult } from 'lit';
Expand Down Expand Up @@ -187,9 +187,9 @@ export class BlockComponent<

private _renderViewType(content: unknown) {
return choose(this.viewType, [
[BlockViewType.Display, () => content],
[BlockViewType.Hidden, () => nothing],
[BlockViewType.Bypass, () => this.renderChildren(this.model)],
['display', () => content],
['hidden', () => nothing],
['bypass', () => this.renderChildren(this.model)],
]);
}

Expand Down Expand Up @@ -310,7 +310,7 @@ export class BlockComponent<
accessor doc!: Blocks;

@property({ attribute: false })
accessor viewType: BlockViewType = BlockViewType.Display;
accessor viewType: BlockViewType = 'display';

@property({
attribute: false,
Expand Down
4 changes: 2 additions & 2 deletions blocksuite/framework/block-std/src/view/element/lit-host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
handleError,
} from '@blocksuite/global/exceptions';
import { SignalWatcher, Slot, WithDisposable } from '@blocksuite/global/utils';
import { type BlockModel, Blocks, BlockViewType } from '@blocksuite/store';
import { type BlockModel, Blocks } from '@blocksuite/store';
import { createContext, provide } from '@lit/context';
import { css, LitElement, nothing, type TemplateResult } from 'lit';
import { property } from 'lit/decorators.js';
Expand Down Expand Up @@ -44,7 +44,7 @@ export class EditorHost extends SignalWatcher(
private readonly _renderModel = (model: BlockModel): TemplateResult => {
const { flavour } = model;
const block = this.doc.getBlock(model.id);
if (!block || block.blockViewType === BlockViewType.Hidden) {
if (!block || block.blockViewType === 'hidden') {
return html`${nothing}`;
}
const schema = this.doc.schema.flavourSchemaMap.get(flavour);
Expand Down
9 changes: 5 additions & 4 deletions blocksuite/framework/store/src/__tests__/block.unit.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ import { describe, expect, test, vi } from 'vitest';
import * as Y from 'yjs';

import {
Block,
defineBlockSchema,
internalPrimitives,
Schema,
type SchemaToModel,
} from '../schema/index.js';
import { Block, type YBlock } from '../store/doc/block/index.js';
} from '../model/block/index.js';
import type { YBlock } from '../model/block/types.js';
import { Schema } from '../schema/index.js';
import { createAutoIncrementIdGenerator } from '../test/index.js';
import { TestWorkspace } from '../test/test-workspace.js';
import { createAutoIncrementIdGenerator } from '../utils/id-generator.js';

const pageSchema = defineBlockSchema({
flavour: 'page',
Expand Down
10 changes: 4 additions & 6 deletions blocksuite/framework/store/src/__tests__/collection.unit.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ import type { Slot } from '@blocksuite/global/utils';
import { assert, beforeEach, describe, expect, it, vi } from 'vitest';
import { applyUpdate, type Doc, encodeStateAsUpdate } from 'yjs';

import { COLLECTION_VERSION, PAGE_VERSION } from '../consts.js';
import type { BlockModel, Blocks, BlockSchemaType } from '../index.js';
import type { BlockModel, Blocks, BlockSchemaType, DocMeta } from '../index.js';
import { Schema } from '../index.js';
import { Text } from '../reactive/text.js';
import type { DocMeta } from '../store/workspace.js';
import { createAutoIncrementIdGenerator } from '../test/index.js';
import { TestWorkspace } from '../test/test-workspace.js';
import { createAutoIncrementIdGenerator } from '../utils/id-generator.js';
import {
NoteBlockSchema,
ParagraphBlockSchema,
Expand Down Expand Up @@ -115,8 +113,8 @@ describe('basic', () => {
tags: [],
},
],
workspaceVersion: COLLECTION_VERSION,
pageVersion: PAGE_VERSION,
workspaceVersion: 2,
pageVersion: 2,
blockVersions: {
'affine:note': 1,
'affine:page': 2,
Expand Down
15 changes: 7 additions & 8 deletions blocksuite/framework/store/src/__tests__/doc.unit.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ import { expect, test, vi } from 'vitest';
import * as Y from 'yjs';

import { Schema } from '../schema/index.js';
import { BlockViewType } from '../store/index.js';
import { createAutoIncrementIdGenerator } from '../test/index.js';
import { TestWorkspace } from '../test/test-workspace.js';
import { createAutoIncrementIdGenerator } from '../utils/id-generator.js';
import {
DividerBlockSchema,
ListBlockSchema,
Expand Down Expand Up @@ -220,7 +219,7 @@ test('query', () => {
match: [
{
flavour: 'affine:list',
viewType: BlockViewType.Hidden,
viewType: 'hidden',
},
],
},
Expand All @@ -233,14 +232,14 @@ test('query', () => {
const paragraph1 = doc1.addBlock('affine:paragraph', {}, note);
const list1 = doc1.addBlock('affine:list' as never, {}, note);

expect(doc2?.getBlock(paragraph1)?.blockViewType).toBe(BlockViewType.Display);
expect(doc2?.getBlock(list1)?.blockViewType).toBe(BlockViewType.Display);
expect(doc3?.getBlock(list1)?.blockViewType).toBe(BlockViewType.Hidden);
expect(doc2?.getBlock(paragraph1)?.blockViewType).toBe('display');
expect(doc2?.getBlock(list1)?.blockViewType).toBe('display');
expect(doc3?.getBlock(list1)?.blockViewType).toBe('hidden');

const list2 = doc1.addBlock('affine:list' as never, {}, note);

expect(doc2?.getBlock(list2)?.blockViewType).toBe(BlockViewType.Display);
expect(doc3?.getBlock(list2)?.blockViewType).toBe(BlockViewType.Hidden);
expect(doc2?.getBlock(list2)?.blockViewType).toBe('display');
expect(doc3?.getBlock(list2)?.blockViewType).toBe('hidden');
});

test('local readonly', () => {
Expand Down
5 changes: 3 additions & 2 deletions blocksuite/framework/store/src/__tests__/schema.unit.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { literal } from 'lit/static-html.js';
import { describe, expect, it, vi } from 'vitest';

import type { BlockModel } from '../model/block/block-model.js';
import { defineBlockSchema } from '../model/block/zod.js';
// import some blocks
import { type BlockModel, defineBlockSchema } from '../schema/base.js';
import { SchemaValidateError } from '../schema/error.js';
import { Schema } from '../schema/index.js';
import { createAutoIncrementIdGenerator } from '../test/index.js';
import { TestWorkspace } from '../test/test-workspace.js';
import { createAutoIncrementIdGenerator } from '../utils/id-generator.js';
import {
DividerBlockSchema,
ListBlockSchema,
Expand Down
2 changes: 1 addition & 1 deletion blocksuite/framework/store/src/__tests__/test-schema.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defineBlockSchema, type SchemaToModel } from '../schema/index.js';
import { defineBlockSchema, type SchemaToModel } from '../model/index.js';

export const RootBlockSchema = defineBlockSchema({
flavour: 'affine:page',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@ import { expect, test } from 'vitest';
import * as Y from 'yjs';

import { MemoryBlobCRUD } from '../adapter/index.js';
import type { BlockModel } from '../model/block/block-model.js';
import { defineBlockSchema, type SchemaToModel } from '../model/block/zod.js';
import { Text } from '../reactive/index.js';
import {
type BlockModel,
defineBlockSchema,
Schema,
type SchemaToModel,
} from '../schema/index.js';
import { Schema } from '../schema/index.js';
import { createAutoIncrementIdGenerator } from '../test/index.js';
import { TestWorkspace } from '../test/test-workspace.js';
import { AssetsManager, BaseBlockTransformer } from '../transformer/index.js';
import { createAutoIncrementIdGenerator } from '../utils/id-generator.js';

const docSchema = defineBlockSchema({
flavour: 'page',
Expand Down
4 changes: 2 additions & 2 deletions blocksuite/framework/store/src/adapter/base.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { BlockSuiteError } from '@blocksuite/global/exceptions';

import type { Blocks } from '../store/index.js';
import type { Blocks, DraftModel } from '../model/index.js';
import type { AssetsManager } from '../transformer/assets.js';
import type { DraftModel, Job, Slice } from '../transformer/index.js';
import type { Job, Slice } from '../transformer/index.js';
import type {
BlockSnapshot,
DocSnapshot,
Expand Down
4 changes: 0 additions & 4 deletions blocksuite/framework/store/src/consts.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
export const COLLECTION_VERSION = 2;

export const PAGE_VERSION = 2;

export const SCHEMA_NOT_FOUND_MESSAGE =
'Schema not found. The block flavour may not be registered.';

Expand Down
3 changes: 1 addition & 2 deletions blocksuite/framework/store/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@
/// <reference path="../shim.d.ts" />

export * from './adapter/index.js';
export * from './model/index.js';
export * from './reactive/index.js';
export * from './schema/index.js';
export * from './store/index.js';
export * from './transformer/index.js';
export { type IdGenerator, nanoid, uuidv4 } from './utils/id-generator.js';
export * as Utils from './utils/utils.js';
export * from './yjs/index.js';

const env =
Expand Down
Loading

0 comments on commit 3d168ba

Please sign in to comment.