Skip to content

Commit a74def2

Browse files
fix: Isolated drag/drop per editor instance (#174)
* Made dragging & dropping confined to the editor instance * Reverted drop cursor fix * Minor change * Fixed build error
1 parent accf253 commit a74def2

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

packages/core/src/extensions/DraggableBlocks/DraggableBlocksPlugin.ts

+20-4
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,8 @@ export class BlockMenuView {
244244

245245
hoveredBlock: HTMLElement | undefined;
246246

247+
// Used to check if currently dragged content comes from this editor instance.
248+
isDragging = false;
247249
menuOpen = false;
248250
menuFrozen = false;
249251

@@ -264,6 +266,7 @@ export class BlockMenuView {
264266

265267
document.body.addEventListener("drop", this.onDrop, true);
266268
document.body.addEventListener("dragover", this.onDragOver);
269+
this.ttEditor.view.dom.addEventListener("dragstart", this.onDragStart);
267270

268271
// Shows or updates menu position whenever the cursor moves, if the menu isn't frozen.
269272
document.body.addEventListener("mousemove", this.onMouseMove, true);
@@ -277,20 +280,29 @@ export class BlockMenuView {
277280
document.body.addEventListener("keydown", this.onKeyDown, true);
278281
}
279282

283+
/**
284+
* Sets isDragging when dragging text.
285+
*/
286+
onDragStart = () => {
287+
this.isDragging = true;
288+
};
289+
280290
/**
281291
* If the event is outside the editor contents,
282292
* we dispatch a fake event, so that we can still drop the content
283293
* when dragging / dropping to the side of the editor
284294
*/
285295
onDrop = (event: DragEvent) => {
286-
if ((event as any).synthetic) {
296+
if ((event as any).synthetic || !this.isDragging) {
287297
return;
288298
}
289299
let pos = this.ttEditor.view.posAtCoords({
290300
left: event.clientX,
291301
top: event.clientY,
292302
});
293303

304+
this.isDragging = false;
305+
294306
if (!pos || pos.inside === -1) {
295307
const evt = new Event("drop", event) as any;
296308
const editorBoundingBox = (
@@ -312,7 +324,7 @@ export class BlockMenuView {
312324
* when dragging / dropping to the side of the editor
313325
*/
314326
onDragOver = (event: DragEvent) => {
315-
if ((event as any).synthetic) {
327+
if ((event as any).synthetic || !this.isDragging) {
316328
return;
317329
}
318330
let pos = this.ttEditor.view.posAtCoords({
@@ -429,6 +441,7 @@ export class BlockMenuView {
429441
}
430442
document.body.removeEventListener("mousemove", this.onMouseMove);
431443
document.body.removeEventListener("dragover", this.onDragOver);
444+
this.ttEditor.view.dom.removeEventListener("dragstart", this.onDragStart);
432445
document.body.removeEventListener("drop", this.onDrop);
433446
document.body.removeEventListener("mousedown", this.onMouseDown);
434447
document.removeEventListener("scroll", this.onScroll);
@@ -488,8 +501,11 @@ export class BlockMenuView {
488501
return {
489502
editor: this.editor,
490503
addBlock: () => this.addBlock(),
491-
blockDragStart: (event: DragEvent) =>
492-
dragStart(event, this.ttEditor.view),
504+
blockDragStart: (event: DragEvent) => {
505+
// Sets isDragging when dragging blocks.
506+
this.isDragging = true;
507+
dragStart(event, this.ttEditor.view);
508+
},
493509
blockDragEnd: () => unsetDragImage(),
494510
freezeMenu: () => {
495511
this.menuFrozen = true;

0 commit comments

Comments
 (0)