Skip to content

Commit b035a96

Browse files
committed
MAGETWO-91375: Refactor drag & drop code
- Resolve issue with button sorting - Focus out of button after inline editing
1 parent e818c46 commit b035a96

File tree

8 files changed

+114
-59
lines changed

8 files changed

+114
-59
lines changed

app/code/Magento/PageBuilder/Test/Mftf/ActionGroup/ButtonActionGroup.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@
5252
<waitForElementVisible selector="{{ButtonItemOnStage.editableButtonItemElement(index)}}" stepKey="waitForButtonEditable"/>
5353
<pressKey selector="{{ButtonItemOnStage.editableButtonItemElement(index)}}" userInput="{{text.value}}" stepKey="enterButtonText"/>
5454
<waitForElementVisible selector="{{ButtonItemOnStage.buttonItemText(index, text.value)}}" stepKey="waitForButtonText"/>
55+
<click selector="{{PageBuilderActionsSection.searchPanel}}" stepKey="unfocusLiveEdit"/>
56+
<waitForPageLoad stepKey="waitForUnfocus"/>
5557
</actionGroup>
5658
<actionGroup name="moveButton">
5759
<arguments>

app/code/Magento/PageBuilder/view/adminhtml/web/js/binding/sortable-children.js

Lines changed: 25 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/code/Magento/PageBuilder/view/adminhtml/web/js/content-type/buttons/preview.js

Lines changed: 15 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/code/Magento/PageBuilder/view/adminhtml/web/js/content-type/tabs/preview.js

Lines changed: 18 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/code/Magento/PageBuilder/view/adminhtml/web/template/content-type/buttons/inline/preview.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
<div class="pagebuilder-content-type pagebuilder-entity pagebuilder-entity-preview pagebuilder-no-blur pagebuilder-live-edit pagebuilder-buttons" event="{ mouseover: onMouseOver, mouseout: onMouseOut }, mouseoverBubble: false">
99
<render args="getOptions().template" />
10-
<div class="element-children" each="parent.getChildren()" attr="Object.assign({}, data.main.attributes(), { id: parent.id + '-children' })" ko-style="data.main.style" css="data.main.css" data-bind="sortable: { sortableClass: config.name + '-container', handle: '.move-structural', items: '.pagebuilder-content-type-wrapper', connectWith: '.element-children' }">
10+
<div class="element-children buttons-container" each="parent.getChildren()" attr="Object.assign({}, data.main.attributes(), { id: parent.id + '-children' })" ko-style="data.main.style" css="data.main.css" data-bind="sortableChildren: { tolerance: 'pointer', handle: '.move-structural', items: '.pagebuilder-content-type-wrapper', connectWith: '.buttons-container', forceRefresh: true }">
1111
<div class="pagebuilder-content-type-wrapper" template="{ name: preview.previewTemplate, data: preview }" attr="{ id: id }"></div>
1212
</div>
1313
</div>

app/code/Magento/PageBuilder/view/adminhtml/web/ts/js/binding/sortable-children.ts

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@ import $ from "jquery";
77
import ko from "knockout";
88
import events from "uiEvents";
99
import ContentTypeInterface from "../content-type.d";
10+
import {moveContentType} from "../drag-drop/move-content-type";
1011
import {moveArrayItem} from "../utils/array";
1112

