Skip to content

Commit

Permalink
fix(core): dnd should not create new doc on dragging start (#9201)
Browse files Browse the repository at this point in the history
  • Loading branch information
pengx17 committed Dec 19, 2024
1 parent a153a1c commit 6dc512e
Showing 1 changed file with 21 additions and 13 deletions.
34 changes: 21 additions & 13 deletions packages/frontend/core/src/modules/dnd/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
import { createPageModeSpecs } from '@affine/core/components/blocksuite/block-suite-editor/specs/page';
import type { AffineDNDData } from '@affine/core/types/dnd';
import { BlockStdScope } from '@blocksuite/affine/block-std';
import type { DNDAPIExtension } from '@blocksuite/affine/blocks';
import { DndApiExtensionIdentifier } from '@blocksuite/affine/blocks';
import { type SliceSnapshot } from '@blocksuite/affine/store';
import type { DocsService, WorkspaceService } from '@toeverything/infra';
Expand Down Expand Up @@ -51,21 +50,20 @@ export class DndService extends Service {
source: ExternalDragPayload
) => Entity | null)[] = [];

private _blocksuiteDndAPI: DNDAPIExtension | null = null;
getBlocksuiteDndAPI(sourceDocId?: string) {
const collection = this.workspaceService.workspace.docCollection;
sourceDocId ??= collection.docs.keys().next().value;
const doc = sourceDocId ? collection.getDoc(sourceDocId) : null;

get blocksuiteDndAPI() {
if (this._blocksuiteDndAPI) {
return this._blocksuiteDndAPI;
if (!doc) {
return null;
}

const collection = this.workspaceService.workspace.docCollection;
const doc = collection.createDoc();
const std = new BlockStdScope({
doc,
extensions: createPageModeSpecs(this.framework),
});
const dndAPI = std.get(DndApiExtensionIdentifier);
this._blocksuiteDndAPI = dndAPI;
return dndAPI;
}

Expand Down Expand Up @@ -113,7 +111,13 @@ export class DndService extends Service {
return {};
}

const snapshotSlice = this.blocksuiteDndAPI.fromEntity({
const dndAPI = this.getBlocksuiteDndAPI(normalData.entity.id);

if (!dndAPI) {
return {};
}

const snapshotSlice = dndAPI.fromEntity({
docId: normalData.entity.id,
flavour: 'affine:embed-linked-doc',
});
Expand All @@ -122,10 +126,10 @@ export class DndService extends Service {
return {};
}

const encoded = this.blocksuiteDndAPI.encodeSnapshot(snapshotSlice);
const encoded = dndAPI.encodeSnapshot(snapshotSlice);

return {
[this.blocksuiteDndAPI.mimeType]: encoded,
[dndAPI.mimeType]: encoded,
};
};

Expand Down Expand Up @@ -157,11 +161,15 @@ export class DndService extends Service {
private readonly resolveBlocksuiteExternalData = (
source: ExternalDragPayload
): Entity | null => {
const encoded = source.getStringData(this.blocksuiteDndAPI.mimeType);
const dndAPI = this.getBlocksuiteDndAPI();
if (!dndAPI) {
return null;
}
const encoded = source.getStringData(dndAPI.mimeType);
if (!encoded) {
return null;
}
const snapshot = this.blocksuiteDndAPI.decodeSnapshot(encoded);
const snapshot = dndAPI.decodeSnapshot(encoded);
if (!snapshot) {
return null;
}
Expand Down

0 comments on commit 6dc512e

Please sign in to comment.