Skip to content

Commit e57beab

Browse files
committed
Call prepare_drag resource for snippets too and properly cancel
1 parent 1e21ff9 commit e57beab

File tree

2 files changed

+30
-9
lines changed

2 files changed

+30
-9
lines changed

addons/html_builder/static/src/core/drag_and_drop_plugin.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,11 @@ export class DragAndDropPlugin extends Plugin {
137137
},
138138
{ withLoadingEffect: false }
139139
);
140-
this.restoreDragSavePoint = this.dependencies.history.makeSavePoint();
140+
const restoreDragSavePoint = this.dependencies.history.makeSavePoint();
141141
this.cancelDragAndDrop = () => {
142-
this.restoreDragSavePoint();
142+
// Undo the changes needed to ease the drag and drop.
143+
this.dragState.restoreCallbacks?.forEach((restore) => restore());
144+
restoreDragSavePoint();
143145
dragAndDropResolve();
144146
this.dependencies["builder-options"].updateContainers(this.overlayTarget);
145147
};
@@ -326,6 +328,7 @@ export class DragAndDropPlugin extends Plugin {
326328

327329
// Undo the changes needed to ease the drag and drop.
328330
this.dragState.restoreCallbacks.forEach((restore) => restore());
331+
this.dragState.restoreCallbacks = null;
329332

330333
// Add a history step only if the element was not dropped where
331334
// it was before, otherwise cancel everything.

addons/html_builder/static/src/sidebar/block_tab.js

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ export class BlockTab extends Component {
5454
onSnippetGroupClick(snippet) {
5555
this.shared.operation.next(
5656
async () => {
57+
this.cancelDragAndDrop = this.shared.history.makeSavePoint();
5758
let snippetEl;
5859
const baseSectionEl = snippet.content.cloneNode(true);
5960
this.state.ongoingInsertion = true;
@@ -101,6 +102,7 @@ export class BlockTab extends Component {
101102
await this.processDroppedSnippet(snippetEl);
102103
}
103104
this.state.ongoingInsertion = false;
105+
delete this.cancelDragAndDrop;
104106
},
105107
{ withLoadingEffect: false }
106108
);
@@ -223,7 +225,12 @@ export class BlockTab extends Component {
223225
},
224226
{ withLoadingEffect: false }
225227
);
226-
this.cancelDragAndDrop = this.shared.history.makeSavePoint();
228+
const restoreDragSavePoint = this.shared.history.makeSavePoint();
229+
this.cancelDragAndDrop = () => {
230+
// Undo the changes needed to ease the drag and drop.
231+
this.dragState.restoreCallbacks?.forEach((restore) => restore());
232+
restoreDragSavePoint();
233+
};
227234
this.hideSnippetToolTip?.();
228235

229236
this.document.body.classList.add("oe_dropzone_active");
@@ -232,6 +239,14 @@ export class BlockTab extends Component {
232239
this.dragState = {};
233240
dropzoneEls = [];
234241

242+
// Make some changes on the page to ease the drag and drop.
243+
const restoreCallbacks = [];
244+
for (const prepareDrag of this.env.editor.getResource("on_prepare_drag_handlers")) {
245+
const restore = prepareDrag();
246+
restoreCallbacks.push(restore);
247+
}
248+
this.dragState.restoreCallbacks = restoreCallbacks;
249+
235250
const category = element.closest(".o_snippets_container").id;
236251
const id = element.dataset.id;
237252
snippet = this.snippetModel.getSnippet(category, id);
@@ -338,17 +353,20 @@ export class BlockTab extends Component {
338353
if (closestDropzoneEl) {
339354
currentDropzoneEl = closestDropzoneEl;
340355
}
341-
} else {
342-
this.cancelDragAndDrop();
343356
}
344357
}
345358

346359
if (currentDropzoneEl) {
347360
currentDropzoneEl.after(snippetEl);
348361
this.shared.dropzone.removeDropzones();
349362

363+
// Undo the changes needed to ease the drag and drop.
364+
this.dragState.restoreCallbacks.forEach((restore) => restore());
365+
this.dragState.restoreCallbacks = null;
366+
350367
if (!isSnippetGroup) {
351368
await this.processDroppedSnippet(snippetEl);
369+
delete this.cancelDragAndDrop;
352370
} else {
353371
this.shared.operation.next(
354372
async () => {
@@ -357,13 +375,13 @@ export class BlockTab extends Component {
357375
{ withLoadingEffect: false }
358376
);
359377
}
378+
} else {
379+
this.cancelDragAndDrop();
380+
delete this.cancelDragAndDrop;
360381
}
361382

362383
this.state.ongoingInsertion = false;
363384
delete this.cancelSnippetPreview;
364-
if (!isSnippetGroup) {
365-
delete this.cancelDragAndDrop;
366-
}
367385
dragAndDropResolve();
368386
},
369387
};
@@ -382,7 +400,7 @@ export class BlockTab extends Component {
382400
const cancel = await onSnippetDropped({ snippetEl, dragState: this.dragState });
383401
// Cancel everything if the resource asked to.
384402
if (cancel) {
385-
this.cancelDragAndDrop?.();
403+
this.cancelDragAndDrop();
386404
return;
387405
}
388406
}

0 commit comments

Comments
 (0)