Skip to content

Commit a5bc0c7

Browse files
committed
Merge branch 'PB-77' of github.com:magento-obsessive-owls/magento2-page-builder into PB-77
2 parents c739378 + fb189c6 commit a5bc0c7

File tree

6 files changed

+133
-117
lines changed

6 files changed

+133
-117
lines changed

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

Lines changed: 0 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/js/content-type/products/preview.js

Lines changed: 59 additions & 47 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/products/grid/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-products" data-bind="event: { mouseover: onMouseOver, mouseout: onMouseOut }, mouseoverBubble: false">
99
<div attr="data.main.attributes" ko-style="data.main.style" css="data.main.css">
10-
<div if="displayPreview" class="rendered-content" html="data.main.html" afterRender="onAfterRender"></div>
10+
<div if="displayPreview" class="rendered-content" html="widgetHtml" afterRender="onAfterRender"></div>
1111
<div ifnot="displayPreview" class="pagebuilder-products-placeholder">
1212
<span class="placeholder-text" text="placeholderText"/>
1313
</div>

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,6 @@ export default class Preview implements PreviewInterface {
569569
_.extend({}, this.contentType.dataStore.getState()),
570570
);
571571
this.afterObservablesUpdated();
572-
events.trigger("previewData:updateAfter", {preview: this});
573572
}
574573

575574
/**

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

Lines changed: 73 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ import $ from "jquery";
77
import ko from "knockout";
88
import $t from "mage/translate";
99
import "slick";
10+
import _ from "underscore";
1011
import Config from "../../config";
1112
import ContentTypeInterface from "../../content-type";
1213
import ContentTypeConfigInterface from "../../content-type-config.types";
1314
import HideShowOption from "../../content-type-menu/hide-show-option";
1415
import {OptionsInterface} from "../../content-type-menu/option.types";
16+
import {DataObject} from "../../data-store";
1517
import ObservableUpdater from "../observable-updater";
1618
import BasePreview from "../preview";
1719

@@ -21,15 +23,31 @@ import BasePreview from "../preview";
2123
export default class Preview extends BasePreview {
2224
public displayPreview: KnockoutObservable<boolean> = ko.observable(false);
2325
public placeholderText: KnockoutObservable<string>;
26+
public widgetHtml: KnockoutObservable<string> = ko.observable();
2427
private element: Element;
25-
private sliderReady: boolean = false;
2628
private messages = {
2729
EMPTY: $t("Empty Products"),
2830
NO_RESULTS: $t("No products were found matching your condition"),
2931
LOADING: $t("Loading..."),
3032
UNKNOWN_ERROR: $t("An unknown error occurred. Please try again."),
3133
};
3234

35+
/**
36+
* Define keys which when changed should not trigger the slider to be rebuilt
37+
*
38+
* @type {string[]}
39+
*/
40+
private ignoredKeysForBuild: string[] = [
41+
"margins_and_padding",
42+
"border",
43+
"border_color",
44+
"border_radius",
45+
"border_width",
46+
"css_classes",
47+
"text_align",
48+
];
49+
private previousData: DataObject;
50+
3351
/**
3452
* @inheritdoc
3553
*/
@@ -72,67 +90,59 @@ export default class Preview extends BasePreview {
7290
this.initSlider();
7391
}
7492

