Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(editor): playground doc collection typing migration #9593

Merged
merged 1 commit into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import {
import { type SerializedXYWH, SignalWatcher } from '@blocksuite/global/utils';
import type { DeltaInsert } from '@blocksuite/inline';
import type { AffineEditorContainer } from '@blocksuite/presets';
import { type DocCollection, Text } from '@blocksuite/store';
import { Text, type Workspace } from '@blocksuite/store';
import { setBasePath } from '@shoelace-style/shoelace/dist/utilities/base-path.js';
import { css, html, nothing } from 'lit';
import { customElement, property, state } from 'lit/decorators.js';
Expand Down Expand Up @@ -158,7 +158,9 @@ export class CollabDebugMenu extends SignalWatcher(ShadowlessElement) {
const zipTransformer = this.rootService.transformers.zip;
await zipTransformer.exportDocs(
this.collection,
[...this.collection.docs.values()].map(collection => collection.getDoc())
Array.from(this.collection.docs.values()).map(collection =>
collection.getStore()
)
);
}

Expand Down Expand Up @@ -611,7 +613,7 @@ export class CollabDebugMenu extends SignalWatcher(ShadowlessElement) {
private accessor _docMode: DocMode = 'page';

@property({ attribute: false })
accessor collection!: DocCollection;
accessor collection!: Workspace;

@property({ attribute: false })
accessor docsPanel!: DocsPanel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ import { BlockSuiteError, ErrorCode } from '@blocksuite/global/exceptions';
import type { SerializedXYWH } from '@blocksuite/global/utils';
import type { DeltaInsert } from '@blocksuite/inline/types';
import { AffineEditorContainer, type CommentPanel } from '@blocksuite/presets';
import { type DocCollection, Job, Text } from '@blocksuite/store';
import { Job, Text, type Workspace } from '@blocksuite/store';
import type { SlDropdown } from '@shoelace-style/shoelace';
import { setBasePath } from '@shoelace-style/shoelace/dist/utilities/base-path.js';
import { css, html } from 'lit';
Expand Down Expand Up @@ -329,7 +329,9 @@ export class StarterDebugMenu extends ShadowlessElement {
private async _exportSnapshot() {
await ZipTransformer.exportDocs(
this.collection,
[...this.collection.docs.values()].map(collection => collection.getDoc())
Array.from(this.collection.docs.values()).map(collection =>
collection.getStore()
)
);
}

Expand Down Expand Up @@ -1019,7 +1021,7 @@ export class StarterDebugMenu extends ShadowlessElement {
accessor blockTypeDropdown!: SlDropdown;

@property({ attribute: false })
accessor collection!: DocCollection;
accessor collection!: Workspace;

@property({ attribute: false })
accessor commentPanel!: CommentPanel;
Expand Down
8 changes: 4 additions & 4 deletions blocksuite/playground/apps/_common/mock-services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
} from '@blocksuite/blocks';
import { Slot } from '@blocksuite/global/utils';
import type { AffineEditorContainer } from '@blocksuite/presets';
import { type DocCollection } from '@blocksuite/store';
import { type Workspace } from '@blocksuite/store';
import { Signal, signal } from '@preact/signals-core';
import type { TemplateResult } from 'lit';

Expand Down Expand Up @@ -109,14 +109,14 @@ export function mockNotificationService(editor: AffineEditorContainer) {
return notificationService;
}

export function mockParseDocUrlService(collection: DocCollection) {
export function mockParseDocUrlService(collection: Workspace) {
const parseDocUrlService: ParseDocUrlService = {
parseDocUrl: (url: string) => {
if (url && URL.canParse(url)) {
const path = decodeURIComponent(new URL(url).hash.slice(1));
const item =
path.length > 0
? [...collection.docs.values()].find(doc => doc.id === path)
? Array.from(collection.docs.values()).find(doc => doc.id === path)
: null;
if (item) {
return {
Expand Down Expand Up @@ -185,7 +185,7 @@ export function mockPeekViewExtension(
} satisfies PeekViewService);
}

export function mockGenerateDocUrlService(collection: DocCollection) {
export function mockGenerateDocUrlService(collection: Workspace) {
const generateDocUrlService: GenerateDocUrlService = {
generateDocUrl: (docId: string, params?: ReferenceParams) => {
const doc = collection.getDoc(docId);
Expand Down
4 changes: 2 additions & 2 deletions blocksuite/playground/apps/default/utils/editor.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { EditorHost, ExtensionType } from '@blocksuite/block-std';
import type { EditorHost } from '@blocksuite/block-std';
import {
CommunityCanvasTextFonts,
DocModeExtension,
Expand All @@ -14,7 +14,7 @@ import {
SpecProvider,
} from '@blocksuite/blocks';
import { AffineEditorContainer } from '@blocksuite/presets';
import type { Workspace } from '@blocksuite/store';
import type { ExtensionType, Workspace } from '@blocksuite/store';

import { AttachmentViewerPanel } from '../../_common/components/attachment-viewer-panel.js';
import { CollabDebugMenu } from '../../_common/components/collab-debug-menu.js';
Expand Down
4 changes: 2 additions & 2 deletions blocksuite/playground/apps/starter/data/affine-snapshot.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ZipTransformer } from '@blocksuite/blocks';
import { type DocCollection, Text } from '@blocksuite/store';
import { Text, type Workspace } from '@blocksuite/store';

export async function affineSnapshot(collection: DocCollection, id: string) {
export async function affineSnapshot(collection: Workspace, id: string) {
const doc = collection.createDoc({ id });
doc.load();
// Add root block and surface block at root level
Expand Down
4 changes: 2 additions & 2 deletions blocksuite/playground/apps/starter/data/embed.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { type DocCollection, Text } from '@blocksuite/store';
import { Text, type Workspace } from '@blocksuite/store';

import type { InitFn } from './utils.js';

export const embed: InitFn = (collection: DocCollection, id: string) => {
export const embed: InitFn = (collection: Workspace, id: string) => {
const doc = collection.getDoc(id) ?? collection.createDoc({ id });
doc.clear();

Expand Down
4 changes: 2 additions & 2 deletions blocksuite/playground/apps/starter/data/empty.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { type DocCollection, Text } from '@blocksuite/store';
import { Text, type Workspace } from '@blocksuite/store';

import type { InitFn } from './utils.js';

export const empty: InitFn = (collection: DocCollection, id: string) => {
export const empty: InitFn = (collection: Workspace, id: string) => {
const doc = collection.getDoc(id) ?? collection.createDoc({ id });
doc.clear();

Expand Down
9 changes: 3 additions & 6 deletions blocksuite/playground/apps/starter/data/heavy-whiteboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import { DEFAULT_ROUGHNESS } from '@blocksuite/affine-model';
import type { SerializedXYWH } from '@blocksuite/global/utils';
import {
Boxed,
type DocCollection,
nanoid,
native2Y,
Text,
type Y,
type Workspace,
} from '@blocksuite/store';
import type * as Y from 'yjs';

import type { InitFn } from './utils.js';

Expand Down Expand Up @@ -46,10 +46,7 @@ function createShapes(count: number): Record<string, unknown> {
const SHAPES_COUNT = 100;
const RANGE = 2000;

export const heavyWhiteboard: InitFn = (
collection: DocCollection,
id: string
) => {
export const heavyWhiteboard: InitFn = (collection: Workspace, id: string) => {
const count = Number(params.get('count')) || SHAPES_COUNT;
const enableShapes = !!params.get('shapes');

Expand Down
4 changes: 2 additions & 2 deletions blocksuite/playground/apps/starter/data/heavy.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { type DocCollection, Text } from '@blocksuite/store';
import { Text, type Workspace } from '@blocksuite/store';

import type { InitFn } from './utils.js';

const params = new URLSearchParams(location.search);

export const heavy: InitFn = (collection: DocCollection, docId: string) => {
export const heavy: InitFn = (collection: Workspace, docId: string) => {
const count = Number(params.get('count')) || 1000;

const doc = collection.createDoc({ id: docId });
Expand Down
4 changes: 2 additions & 2 deletions blocksuite/playground/apps/starter/data/linked.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { type DocCollection, Text } from '@blocksuite/store';
import { Text, type Workspace } from '@blocksuite/store';

import type { InitFn } from './utils.js';

export const linked: InitFn = (collection: DocCollection, id: string) => {
export const linked: InitFn = (collection: Workspace, id: string) => {
const docA = collection.getDoc(id) ?? collection.createDoc({ id });

const docBId = 'doc:linked-page';
Expand Down
6 changes: 3 additions & 3 deletions blocksuite/playground/apps/starter/data/multiple-editor.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { RefNodeSlotsProvider } from '@blocksuite/affine-components/rich-text';
import { AffineEditorContainer } from '@blocksuite/presets';
import { type DocCollection, Text } from '@blocksuite/store';
import { Text, type Workspace } from '@blocksuite/store';

import type { InitFn } from './utils.js';

export const multiEditor: InitFn = (collection: DocCollection, id: string) => {
export const multiEditor: InitFn = (collection: Workspace, id: string) => {
const doc = collection.createDoc({ id });
doc.load(() => {
// Add root block and surface block at root level
Expand Down Expand Up @@ -48,7 +48,7 @@ multiEditor.displayName = 'Multiple Editor Example';
multiEditor.description = 'Multiple Editor basic example';

export const multiEditorVertical: InitFn = (
collection: DocCollection,
collection: Workspace,
docId: string
) => {
const doc = collection.createDoc({ id: docId });
Expand Down
7 changes: 2 additions & 5 deletions blocksuite/playground/apps/starter/data/pending-structs.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import { type DocCollection, Text } from '@blocksuite/store';
import { Text, type Workspace } from '@blocksuite/store';
import * as Y from 'yjs';

import type { InitFn } from './utils.js';

export const pendingStructs: InitFn = (
collection: DocCollection,
id: string
) => {
export const pendingStructs: InitFn = (collection: Workspace, id: string) => {
const doc = collection.createDoc({ id });
const tempDoc = collection.createDoc({ id: 'tempDoc' });
doc.load();
Expand Down
4 changes: 2 additions & 2 deletions blocksuite/playground/apps/starter/data/preset.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { MarkdownTransformer } from '@blocksuite/blocks';
import { type DocCollection, Text } from '@blocksuite/store';
import { Text, type Workspace } from '@blocksuite/store';

import type { InitFn } from './utils.js';

const presetMarkdown = `Click the 🔁 button to switch between editors dynamically - they are fully compatible!`;

export const preset: InitFn = async (collection: DocCollection, id: string) => {
export const preset: InitFn = async (collection: Workspace, id: string) => {
const doc = collection.createDoc({ id });
doc.load();
// Add root block and surface block at root level
Expand Down
4 changes: 2 additions & 2 deletions blocksuite/playground/apps/starter/data/synced.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { MarkdownTransformer } from '@blocksuite/blocks';
import { type DocCollection, Text } from '@blocksuite/store';
import { Text, type Workspace } from '@blocksuite/store';

import type { InitFn } from './utils';

Expand All @@ -11,7 +11,7 @@ This article has also been published [in PDF format](https://www.inkandswitch.co

We welcome your feedback: [@inkandswitch](https://twitter.com/inkandswitch) or [email protected].`;

export const synced: InitFn = (collection: DocCollection, id: string) => {
export const synced: InitFn = (collection: Workspace, id: string) => {
const docMain = collection.getDoc(id) ?? collection.createDoc({ id });

const docSyncedPageId = 'doc:synced-page';
Expand Down
4 changes: 2 additions & 2 deletions blocksuite/playground/apps/starter/data/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { DocCollection } from '@blocksuite/store';
import type { Workspace } from '@blocksuite/store';

export interface InitFn {
(collection: DocCollection, docId: string): Promise<void> | void;
(collection: Workspace, docId: string): Promise<void> | void;
id: string;
displayName: string;
description: string;
Expand Down
7 changes: 2 additions & 5 deletions blocksuite/playground/apps/starter/data/version-mismatch.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import type { DocCollection } from '@blocksuite/store';
import type { Workspace } from '@blocksuite/store';
import * as Y from 'yjs';

import type { InitFn } from './utils.js';

export const versionMismatch: InitFn = (
collection: DocCollection,
id: string
) => {
export const versionMismatch: InitFn = (collection: Workspace, id: string) => {
const doc = collection.createDoc({ id });
const tempDoc = collection.createDoc({ id: 'tempDoc' });
doc.load();
Expand Down
2 changes: 1 addition & 1 deletion blocksuite/playground/apps/starter/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import '../../style.css';
import '../dev-format.js';

import {
type ExtensionType,
WidgetViewMapExtension,
WidgetViewMapIdentifier,
} from '@blocksuite/block-std';
Expand All @@ -19,6 +18,7 @@ import { effects as blocksEffects } from '@blocksuite/blocks/effects';
import * as globalUtils from '@blocksuite/global/utils';
import * as editor from '@blocksuite/presets';
import { effects as presetsEffects } from '@blocksuite/presets/effects';
import type { ExtensionType } from '@blocksuite/store';
// eslint-disable-next-line @typescript-eslint/no-restricted-imports
import * as store from '@blocksuite/store';

Expand Down
31 changes: 2 additions & 29 deletions blocksuite/playground/apps/starter/utils/collection.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { AffineSchemas, SpecProvider, TestUtils } from '@blocksuite/blocks';
import type { BlockSuiteFlags } from '@blocksuite/global/types';
import { assertExists } from '@blocksuite/global/utils';
import { type BlockCollection, Job, nanoid, Schema } from '@blocksuite/store';
import { Job, nanoid, Schema } from '@blocksuite/store';
import {
createAutoIncrementIdGenerator,
type DocCollectionOptions,
Expand Down Expand Up @@ -50,7 +49,7 @@ export function createStarterDocCollection() {
}

const flags: Partial<BlockSuiteFlags> = Object.fromEntries(
[...params.entries()]
Array.from(params.entries())
.filter(([key]) => key.startsWith('enable_'))
.map(([k, v]) => [k, v === 'true'])
);
Expand Down Expand Up @@ -98,32 +97,6 @@ export function createStarterDocCollection() {
}

export async function initStarterDocCollection(collection: TestWorkspace) {
// init from other clients
if (room && !params.has('init')) {
const firstCollection = collection.docs.values().next().value as
| BlockCollection
| undefined;
let firstDoc = firstCollection?.getDoc();
if (!firstDoc) {
await new Promise<string>(resolve =>
collection.slots.docCreated.once(resolve)
);
const firstCollection = collection.docs.values().next().value as
| BlockCollection
| undefined;
firstDoc = firstCollection?.getDoc();
}
assertExists(firstDoc);
const doc = firstDoc;

doc.load();
if (!doc.root) {
await new Promise(resolve => doc.slots.rootAdded.once(resolve));
}
doc.resetHistory();
return;
}

// use built-in init function
const functionMap = new Map<
string,
Expand Down
10 changes: 3 additions & 7 deletions blocksuite/playground/apps/starter/utils/editor.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import {
BlockServiceWatcher,
type EditorHost,
type ExtensionType,
} from '@blocksuite/block-std';
import { BlockServiceWatcher, type EditorHost } from '@blocksuite/block-std';
import {
AffineFormatBarWidget,
CommunityCanvasTextFonts,
Expand All @@ -19,7 +15,7 @@ import {
toolbarDefaultConfig,
} from '@blocksuite/blocks';
import { AffineEditorContainer, CommentPanel } from '@blocksuite/presets';
import type { DocCollection } from '@blocksuite/store';
import type { ExtensionType, Workspace } from '@blocksuite/store';

import { AttachmentViewerPanel } from '../../_common/components/attachment-viewer-panel.js';
import { CustomFramePanel } from '../../_common/components/custom-frame-panel.js';
Expand Down Expand Up @@ -47,7 +43,7 @@ function configureFormatBar(formatBar: AffineFormatBarWidget) {
toolbarDefaultConfig(formatBar);
}

export async function mountDefaultDocEditor(collection: DocCollection) {
export async function mountDefaultDocEditor(collection: Workspace) {
const app = document.getElementById('app');
if (!app) return;

Expand Down
Loading