Skip to content

Commit 6269cda

Browse files
committed
Scroll to the clone and activate its option only if needed
1 parent fef89bc commit 6269cda

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

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

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,25 +70,43 @@ export class ClonePlugin extends Plugin {
7070
title: _t("Duplicate"),
7171
disabledReason,
7272
handler: () => {
73-
this.cloneElement(this.overlayTarget, { scrollToClone: true });
73+
this.cloneElement(this.overlayTarget, { activateClone: false });
7474
this.dependencies.history.addStep();
7575
},
7676
});
7777
return buttons;
7878
}
7979

80-
cloneElement(el, { position = "afterend", scrollToClone = false } = {}) {
80+
/**
81+
* Duplicates the given element and returns the created clone.
82+
*
83+
* @param {HTMLElement} el the element to clone
84+
* @param {Object}
85+
* - `position`: specifies where to position the clone (first parameter of
86+
* the `insertAdjacentElement` function)
87+
* - `scrollToClone`: true if the we should scroll to the clone (if not in
88+
* the viewport), false otherwise
89+
* - `activateClone`: true if the option containers of the clone should be
90+
* the active ones, false otherwise
91+
* @returns {HTMLElement}
92+
*/
93+
cloneElement(el, { position = "afterend", scrollToClone = false, activateClone = true } = {}) {
8194
this.dispatchTo("on_will_clone_handlers", { originalEl: el });
82-
// TODO cleanUI resource for each option
8395
const cloneEl = el.cloneNode(true);
84-
this.cleanElement(cloneEl);
96+
this.cleanElement(cloneEl); // TODO check that
8597
el.insertAdjacentElement(position, cloneEl);
86-
this.dependencies["builder-options"].updateContainers(cloneEl);
87-
this.dispatchTo("on_cloned_handlers", { cloneEl: cloneEl, originalEl: el });
98+
99+
// Update the containers if required.
100+
if (activateClone) {
101+
this.dependencies["builder-options"].updateContainers(cloneEl);
102+
}
103+
104+
// Scroll to the clone if required and if it is not visible.
88105
if (scrollToClone && !isElementInViewport(cloneEl)) {
89106
cloneEl.scrollIntoView({ behavior: "smooth", block: "center" });
90107
}
91-
// TODO snippet_cloned ?
108+
109+
this.dispatchTo("on_cloned_handlers", { cloneEl: cloneEl, originalEl: el });
92110
return cloneEl;
93111
}
94112

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ export class OptionsContainer extends BaseOptionComponent {
124124
cloneElement() {
125125
this.callOperation(() => {
126126
this.env.editor.shared.clone.cloneElement(this.props.editingElement, {
127-
scrollToClone: true,
127+
activateClone: false,
128128
});
129129
});
130130
}

0 commit comments

Comments
 (0)