fix(core): dnd should not create new doc on dragging start (#9201)

fix AF-2000
This commit is contained in:
pengx17
2024-12-19 03:28:29 +00:00
parent a153a1c3c9
commit 6dc512e0da

View File

@@ -6,7 +6,6 @@ import {
import { createPageModeSpecs } from '@affine/core/components/blocksuite/block-suite-editor/specs/page'; import { createPageModeSpecs } from '@affine/core/components/blocksuite/block-suite-editor/specs/page';
import type { AffineDNDData } from '@affine/core/types/dnd'; import type { AffineDNDData } from '@affine/core/types/dnd';
import { BlockStdScope } from '@blocksuite/affine/block-std'; import { BlockStdScope } from '@blocksuite/affine/block-std';
import type { DNDAPIExtension } from '@blocksuite/affine/blocks';
import { DndApiExtensionIdentifier } from '@blocksuite/affine/blocks'; import { DndApiExtensionIdentifier } from '@blocksuite/affine/blocks';
import { type SliceSnapshot } from '@blocksuite/affine/store'; import { type SliceSnapshot } from '@blocksuite/affine/store';
import type { DocsService, WorkspaceService } from '@toeverything/infra'; import type { DocsService, WorkspaceService } from '@toeverything/infra';
@@ -51,21 +50,20 @@ export class DndService extends Service {
source: ExternalDragPayload source: ExternalDragPayload
) => Entity | null)[] = []; ) => 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 (!doc) {
if (this._blocksuiteDndAPI) { return null;
return this._blocksuiteDndAPI;
} }
const collection = this.workspaceService.workspace.docCollection;
const doc = collection.createDoc();
const std = new BlockStdScope({ const std = new BlockStdScope({
doc, doc,
extensions: createPageModeSpecs(this.framework), extensions: createPageModeSpecs(this.framework),
}); });
const dndAPI = std.get(DndApiExtensionIdentifier); const dndAPI = std.get(DndApiExtensionIdentifier);
this._blocksuiteDndAPI = dndAPI;
return dndAPI; return dndAPI;
} }
@@ -113,7 +111,13 @@ export class DndService extends Service {
return {}; return {};
} }
const snapshotSlice = this.blocksuiteDndAPI.fromEntity({ const dndAPI = this.getBlocksuiteDndAPI(normalData.entity.id);
if (!dndAPI) {
return {};
}
const snapshotSlice = dndAPI.fromEntity({
docId: normalData.entity.id, docId: normalData.entity.id,
flavour: 'affine:embed-linked-doc', flavour: 'affine:embed-linked-doc',
}); });
@@ -122,10 +126,10 @@ export class DndService extends Service {
return {}; return {};
} }
const encoded = this.blocksuiteDndAPI.encodeSnapshot(snapshotSlice); const encoded = dndAPI.encodeSnapshot(snapshotSlice);
return { return {
[this.blocksuiteDndAPI.mimeType]: encoded, [dndAPI.mimeType]: encoded,
}; };
}; };
@@ -157,11 +161,15 @@ export class DndService extends Service {
private readonly resolveBlocksuiteExternalData = ( private readonly resolveBlocksuiteExternalData = (
source: ExternalDragPayload source: ExternalDragPayload
): Entity | null => { ): 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) { if (!encoded) {
return null; return null;
} }
const snapshot = this.blocksuiteDndAPI.decodeSnapshot(encoded); const snapshot = dndAPI.decodeSnapshot(encoded);
if (!snapshot) { if (!snapshot) {
return null; return null;
} }