@@ -70,25 +70,43 @@ export class ClonePlugin extends Plugin {
70
70
title : _t ( "Duplicate" ) ,
71
71
disabledReason,
72
72
handler : ( ) => {
73
- this . cloneElement ( this . overlayTarget , { scrollToClone : true } ) ;
73
+ this . cloneElement ( this . overlayTarget , { activateClone : false } ) ;
74
74
this . dependencies . history . addStep ( ) ;
75
75
} ,
76
76
} ) ;
77
77
return buttons ;
78
78
}
79
79
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 } = { } ) {
81
94
this . dispatchTo ( "on_will_clone_handlers" , { originalEl : el } ) ;
82
- // TODO cleanUI resource for each option
83
95
const cloneEl = el . cloneNode ( true ) ;
84
- this . cleanElement ( cloneEl ) ;
96
+ this . cleanElement ( cloneEl ) ; // TODO check that
85
97
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.
88
105
if ( scrollToClone && ! isElementInViewport ( cloneEl ) ) {
89
106
cloneEl . scrollIntoView ( { behavior : "smooth" , block : "center" } ) ;
90
107
}
91
- // TODO snippet_cloned ?
108
+
109
+ this . dispatchTo ( "on_cloned_handlers" , { cloneEl : cloneEl , originalEl : el } ) ;
92
110
return cloneEl ;
93
111
}
94
112
0 commit comments