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): use transformer from store when possible #10453

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 @@ -64,7 +64,6 @@ import {
BlockSnapshotSchema,
fromJSON,
type SliceSnapshot,
Transformer,
} from '@blocksuite/store';
import DOMPurify from 'dompurify';
import * as Y from 'yjs';
Expand Down Expand Up @@ -373,15 +372,7 @@ export class EdgelessClipboardController extends PageClipboard {
if (mayBeSurfaceDataJson !== undefined) {
const elementsRawData = JSON.parse(mayBeSurfaceDataJson);
const { snapshot, blobs } = elementsRawData;
const job = new Transformer({
schema: this.std.store.schema,
blobCRUD: this.std.workspace.blobSync,
docCRUD: {
create: (id: string) => this.std.workspace.createDoc({ id }),
get: (id: string) => this.std.workspace.getDoc(id),
delete: (id: string) => this.std.workspace.removeDoc(id),
},
});
const job = this.std.store.getTransformer();
const map = job.assetsManager.getAssets();
decodeClipboardBlobs(blobs, map);
for (const blobId of map.keys()) {
Expand Down Expand Up @@ -1377,15 +1368,7 @@ export async function prepareClipboardData(
selectedAll: GfxModel[],
std: BlockStdScope
) {
const job = new Transformer({
schema: std.store.schema,
blobCRUD: std.workspace.blobSync,
docCRUD: {
create: (id: string) => std.workspace.createDoc({ id }),
get: (id: string) => std.workspace.getDoc(id),
delete: (id: string) => std.workspace.removeDoc(id),
},
});
const job = std.store.getTransformer();
const selected = await Promise.all(
selectedAll.map(async selected => {
const data = serializeElement(selected, selectedAll, job);
Expand Down
13 changes: 2 additions & 11 deletions blocksuite/affine/block-root/src/edgeless/services/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
type DocSnapshot,
DocSnapshotSchema,
type SnapshotNode,
Transformer,
type Transformer,
} from '@blocksuite/store';
import type * as Y from 'yjs';
/**
Expand Down Expand Up @@ -90,16 +90,7 @@ export class TemplateJob {
type: TemplateType;

constructor({ model, type, middlewares }: TemplateJobConfig) {
this.job = new Transformer({
schema: model.doc.schema,
blobCRUD: model.doc.workspace.blobSync,
docCRUD: {
create: (id: string) => model.doc.workspace.createDoc({ id }),
get: (id: string) => model.doc.workspace.getDoc(id),
delete: (id: string) => model.doc.workspace.removeDoc(id),
},
middlewares: [],
});
this.job = model.doc.getTransformer();
this.model = model;
this.type = TEMPLATE_TYPES.includes(type as TemplateType)
? (type as TemplateType)
Expand Down
12 changes: 2 additions & 10 deletions blocksuite/affine/block-root/src/edgeless/utils/clone-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
isGfxGroupCompatibleModel,
type SerializedElement,
} from '@blocksuite/block-std/gfx';
import { type BlockSnapshot, Transformer } from '@blocksuite/store';
import type { BlockSnapshot, Transformer } from '@blocksuite/store';

/**
* return all elements in the tree of the elements
Expand All @@ -40,15 +40,7 @@ export function getSortedCloneElements(elements: GfxModel[]) {

export function prepareCloneData(elements: GfxModel[], std: BlockStdScope) {
elements = sortEdgelessElements(elements);
const job = new Transformer({
schema: std.store.schema,
blobCRUD: std.workspace.blobSync,
docCRUD: {
create: (id: string) => std.workspace.createDoc({ id }),
get: (id: string) => std.workspace.getDoc(id),
delete: (id: string) => std.workspace.removeDoc(id),
},
});
const job = std.store.getTransformer();
const res = elements.map(element => {
const data = serializeElement(element, elements, job);
return data;
Expand Down
17 changes: 4 additions & 13 deletions blocksuite/affine/block-root/src/transformers/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,10 @@ function getProvider() {
*/
async function exportDoc(doc: Store) {
const provider = getProvider();
const job = new Transformer({
schema: doc.schema,
blobCRUD: doc.blobSync,
docCRUD: {
create: (id: string) => doc.workspace.createDoc({ id }),
get: (id: string) => doc.workspace.getDoc(id),
delete: (id: string) => doc.workspace.removeDoc(id),
},
middlewares: [
docLinkBaseURLMiddleware(doc.workspace.id),
titleMiddleware(doc.workspace.meta.docMetas),
],
});
const job = doc.getTransformer([
docLinkBaseURLMiddleware(doc.workspace.id),
titleMiddleware(doc.workspace.meta.docMetas),
]);
const snapshot = job.docToSnapshot(doc);
const adapter = new HtmlAdapter(job, provider);
if (!snapshot) {
Expand Down
34 changes: 8 additions & 26 deletions blocksuite/affine/block-root/src/transformers/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,10 @@ type ImportMarkdownZipOptions = {
*/
async function exportDoc(doc: Store) {
const provider = getProvider();
const job = new Transformer({
schema: doc.schema,
blobCRUD: doc.blobSync,
docCRUD: {
create: (id: string) => doc.workspace.createDoc({ id }),
get: (id: string) => doc.workspace.getDoc(id),
delete: (id: string) => doc.workspace.removeDoc(id),
},
middlewares: [
docLinkBaseURLMiddleware(doc.workspace.id),
titleMiddleware(doc.workspace.meta.docMetas),
],
});
const job = doc.getTransformer([
docLinkBaseURLMiddleware(doc.workspace.id),
titleMiddleware(doc.workspace.meta.docMetas),
]);
const snapshot = job.docToSnapshot(doc);

const adapter = new MarkdownAdapter(job, provider);
Expand Down Expand Up @@ -109,19 +100,10 @@ async function importMarkdownToBlock({
blockId,
}: ImportMarkdownToBlockOptions) {
const provider = getProvider();
const job = new Transformer({
schema: doc.schema,
blobCRUD: doc.blobSync,
docCRUD: {
create: (id: string) => doc.workspace.createDoc({ id }),
get: (id: string) => doc.workspace.getDoc(id),
delete: (id: string) => doc.workspace.removeDoc(id),
},
middlewares: [
defaultImageProxyMiddleware,
docLinkBaseURLMiddleware(doc.workspace.id),
],
});
const job = doc.getTransformer([
defaultImageProxyMiddleware,
docLinkBaseURLMiddleware(doc.workspace.id),
]);
const adapter = new MarkdownAdapter(job, provider);
const snapshot = await adapter.toSliceSnapshot({
file: markdown,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class DNDAPIExtension extends Extension {
const { docId, flavour = 'affine:embed-linked-doc', blockId } = options;

const slice = Slice.fromModels(this.std.store, []);
const job = this.std.getTransformer();
const job = this.std.store.getTransformer();
const snapshot = job.sliceToSnapshot(slice);
if (!snapshot) {
console.error('Failed to convert slice to snapshot');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1354,7 +1354,7 @@ export class DragEventWatcher {
middlewares.push(gfxBlocksFilter(selectedIds, std));
}

return std.getTransformer(middlewares);
return std.store.getTransformer(middlewares);
}

private _isDropOnCurrentEditor(std?: BlockStdScope) {
Expand Down
2 changes: 1 addition & 1 deletion blocksuite/framework/block-std/src/clipboard/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ export class Clipboard extends LifeCycleWatcher {
}

private _getJob() {
return this.std.getTransformer(this._jobMiddlewares);
return this.std.store.getTransformer(this._jobMiddlewares);
}

readFromClipboard(clipboardData: DataTransfer) {
Expand Down
15 changes: 0 additions & 15 deletions blocksuite/framework/block-std/src/scope/block-std-scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import {
type ExtensionType,
type Store,
StoreSelectionExtension,
Transformer,
type TransformerMiddleware,
} from '@blocksuite/store';

import { Clipboard } from '../clipboard/index.js';
Expand Down Expand Up @@ -140,19 +138,6 @@ export class BlockStdScope {
return this.getOptional(BlockViewIdentifier(flavour));
}

getTransformer(middlewares: TransformerMiddleware[] = []) {
return new Transformer({
schema: this.store.schema,
blobCRUD: this.workspace.blobSync,
docCRUD: {
create: (id: string) => this.workspace.createDoc({ id }),
get: (id: string) => this.workspace.getDoc(id),
delete: (id: string) => this.workspace.removeDoc(id),
},
middlewares,
});
}

mount() {
this._lifeCycleWatchers.forEach(watcher => {
watcher.mounted();
Expand Down
15 changes: 15 additions & 0 deletions blocksuite/framework/store/src/model/store/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import {
StoreSelectionExtension,
} from '../../extension/index.js';
import { Schema } from '../../schema/index.js';
import type { TransformerMiddleware } from '../../transformer/middleware.js';
import { Transformer } from '../../transformer/transformer.js';
import {
Block,
type BlockModel,
Expand Down Expand Up @@ -715,4 +717,17 @@ export class Store {
get getOptional() {
return this.provider.getOptional.bind(this.provider);
}

getTransformer(middlewares: TransformerMiddleware[] = []) {
return new Transformer({
schema: this.schema,
blobCRUD: this.workspace.blobSync,
docCRUD: {
create: (id: string) => this.workspace.createDoc({ id }),
get: (id: string) => this.workspace.getDoc(id),
delete: (id: string) => this.workspace.removeDoc(id),
},
middlewares,
});
}
}
29 changes: 10 additions & 19 deletions blocksuite/playground/apps/_common/components/adapters-panel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
} from '@blocksuite/blocks';
import { WithDisposable } from '@blocksuite/global/utils';
import type { TestAffineEditorContainer } from '@blocksuite/integration-test';
import { type DocSnapshot, Transformer } from '@blocksuite/store';
import type { DocSnapshot } from '@blocksuite/store';
import { effect } from '@preact/signals-core';
import type SlTabPanel from '@shoelace-style/shoelace/dist/components/tab-panel/tab-panel.js';
import { css, html, type PropertyValues } from 'lit';
Expand Down Expand Up @@ -101,24 +101,15 @@ export class AdaptersPanel extends WithDisposable(ShadowlessElement) {
}

private _createJob() {
return new Transformer({
schema: this.doc.schema,
blobCRUD: this.doc.blobSync,
docCRUD: {
create: (id: string) => this.doc.workspace.createDoc({ id }),
get: (id: string) => this.doc.workspace.getDoc(id),
delete: (id: string) => this.doc.workspace.removeDoc(id),
},
middlewares: [
docLinkBaseURLMiddlewareBuilder(
'https://example.com',
this.doc.workspace.id
).get(),
titleMiddleware(this.doc.workspace.meta.docMetas),
embedSyncedDocMiddleware('content'),
defaultImageProxyMiddleware,
],
});
return this.doc.getTransformer([
docLinkBaseURLMiddlewareBuilder(
'https://example.com',
this.doc.workspace.id
).get(),
titleMiddleware(this.doc.workspace.meta.docMetas),
embedSyncedDocMiddleware('content'),
defaultImageProxyMiddleware,
]);
}

private _getDocSnapshot() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ import { BlockSuiteError, ErrorCode } from '@blocksuite/global/exceptions';
import type { SerializedXYWH } from '@blocksuite/global/utils';
import type { DeltaInsert } from '@blocksuite/inline/types';
import { TestAffineEditorContainer } from '@blocksuite/integration-test';
import { Text, Transformer, type Workspace } from '@blocksuite/store';
import { 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 @@ -237,19 +237,10 @@ export class StarterDebugMenu extends ShadowlessElement {

private async _exportFile(config: AdapterConfig) {
const doc = this.editor.doc;
const job = new Transformer({
schema: doc.schema,
blobCRUD: this.collection.blobSync,
docCRUD: {
create: (id: string) => this.collection.createDoc({ id }),
get: (id: string) => this.collection.getDoc(id),
delete: (id: string) => this.collection.removeDoc(id),
},
middlewares: [
docLinkBaseURLMiddleware(this.collection.id),
titleMiddleware(this.collection.meta.docMetas),
],
});
const job = doc.getTransformer([
docLinkBaseURLMiddleware(this.collection.id),
titleMiddleware(this.collection.meta.docMetas),
]);

const adapterFactory = this.editor.std.provider.get(config.identifier);
const adapter = adapterFactory.get(job);
Expand Down Expand Up @@ -444,16 +435,7 @@ export class StarterDebugMenu extends ShadowlessElement {
});
if (!file) return;
const doc = this.editor.doc;
const job = new Transformer({
schema: doc.schema,
blobCRUD: this.collection.blobSync,
docCRUD: {
create: (id: string) => this.collection.createDoc({ id }),
get: (id: string) => this.collection.getDoc(id),
delete: (id: string) => this.collection.removeDoc(id),
},
middlewares: [defaultImageProxyMiddleware],
});
const job = doc.getTransformer([defaultImageProxyMiddleware]);
const htmlAdapter = new NotionHtmlAdapter(job, this.editor.std.provider);
await htmlAdapter.toDoc({
file: await file.text(),
Expand Down
18 changes: 4 additions & 14 deletions packages/frontend/apps/android/src/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import {
titleMiddleware,
} from '@blocksuite/affine/blocks';
import { Container } from '@blocksuite/affine/global/di';
import { Transformer } from '@blocksuite/affine/store';
import { App as CapacitorApp } from '@capacitor/app';
import { Keyboard } from '@capacitor/keyboard';
import { StatusBar, Style } from '@capacitor/status-bar';
Expand Down Expand Up @@ -165,19 +164,10 @@ framework.impl(AIButtonProvider, {
try {
const blockSuiteDoc = doc.blockSuiteDoc;

const transformer = new Transformer({
schema: blockSuiteDoc.schema,
blobCRUD: blockSuiteDoc.workspace.blobSync,
docCRUD: {
create: (id: string) => blockSuiteDoc.workspace.createDoc({ id }),
get: (id: string) => blockSuiteDoc.workspace.getDoc(id),
delete: (id: string) => blockSuiteDoc.workspace.removeDoc(id),
},
middlewares: [
docLinkBaseURLMiddleware(blockSuiteDoc.workspace.id),
titleMiddleware(blockSuiteDoc.workspace.meta.docMetas),
],
});
const transformer = blockSuiteDoc.getTransformer([
docLinkBaseURLMiddleware(blockSuiteDoc.workspace.id),
titleMiddleware(blockSuiteDoc.workspace.meta.docMetas),
]);
const snapshot = transformer.docToSnapshot(blockSuiteDoc);

const container = new Container();
Expand Down
Loading
Loading