refactor(editor): job should not rely on doc collection directly (#9488)
This commit is contained in:
@@ -27,7 +27,7 @@ export const embedSyncedDocBlockHtmlAdapterMatcher: BlockHtmlAdapterMatcher = {
|
|||||||
|
|
||||||
if (type === 'content') {
|
if (type === 'content') {
|
||||||
const syncedDocId = o.node.props.pageId as string;
|
const syncedDocId = o.node.props.pageId as string;
|
||||||
const syncedDoc = job.collection.getDoc(syncedDocId);
|
const syncedDoc = job.docCRUD.get(syncedDocId);
|
||||||
walkerContext.setGlobalContext('hast:html-root-doc', false);
|
walkerContext.setGlobalContext('hast:html-root-doc', false);
|
||||||
if (!syncedDoc) return;
|
if (!syncedDoc) return;
|
||||||
|
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ export const embedSyncedDocBlockMarkdownAdapterMatcher: BlockMarkdownAdapterMatc
|
|||||||
|
|
||||||
if (type === 'content') {
|
if (type === 'content') {
|
||||||
const syncedDocId = o.node.props.pageId as string;
|
const syncedDocId = o.node.props.pageId as string;
|
||||||
const syncedDoc = job.collection.getDoc(syncedDocId);
|
const syncedDoc = job.docCRUD.get(syncedDocId);
|
||||||
if (!syncedDoc) return;
|
if (!syncedDoc) return;
|
||||||
|
|
||||||
if (counter === 1) {
|
if (counter === 1) {
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ export const embedSyncedDocBlockPlainTextAdapterMatcher: BlockPlainTextAdapterMa
|
|||||||
|
|
||||||
if (type === 'content') {
|
if (type === 'content') {
|
||||||
const syncedDocId = o.node.props.pageId as string;
|
const syncedDocId = o.node.props.pageId as string;
|
||||||
const syncedDoc = job.collection.getDoc(syncedDocId);
|
const syncedDoc = job.docCRUD.get(syncedDocId);
|
||||||
if (!syncedDoc) return;
|
if (!syncedDoc) return;
|
||||||
|
|
||||||
if (counter === 1) {
|
if (counter === 1) {
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import {
|
|||||||
StdIdentifier,
|
StdIdentifier,
|
||||||
} from '@blocksuite/block-std';
|
} from '@blocksuite/block-std';
|
||||||
import { type Container, createIdentifier } from '@blocksuite/global/di';
|
import { type Container, createIdentifier } from '@blocksuite/global/di';
|
||||||
import { Job, Slice, type SliceSnapshot } from '@blocksuite/store';
|
import { Slice, type SliceSnapshot } from '@blocksuite/store';
|
||||||
|
|
||||||
export const DndApiExtensionIdentifier = createIdentifier<DNDAPIExtension>(
|
export const DndApiExtensionIdentifier = createIdentifier<DNDAPIExtension>(
|
||||||
'AffineDndApiIdentifier'
|
'AffineDndApiIdentifier'
|
||||||
@@ -40,7 +40,7 @@ export class DNDAPIExtension extends Extension {
|
|||||||
const { docId, flavour = 'affine:embed-linked-doc', blockId } = options;
|
const { docId, flavour = 'affine:embed-linked-doc', blockId } = options;
|
||||||
|
|
||||||
const slice = Slice.fromModels(this.std.doc, []);
|
const slice = Slice.fromModels(this.std.doc, []);
|
||||||
const job = new Job({ collection: this.std.collection });
|
const job = this.std.getJob();
|
||||||
const snapshot = job.sliceToSnapshot(slice);
|
const snapshot = job.sliceToSnapshot(slice);
|
||||||
if (!snapshot) {
|
if (!snapshot) {
|
||||||
console.error('Failed to convert slice to snapshot');
|
console.error('Failed to convert slice to snapshot');
|
||||||
|
|||||||
@@ -3,14 +3,14 @@ import type { JobMiddleware } from '@blocksuite/store';
|
|||||||
|
|
||||||
export const newIdCrossDoc =
|
export const newIdCrossDoc =
|
||||||
(std: BlockStdScope): JobMiddleware =>
|
(std: BlockStdScope): JobMiddleware =>
|
||||||
({ slots, collection }) => {
|
({ slots }) => {
|
||||||
let samePage = false;
|
let samePage = false;
|
||||||
slots.beforeImport.on(payload => {
|
slots.beforeImport.on(payload => {
|
||||||
if (payload.type === 'slice') {
|
if (payload.type === 'slice') {
|
||||||
samePage = payload.snapshot.pageId === std.doc.id;
|
samePage = payload.snapshot.pageId === std.doc.id;
|
||||||
}
|
}
|
||||||
if (payload.type === 'block' && !samePage) {
|
if (payload.type === 'block' && !samePage) {
|
||||||
payload.snapshot.id = collection.idGenerator();
|
payload.snapshot.id = std.collection.idGenerator();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import type { JobMiddleware } from '@blocksuite/store';
|
|||||||
|
|
||||||
export const surfaceRefToEmbed =
|
export const surfaceRefToEmbed =
|
||||||
(std: BlockStdScope): JobMiddleware =>
|
(std: BlockStdScope): JobMiddleware =>
|
||||||
({ slots, collection }) => {
|
({ slots }) => {
|
||||||
let pageId: string | null = null;
|
let pageId: string | null = null;
|
||||||
slots.beforeImport.on(payload => {
|
slots.beforeImport.on(payload => {
|
||||||
if (payload.type === 'slice') {
|
if (payload.type === 'slice') {
|
||||||
@@ -18,7 +18,7 @@ export const surfaceRefToEmbed =
|
|||||||
!std.doc.hasBlock(payload.snapshot.id)
|
!std.doc.hasBlock(payload.snapshot.id)
|
||||||
) {
|
) {
|
||||||
const id = payload.snapshot.id;
|
const id = payload.snapshot.id;
|
||||||
payload.snapshot.id = collection.idGenerator();
|
payload.snapshot.id = std.collection.idGenerator();
|
||||||
payload.snapshot.flavour = 'affine:embed-linked-doc';
|
payload.snapshot.flavour = 'affine:embed-linked-doc';
|
||||||
payload.snapshot.props = {
|
payload.snapshot.props = {
|
||||||
blockId: id,
|
blockId: id,
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ import {
|
|||||||
} from '@blocksuite/block-std';
|
} from '@blocksuite/block-std';
|
||||||
import { GfxControllerIdentifier } from '@blocksuite/block-std/gfx';
|
import { GfxControllerIdentifier } from '@blocksuite/block-std/gfx';
|
||||||
import { Bound, Point } from '@blocksuite/global/utils';
|
import { Bound, Point } from '@blocksuite/global/utils';
|
||||||
import { Job, Slice, type SliceSnapshot } from '@blocksuite/store';
|
import { Slice, type SliceSnapshot } from '@blocksuite/store';
|
||||||
|
|
||||||
import { DropIndicator } from '../components/drop-indicator.js';
|
import { DropIndicator } from '../components/drop-indicator.js';
|
||||||
import { AFFINE_DRAG_HANDLE_WIDGET } from '../consts.js';
|
import { AFFINE_DRAG_HANDLE_WIDGET } from '../consts.js';
|
||||||
@@ -544,14 +544,11 @@ export class DragEventWatcher {
|
|||||||
|
|
||||||
private _getJob() {
|
private _getJob() {
|
||||||
const std = this._std;
|
const std = this._std;
|
||||||
return new Job({
|
return std.getJob([
|
||||||
collection: std.collection,
|
|
||||||
middlewares: [
|
|
||||||
newIdCrossDoc(std),
|
newIdCrossDoc(std),
|
||||||
reorderList(std),
|
reorderList(std),
|
||||||
surfaceRefToEmbed(std),
|
surfaceRefToEmbed(std),
|
||||||
],
|
]);
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private _serializeData(slice: Slice, state: DndEventState) {
|
private _serializeData(slice: Slice, state: DndEventState) {
|
||||||
|
|||||||
@@ -27,5 +27,14 @@ export function createJob(middlewares?: JobMiddleware[]) {
|
|||||||
const schema = new Schema().register(AffineSchemas);
|
const schema = new Schema().register(AffineSchemas);
|
||||||
const docCollection = new DocCollection({ schema });
|
const docCollection = new DocCollection({ schema });
|
||||||
docCollection.meta.initialize();
|
docCollection.meta.initialize();
|
||||||
return new Job({ collection: docCollection, middlewares: testMiddlewares });
|
return new Job({
|
||||||
|
schema,
|
||||||
|
blobCRUD: docCollection.blobSync,
|
||||||
|
middlewares: testMiddlewares,
|
||||||
|
docCRUD: {
|
||||||
|
create: (id: string) => docCollection.createDoc({ id }),
|
||||||
|
get: (id: string) => docCollection.getDoc(id),
|
||||||
|
delete: (id: string) => docCollection.removeDoc(id),
|
||||||
|
},
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -45,8 +45,17 @@ const provider = container.provider();
|
|||||||
*/
|
*/
|
||||||
async function exportDoc(doc: Doc) {
|
async function exportDoc(doc: Doc) {
|
||||||
const job = new Job({
|
const job = new Job({
|
||||||
collection: doc.collection,
|
schema: doc.schema,
|
||||||
middlewares: [docLinkBaseURLMiddleware, titleMiddleware],
|
blobCRUD: doc.blobSync,
|
||||||
|
docCRUD: {
|
||||||
|
create: (id: string) => doc.collection.createDoc({ id }),
|
||||||
|
get: (id: string) => doc.collection.getDoc(id),
|
||||||
|
delete: (id: string) => doc.collection.removeDoc(id),
|
||||||
|
},
|
||||||
|
middlewares: [
|
||||||
|
docLinkBaseURLMiddleware(doc.collection.id),
|
||||||
|
titleMiddleware(doc.collection.meta.docMetas),
|
||||||
|
],
|
||||||
});
|
});
|
||||||
const snapshot = job.docToSnapshot(doc);
|
const snapshot = job.docToSnapshot(doc);
|
||||||
const adapter = new HtmlAdapter(job, provider);
|
const adapter = new HtmlAdapter(job, provider);
|
||||||
@@ -91,11 +100,17 @@ async function importHTMLToDoc({
|
|||||||
fileName,
|
fileName,
|
||||||
}: ImportHTMLToDocOptions) {
|
}: ImportHTMLToDocOptions) {
|
||||||
const job = new Job({
|
const job = new Job({
|
||||||
collection,
|
schema: collection.schema,
|
||||||
|
blobCRUD: collection.blobSync,
|
||||||
|
docCRUD: {
|
||||||
|
create: (id: string) => collection.createDoc({ id }),
|
||||||
|
get: (id: string) => collection.getDoc(id),
|
||||||
|
delete: (id: string) => collection.removeDoc(id),
|
||||||
|
},
|
||||||
middlewares: [
|
middlewares: [
|
||||||
defaultImageProxyMiddleware,
|
defaultImageProxyMiddleware,
|
||||||
fileNameMiddleware(fileName),
|
fileNameMiddleware(fileName),
|
||||||
docLinkBaseURLMiddleware,
|
docLinkBaseURLMiddleware(collection.id),
|
||||||
],
|
],
|
||||||
});
|
});
|
||||||
const htmlAdapter = new HtmlAdapter(job, provider);
|
const htmlAdapter = new HtmlAdapter(job, provider);
|
||||||
@@ -147,11 +162,17 @@ async function importHTMLZip({ collection, imported }: ImportHTMLZipOptions) {
|
|||||||
htmlBlobs.map(async ([fileName, blob]) => {
|
htmlBlobs.map(async ([fileName, blob]) => {
|
||||||
const fileNameWithoutExt = fileName.replace(/\.[^/.]+$/, '');
|
const fileNameWithoutExt = fileName.replace(/\.[^/.]+$/, '');
|
||||||
const job = new Job({
|
const job = new Job({
|
||||||
collection,
|
schema: collection.schema,
|
||||||
|
blobCRUD: collection.blobSync,
|
||||||
|
docCRUD: {
|
||||||
|
create: (id: string) => collection.createDoc({ id }),
|
||||||
|
get: (id: string) => collection.getDoc(id),
|
||||||
|
delete: (id: string) => collection.removeDoc(id),
|
||||||
|
},
|
||||||
middlewares: [
|
middlewares: [
|
||||||
defaultImageProxyMiddleware,
|
defaultImageProxyMiddleware,
|
||||||
fileNameMiddleware(fileNameWithoutExt),
|
fileNameMiddleware(fileNameWithoutExt),
|
||||||
docLinkBaseURLMiddleware,
|
docLinkBaseURLMiddleware(collection.id),
|
||||||
],
|
],
|
||||||
});
|
});
|
||||||
const assets = job.assets;
|
const assets = job.assets;
|
||||||
|
|||||||
@@ -51,8 +51,17 @@ type ImportMarkdownZipOptions = {
|
|||||||
*/
|
*/
|
||||||
async function exportDoc(doc: Doc) {
|
async function exportDoc(doc: Doc) {
|
||||||
const job = new Job({
|
const job = new Job({
|
||||||
collection: doc.collection,
|
schema: doc.schema,
|
||||||
middlewares: [docLinkBaseURLMiddleware, titleMiddleware],
|
blobCRUD: doc.blobSync,
|
||||||
|
docCRUD: {
|
||||||
|
create: (id: string) => doc.collection.createDoc({ id }),
|
||||||
|
get: (id: string) => doc.collection.getDoc(id),
|
||||||
|
delete: (id: string) => doc.collection.removeDoc(id),
|
||||||
|
},
|
||||||
|
middlewares: [
|
||||||
|
docLinkBaseURLMiddleware(doc.collection.id),
|
||||||
|
titleMiddleware(doc.collection.meta.docMetas),
|
||||||
|
],
|
||||||
});
|
});
|
||||||
const snapshot = job.docToSnapshot(doc);
|
const snapshot = job.docToSnapshot(doc);
|
||||||
|
|
||||||
@@ -101,8 +110,17 @@ async function importMarkdownToBlock({
|
|||||||
blockId,
|
blockId,
|
||||||
}: ImportMarkdownToBlockOptions) {
|
}: ImportMarkdownToBlockOptions) {
|
||||||
const job = new Job({
|
const job = new Job({
|
||||||
collection: doc.collection,
|
schema: doc.schema,
|
||||||
middlewares: [defaultImageProxyMiddleware, docLinkBaseURLMiddleware],
|
blobCRUD: doc.blobSync,
|
||||||
|
docCRUD: {
|
||||||
|
create: (id: string) => doc.collection.createDoc({ id }),
|
||||||
|
get: (id: string) => doc.collection.getDoc(id),
|
||||||
|
delete: (id: string) => doc.collection.removeDoc(id),
|
||||||
|
},
|
||||||
|
middlewares: [
|
||||||
|
defaultImageProxyMiddleware,
|
||||||
|
docLinkBaseURLMiddleware(doc.collection.id),
|
||||||
|
],
|
||||||
});
|
});
|
||||||
const adapter = new MarkdownAdapter(job, provider);
|
const adapter = new MarkdownAdapter(job, provider);
|
||||||
const snapshot = await adapter.toSliceSnapshot({
|
const snapshot = await adapter.toSliceSnapshot({
|
||||||
@@ -137,11 +155,17 @@ async function importMarkdownToDoc({
|
|||||||
fileName,
|
fileName,
|
||||||
}: ImportMarkdownToDocOptions) {
|
}: ImportMarkdownToDocOptions) {
|
||||||
const job = new Job({
|
const job = new Job({
|
||||||
collection,
|
schema: collection.schema,
|
||||||
|
blobCRUD: collection.blobSync,
|
||||||
|
docCRUD: {
|
||||||
|
create: (id: string) => collection.createDoc({ id }),
|
||||||
|
get: (id: string) => collection.getDoc(id),
|
||||||
|
delete: (id: string) => collection.removeDoc(id),
|
||||||
|
},
|
||||||
middlewares: [
|
middlewares: [
|
||||||
defaultImageProxyMiddleware,
|
defaultImageProxyMiddleware,
|
||||||
fileNameMiddleware(fileName),
|
fileNameMiddleware(fileName),
|
||||||
docLinkBaseURLMiddleware,
|
docLinkBaseURLMiddleware(collection.id),
|
||||||
],
|
],
|
||||||
});
|
});
|
||||||
const mdAdapter = new MarkdownAdapter(job, provider);
|
const mdAdapter = new MarkdownAdapter(job, provider);
|
||||||
@@ -195,11 +219,17 @@ async function importMarkdownZip({
|
|||||||
markdownBlobs.map(async ([fileName, blob]) => {
|
markdownBlobs.map(async ([fileName, blob]) => {
|
||||||
const fileNameWithoutExt = fileName.replace(/\.[^/.]+$/, '');
|
const fileNameWithoutExt = fileName.replace(/\.[^/.]+$/, '');
|
||||||
const job = new Job({
|
const job = new Job({
|
||||||
collection,
|
schema: collection.schema,
|
||||||
|
blobCRUD: collection.blobSync,
|
||||||
|
docCRUD: {
|
||||||
|
create: (id: string) => collection.createDoc({ id }),
|
||||||
|
get: (id: string) => collection.getDoc(id),
|
||||||
|
delete: (id: string) => collection.removeDoc(id),
|
||||||
|
},
|
||||||
middlewares: [
|
middlewares: [
|
||||||
defaultImageProxyMiddleware,
|
defaultImageProxyMiddleware,
|
||||||
fileNameMiddleware(fileNameWithoutExt),
|
fileNameMiddleware(fileNameWithoutExt),
|
||||||
docLinkBaseURLMiddleware,
|
docLinkBaseURLMiddleware(collection.id),
|
||||||
],
|
],
|
||||||
});
|
});
|
||||||
const assets = job.assets;
|
const assets = job.assets;
|
||||||
|
|||||||
@@ -8,9 +8,11 @@ import type {
|
|||||||
} from '@blocksuite/affine-model';
|
} from '@blocksuite/affine-model';
|
||||||
import { DEFAULT_IMAGE_PROXY_ENDPOINT } from '@blocksuite/affine-shared/consts';
|
import { DEFAULT_IMAGE_PROXY_ENDPOINT } from '@blocksuite/affine-shared/consts';
|
||||||
import { assertExists } from '@blocksuite/global/utils';
|
import { assertExists } from '@blocksuite/global/utils';
|
||||||
import type { DeltaOperation, JobMiddleware } from '@blocksuite/store';
|
import type { DeltaOperation, DocMeta, JobMiddleware } from '@blocksuite/store';
|
||||||
|
|
||||||
export const replaceIdMiddleware: JobMiddleware = ({ slots, collection }) => {
|
export const replaceIdMiddleware =
|
||||||
|
(idGenerator: () => string): JobMiddleware =>
|
||||||
|
({ slots, docCRUD }) => {
|
||||||
const idMap = new Map<string, string>();
|
const idMap = new Map<string, string>();
|
||||||
slots.afterImport.on(payload => {
|
slots.afterImport.on(payload => {
|
||||||
if (
|
if (
|
||||||
@@ -81,7 +83,7 @@ export const replaceIdMiddleware: JobMiddleware = ({ slots, collection }) => {
|
|||||||
model.refFlavour === 'affine:frame' &&
|
model.refFlavour === 'affine:frame' &&
|
||||||
!model.doc.hasBlock(original)
|
!model.doc.hasBlock(original)
|
||||||
) {
|
) {
|
||||||
const newId = collection.idGenerator();
|
const newId = idGenerator();
|
||||||
idMap.set(original, newId);
|
idMap.set(original, newId);
|
||||||
model.reference = newId;
|
model.reference = newId;
|
||||||
}
|
}
|
||||||
@@ -94,15 +96,17 @@ export const replaceIdMiddleware: JobMiddleware = ({ slots, collection }) => {
|
|||||||
payload.snapshot.flavour
|
payload.snapshot.flavour
|
||||||
)
|
)
|
||||||
) {
|
) {
|
||||||
const model = payload.model as EmbedLinkedDocModel | EmbedSyncedDocModel;
|
const model = payload.model as
|
||||||
|
| EmbedLinkedDocModel
|
||||||
|
| EmbedSyncedDocModel;
|
||||||
const original = model.pageId;
|
const original = model.pageId;
|
||||||
// If the pageId is not in the doc, generate a new id.
|
// If the pageId is not in the doc, generate a new id.
|
||||||
// If we already have a replacement, use it.
|
// If we already have a replacement, use it.
|
||||||
if (!collection.getDoc(original)) {
|
if (!docCRUD.get(original)) {
|
||||||
if (idMap.has(original)) {
|
if (idMap.has(original)) {
|
||||||
model.pageId = idMap.get(original)!;
|
model.pageId = idMap.get(original)!;
|
||||||
} else {
|
} else {
|
||||||
const newId = collection.idGenerator();
|
const newId = idGenerator();
|
||||||
idMap.set(original, newId);
|
idMap.set(original, newId);
|
||||||
model.pageId = newId;
|
model.pageId = newId;
|
||||||
}
|
}
|
||||||
@@ -115,7 +119,7 @@ export const replaceIdMiddleware: JobMiddleware = ({ slots, collection }) => {
|
|||||||
payload.snapshot.meta.id = idMap.get(payload.snapshot.meta.id)!;
|
payload.snapshot.meta.id = idMap.get(payload.snapshot.meta.id)!;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const newId = collection.idGenerator();
|
const newId = idGenerator();
|
||||||
idMap.set(payload.snapshot.meta.id, newId);
|
idMap.set(payload.snapshot.meta.id, newId);
|
||||||
payload.snapshot.meta.id = newId;
|
payload.snapshot.meta.id = newId;
|
||||||
return;
|
return;
|
||||||
@@ -138,7 +142,7 @@ export const replaceIdMiddleware: JobMiddleware = ({ slots, collection }) => {
|
|||||||
if (idMap.has(original)) {
|
if (idMap.has(original)) {
|
||||||
newId = idMap.get(original)!;
|
newId = idMap.get(original)!;
|
||||||
} else {
|
} else {
|
||||||
newId = collection.idGenerator();
|
newId = idGenerator();
|
||||||
idMap.set(original, newId);
|
idMap.set(original, newId);
|
||||||
}
|
}
|
||||||
snapshot.id = newId;
|
snapshot.id = newId;
|
||||||
@@ -150,7 +154,7 @@ export const replaceIdMiddleware: JobMiddleware = ({ slots, collection }) => {
|
|||||||
if (idMap.has(original)) {
|
if (idMap.has(original)) {
|
||||||
newId = idMap.get(original)!;
|
newId = idMap.get(original)!;
|
||||||
} else {
|
} else {
|
||||||
newId = collection.idGenerator();
|
newId = idGenerator();
|
||||||
idMap.set(original, newId);
|
idMap.set(original, newId);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -204,46 +208,52 @@ export const customImageProxyMiddleware = (
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
const customDocLinkBaseUrlMiddleware = (baseUrl: string): JobMiddleware => {
|
const customDocLinkBaseUrlMiddleware = (
|
||||||
return ({ adapterConfigs, collection }) => {
|
baseUrl: string,
|
||||||
|
collectionId: string
|
||||||
|
): JobMiddleware => {
|
||||||
|
return ({ adapterConfigs }) => {
|
||||||
const docLinkBaseUrl = baseUrl
|
const docLinkBaseUrl = baseUrl
|
||||||
? `${baseUrl}/workspace/${collection.id}`
|
? `${baseUrl}/workspace/${collectionId}`
|
||||||
: '';
|
: '';
|
||||||
adapterConfigs.set('docLinkBaseUrl', docLinkBaseUrl);
|
adapterConfigs.set('docLinkBaseUrl', docLinkBaseUrl);
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
export const titleMiddleware: JobMiddleware = ({
|
export const titleMiddleware =
|
||||||
slots,
|
(metas: DocMeta[]): JobMiddleware =>
|
||||||
collection,
|
({ slots, adapterConfigs }) => {
|
||||||
adapterConfigs,
|
|
||||||
}) => {
|
|
||||||
slots.beforeExport.on(() => {
|
slots.beforeExport.on(() => {
|
||||||
for (const meta of collection.meta.docMetas) {
|
for (const meta of metas) {
|
||||||
adapterConfigs.set('title:' + meta.id, meta.title);
|
adapterConfigs.set('title:' + meta.id, meta.title);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
export const docLinkBaseURLMiddlewareBuilder = (baseUrl: string) => {
|
export const docLinkBaseURLMiddlewareBuilder = (
|
||||||
let middleware = customDocLinkBaseUrlMiddleware(baseUrl);
|
baseUrl: string,
|
||||||
|
collectionId: string
|
||||||
|
) => {
|
||||||
|
let middleware = customDocLinkBaseUrlMiddleware(baseUrl, collectionId);
|
||||||
return {
|
return {
|
||||||
get: () => middleware,
|
get: () => middleware,
|
||||||
set: (url: string) => {
|
set: (url: string) => {
|
||||||
middleware = customDocLinkBaseUrlMiddleware(url);
|
middleware = customDocLinkBaseUrlMiddleware(url, collectionId);
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
const defaultDocLinkBaseURLMiddlewareBuilder = docLinkBaseURLMiddlewareBuilder(
|
const defaultDocLinkBaseURLMiddlewareBuilder = (collectionId: string) =>
|
||||||
typeof window !== 'undefined' ? window.location.origin : '.'
|
docLinkBaseURLMiddlewareBuilder(
|
||||||
|
typeof window !== 'undefined' ? window.location.origin : '.',
|
||||||
|
collectionId
|
||||||
);
|
);
|
||||||
|
|
||||||
export const docLinkBaseURLMiddleware =
|
export const docLinkBaseURLMiddleware = (collectionId: string) =>
|
||||||
defaultDocLinkBaseURLMiddlewareBuilder.get();
|
defaultDocLinkBaseURLMiddlewareBuilder(collectionId).get();
|
||||||
|
|
||||||
export const setDocLinkBaseURLMiddleware =
|
export const setDocLinkBaseURLMiddleware = (collectionId: string) =>
|
||||||
defaultDocLinkBaseURLMiddlewareBuilder.set;
|
defaultDocLinkBaseURLMiddlewareBuilder(collectionId).set;
|
||||||
|
|
||||||
const imageProxyMiddlewareBuilder = () => {
|
const imageProxyMiddlewareBuilder = () => {
|
||||||
let middleware = customImageProxyMiddleware(DEFAULT_IMAGE_PROXY_ENDPOINT);
|
let middleware = customImageProxyMiddleware(DEFAULT_IMAGE_PROXY_ENDPOINT);
|
||||||
|
|||||||
@@ -119,7 +119,13 @@ async function importNotionZip({
|
|||||||
}
|
}
|
||||||
const pagePromises = Array.from(pagePaths).map(async path => {
|
const pagePromises = Array.from(pagePaths).map(async path => {
|
||||||
const job = new Job({
|
const job = new Job({
|
||||||
collection: collection,
|
schema: collection.schema,
|
||||||
|
blobCRUD: collection.blobSync,
|
||||||
|
docCRUD: {
|
||||||
|
create: (id: string) => collection.createDoc({ id }),
|
||||||
|
get: (id: string) => collection.getDoc(id),
|
||||||
|
delete: (id: string) => collection.removeDoc(id),
|
||||||
|
},
|
||||||
middlewares: [defaultImageProxyMiddleware],
|
middlewares: [defaultImageProxyMiddleware],
|
||||||
});
|
});
|
||||||
const htmlAdapter = new NotionHtmlAdapter(job, provider);
|
const htmlAdapter = new NotionHtmlAdapter(job, provider);
|
||||||
|
|||||||
@@ -7,12 +7,21 @@ import { replaceIdMiddleware, titleMiddleware } from './middlewares.js';
|
|||||||
|
|
||||||
async function exportDocs(collection: DocCollection, docs: Doc[]) {
|
async function exportDocs(collection: DocCollection, docs: Doc[]) {
|
||||||
const zip = new Zip();
|
const zip = new Zip();
|
||||||
const job = new Job({ collection });
|
const job = new Job({
|
||||||
|
schema: collection.schema,
|
||||||
|
blobCRUD: collection.blobSync,
|
||||||
|
docCRUD: {
|
||||||
|
create: (id: string) => collection.createDoc({ id }),
|
||||||
|
get: (id: string) => collection.getDoc(id),
|
||||||
|
delete: (id: string) => collection.removeDoc(id),
|
||||||
|
},
|
||||||
|
middlewares: [
|
||||||
|
replaceIdMiddleware(collection.idGenerator),
|
||||||
|
titleMiddleware(collection.meta.docMetas),
|
||||||
|
],
|
||||||
|
});
|
||||||
const snapshots = await Promise.all(docs.map(job.docToSnapshot));
|
const snapshots = await Promise.all(docs.map(job.docToSnapshot));
|
||||||
|
|
||||||
const collectionInfo = job.collectionInfoToSnapshot();
|
|
||||||
await zip.file('info.json', JSON.stringify(collectionInfo, null, 2));
|
|
||||||
|
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
snapshots
|
snapshots
|
||||||
.filter((snapshot): snapshot is DocSnapshot => !!snapshot)
|
.filter((snapshot): snapshot is DocSnapshot => !!snapshot)
|
||||||
@@ -69,8 +78,17 @@ async function importDocs(collection: DocCollection, imported: Blob) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const job = new Job({
|
const job = new Job({
|
||||||
collection,
|
schema: collection.schema,
|
||||||
middlewares: [replaceIdMiddleware, titleMiddleware],
|
blobCRUD: collection.blobSync,
|
||||||
|
docCRUD: {
|
||||||
|
create: (id: string) => collection.createDoc({ id }),
|
||||||
|
get: (id: string) => collection.getDoc(id),
|
||||||
|
delete: (id: string) => collection.removeDoc(id),
|
||||||
|
},
|
||||||
|
middlewares: [
|
||||||
|
replaceIdMiddleware(collection.idGenerator),
|
||||||
|
titleMiddleware(collection.meta.docMetas),
|
||||||
|
],
|
||||||
});
|
});
|
||||||
const assetsMap = job.assets;
|
const assetsMap = job.assets;
|
||||||
|
|
||||||
|
|||||||
@@ -59,8 +59,12 @@ export class PageClipboard {
|
|||||||
const paste = pasteMiddleware(this._std);
|
const paste = pasteMiddleware(this._std);
|
||||||
this._std.clipboard.use(copy);
|
this._std.clipboard.use(copy);
|
||||||
this._std.clipboard.use(paste);
|
this._std.clipboard.use(paste);
|
||||||
this._std.clipboard.use(replaceIdMiddleware);
|
this._std.clipboard.use(
|
||||||
this._std.clipboard.use(titleMiddleware);
|
replaceIdMiddleware(this._std.doc.collection.idGenerator)
|
||||||
|
);
|
||||||
|
this._std.clipboard.use(
|
||||||
|
titleMiddleware(this._std.doc.collection.meta.docMetas)
|
||||||
|
);
|
||||||
this._std.clipboard.use(defaultImageProxyMiddleware);
|
this._std.clipboard.use(defaultImageProxyMiddleware);
|
||||||
|
|
||||||
this._disposables.add({
|
this._disposables.add({
|
||||||
@@ -80,8 +84,12 @@ export class PageClipboard {
|
|||||||
this._std.clipboard.unregisterAdapter('*/*');
|
this._std.clipboard.unregisterAdapter('*/*');
|
||||||
this._std.clipboard.unuse(copy);
|
this._std.clipboard.unuse(copy);
|
||||||
this._std.clipboard.unuse(paste);
|
this._std.clipboard.unuse(paste);
|
||||||
this._std.clipboard.unuse(replaceIdMiddleware);
|
this._std.clipboard.unuse(
|
||||||
this._std.clipboard.unuse(titleMiddleware);
|
replaceIdMiddleware(this._std.doc.collection.idGenerator)
|
||||||
|
);
|
||||||
|
this._std.clipboard.unuse(
|
||||||
|
titleMiddleware(this._std.doc.collection.meta.docMetas)
|
||||||
|
);
|
||||||
this._std.clipboard.unuse(defaultImageProxyMiddleware);
|
this._std.clipboard.unuse(defaultImageProxyMiddleware);
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -369,7 +369,15 @@ export class EdgelessClipboardController extends PageClipboard {
|
|||||||
if (mayBeSurfaceDataJson !== undefined) {
|
if (mayBeSurfaceDataJson !== undefined) {
|
||||||
const elementsRawData = JSON.parse(mayBeSurfaceDataJson);
|
const elementsRawData = JSON.parse(mayBeSurfaceDataJson);
|
||||||
const { snapshot, blobs } = elementsRawData;
|
const { snapshot, blobs } = elementsRawData;
|
||||||
const job = new Job({ collection: this.std.collection });
|
const job = new Job({
|
||||||
|
schema: this.std.collection.schema,
|
||||||
|
blobCRUD: this.std.collection.blobSync,
|
||||||
|
docCRUD: {
|
||||||
|
create: (id: string) => this.std.collection.createDoc({ id }),
|
||||||
|
get: (id: string) => this.std.collection.getDoc(id),
|
||||||
|
delete: (id: string) => this.std.collection.removeDoc(id),
|
||||||
|
},
|
||||||
|
});
|
||||||
const map = job.assetsManager.getAssets();
|
const map = job.assetsManager.getAssets();
|
||||||
decodeClipboardBlobs(blobs, map);
|
decodeClipboardBlobs(blobs, map);
|
||||||
for (const blobId of map.keys()) {
|
for (const blobId of map.keys()) {
|
||||||
@@ -1366,7 +1374,13 @@ export async function prepareClipboardData(
|
|||||||
std: BlockStdScope
|
std: BlockStdScope
|
||||||
) {
|
) {
|
||||||
const job = new Job({
|
const job = new Job({
|
||||||
collection: std.collection,
|
schema: std.collection.schema,
|
||||||
|
blobCRUD: std.collection.blobSync,
|
||||||
|
docCRUD: {
|
||||||
|
create: (id: string) => std.collection.createDoc({ id }),
|
||||||
|
get: (id: string) => std.collection.getDoc(id),
|
||||||
|
delete: (id: string) => std.collection.removeDoc(id),
|
||||||
|
},
|
||||||
});
|
});
|
||||||
const selected = await Promise.all(
|
const selected = await Promise.all(
|
||||||
selectedAll.map(async selected => {
|
selectedAll.map(async selected => {
|
||||||
|
|||||||
@@ -87,7 +87,16 @@ export class TemplateJob {
|
|||||||
type: TemplateType;
|
type: TemplateType;
|
||||||
|
|
||||||
constructor({ model, type, middlewares }: TemplateJobConfig) {
|
constructor({ model, type, middlewares }: TemplateJobConfig) {
|
||||||
this.job = new Job({ collection: model.doc.collection, middlewares: [] });
|
this.job = new Job({
|
||||||
|
schema: model.doc.collection.schema,
|
||||||
|
blobCRUD: model.doc.collection.blobSync,
|
||||||
|
docCRUD: {
|
||||||
|
create: (id: string) => model.doc.collection.createDoc({ id }),
|
||||||
|
get: (id: string) => model.doc.collection.getDoc(id),
|
||||||
|
delete: (id: string) => model.doc.collection.removeDoc(id),
|
||||||
|
},
|
||||||
|
middlewares: [],
|
||||||
|
});
|
||||||
this.model = model;
|
this.model = model;
|
||||||
this.type = TEMPLATE_TYPES.includes(type as TemplateType)
|
this.type = TEMPLATE_TYPES.includes(type as TemplateType)
|
||||||
? (type as TemplateType)
|
? (type as TemplateType)
|
||||||
|
|||||||
@@ -40,7 +40,13 @@ export function getSortedCloneElements(elements: GfxModel[]) {
|
|||||||
export function prepareCloneData(elements: GfxModel[], std: BlockStdScope) {
|
export function prepareCloneData(elements: GfxModel[], std: BlockStdScope) {
|
||||||
elements = sortEdgelessElements(elements);
|
elements = sortEdgelessElements(elements);
|
||||||
const job = new Job({
|
const job = new Job({
|
||||||
collection: std.collection,
|
schema: std.collection.schema,
|
||||||
|
blobCRUD: std.collection.blobSync,
|
||||||
|
docCRUD: {
|
||||||
|
create: (id: string) => std.collection.createDoc({ id }),
|
||||||
|
get: (id: string) => std.collection.getDoc(id),
|
||||||
|
delete: (id: string) => std.collection.removeDoc(id),
|
||||||
|
},
|
||||||
});
|
});
|
||||||
const res = elements.map(element => {
|
const res = elements.map(element => {
|
||||||
const data = serializeElement(element, elements, job);
|
const data = serializeElement(element, elements, job);
|
||||||
|
|||||||
@@ -247,7 +247,15 @@ export const markdownToMindmap = (
|
|||||||
provider: ServiceProvider
|
provider: ServiceProvider
|
||||||
) => {
|
) => {
|
||||||
let result: Node | null = null;
|
let result: Node | null = null;
|
||||||
const job = new Job({ collection: doc.collection });
|
const job = new Job({
|
||||||
|
schema: doc.collection.schema,
|
||||||
|
blobCRUD: doc.collection.blobSync,
|
||||||
|
docCRUD: {
|
||||||
|
create: (id: string) => doc.collection.createDoc({ id }),
|
||||||
|
get: (id: string) => doc.collection.getDoc(id),
|
||||||
|
delete: (id: string) => doc.collection.removeDoc(id),
|
||||||
|
},
|
||||||
|
});
|
||||||
const markdown = new MarkdownAdapter(job, provider);
|
const markdown = new MarkdownAdapter(job, provider);
|
||||||
const ast: Root = markdown['_markdownToAst'](answer);
|
const ast: Root = markdown['_markdownToAst'](answer);
|
||||||
const traverse = (
|
const traverse = (
|
||||||
|
|||||||
@@ -4,10 +4,10 @@ import type {
|
|||||||
BaseAdapter,
|
BaseAdapter,
|
||||||
BlockSnapshot,
|
BlockSnapshot,
|
||||||
Doc,
|
Doc,
|
||||||
|
Job,
|
||||||
JobMiddleware,
|
JobMiddleware,
|
||||||
Slice,
|
Slice,
|
||||||
} from '@blocksuite/store';
|
} from '@blocksuite/store';
|
||||||
import { Job } from '@blocksuite/store';
|
|
||||||
import DOMPurify from 'dompurify';
|
import DOMPurify from 'dompurify';
|
||||||
import type { RootContentMap } from 'hast';
|
import type { RootContentMap } from 'hast';
|
||||||
import * as lz from 'lz-string';
|
import * as lz from 'lz-string';
|
||||||
@@ -286,10 +286,7 @@ export class Clipboard extends LifeCycleWatcher {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private _getJob() {
|
private _getJob() {
|
||||||
return new Job({
|
return this.std.getJob(this._jobMiddlewares);
|
||||||
middlewares: this._jobMiddlewares,
|
|
||||||
collection: this.std.collection,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
readFromClipboard(clipboardData: DataTransfer) {
|
readFromClipboard(clipboardData: DataTransfer) {
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import type { ServiceProvider } from '@blocksuite/global/di';
|
import type { ServiceProvider } from '@blocksuite/global/di';
|
||||||
import { Container } from '@blocksuite/global/di';
|
import { Container } from '@blocksuite/global/di';
|
||||||
import { BlockSuiteError, ErrorCode } from '@blocksuite/global/exceptions';
|
import { BlockSuiteError, ErrorCode } from '@blocksuite/global/exceptions';
|
||||||
import type { Doc } from '@blocksuite/store';
|
import { type Doc, Job, type JobMiddleware } from '@blocksuite/store';
|
||||||
|
|
||||||
import { Clipboard } from '../clipboard/index.js';
|
import { Clipboard } from '../clipboard/index.js';
|
||||||
import { CommandManager } from '../command/index.js';
|
import { CommandManager } from '../command/index.js';
|
||||||
@@ -168,6 +168,19 @@ export class BlockStdScope {
|
|||||||
return this.getOptional(BlockViewIdentifier(flavour));
|
return this.getOptional(BlockViewIdentifier(flavour));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getJob(middlewares: JobMiddleware[] = []) {
|
||||||
|
return new Job({
|
||||||
|
schema: this.collection.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,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
mount() {
|
mount() {
|
||||||
this._lifeCycleWatchers.forEach(watcher => {
|
this._lifeCycleWatchers.forEach(watcher => {
|
||||||
watcher.mounted.call(watcher);
|
watcher.mounted.call(watcher);
|
||||||
|
|||||||
@@ -1,11 +1,6 @@
|
|||||||
import { BlockSuiteError, ErrorCode } from '@blocksuite/global/exceptions';
|
import { BlockSuiteError, ErrorCode } from '@blocksuite/global/exceptions';
|
||||||
|
|
||||||
interface BlobCRUD {
|
import type { BlobCRUD } from './type.js';
|
||||||
get: (key: string) => Promise<Blob | null> | Blob | null;
|
|
||||||
set: (key: string, value: Blob) => Promise<string> | string;
|
|
||||||
delete: (key: string) => Promise<void> | void;
|
|
||||||
list: () => Promise<string[]> | string[];
|
|
||||||
}
|
|
||||||
|
|
||||||
type AssetsManagerConfig = {
|
type AssetsManagerConfig = {
|
||||||
blob: BlobCRUD;
|
blob: BlobCRUD;
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
import { BlockSuiteError, ErrorCode } from '@blocksuite/global/exceptions';
|
import { BlockSuiteError, ErrorCode } from '@blocksuite/global/exceptions';
|
||||||
import { nextTick, Slot } from '@blocksuite/global/utils';
|
import { nextTick, Slot } from '@blocksuite/global/utils';
|
||||||
|
|
||||||
import type { BlockModel, BlockSchemaType } from '../schema/index.js';
|
import type { BlockModel, BlockSchemaType, Schema } from '../schema/index.js';
|
||||||
import type { Doc, DocCollection, DocMeta } from '../store/index.js';
|
import type { Doc } from '../store/index.js';
|
||||||
import { AssetsManager } from './assets.js';
|
import { AssetsManager } from './assets.js';
|
||||||
import { BaseBlockTransformer } from './base.js';
|
import { BaseBlockTransformer } from './base.js';
|
||||||
import type { DraftModel } from './draft.js';
|
import type { DraftModel } from './draft.js';
|
||||||
@@ -15,20 +15,22 @@ import type {
|
|||||||
} from './middleware.js';
|
} from './middleware.js';
|
||||||
import { Slice } from './slice.js';
|
import { Slice } from './slice.js';
|
||||||
import type {
|
import type {
|
||||||
|
BlobCRUD,
|
||||||
BlockSnapshot,
|
BlockSnapshot,
|
||||||
CollectionInfoSnapshot,
|
DocCRUD,
|
||||||
DocSnapshot,
|
DocSnapshot,
|
||||||
SliceSnapshot,
|
SliceSnapshot,
|
||||||
} from './type.js';
|
} from './type.js';
|
||||||
import {
|
import {
|
||||||
BlockSnapshotSchema,
|
BlockSnapshotSchema,
|
||||||
CollectionInfoSnapshotSchema,
|
|
||||||
DocSnapshotSchema,
|
DocSnapshotSchema,
|
||||||
SliceSnapshotSchema,
|
SliceSnapshotSchema,
|
||||||
} from './type.js';
|
} from './type.js';
|
||||||
|
|
||||||
export type JobConfig = {
|
export type JobConfig = {
|
||||||
collection: DocCollection;
|
schema: Schema;
|
||||||
|
blobCRUD: BlobCRUD;
|
||||||
|
docCRUD: DocCRUD;
|
||||||
middlewares?: JobMiddleware[];
|
middlewares?: JobMiddleware[];
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -52,7 +54,9 @@ export class Job {
|
|||||||
|
|
||||||
private readonly _assetsManager: AssetsManager;
|
private readonly _assetsManager: AssetsManager;
|
||||||
|
|
||||||
private readonly _collection: DocCollection;
|
private readonly _schema: Schema;
|
||||||
|
|
||||||
|
private readonly _docCRUD: DocCRUD;
|
||||||
|
|
||||||
private readonly _slots: JobSlots = {
|
private readonly _slots: JobSlots = {
|
||||||
beforeImport: new Slot<BeforeImportPayload>(),
|
beforeImport: new Slot<BeforeImportPayload>(),
|
||||||
@@ -74,31 +78,6 @@ export class Job {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
collectionInfoToSnapshot = (): CollectionInfoSnapshot | undefined => {
|
|
||||||
try {
|
|
||||||
this._slots.beforeExport.emit({
|
|
||||||
type: 'info',
|
|
||||||
});
|
|
||||||
const collectionMeta = this._getCollectionMeta();
|
|
||||||
const snapshot: CollectionInfoSnapshot = {
|
|
||||||
type: 'info',
|
|
||||||
id: this._collection.id,
|
|
||||||
...collectionMeta,
|
|
||||||
};
|
|
||||||
this._slots.afterExport.emit({
|
|
||||||
type: 'info',
|
|
||||||
snapshot,
|
|
||||||
});
|
|
||||||
CollectionInfoSnapshotSchema.parse(snapshot);
|
|
||||||
|
|
||||||
return snapshot;
|
|
||||||
} catch (error) {
|
|
||||||
console.error(`Error when transforming collection info to snapshot:`);
|
|
||||||
console.error(error);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
docToSnapshot = (doc: Doc): DocSnapshot | undefined => {
|
docToSnapshot = (doc: Doc): DocSnapshot | undefined => {
|
||||||
try {
|
try {
|
||||||
this._slots.beforeExport.emit({
|
this._slots.beforeExport.emit({
|
||||||
@@ -199,7 +178,7 @@ export class Job {
|
|||||||
});
|
});
|
||||||
DocSnapshotSchema.parse(snapshot);
|
DocSnapshotSchema.parse(snapshot);
|
||||||
const { meta, blocks } = snapshot;
|
const { meta, blocks } = snapshot;
|
||||||
const doc = this._collection.createDoc({ id: meta.id });
|
const doc = this.docCRUD.create(meta.id);
|
||||||
doc.load();
|
doc.load();
|
||||||
await this.snapshotToBlock(blocks, doc);
|
await this.snapshotToBlock(blocks, doc);
|
||||||
this._slots.afterImport.emit({
|
this._slots.afterImport.emit({
|
||||||
@@ -328,19 +307,24 @@ export class Job {
|
|||||||
return this._assetsManager;
|
return this._assetsManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
get collection() {
|
get schema() {
|
||||||
return this._collection;
|
return this._schema;
|
||||||
}
|
}
|
||||||
|
|
||||||
constructor({ collection, middlewares = [] }: JobConfig) {
|
get docCRUD() {
|
||||||
this._collection = collection;
|
return this._docCRUD;
|
||||||
this._assetsManager = new AssetsManager({ blob: collection.blobSync });
|
}
|
||||||
|
|
||||||
|
constructor({ blobCRUD, schema, docCRUD, middlewares = [] }: JobConfig) {
|
||||||
|
this._assetsManager = new AssetsManager({ blob: blobCRUD });
|
||||||
|
this._schema = schema;
|
||||||
|
this._docCRUD = docCRUD;
|
||||||
|
|
||||||
middlewares.forEach(middleware => {
|
middlewares.forEach(middleware => {
|
||||||
middleware({
|
middleware({
|
||||||
slots: this._slots,
|
slots: this._slots,
|
||||||
|
docCRUD: this._docCRUD,
|
||||||
assetsManager: this._assetsManager,
|
assetsManager: this._assetsManager,
|
||||||
collection: this._collection,
|
|
||||||
adapterConfigs: this._adapterConfigs,
|
adapterConfigs: this._adapterConfigs,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -465,20 +449,8 @@ export class Job {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private _getCollectionMeta() {
|
|
||||||
const { meta } = this._collection;
|
|
||||||
const { docs } = meta;
|
|
||||||
if (!docs) {
|
|
||||||
throw new BlockSuiteError(ErrorCode.TransformerError, 'Docs not found');
|
|
||||||
}
|
|
||||||
return {
|
|
||||||
properties: {}, // for backward compatibility
|
|
||||||
pages: JSON.parse(JSON.stringify(docs)) as DocMeta[],
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
private _getSchema(flavour: string) {
|
private _getSchema(flavour: string) {
|
||||||
const schema = this._collection.schema.flavourSchemaMap.get(flavour);
|
const schema = this.schema.flavourSchemaMap.get(flavour);
|
||||||
if (!schema) {
|
if (!schema) {
|
||||||
throw new BlockSuiteError(
|
throw new BlockSuiteError(
|
||||||
ErrorCode.TransformerError,
|
ErrorCode.TransformerError,
|
||||||
|
|||||||
@@ -1,12 +1,13 @@
|
|||||||
import type { Slot } from '@blocksuite/global/utils';
|
import type { Slot } from '@blocksuite/global/utils';
|
||||||
|
|
||||||
import type { Doc, DocCollection } from '../store/index.js';
|
import type { Doc } from '../store/index.js';
|
||||||
import type { AssetsManager } from './assets.js';
|
import type { AssetsManager } from './assets.js';
|
||||||
import type { DraftModel } from './draft.js';
|
import type { DraftModel } from './draft.js';
|
||||||
import type { Slice } from './slice.js';
|
import type { Slice } from './slice.js';
|
||||||
import type {
|
import type {
|
||||||
BlockSnapshot,
|
BlockSnapshot,
|
||||||
CollectionInfoSnapshot,
|
CollectionInfoSnapshot,
|
||||||
|
DocCRUD,
|
||||||
DocSnapshot,
|
DocSnapshot,
|
||||||
SliceSnapshot,
|
SliceSnapshot,
|
||||||
} from './type.js';
|
} from './type.js';
|
||||||
@@ -79,9 +80,9 @@ export type JobSlots = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
type JobMiddlewareOptions = {
|
type JobMiddlewareOptions = {
|
||||||
collection: DocCollection;
|
|
||||||
assetsManager: AssetsManager;
|
assetsManager: AssetsManager;
|
||||||
slots: JobSlots;
|
slots: JobSlots;
|
||||||
|
docCRUD: DocCRUD;
|
||||||
adapterConfigs: Map<string, string>;
|
adapterConfigs: Map<string, string>;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
|
|
||||||
|
import type { Doc } from '../store/doc/doc.js';
|
||||||
import type { DocMeta, DocsPropertiesMeta } from '../store/meta.js';
|
import type { DocMeta, DocsPropertiesMeta } from '../store/meta.js';
|
||||||
|
|
||||||
export type BlockSnapshot = {
|
export type BlockSnapshot = {
|
||||||
@@ -65,3 +66,16 @@ export const DocSnapshotSchema: z.ZodType<DocSnapshot> = z.object({
|
|||||||
meta: DocMetaSchema,
|
meta: DocMetaSchema,
|
||||||
blocks: BlockSnapshotSchema,
|
blocks: BlockSnapshotSchema,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
export interface BlobCRUD {
|
||||||
|
get: (key: string) => Promise<Blob | null> | Blob | null;
|
||||||
|
set: (key: string, value: Blob) => Promise<string> | string;
|
||||||
|
delete: (key: string) => Promise<void> | void;
|
||||||
|
list: () => Promise<string[]> | string[];
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface DocCRUD {
|
||||||
|
create: (id: string) => Doc;
|
||||||
|
get: (id: string) => Doc | null;
|
||||||
|
delete: (id: string) => void;
|
||||||
|
}
|
||||||
|
|||||||
@@ -102,10 +102,19 @@ export class AdaptersPanel extends WithDisposable(ShadowlessElement) {
|
|||||||
|
|
||||||
private _createJob() {
|
private _createJob() {
|
||||||
return new Job({
|
return new Job({
|
||||||
collection: this.doc.collection,
|
schema: this.doc.collection.schema,
|
||||||
|
blobCRUD: this.doc.collection.blobSync,
|
||||||
|
docCRUD: {
|
||||||
|
create: (id: string) => this.doc.collection.createDoc({ id }),
|
||||||
|
get: (id: string) => this.doc.collection.getDoc(id),
|
||||||
|
delete: (id: string) => this.doc.collection.removeDoc(id),
|
||||||
|
},
|
||||||
middlewares: [
|
middlewares: [
|
||||||
docLinkBaseURLMiddlewareBuilder('https://example.com').get(),
|
docLinkBaseURLMiddlewareBuilder(
|
||||||
titleMiddleware,
|
'https://example.com',
|
||||||
|
this.doc.collection.id
|
||||||
|
).get(),
|
||||||
|
titleMiddleware(this.doc.collection.meta.docMetas),
|
||||||
embedSyncedDocMiddleware('content'),
|
embedSyncedDocMiddleware('content'),
|
||||||
defaultImageProxyMiddleware,
|
defaultImageProxyMiddleware,
|
||||||
],
|
],
|
||||||
|
|||||||
@@ -242,8 +242,17 @@ export class StarterDebugMenu extends ShadowlessElement {
|
|||||||
private async _exportFile(config: AdapterConfig) {
|
private async _exportFile(config: AdapterConfig) {
|
||||||
const doc = this.editor.doc;
|
const doc = this.editor.doc;
|
||||||
const job = new Job({
|
const job = new Job({
|
||||||
collection: this.editor.doc.collection,
|
schema: this.collection.schema,
|
||||||
middlewares: [docLinkBaseURLMiddleware, titleMiddleware],
|
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 adapterFactory = this.editor.std.provider.get(config.identifier);
|
const adapterFactory = this.editor.std.provider.get(config.identifier);
|
||||||
@@ -432,7 +441,13 @@ export class StarterDebugMenu extends ShadowlessElement {
|
|||||||
});
|
});
|
||||||
if (!file) return;
|
if (!file) return;
|
||||||
const job = new Job({
|
const job = new Job({
|
||||||
collection: this.collection,
|
schema: this.collection.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],
|
middlewares: [defaultImageProxyMiddleware],
|
||||||
});
|
});
|
||||||
const htmlAdapter = new NotionHtmlAdapter(job, this.editor.std.provider);
|
const htmlAdapter = new NotionHtmlAdapter(job, this.editor.std.provider);
|
||||||
|
|||||||
@@ -84,7 +84,15 @@ export async function createDefaultDocCollection() {
|
|||||||
// debug info
|
// debug info
|
||||||
window.collection = collection;
|
window.collection = collection;
|
||||||
window.blockSchemas = AffineSchemas;
|
window.blockSchemas = AffineSchemas;
|
||||||
window.job = new Job({ collection });
|
window.job = new Job({
|
||||||
|
schema: collection.schema,
|
||||||
|
blobCRUD: collection.blobSync,
|
||||||
|
docCRUD: {
|
||||||
|
create: (id: string) => collection.createDoc({ id }),
|
||||||
|
get: (id: string) => collection.getDoc(id),
|
||||||
|
delete: (id: string) => collection.removeDoc(id),
|
||||||
|
},
|
||||||
|
});
|
||||||
window.Y = DocCollection.Y;
|
window.Y = DocCollection.Y;
|
||||||
|
|
||||||
return collection;
|
return collection;
|
||||||
|
|||||||
@@ -83,7 +83,15 @@ export function createStarterDocCollection() {
|
|||||||
// debug info
|
// debug info
|
||||||
window.collection = collection;
|
window.collection = collection;
|
||||||
window.blockSchemas = AffineSchemas;
|
window.blockSchemas = AffineSchemas;
|
||||||
window.job = new Job({ collection: collection });
|
window.job = new Job({
|
||||||
|
schema: collection.schema,
|
||||||
|
blobCRUD: collection.blobSync,
|
||||||
|
docCRUD: {
|
||||||
|
create: (id: string) => collection.createDoc({ id }),
|
||||||
|
get: (id: string) => collection.getDoc(id),
|
||||||
|
delete: (id: string) => collection.removeDoc(id),
|
||||||
|
},
|
||||||
|
});
|
||||||
window.Y = DocCollection.Y;
|
window.Y = DocCollection.Y;
|
||||||
window.testUtils = new TestUtils();
|
window.testUtils = new TestUtils();
|
||||||
|
|
||||||
|
|||||||
@@ -6,8 +6,14 @@ export async function importFromSnapshot(
|
|||||||
snapshot: DocSnapshot
|
snapshot: DocSnapshot
|
||||||
) {
|
) {
|
||||||
const job = new Job({
|
const job = new Job({
|
||||||
collection,
|
schema: collection.schema,
|
||||||
middlewares: [replaceIdMiddleware],
|
blobCRUD: collection.blobSync,
|
||||||
|
docCRUD: {
|
||||||
|
create: (id: string) => collection.createDoc({ id }),
|
||||||
|
get: (id: string) => collection.getDoc(id),
|
||||||
|
delete: (id: string) => collection.removeDoc(id),
|
||||||
|
},
|
||||||
|
middlewares: [replaceIdMiddleware(collection.idGenerator)],
|
||||||
});
|
});
|
||||||
|
|
||||||
return job.snapshotToDoc(snapshot);
|
return job.snapshotToDoc(snapshot);
|
||||||
|
|||||||
@@ -174,8 +174,17 @@ const frameworkProvider = framework.provider();
|
|||||||
const blockSuiteDoc = doc.blockSuiteDoc;
|
const blockSuiteDoc = doc.blockSuiteDoc;
|
||||||
|
|
||||||
const job = new Job({
|
const job = new Job({
|
||||||
collection: blockSuiteDoc.collection,
|
schema: blockSuiteDoc.collection.schema,
|
||||||
middlewares: [docLinkBaseURLMiddleware, titleMiddleware],
|
blobCRUD: blockSuiteDoc.collection.blobSync,
|
||||||
|
docCRUD: {
|
||||||
|
create: (id: string) => blockSuiteDoc.collection.createDoc({ id }),
|
||||||
|
get: (id: string) => blockSuiteDoc.collection.getDoc(id),
|
||||||
|
delete: (id: string) => blockSuiteDoc.collection.removeDoc(id),
|
||||||
|
},
|
||||||
|
middlewares: [
|
||||||
|
docLinkBaseURLMiddleware(blockSuiteDoc.collection.id),
|
||||||
|
titleMiddleware(blockSuiteDoc.collection.meta.docMetas),
|
||||||
|
],
|
||||||
});
|
});
|
||||||
const snapshot = job.docToSnapshot(blockSuiteDoc);
|
const snapshot = job.docToSnapshot(blockSuiteDoc);
|
||||||
|
|
||||||
|
|||||||
@@ -78,8 +78,17 @@ export async function getContentFromSlice(
|
|||||||
type: 'markdown' | 'plain-text' = 'markdown'
|
type: 'markdown' | 'plain-text' = 'markdown'
|
||||||
) {
|
) {
|
||||||
const job = new Job({
|
const job = new Job({
|
||||||
collection: host.std.doc.collection,
|
schema: host.std.doc.collection.schema,
|
||||||
middlewares: [titleMiddleware, embedSyncedDocMiddleware('content')],
|
blobCRUD: host.std.doc.collection.blobSync,
|
||||||
|
docCRUD: {
|
||||||
|
create: (id: string) => host.std.doc.collection.createDoc({ id }),
|
||||||
|
get: (id: string) => host.std.doc.collection.getDoc(id),
|
||||||
|
delete: (id: string) => host.std.doc.collection.removeDoc(id),
|
||||||
|
},
|
||||||
|
middlewares: [
|
||||||
|
titleMiddleware(host.std.doc.collection.meta.docMetas),
|
||||||
|
embedSyncedDocMiddleware('content'),
|
||||||
|
],
|
||||||
});
|
});
|
||||||
const snapshot = job.sliceToSnapshot(slice);
|
const snapshot = job.sliceToSnapshot(slice);
|
||||||
if (!snapshot) {
|
if (!snapshot) {
|
||||||
@@ -99,8 +108,14 @@ export async function getContentFromSlice(
|
|||||||
|
|
||||||
export async function getPlainTextFromSlice(host: EditorHost, slice: Slice) {
|
export async function getPlainTextFromSlice(host: EditorHost, slice: Slice) {
|
||||||
const job = new Job({
|
const job = new Job({
|
||||||
collection: host.std.doc.collection,
|
schema: host.std.doc.collection.schema,
|
||||||
middlewares: [titleMiddleware],
|
blobCRUD: host.std.doc.collection.blobSync,
|
||||||
|
docCRUD: {
|
||||||
|
create: (id: string) => host.std.doc.collection.createDoc({ id }),
|
||||||
|
get: (id: string) => host.std.doc.collection.getDoc(id),
|
||||||
|
delete: (id: string) => host.std.doc.collection.removeDoc(id),
|
||||||
|
},
|
||||||
|
middlewares: [titleMiddleware(host.std.doc.collection.meta.docMetas)],
|
||||||
});
|
});
|
||||||
const snapshot = job.sliceToSnapshot(slice);
|
const snapshot = job.sliceToSnapshot(slice);
|
||||||
if (!snapshot) {
|
if (!snapshot) {
|
||||||
@@ -120,7 +135,13 @@ export const markdownToSnapshot = async (
|
|||||||
host: EditorHost
|
host: EditorHost
|
||||||
) => {
|
) => {
|
||||||
const job = new Job({
|
const job = new Job({
|
||||||
collection: host.std.doc.collection,
|
schema: host.std.doc.collection.schema,
|
||||||
|
blobCRUD: host.std.doc.collection.blobSync,
|
||||||
|
docCRUD: {
|
||||||
|
create: (id: string) => host.std.doc.collection.createDoc({ id }),
|
||||||
|
get: (id: string) => host.std.doc.collection.getDoc(id),
|
||||||
|
delete: (id: string) => host.std.doc.collection.removeDoc(id),
|
||||||
|
},
|
||||||
middlewares: [defaultImageProxyMiddleware, pasteMiddleware(host.std)],
|
middlewares: [defaultImageProxyMiddleware, pasteMiddleware(host.std)],
|
||||||
});
|
});
|
||||||
const markdownAdapter = new MixTextAdapter(job, host.std.provider);
|
const markdownAdapter = new MixTextAdapter(job, host.std.provider);
|
||||||
@@ -204,7 +225,13 @@ export async function markDownToDoc(
|
|||||||
middlewares.push(...additionalMiddlewares);
|
middlewares.push(...additionalMiddlewares);
|
||||||
}
|
}
|
||||||
const job = new Job({
|
const job = new Job({
|
||||||
collection,
|
schema: collection.schema,
|
||||||
|
blobCRUD: collection.blobSync,
|
||||||
|
docCRUD: {
|
||||||
|
create: (id: string) => collection.createDoc({ id }),
|
||||||
|
get: (id: string) => collection.getDoc(id),
|
||||||
|
delete: (id: string) => collection.removeDoc(id),
|
||||||
|
},
|
||||||
middlewares,
|
middlewares,
|
||||||
});
|
});
|
||||||
const mdAdapter = new MarkdownAdapter(job, provider);
|
const mdAdapter = new MarkdownAdapter(job, provider);
|
||||||
|
|||||||
@@ -55,10 +55,16 @@ interface AdapterConfig {
|
|||||||
|
|
||||||
async function exportDoc(doc: Doc, std: BlockStdScope, config: AdapterConfig) {
|
async function exportDoc(doc: Doc, std: BlockStdScope, config: AdapterConfig) {
|
||||||
const job = new Job({
|
const job = new Job({
|
||||||
collection: doc.collection,
|
schema: doc.collection.schema,
|
||||||
|
blobCRUD: doc.collection.blobSync,
|
||||||
|
docCRUD: {
|
||||||
|
create: (id: string) => doc.collection.createDoc({ id }),
|
||||||
|
get: (id: string) => doc.collection.getDoc(id),
|
||||||
|
delete: (id: string) => doc.collection.removeDoc(id),
|
||||||
|
},
|
||||||
middlewares: [
|
middlewares: [
|
||||||
docLinkBaseURLMiddleware,
|
docLinkBaseURLMiddleware(doc.collection.id),
|
||||||
titleMiddleware,
|
titleMiddleware(doc.collection.meta.docMetas),
|
||||||
embedSyncedDocMiddleware('content'),
|
embedSyncedDocMiddleware('content'),
|
||||||
],
|
],
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -79,7 +79,13 @@ async function initDoc(name: DocName) {
|
|||||||
const snapshot = (await loaders[name]()) as DocSnapshot;
|
const snapshot = (await loaders[name]()) as DocSnapshot;
|
||||||
const collection = await getCollection();
|
const collection = await getCollection();
|
||||||
const job = new Job({
|
const job = new Job({
|
||||||
collection,
|
schema: collection.schema,
|
||||||
|
blobCRUD: collection.blobSync,
|
||||||
|
docCRUD: {
|
||||||
|
create: (id: string) => collection.createDoc({ id }),
|
||||||
|
get: (id: string) => collection.getDoc(id),
|
||||||
|
delete: (id: string) => collection.removeDoc(id),
|
||||||
|
},
|
||||||
middlewares: [],
|
middlewares: [],
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -192,7 +192,13 @@ function generateMarkdownPreviewBuilder(
|
|||||||
const provider = container.provider();
|
const provider = container.provider();
|
||||||
const markdownAdapter = new MarkdownAdapter(
|
const markdownAdapter = new MarkdownAdapter(
|
||||||
new Job({
|
new Job({
|
||||||
collection: markdownPreviewDocCollection,
|
schema: markdownPreviewDocCollection.schema,
|
||||||
|
blobCRUD: markdownPreviewDocCollection.blobSync,
|
||||||
|
docCRUD: {
|
||||||
|
create: (id: string) => markdownPreviewDocCollection.createDoc({ id }),
|
||||||
|
get: (id: string) => markdownPreviewDocCollection.getDoc(id),
|
||||||
|
delete: (id: string) => markdownPreviewDocCollection.removeDoc(id),
|
||||||
|
},
|
||||||
middlewares: [docLinkBaseURLMiddleware, titleMiddleware],
|
middlewares: [docLinkBaseURLMiddleware, titleMiddleware],
|
||||||
}),
|
}),
|
||||||
provider
|
provider
|
||||||
|
|||||||
Reference in New Issue
Block a user