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): job should not rely on doc collection directly #9488

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 @@ -27,7 +27,7 @@ export const embedSyncedDocBlockHtmlAdapterMatcher: BlockHtmlAdapterMatcher = {

if (type === 'content') {
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);
if (!syncedDoc) return;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const embedSyncedDocBlockMarkdownAdapterMatcher: BlockMarkdownAdapterMatc

if (type === 'content') {
const syncedDocId = o.node.props.pageId as string;
const syncedDoc = job.collection.getDoc(syncedDocId);
const syncedDoc = job.docCRUD.get(syncedDocId);
if (!syncedDoc) return;

if (counter === 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const embedSyncedDocBlockPlainTextAdapterMatcher: BlockPlainTextAdapterMa

if (type === 'content') {
const syncedDocId = o.node.props.pageId as string;
const syncedDoc = job.collection.getDoc(syncedDocId);
const syncedDoc = job.docCRUD.get(syncedDocId);
if (!syncedDoc) return;

if (counter === 1) {
Expand Down
4 changes: 2 additions & 2 deletions blocksuite/affine/shared/src/services/drag-handle-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
StdIdentifier,
} from '@blocksuite/block-std';
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>(
'AffineDndApiIdentifier'
Expand Down Expand Up @@ -40,7 +40,7 @@ export class DNDAPIExtension extends Extension {
const { docId, flavour = 'affine:embed-linked-doc', blockId } = options;

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);
if (!snapshot) {
console.error('Failed to convert slice to snapshot');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import type { JobMiddleware } from '@blocksuite/store';

export const newIdCrossDoc =
(std: BlockStdScope): JobMiddleware =>
({ slots, collection }) => {
({ slots }) => {
let samePage = false;
slots.beforeImport.on(payload => {
if (payload.type === 'slice') {
samePage = payload.snapshot.pageId === std.doc.id;
}
if (payload.type === 'block' && !samePage) {
payload.snapshot.id = collection.idGenerator();
payload.snapshot.id = std.collection.idGenerator();
}
});
};
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { JobMiddleware } from '@blocksuite/store';

export const surfaceRefToEmbed =
(std: BlockStdScope): JobMiddleware =>
({ slots, collection }) => {
({ slots }) => {
let pageId: string | null = null;
slots.beforeImport.on(payload => {
if (payload.type === 'slice') {
Expand All @@ -18,7 +18,7 @@ export const surfaceRefToEmbed =
!std.doc.hasBlock(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.props = {
blockId: id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import {
} from '@blocksuite/block-std';
import { GfxControllerIdentifier } from '@blocksuite/block-std/gfx';
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 { AFFINE_DRAG_HANDLE_WIDGET } from '../consts.js';
Expand Down Expand Up @@ -544,14 +544,11 @@ export class DragEventWatcher {

private _getJob() {
const std = this._std;
return new Job({
collection: std.collection,
middlewares: [
newIdCrossDoc(std),
reorderList(std),
surfaceRefToEmbed(std),
],
});
return std.getJob([
newIdCrossDoc(std),
reorderList(std),
surfaceRefToEmbed(std),
]);
}

private _serializeData(slice: Slice, state: DndEventState) {
Expand Down
11 changes: 10 additions & 1 deletion blocksuite/blocks/src/__tests__/utils/create-job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,14 @@ export function createJob(middlewares?: JobMiddleware[]) {
const schema = new Schema().register(AffineSchemas);
const docCollection = new DocCollection({ schema });
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),
},
});
}
33 changes: 27 additions & 6 deletions blocksuite/blocks/src/_common/transformers/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,17 @@ const provider = container.provider();
*/
async function exportDoc(doc: Doc) {
const job = new Job({
collection: doc.collection,
middlewares: [docLinkBaseURLMiddleware, titleMiddleware],
schema: doc.schema,
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 adapter = new HtmlAdapter(job, provider);
Expand Down Expand Up @@ -91,11 +100,17 @@ async function importHTMLToDoc({
fileName,
}: ImportHTMLToDocOptions) {
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: [
defaultImageProxyMiddleware,
fileNameMiddleware(fileName),
docLinkBaseURLMiddleware,
docLinkBaseURLMiddleware(collection.id),
],
});
const htmlAdapter = new HtmlAdapter(job, provider);
Expand Down Expand Up @@ -147,11 +162,17 @@ async function importHTMLZip({ collection, imported }: ImportHTMLZipOptions) {
htmlBlobs.map(async ([fileName, blob]) => {
const fileNameWithoutExt = fileName.replace(/\.[^/.]+$/, '');
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: [
defaultImageProxyMiddleware,
fileNameMiddleware(fileNameWithoutExt),
docLinkBaseURLMiddleware,
docLinkBaseURLMiddleware(collection.id),
],
});
const assets = job.assets;
Expand Down
46 changes: 38 additions & 8 deletions blocksuite/blocks/src/_common/transformers/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,17 @@ type ImportMarkdownZipOptions = {
*/
async function exportDoc(doc: Doc) {
const job = new Job({
collection: doc.collection,
middlewares: [docLinkBaseURLMiddleware, titleMiddleware],
schema: doc.schema,
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);

Expand Down Expand Up @@ -101,8 +110,17 @@ async function importMarkdownToBlock({
blockId,
}: ImportMarkdownToBlockOptions) {
const job = new Job({
collection: doc.collection,
middlewares: [defaultImageProxyMiddleware, docLinkBaseURLMiddleware],
schema: doc.schema,
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 snapshot = await adapter.toSliceSnapshot({
Expand Down Expand Up @@ -137,11 +155,17 @@ async function importMarkdownToDoc({
fileName,
}: ImportMarkdownToDocOptions) {
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: [
defaultImageProxyMiddleware,
fileNameMiddleware(fileName),
docLinkBaseURLMiddleware,
docLinkBaseURLMiddleware(collection.id),
],
});
const mdAdapter = new MarkdownAdapter(job, provider);
Expand Down Expand Up @@ -195,11 +219,17 @@ async function importMarkdownZip({
markdownBlobs.map(async ([fileName, blob]) => {
const fileNameWithoutExt = fileName.replace(/\.[^/.]+$/, '');
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: [
defaultImageProxyMiddleware,
fileNameMiddleware(fileNameWithoutExt),
docLinkBaseURLMiddleware,
docLinkBaseURLMiddleware(collection.id),
],
});
const assets = job.assets;
Expand Down
Loading
Loading