summaryrefslogtreecommitdiff
path: root/addons/web_editor/static
diff options
context:
space:
mode:
authorBenjamin Vray <bvr@odoo.com>2021-01-14 15:51:52 +0000
committerqsm-odoo <qsm@odoo.com>2021-01-21 14:32:39 +0000
commitb259b8069f74088bd8add50ddab76c1a7630ac8f (patch)
tree3a2e7a9ac91c29da8eb879689b310d851dcfead2 /addons/web_editor/static
parent5db67fb2931517ae94fe0396ef80e9ad997e59cd (diff)
[FIX] website, web_editor: fix drop zones when dragging a snippet
Before this commit: -when moving a snippet using the drag and drop button, two drop zones instead of one was added at the initial location of the dragged snippet. -when moving a snippet using the drag and drop button, no drop zone was added at the initial location of the dragged snippet if the snippet was alone in its parent. For example, the left jumbotron in the banner snippet. -the drop zones handled by the "children rules" were never vertical when it was necessary. This commit also refactors the code to avoid some duplicated lines. task-2312878 closes odoo/odoo#64838 X-original-commit: e637056b3b1f4129342e5c79a4d761c7aaf56a40 Signed-off-by: Quentin Smetz (qsm) <qsm@odoo.com>
Diffstat (limited to 'addons/web_editor/static')
-rw-r--r--addons/web_editor/static/src/js/editor/snippets.editor.js150
1 files changed, 76 insertions, 74 deletions
diff --git a/addons/web_editor/static/src/js/editor/snippets.editor.js b/addons/web_editor/static/src/js/editor/snippets.editor.js
index 391310b57b1..6e4affc028b 100644
--- a/addons/web_editor/static/src/js/editor/snippets.editor.js
+++ b/addons/web_editor/static/src/js/editor/snippets.editor.js
@@ -1251,57 +1251,81 @@ var SnippetsMenu = Widget.extend({
_activateInsertionZones: function ($selectorSiblings, $selectorChildren) {
var self = this;
- function isFullWidth($elem) {
- return $elem.parent().width() === $elem.outerWidth(true);
+ // Check if the drop zone should be horizontal or vertical
+ function setDropZoneDirection($elem, $parent, $sibling) {
+ var vertical = false;
+ var style = {};
+ $sibling = $sibling || $elem;
+ var css = window.getComputedStyle($elem[0]);
+ var parentCss = window.getComputedStyle($parent[0]);
+ var float = css.float || css.cssFloat;
+ var display = parentCss.display;
+ var flex = parentCss.flexDirection;
+ if (float === 'left' || float === 'right' || (display === 'flex' && flex === 'row')) {
+ style['float'] = float;
+ if ($sibling.parent().width() !== $sibling.outerWidth(true)) {
+ vertical = true;
+ style['height'] = Math.max($sibling.outerHeight(), 30) + 'px';
+ }
+ }
+ return {
+ vertical: vertical,
+ style: style,
+ };
+ }
+
+ // If the previous sibling is a BR tag or a non-whitespace text, it
+ // should be a vertical dropzone.
+ function testPreviousSibling(node, $zone) {
+ if (!node || ((node.tagName || !node.textContent.match(/\S/)) && node.tagName !== 'BR')) {
+ return false;
+ }
+ return {
+ vertical: true,
+ style: {
+ 'float': 'none',
+ 'display': 'inline-block',
+ 'height': parseInt(self.window.getComputedStyle($zone[0]).lineHeight) + 'px',
+ },
+ };
+ }
+
+ // Firstly, add a dropzone after the clone
+ var $clone = $('.oe_drop_clone');
+ if ($clone.length) {
+ var $neighbor = $clone.prev();
+ if (!$neighbor.length) {
+ $neighbor = $clone.next();
+ }
+ var data;
+ if ($neighbor.length) {
+ data = setDropZoneDirection($neighbor, $neighbor.parent());
+ } else {
+ data = {
+ vertical: false,
+ style: {},
+ };
+ }
+ self._insertDropzone($('<we-hook/>').insertAfter($clone), data.vertical, data.style);
}
if ($selectorChildren) {
$selectorChildren.each(function () {
+ var data;
var $zone = $(this);
- var style;
- var vertical;
- var node;
- var css = self.window.getComputedStyle(this);
- var parentCss = self.window.getComputedStyle($zone.parent()[0]);
- var float = css.float || css.cssFloat;
- var parentDisplay = parentCss.display;
- var parentFlex = parentCss.flexDirection;
-
- style = {};
- vertical = false;
- node = $zone[0].lastChild;
- var test = !!(node && ((!node.tagName && node.textContent.match(/\S/)) || node.tagName === 'BR'));
- if (test) {
- vertical = true;
- style['float'] = 'none';
- style['height'] = parseInt(self.window.getComputedStyle($zone[0]).lineHeight) + 'px';
- style['display'] = 'inline-block';
- } else if (float === 'left' || float === 'right' || (parentDisplay === 'flex' && parentFlex === 'row')) {
- style['float'] = float;
- if (!isFullWidth($zone) && !$zone.hasClass('oe_structure')) {
- vertical = true;
- style['height'] = Math.max($zone.outerHeight(), 30) + 'px';
- }
+ var $children = $zone.find('> :not(.oe_drop_zone, .oe_drop_clone)');
+
+ if (!$zone.children().last().is('.oe_drop_zone')) {
+ data = testPreviousSibling($zone[0].lastChild, $zone)
+ || setDropZoneDirection($zone, $zone, $children.last());
+ self._insertDropzone($('<we-hook/>').appendTo($zone), data.vertical, data.style);
}
- self._insertDropzone($('<we-hook/>').appendTo($zone), vertical, style);
- style = {};
- vertical = false;
- node = $zone[0].firstChild;
- test = !!(node && ((!node.tagName && node.textContent.match(/\S/)) || node.tagName === 'BR'));
- if (test) {
- vertical = true;
- style['float'] = 'none';
- style['height'] = parseInt(self.window.getComputedStyle($zone[0]).lineHeight) + 'px';
- style['display'] = 'inline-block';
- } else if (float === 'left' || float === 'right' || (parentDisplay === 'flex' && parentFlex === 'row')) {
- style['float'] = float;
- if (!isFullWidth($zone) && !$zone.hasClass('oe_structure')) {
- vertical = true;
- style['height'] = Math.max($zone.outerHeight(), 30) + 'px';
- }
+ if (!$zone.children().first().is('.oe_drop_clone')) {
+ data = testPreviousSibling($zone[0].firstChild, $zone)
+ || setDropZoneDirection($zone, $zone, $children.first());
+ self._insertDropzone($('<we-hook/>').prependTo($zone), data.vertical, data.style);
}
- self._insertDropzone($('<we-hook/>').prependTo($zone), vertical, style);
});
// add children near drop zone
@@ -1309,39 +1333,17 @@ var SnippetsMenu = Widget.extend({
}
if ($selectorSiblings) {
- $selectorSiblings.filter(':not(.oe_drop_zone):not(.oe_drop_clone)').each(function () {
+ $selectorSiblings.not('.oe_drop_zone, .oe_drop_clone').each(function () {
+ var data;
var $zone = $(this);
- var style;
- var vertical;
- var css = self.window.getComputedStyle(this);
- var parentCss = self.window.getComputedStyle($zone.parent()[0]);
- var float = css.float || css.cssFloat;
- var parentDisplay = parentCss.display;
- var parentFlex = parentCss.flexDirection;
-
- if ($zone.prev('.oe_drop_zone:visible').length === 0) {
- style = {};
- vertical = false;
- if (float === 'left' || float === 'right' || (parentDisplay === 'flex' && parentFlex === 'row')) {
- style['float'] = float;
- if (!isFullWidth($zone)) {
- vertical = true;
- style['height'] = Math.max($zone.outerHeight(), 30) + 'px';
- }
- }
- self._insertDropzone($('<we-hook/>').insertBefore($zone), vertical, style);
+
+ if (!$zone.prev('.oe_drop_zone:visible, .oe_drop_clone').length) {
+ data = setDropZoneDirection($zone, $zone.parent());
+ self._insertDropzone($('<we-hook/>').insertBefore($zone), data.vertical, data.style);
}
- if ($zone.next('.oe_drop_zone:visible').length === 0) {
- style = {};
- vertical = false;
- if (float === 'left' || float === 'right' || (parentDisplay === 'flex' && parentFlex === 'row')) {
- style['float'] = float;
- if (!isFullWidth($zone)) {
- vertical = true;
- style['height'] = Math.max($zone.outerHeight(), 30) + 'px';
- }
- }
- self._insertDropzone($('<we-hook/>').insertAfter($zone), vertical, style);
+ if (!$zone.next('.oe_drop_zone:visible, .oe_drop_clone').length) {
+ data = setDropZoneDirection($zone, $zone.parent());
+ self._insertDropzone($('<we-hook/>').insertAfter($zone), data.vertical, data.style);
}
});
}