mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-24 18:02:47 +08:00
refactor(editor): rename job to transformer (#9639)
This commit is contained in:
@@ -20,10 +20,10 @@ import { Container, type ServiceProvider } from '@blocksuite/affine/global/di';
|
||||
import { WithDisposable } from '@blocksuite/affine/global/utils';
|
||||
import type {
|
||||
ExtensionType,
|
||||
JobMiddleware,
|
||||
Query,
|
||||
Schema,
|
||||
Store,
|
||||
TransformerMiddleware,
|
||||
} from '@blocksuite/affine/store';
|
||||
import { css, html, nothing, type PropertyValues } from 'lit';
|
||||
import { property, query } from 'lit/decorators.js';
|
||||
@@ -85,7 +85,7 @@ export type TextRendererOptions = {
|
||||
maxHeight?: number;
|
||||
customHeading?: boolean;
|
||||
extensions?: ExtensionType[];
|
||||
additionalMiddlewares?: JobMiddleware[];
|
||||
additionalMiddlewares?: TransformerMiddleware[];
|
||||
};
|
||||
|
||||
// todo: refactor it for more general purpose usage instead of AI only?
|
||||
|
||||
@@ -19,13 +19,13 @@ import type {
|
||||
BlockModel,
|
||||
BlockSnapshot,
|
||||
DraftModel,
|
||||
JobMiddleware,
|
||||
Schema,
|
||||
Slice,
|
||||
SliceSnapshot,
|
||||
Store,
|
||||
TransformerMiddleware,
|
||||
} from '@blocksuite/affine/store';
|
||||
import { Job } from '@blocksuite/affine/store';
|
||||
import { Transformer } from '@blocksuite/affine/store';
|
||||
|
||||
const updateSnapshotText = (
|
||||
point: TextRangePoint,
|
||||
@@ -79,7 +79,7 @@ export async function getContentFromSlice(
|
||||
slice: Slice,
|
||||
type: 'markdown' | 'plain-text' = 'markdown'
|
||||
) {
|
||||
const job = new Job({
|
||||
const transformer = new Transformer({
|
||||
schema: host.std.store.workspace.schema,
|
||||
blobCRUD: host.std.store.workspace.blobSync,
|
||||
docCRUD: {
|
||||
@@ -92,24 +92,24 @@ export async function getContentFromSlice(
|
||||
embedSyncedDocMiddleware('content'),
|
||||
],
|
||||
});
|
||||
const snapshot = job.sliceToSnapshot(slice);
|
||||
const snapshot = transformer.sliceToSnapshot(slice);
|
||||
if (!snapshot) {
|
||||
return '';
|
||||
}
|
||||
processTextInSnapshot(snapshot, host);
|
||||
const adapter =
|
||||
type === 'markdown'
|
||||
? new MarkdownAdapter(job, host.std.provider)
|
||||
: new PlainTextAdapter(job, host.std.provider);
|
||||
? new MarkdownAdapter(transformer, host.std.provider)
|
||||
: new PlainTextAdapter(transformer, host.std.provider);
|
||||
const content = await adapter.fromSliceSnapshot({
|
||||
snapshot,
|
||||
assets: job.assetsManager,
|
||||
assets: transformer.assetsManager,
|
||||
});
|
||||
return content.file;
|
||||
}
|
||||
|
||||
export async function getPlainTextFromSlice(host: EditorHost, slice: Slice) {
|
||||
const job = new Job({
|
||||
const transformer = new Transformer({
|
||||
schema: host.std.store.workspace.schema,
|
||||
blobCRUD: host.std.store.workspace.blobSync,
|
||||
docCRUD: {
|
||||
@@ -119,15 +119,15 @@ export async function getPlainTextFromSlice(host: EditorHost, slice: Slice) {
|
||||
},
|
||||
middlewares: [titleMiddleware(host.std.store.workspace.meta.docMetas)],
|
||||
});
|
||||
const snapshot = job.sliceToSnapshot(slice);
|
||||
const snapshot = transformer.sliceToSnapshot(slice);
|
||||
if (!snapshot) {
|
||||
return '';
|
||||
}
|
||||
processTextInSnapshot(snapshot, host);
|
||||
const plainTextAdapter = new PlainTextAdapter(job, host.std.provider);
|
||||
const plainTextAdapter = new PlainTextAdapter(transformer, host.std.provider);
|
||||
const plainText = await plainTextAdapter.fromSliceSnapshot({
|
||||
snapshot,
|
||||
assets: job.assetsManager,
|
||||
assets: transformer.assetsManager,
|
||||
});
|
||||
return plainText.file;
|
||||
}
|
||||
@@ -136,7 +136,7 @@ export const markdownToSnapshot = async (
|
||||
markdown: string,
|
||||
host: EditorHost
|
||||
) => {
|
||||
const job = new Job({
|
||||
const transformer = new Transformer({
|
||||
schema: host.std.store.workspace.schema,
|
||||
blobCRUD: host.std.store.workspace.blobSync,
|
||||
docCRUD: {
|
||||
@@ -146,10 +146,10 @@ export const markdownToSnapshot = async (
|
||||
},
|
||||
middlewares: [defaultImageProxyMiddleware, pasteMiddleware(host.std)],
|
||||
});
|
||||
const markdownAdapter = new MixTextAdapter(job, host.std.provider);
|
||||
const markdownAdapter = new MixTextAdapter(transformer, host.std.provider);
|
||||
const payload = {
|
||||
file: markdown,
|
||||
assets: job.assetsManager,
|
||||
assets: transformer.assetsManager,
|
||||
workspaceId: host.std.store.workspace.id,
|
||||
pageId: host.std.store.id,
|
||||
};
|
||||
@@ -159,7 +159,7 @@ export const markdownToSnapshot = async (
|
||||
|
||||
return {
|
||||
snapshot,
|
||||
job,
|
||||
transformer,
|
||||
};
|
||||
};
|
||||
|
||||
@@ -170,14 +170,14 @@ export async function insertFromMarkdown(
|
||||
parent?: string,
|
||||
index?: number
|
||||
) {
|
||||
const { snapshot, job } = await markdownToSnapshot(markdown, host);
|
||||
const { snapshot, transformer } = await markdownToSnapshot(markdown, host);
|
||||
|
||||
const snapshots = snapshot.content.flatMap(x => x.children);
|
||||
|
||||
const models: BlockModel[] = [];
|
||||
for (let i = 0; i < snapshots.length; i++) {
|
||||
const blockSnapshot = snapshots[i];
|
||||
const model = await job.snapshotToBlock(
|
||||
const model = await transformer.snapshotToBlock(
|
||||
blockSnapshot,
|
||||
doc,
|
||||
parent,
|
||||
@@ -198,15 +198,15 @@ export async function replaceFromMarkdown(
|
||||
parent?: string,
|
||||
index?: number
|
||||
) {
|
||||
const { snapshot, job } = await markdownToSnapshot(markdown, host);
|
||||
await job.snapshotToSlice(snapshot, host.doc, parent, index);
|
||||
const { snapshot, transformer } = await markdownToSnapshot(markdown, host);
|
||||
await transformer.snapshotToSlice(snapshot, host.doc, parent, index);
|
||||
}
|
||||
|
||||
export async function markDownToDoc(
|
||||
provider: ServiceProvider,
|
||||
schema: Schema,
|
||||
answer: string,
|
||||
additionalMiddlewares?: JobMiddleware[]
|
||||
additionalMiddlewares?: TransformerMiddleware[]
|
||||
) {
|
||||
// Should not create a new doc in the original collection
|
||||
const collection = new WorkspaceImpl({
|
||||
@@ -217,7 +217,7 @@ export async function markDownToDoc(
|
||||
if (additionalMiddlewares) {
|
||||
middlewares.push(...additionalMiddlewares);
|
||||
}
|
||||
const job = new Job({
|
||||
const transformer = new Transformer({
|
||||
schema: collection.schema,
|
||||
blobCRUD: collection.blobSync,
|
||||
docCRUD: {
|
||||
@@ -227,10 +227,10 @@ export async function markDownToDoc(
|
||||
},
|
||||
middlewares,
|
||||
});
|
||||
const mdAdapter = new MarkdownAdapter(job, provider);
|
||||
const mdAdapter = new MarkdownAdapter(transformer, provider);
|
||||
const doc = await mdAdapter.toDoc({
|
||||
file: answer,
|
||||
assets: job.assetsManager,
|
||||
assets: transformer.assetsManager,
|
||||
});
|
||||
if (!doc) {
|
||||
console.error('Failed to convert markdown to doc');
|
||||
|
||||
@@ -12,7 +12,7 @@ import {
|
||||
} from '@blocksuite/affine/blocks';
|
||||
import type { ServiceProvider } from '@blocksuite/affine/global/di';
|
||||
import { WithDisposable } from '@blocksuite/affine/global/utils';
|
||||
import { Job, Schema, type Store } from '@blocksuite/affine/store';
|
||||
import { Schema, type Store, Transformer } from '@blocksuite/affine/store';
|
||||
import { css, html, LitElement, nothing } from 'lit';
|
||||
import { property, query } from 'lit/decorators.js';
|
||||
import { repeat } from 'lit/directives/repeat.js';
|
||||
@@ -236,7 +236,7 @@ export const markdownToMindmap = (
|
||||
provider: ServiceProvider
|
||||
) => {
|
||||
let result: Node | null = null;
|
||||
const job = new Job({
|
||||
const transformer = new Transformer({
|
||||
schema: doc.workspace.schema,
|
||||
blobCRUD: doc.workspace.blobSync,
|
||||
docCRUD: {
|
||||
@@ -245,7 +245,7 @@ export const markdownToMindmap = (
|
||||
delete: (id: string) => doc.workspace.removeDoc(id),
|
||||
},
|
||||
});
|
||||
const markdown = new MarkdownAdapter(job, provider);
|
||||
const markdown = new MarkdownAdapter(transformer, provider);
|
||||
const ast: Root = markdown['_markdownToAst'](answer);
|
||||
const traverse = (
|
||||
markdownNode: Unpacked<(typeof ast)['children']>,
|
||||
|
||||
@@ -108,8 +108,8 @@ export const replace = async (
|
||||
|
||||
if (textSelection) {
|
||||
host.std.command.exec('deleteText', { textSelection });
|
||||
const { snapshot, job } = await markdownToSnapshot(content, host);
|
||||
await job.snapshotToSlice(
|
||||
const { snapshot, transformer } = await markdownToSnapshot(content, host);
|
||||
await transformer.snapshotToSlice(
|
||||
snapshot,
|
||||
host.doc,
|
||||
firstBlockParent.model.id,
|
||||
|
||||
@@ -19,7 +19,7 @@ import {
|
||||
} from '@affine/core/modules/workspace';
|
||||
import { useI18n } from '@affine/i18n';
|
||||
import track from '@affine/track';
|
||||
import type { JobMiddleware } from '@blocksuite/affine/store';
|
||||
import type { TransformerMiddleware } from '@blocksuite/affine/store';
|
||||
import { ToggleDownIcon } from '@blocksuite/icons/rc';
|
||||
import * as Collapsible from '@radix-ui/react-collapsible';
|
||||
import {
|
||||
@@ -198,7 +198,9 @@ export const BacklinkGroups = () => {
|
||||
|
||||
const backlinkGroups = useBacklinkGroups();
|
||||
const textRendererOptions = useMemo(() => {
|
||||
const docLinkBaseURLMiddleware: JobMiddleware = ({ adapterConfigs }) => {
|
||||
const docLinkBaseURLMiddleware: TransformerMiddleware = ({
|
||||
adapterConfigs,
|
||||
}) => {
|
||||
adapterConfigs.set(
|
||||
'docLinkBaseUrl',
|
||||
`/workspace/${workspaceService.workspace.id}`
|
||||
|
||||
@@ -22,7 +22,7 @@ import {
|
||||
ZipTransformer,
|
||||
} from '@blocksuite/affine/blocks';
|
||||
import type { AffineEditorContainer } from '@blocksuite/affine/presets';
|
||||
import { Job, type Store } from '@blocksuite/affine/store';
|
||||
import { type Store, Transformer } from '@blocksuite/affine/store';
|
||||
import { useLiveData, useService } from '@toeverything/infra';
|
||||
import { useSetAtom } from 'jotai';
|
||||
import { nanoid } from 'nanoid';
|
||||
@@ -58,7 +58,7 @@ async function exportDoc(
|
||||
std: BlockStdScope,
|
||||
config: AdapterConfig
|
||||
) {
|
||||
const job = new Job({
|
||||
const transformer = new Transformer({
|
||||
schema: doc.workspace.schema,
|
||||
blobCRUD: doc.workspace.blobSync,
|
||||
docCRUD: {
|
||||
@@ -74,7 +74,7 @@ async function exportDoc(
|
||||
});
|
||||
|
||||
const adapterFactory = std.provider.get(config.identifier);
|
||||
const adapter = adapterFactory.get(job);
|
||||
const adapter = adapterFactory.get(transformer);
|
||||
const result = (await adapter.fromDoc(doc)) as AdapterResult;
|
||||
|
||||
if (!result || (!result.file && !result.assetsIds.length)) {
|
||||
@@ -88,10 +88,10 @@ async function exportDoc(
|
||||
let name: string;
|
||||
|
||||
if (result.assetsIds.length > 0) {
|
||||
if (!job.assets) {
|
||||
if (!transformer.assets) {
|
||||
throw new Error('No assets found');
|
||||
}
|
||||
const zip = await createAssetsArchive(job.assets, result.assetsIds);
|
||||
const zip = await createAssetsArchive(transformer.assets, result.assetsIds);
|
||||
await zip.file(config.indexFileName, contentBlob);
|
||||
downloadBlob = await zip.generate();
|
||||
name = `${docTitle}.zip`;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { WorkspaceImpl } from '@affine/core/modules/workspace/impls/workspace';
|
||||
import { AffineSchemas } from '@blocksuite/affine/blocks';
|
||||
import type { DocSnapshot, Store } from '@blocksuite/affine/store';
|
||||
import { Job, Schema } from '@blocksuite/affine/store';
|
||||
import { Schema, Transformer } from '@blocksuite/affine/store';
|
||||
|
||||
const getCollection = (() => {
|
||||
let collection: WorkspaceImpl | null = null;
|
||||
@@ -79,7 +79,7 @@ export async function getDocByName(name: DocName) {
|
||||
async function initDoc(name: DocName) {
|
||||
const snapshot = (await loaders[name]()) as DocSnapshot;
|
||||
const collection = await getCollection();
|
||||
const job = new Job({
|
||||
const transformer = new Transformer({
|
||||
schema: collection.schema,
|
||||
blobCRUD: collection.blobSync,
|
||||
docCRUD: {
|
||||
@@ -90,5 +90,5 @@ async function initDoc(name: DocName) {
|
||||
middlewares: [],
|
||||
});
|
||||
|
||||
return await job.snapshotToDoc(snapshot);
|
||||
return await transformer.snapshotToDoc(snapshot);
|
||||
}
|
||||
|
||||
@@ -15,8 +15,8 @@ import { Container } from '@blocksuite/affine/global/di';
|
||||
import {
|
||||
createYProxy,
|
||||
type DraftModel,
|
||||
Job,
|
||||
type JobMiddleware,
|
||||
Transformer,
|
||||
type TransformerMiddleware,
|
||||
type YBlock,
|
||||
} from '@blocksuite/affine/store';
|
||||
import type { AffineTextAttributes } from '@blocksuite/affine-shared/types';
|
||||
@@ -155,7 +155,7 @@ function generateMarkdownPreviewBuilder(
|
||||
};
|
||||
}
|
||||
|
||||
const titleMiddleware: JobMiddleware = ({ adapterConfigs }) => {
|
||||
const titleMiddleware: TransformerMiddleware = ({ adapterConfigs }) => {
|
||||
const pages = yRootDoc.getMap('meta').get('pages');
|
||||
if (!(pages instanceof YArray)) {
|
||||
return;
|
||||
@@ -176,7 +176,9 @@ function generateMarkdownPreviewBuilder(
|
||||
return `${baseUrl}/${docId}?${searchParams.toString()}`;
|
||||
}
|
||||
|
||||
const docLinkBaseURLMiddleware: JobMiddleware = ({ adapterConfigs }) => {
|
||||
const docLinkBaseURLMiddleware: TransformerMiddleware = ({
|
||||
adapterConfigs,
|
||||
}) => {
|
||||
adapterConfigs.set('docLinkBaseUrl', baseUrl);
|
||||
};
|
||||
|
||||
@@ -191,7 +193,7 @@ function generateMarkdownPreviewBuilder(
|
||||
|
||||
const provider = container.provider();
|
||||
const markdownAdapter = new MarkdownAdapter(
|
||||
new Job({
|
||||
new Transformer({
|
||||
schema: markdownPreviewDocCollection.schema,
|
||||
blobCRUD: markdownPreviewDocCollection.blobSync,
|
||||
docCRUD: {
|
||||
|
||||
Reference in New Issue
Block a user