Skip to content

Commit 03ddca5

Browse files
committed
Merge remote-tracking branch 'origin/MC-4217-slider-hide-show-performance' into cms-team-1-delivery
2 parents bf40803 + 7eb6e15 commit 03ddca5

File tree

4 files changed

+77
-12
lines changed

4 files changed

+77
-12
lines changed

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

Lines changed: 2 additions & 2 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/slider/preview.js

Lines changed: 32 additions & 4 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/ts/js/content-type/slide/preview.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ export default class Preview extends BasePreview {
159159
element.focus();
160160
};
161161

162-
if (!this.wysiwyg) {
162+
if (this.element && !this.wysiwyg) {
163163
const selection = this.saveSelection();
164164
this.element.removeAttribute("contenteditable");
165165
_.defer(() => {
@@ -375,7 +375,7 @@ export default class Preview extends BasePreview {
375375
* @param {Selection} selection
376376
*/
377377
private restoreSelection(element: HTMLElement, selection: Selection) {
378-
if (window.getSelection) {
378+
if (selection && window.getSelection) {
379379
// Find the original container that had the selection
380380
const startContainerParent = $(element).find("[data-startContainer]");
381381
startContainerParent.removeAttr("data-startContainer");

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

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,24 @@ export default class Preview extends PreviewCollection {
4848
private afterChildrenRenderDeferred: DeferredInterface = deferred();
4949
private buildSlickDebounce = _.debounce(this.buildSlick.bind(this), 10);
5050

51+
/**
52+
* Define keys which when changed should not trigger the slider to be rebuilt
53+
*
54+
* @type {string[]}
55+
*/
56+
private ignoredKeysForBuild: string[] = [
57+
"display",
58+
"margins_and_padding",
59+
"border",
60+
"border_color",
61+
"border_radius",
62+
"border_width",
63+
"css_classes",
64+
"name",
65+
"text_align",
66+
];
67+
private previousData: DataObject;
68+
5169
/**
5270
* @param {ContentTypeCollectionInterface} parent
5371
* @param {ContentTypeConfigInterface} config
@@ -73,16 +91,22 @@ export default class Preview extends PreviewCollection {
7391
this.element = element as HTMLElement;
7492

7593
this.childSubscribe = this.parent.children.subscribe(this.buildSlickDebounce);
76-
this.parent.dataStore.subscribe(this.buildSlickDebounce);
94+
this.previousData = this.parent.dataStore.get() as DataObject;
95+
this.parent.dataStore.subscribe((data: DataObject) => {
96+
if (this.hasDataChanged(this.previousData, data)) {
97+
this.buildSlickDebounce();
98+
}
99+
this.previousData = data;
100+
});
77101

78102
this.buildSlick();
79103

80104
// Redraw slide after content type gets redrawn
81-
events.on("contentType:redrawAfter", function(args: ContentTypeAfterRenderEventParamsInterface) {
82-
if ($.contains(args.element, this.element)) {
105+
events.on("contentType:redrawAfter", (args: ContentTypeAfterRenderEventParamsInterface) => {
106+
if (args.element && this.element && $.contains(args.element, this.element)) {
83107
$(this.element).slick("setPosition");
84108
}
85-
}.bind(this));
109+
});
86110

87111
// Set the stage to interacting when a slide is focused
88112
this.focusedSlide.subscribe((value: number) => {
@@ -353,6 +377,19 @@ export default class Preview extends PreviewCollection {
353377
});
354378
}
355379

380+
/**
381+
* Determine if the data has changed, whilst ignoring certain keys which don't require a rebuild
382+
*
383+
* @param {DataObject} previousData
384+
* @param {DataObject} newData
385+
* @returns {boolean}
386+
*/
387+
private hasDataChanged(previousData: DataObject, newData: DataObject) {
388+
previousData = _.omit(previousData, this.ignoredKeysForBuild);
389+
newData = _.omit(newData, this.ignoredKeysForBuild);
390+
return !_.isEqual(previousData, newData);
391+
}
392+
356393
/**
357394
* Build our instance of slick
358395
*/

0 commit comments

Comments
 (0)