75-
/**
76-
* @inheritDoc
77-
*/
78-
public onOptionVisibilityToggle(): void {
79-
this.element = undefined;
80-
this.sliderReady = false;
81-
super.onOptionVisibilityToggle();
82-
}
83-
8493
/**
8594
* @inheritdoc
8695
*/
8796
protected afterObservablesUpdated(): void {
8897
super.afterObservablesUpdated();
89-
this.displayPreview(false);
90-
9198
const data = this.contentType.dataStore.getState();
9299

93-
if ((typeof data.conditions_encoded !== "string") || data.conditions_encoded.length === 0) {
94-
this.placeholderText(this.messages.EMPTY);
95-
96-
return;
100+
if (this.hasDataChanged(this.previousData, data)) {
101+
this.displayPreview(false);
102+
103+
if ((typeof data.conditions_encoded !== "string") || data.conditions_encoded.length === 0) {
104+
this.placeholderText(this.messages.EMPTY);
105+
106+
return;
107+
}
108+
109+
const url = Config.getConfig("preview_url");
110+
const requestConfig = {
111+
// Prevent caching
112+
method: "POST",
113+
data: {
114+
role: this.config.name,
115+
directive: this.data.main.html(),
116+
},
117+
};
118+
119+
this.placeholderText(this.messages.LOADING);
120+
121+
$.ajax(url, requestConfig)
122+
.done((response) => {
123+
if (typeof response.data !== "object" || !Boolean(response.data.content)) {
124+
this.placeholderText(this.messages.NO_RESULTS);
125+
126+
return;
127+
}
128+
129+
if (response.data.error) {
130+
this.widgetHtml(response.data.error);
131+
} else {
132+
this.widgetHtml(response.data.content);
133+
this.displayPreview(true);
134+
}
135+
})
136+
.fail(() => {
137+
this.placeholderText(this.messages.UNKNOWN_ERROR);
138+
});
97139
}
98-
99-
const url = Config.getConfig("preview_url");
100-
const requestConfig = {
101-
// Prevent caching
102-
method: "POST",
103-
data: {
104-
role: this.config.name,
105-
directive: this.data.main.html(),
106-
},
107-
};
108-
109-
this.placeholderText(this.messages.LOADING);
110-
111-
$.ajax(url, requestConfig)
112-
.done((response) => {
113-
if (typeof response.data !== "object" || !Boolean(response.data.content)) {
114-
this.placeholderText(this.messages.NO_RESULTS);
115-
116-
return;
117-
}
118-
119-
if (response.data.error) {
120-
this.data.main.html(response.data.error);
121-
} else {
122-
this.data.main.html(response.data.content);
123-
this.initSlider();
124-
this.displayPreview(true);
125-
}
126-
})
127-
.fail(() => {
128-
this.placeholderText(this.messages.UNKNOWN_ERROR);
129-
});
140+
this.previousData = Object.assign({}, data);
130141
}
131142

132143
protected initSlider(): void {
133-
if (!this.sliderReady && this.element && this.appearance() === "carousel") {
144+
if (this.element && this.appearance() === "carousel") {
134145
$(this.element.children).slick(this.buildSlickConfig());
135-
this.sliderReady = true;
136146
}
137147
}
138148

@@ -156,4 +166,17 @@ export default class Preview extends BasePreview {
156166
autoplaySpeed: parseFloat(attributes["data-autoplay-speed"]),
157167
};
158168
}
169+
170+
/**
171+
* Determine if the data has changed, whilst ignoring certain keys which don't require a rebuild
172+
*
173+
* @param {DataObject} previousData
174+
* @param {DataObject} newData
175+
* @returns {boolean}
176+
*/
177+
private hasDataChanged(previousData: DataObject, newData: DataObject) {
178+
previousData = _.omit(previousData, this.ignoredKeysForBuild);
179+
newData = _.omit(newData, this.ignoredKeysForBuild);
180+
return !_.isEqual(previousData, newData);
181+
}
159182
}

docs/reference/events.md

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -385,20 +385,6 @@ events.on(`stage:${this.contentType.stageId}:fullScreenModeChangeAfter`, functio
385385
[Back to top]
386386

387387

388-
### `previewData:updateAfter`
389-
390-
```js
391-
events.on("previewData:updateAfter", function (params) {});
392-
```
393-
394-
| Params | Type |
395-
| --------- | --------- |
396-
| `preview` | `Preview` |
397-
{:style="table-layout:auto"}
398-
399-
[Back to top]
400-
401-
402388
### `childContentType:sortStart`
403389

404390
```js

0 commit comments

Comments
 (0)