Skip to content

Commit 206cb78

Browse files
committed
MC-3782: New content Type Declaration
1 parent 59b1ad7 commit 206cb78

File tree

2 files changed

+86
-25
lines changed
  • app/code/Magento/PageBuilder/view/adminhtml/web

2 files changed

+86
-25
lines changed

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

Lines changed: 44 additions & 12 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/block/preview.ts

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ export default class Preview extends BasePreview {
2323
public displayingBlockPreview: KnockoutObservable<boolean> = ko.observable(false);
2424
public loading: KnockoutObservable<boolean> = ko.observable(false);
2525
public placeholderText: KnockoutObservable<string>;
26-
private lastBlockId: number;
27-
private lastTemplate: string;
28-
private lastRenderedHtml: string;
29-
private messages = {
26+
protected messages = {
3027
NOT_SELECTED: $t("Empty Block"),
3128
UNKNOWN_ERROR: $t("An unknown error occurred. Please try again."),
3229
};
30+
private lastBlockId: number;
31+
private lastTemplate: string;
32+
private lastRenderedHtml: string;
3333

3434
/**
3535
* @inheritdoc
@@ -52,6 +52,16 @@ export default class Preview extends BasePreview {
5252
});
5353
}
5454

55+
/**
56+
*
57+
* @param {DataObject} data
58+
*/
59+
public processBlockData(data: DataObject): void {
60+
// Only load if something changed
61+
this.displayPreviewPlaceholder(data, "banner_ids");
62+
this.processRequest(data, "banner_ids", "name");
63+
}
64+
5565
/**
5666
* @inheritdoc
5767
*/
@@ -76,7 +86,19 @@ export default class Preview extends BasePreview {
7686
const data = this.parent.dataStore.get() as DataObject;
7787

7888
// Only load if something changed
79-
if (this.lastBlockId === data.block_id && this.lastTemplate === data.template) {
89+
this.processBlockData(data);
90+
91+
}
92+
93+
/**
94+
* Displsay preview placeholder
95+
*
96+
* @param {DataObject} data
97+
* @param {string} identifierName
98+
*/
99+
protected displayPreviewPlaceholder(data: DataObject, identifierName: string): void {
100+
// Only load if something changed
101+
if (this.lastBlockId === data[identifierName] && this.lastTemplate === data.template) {
80102
// The mass converter will have transformed the HTML property into a directive
81103
if (this.lastRenderedHtml) {
82104
this.data.main.html(this.lastRenderedHtml);
@@ -88,28 +110,35 @@ export default class Preview extends BasePreview {
88110
this.placeholderText("");
89111
}
90112

91-
if (!data.block_id || data.template.length === 0) {
113+
if (!data[identifierName] || data.template.length === 0) {
92114
this.showBlockPreview(false);
93115
this.placeholderText(this.messages.NOT_SELECTED);
94116
return;
95117
}
118+
}
96119

97-
this.loading(true);
98-
120+
/**
121+
*
122+
* @param {DataObject} data
123+
* @param {string} identifierName
124+
* @param {string} labelKey
125+
*/
126+
protected processRequest(data: DataObject, identifierName: string, labelKey: string): void {
99127
const url = Config.getConfig("preview_url");
128+
const identifier = data[identifierName];
100129
const requestConfig = {
101130
// Prevent caching
102131
method: "POST",
103132
data: {
104133
role: this.config.name,
105-
block_id: data.block_id,
134+
block_id: identifier,
106135
directive: this.data.main.html(),
107136
},
108137
};
109-
138+
this.loading(true);
110139
// Retrieve a state object representing the block from the preview controller and process it on the stage
111140
$.ajax(url, requestConfig)
112-
// The state object will contain the block name and either html or a message why there isn't any.
141+
// The state object will contain the block name and either html or a message why there isn't any.
113142
.done((response) => {
114143
// Empty content means something bad happened in the controller that didn't trigger a 5xx
115144
if (typeof response.data !== "object") {
@@ -120,7 +149,7 @@ export default class Preview extends BasePreview {
120149
}
121150

122151
// Update the stage content type label with the real block title if provided
123-
this.displayLabel(response.data.title ? response.data.title : this.config.label);
152+
this.displayLabel(response.data[labelKey] ? response.data[labelKey] : this.config.label);
124153

125154
if (response.data.content) {
126155
this.showBlockPreview(true);
@@ -131,7 +160,7 @@ export default class Preview extends BasePreview {
131160
this.placeholderText(response.data.error);
132161
}
133162

134-
this.lastBlockId = parseInt(data.block_id.toString(), 10);
163+
this.lastBlockId = parseInt(identifier.toString(), 10);
135164
this.lastTemplate = data.template.toString();
136165
this.lastRenderedHtml = response.data.content;
137166
})

0 commit comments

Comments
 (0)