13+
let draggedContentType: ContentTypeInterface;
14+
1215
// Create a new sortable Knockout binding
1316
ko.bindingHandlers.sortableChildren = {
1417

@@ -28,23 +31,31 @@ ko.bindingHandlers.sortableChildren = {
2831
$(element).sortable(options)
2932
.on("sortstart", (event: Event, ui: JQueryUI.SortableUIParams) => {
3033
originalPosition = ui.item.index();
34+
draggedContentType = instance.children()[originalPosition];
3135
events.trigger("sortableChildren:sortstart", {
3236
instance,
3337
originalPosition,
3438
ui,
3539
});
3640
})
37-
.on("sortupdate", (event: Event, ui: JQueryUI.SortableUIParams) => {
38-
const index = ui.item.index();
39-
if (originalPosition !== index) {
40-
ui.item.remove();
41-
moveArrayItem(instance.children, originalPosition, index);
42-
events.trigger("sortableChildren:sortupdate", {
43-
instance,
44-
newPosition: index,
45-
originalPosition,
46-
ui,
47-
});
41+
.on("sortupdate", function(event: Event, ui: JQueryUI.SortableUIParams) {
42+
if (this === ui.item.parent()[0]) {
43+
const index = ui.item.index();
44+
const targetParent = ko.dataFor(ui.item.parent()[0]).parent;
45+
if (targetParent && (originalPosition !== index || draggedContentType.parent !== targetParent)) {
46+
ui.item.remove();
47+
if (draggedContentType.parent === targetParent) {
48+
moveArrayItem(instance.children, originalPosition, index);
49+
} else {
50+
moveContentType(draggedContentType, index, targetParent);
51+
}
52+
events.trigger("sortableChildren:sortupdate", {
53+
instance,
54+
newPosition: index,
55+
originalPosition,
56+
ui,
57+
});
58+
}
4859
}
4960
});
5061
},

app/code/Magento/PageBuilder/view/adminhtml/web/ts/js/content-type/buttons/preview.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
* See COPYING.txt for license details.
44
*/
55

6+
import $ from "jquery";
67
import ko from "knockout";
78
import $t from "mage/translate";
89
import events from "uiEvents";
910
import Config from "../../config";
1011
import createContentType from "../../content-type-factory";
1112
import Option from "../../content-type-menu/option";
1213
import OptionInterface from "../../content-type-menu/option.d";
13-
import BlockMountEventParamsInterface from "../block-mount-event-params.d";
1414
import ButtonItem from "../button-item/preview";
1515
import ContentTypeMountEventParamsInterface from "../content-type-mount-event-params.d";
1616
import PreviewCollection from "../preview-collection";
@@ -28,6 +28,19 @@ export default class Preview extends PreviewCollection {
2828
});
2929
}
3030

31+
/**
32+
* Set state based on mouseover event for the preview
33+
*
34+
* @param {Preview} context
35+
* @param {Event} event
36+
*/
37+
public onMouseOver(context: Preview, event: Event) {
38+
// Only run the mouse over action when the active element is not a child of buttons
39+
if (!$.contains(this.wrapperElement, document.activeElement)) {
40+
return super.onMouseOver(context, event);
41+
}
42+
}
43+
3144
/**
3245
* Return an array of options
3346
*

app/code/Magento/PageBuilder/view/adminhtml/web/ts/js/content-type/tabs/preview.ts

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -87,22 +87,24 @@ export default class Preview extends PreviewCollection {
8787
});
8888
// Refresh tab contents and set the focus to the new position of the sorted tab
8989
events.on("sortableChildren:sortupdate", (args: PreviewSortableSortUpdateEventParams) => {
90-
this.refreshTabs(args.newPosition, true);
91-
/**
92-
* Update the default active tab if its position was affected by the sorting
93-
*/
94-
const defaultActiveTab = +args.instance.preview.previewData.default_active();
95-
let newDefaultActiveTab = defaultActiveTab;
96-
if (args.originalPosition === defaultActiveTab) {
97-
newDefaultActiveTab = args.newPosition;
98-
} else if (args.originalPosition < defaultActiveTab && args.newPosition >= defaultActiveTab) {
99-
// a tab was moved from the left of the default active tab the right of it, changing its index
100-
newDefaultActiveTab--;
101-
} else if (args.originalPosition > defaultActiveTab && args.newPosition <= defaultActiveTab) {
102-
// a tab was moved from the right of the default active tab the left of it, changing its index
103-
newDefaultActiveTab++;
90+
if (args.instance.id === this.parent.id) {
91+
this.refreshTabs(args.newPosition, true);
92+
/**
93+
* Update the default active tab if its position was affected by the sorting
94+
*/
95+
const defaultActiveTab = +args.instance.preview.previewData.default_active();
96+
let newDefaultActiveTab = defaultActiveTab;
97+
if (args.originalPosition === defaultActiveTab) {
98+
newDefaultActiveTab = args.newPosition;
99+
} else if (args.originalPosition < defaultActiveTab && args.newPosition >= defaultActiveTab) {
100+
// a tab was moved from the left of the default active tab the right of it, changing its index
101+
newDefaultActiveTab--;
102+
} else if (args.originalPosition > defaultActiveTab && args.newPosition <= defaultActiveTab) {
103+
// a tab was moved from the right of the default active tab the left of it, changing its index
104+
newDefaultActiveTab++;
105+
}
106+
this.updateData("default_active", newDefaultActiveTab);
104107
}
105-
this.updateData("default_active", newDefaultActiveTab);
106108
});
107109
}
108110

0 commit comments

Comments
 (0)