Skip to content

Commit

Permalink
fix: dnd from entity api (#8958)
Browse files Browse the repository at this point in the history
  • Loading branch information
Saul-Mirone authored Dec 12, 2024
1 parent e7fed8a commit aefecb2
Showing 1 changed file with 25 additions and 21 deletions.
46 changes: 25 additions & 21 deletions packages/affine/shared/src/services/drag-handle-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,7 @@ import {
StdIdentifier,
} from '@blocksuite/block-std';
import { type Container, createIdentifier } from '@blocksuite/global/di';
import {
type BlockModel,
Job,
Slice,
type SliceSnapshot,
} from '@blocksuite/store';
import { Job, Slice, type SliceSnapshot } from '@blocksuite/store';

export type DropType = 'before' | 'after' | 'in';
export type OnDragStartProps = {
Expand Down Expand Up @@ -97,26 +92,35 @@ export class DNDAPIExtension extends Extension {
return encodeURIComponent(snapshot);
}

fromEntity(docId: string, blockId?: string): SliceSnapshot | null {
const doc = this.std.collection.getDoc(docId);
if (!doc) {
console.error(`Doc ${docId} not found`);
return null;
}
const blocks: BlockModel[] = [];
if (blockId) {
const model = doc.getBlock(blockId)?.model;
if (model) {
blocks.push(model);
}
}
const slice = Slice.fromModels(doc, blocks);
fromEntity(options: {
docId: string;
flavour?: string;
blockId?: string;
}): SliceSnapshot | null {
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 snapshot = job.sliceToSnapshot(slice);
if (!snapshot) {
console.error('Failed to convert slice to snapshot');
return null;
}
return snapshot;
const props = {
...(blockId ? { blockId } : {}),
pageId: docId,
};
return {
...snapshot,
content: [
{
id: this.std.collection.idGenerator(),
type: 'block',
flavour,
props,
children: [],
},
],
};
}
}

0 comments on commit aefecb2

Please sign in to comment.