Skip to content

Commit 8e6e412

Browse files
committed
MC-18601: Page Builder Render
1 parent 8e233e2 commit 8e6e412

File tree

9 files changed

+35
-110
lines changed

9 files changed

+35
-110
lines changed

app/code/Magento/PageBuilder/Controller/Adminhtml/Stage/Render.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
*/
1818
class Render extends \Magento\Backend\App\Action implements HttpGetActionInterface
1919
{
20+
const ADMIN_RESOURCE = 'Magento_Backend::content';
21+
2022
/**
2123
* Preview transactional email action
2224
*

app/code/Magento/PageBuilder/etc/module.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
<module name="Magento_Theme"/>
1717
<module name="Magento_Search"/>
1818
<module name="Magento_CatalogWidget"/>
19+
<module name="Magento_RequireJs"/>
1920
</sequence>
2021
</module>
2122
</config>

app/code/Magento/PageBuilder/view/adminhtml/templates/stage/render.phtml

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@
66

77
/** @var \Magento\PageBuilder\Block\Adminhtml\Stage\Render $block */
88
?>
9-
<script type="text/javascript">
9+
<script>
1010
<?php
11-
// Shim localStorage without detection as it will not work as the iframe is sandboxed.
11+
/**
12+
* Shim localStorage always within the iframe to ensure any calls from Magento API's do not fail due to same origin
13+
* policies.
14+
*/
1215
?>
1316
Object.defineProperty(
1417
window,
@@ -35,11 +38,14 @@
3538
}
3639
);
3740
</script>
38-
<script type="text/javascript" src="<?= $block->escapeUrl($block->getRequireJsUrl()); ?>"></script>
39-
<script type="text/javascript" src="<?= $block->escapeUrl($block->getRequireJsConfigUrl()); ?>"></script>
40-
<script type="text/javascript">
41+
<script src="<?= $block->escapeUrl($block->getRequireJsUrl()); ?>"></script>
42+
<script src="<?= $block->escapeUrl($block->getRequireJsConfigUrl()); ?>"></script>
43+
<script>
4144
<?php
42-
// Override the text! RequireJS plugin to allow us to load templates from the parent window
45+
/**
46+
* Override the text! plugin within the iframe to ensure we can pipe any XHR requests through to the parent window
47+
* as the same origin policy will not allow us to load the templates within this iframe.
48+
*/
4349
?>
4450
require.config({
4551
'map': {
@@ -66,9 +72,9 @@
6672
ko.setTemplateEngine(templateEngine);
6773
});
6874
</script>
69-
<script type="text/javascript">
75+
<script>
7076
require(['Magento_PageBuilder/js/render/frame'], function (listen) {
71-
listen(<?= $this->getPageBuilderConfig(); ?>);
77+
listen(<?= /* @noEscape */ $block->getPageBuilderConfig(); ?>);
7278
});
7379
</script>
74-
Page Builder Render Frame
80+
<div>Page Builder Render Frame</div>

app/code/Magento/PageBuilder/view/adminhtml/web/js/form/form-mixin.js

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

6-
define(['jquery', 'Magento_PageBuilder/js/events', 'consoleLogger', 'Magento_PageBuilder/js/stage'], function ($, events, consoleLogger, stage) {
6+
define(['jquery', 'Magento_PageBuilder/js/events', 'consoleLogger'], function ($, events, consoleLogger) {
77
'use strict';
88

99
var mixin = {
@@ -50,14 +50,12 @@ define(['jquery', 'Magento_PageBuilder/js/events', 'consoleLogger', 'Magento_Pag
5050
if (locks.length === 0) {
5151
submit();
5252
} else {
53-
stage.debugLog('Lock detected, waiting for resolution');
5453
timeout = setTimeout(function () {
5554
consoleLogger.error('Page Builder was rendering for 5 seconds without releasing locks.');
5655
}, 5000);
5756

5857
$('body').trigger('processStart');
5958
$.when.apply(null, locks).then(function () {
60-
stage.debugLog('Resolved lock, saving');
6159
$('body').trigger('processStop');
6260
clearTimeout(timeout);
6361
submit();

app/code/Magento/PageBuilder/view/adminhtml/web/js/master-format/render.js

Lines changed: 8 additions & 40 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/stage.js

Lines changed: 1 addition & 15 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/page-builder.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,5 @@
2121
<render/>
2222
</with>
2323
<iframe width="1" height="1" attr="id: 'render_frame_' + id" sandbox="allow-scripts" style="display: none;"></iframe>
24-
<textarea name="debug" rows="5" cols="5"></textarea>
2524
</if>
2625
</div>

app/code/Magento/PageBuilder/view/adminhtml/web/ts/js/master-format/render.ts

Lines changed: 7 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,10 @@
44
*/
55

66
import $ from "jquery";
7-
import _ from "underscore";
87
import Config from "../config";
98
import ContentTypeCollectionInterface from "../content-type-collection.types";
109
import { getSerializedTree } from "./render/serialize";
1110

12-
function debugLog(message: any) {
13-
if (_.isObject(message) || _.isArray(message)) {
14-
message = JSON.stringify(message);
15-
}
16-
$("[name=debug]").append(message + "\n");
17-
}
18-
1911
export default class MasterFormatRenderer {
2012
public stageId: string;
2113
public channel: MessageChannel;
@@ -37,45 +29,33 @@ export default class MasterFormatRenderer {
3729
*/
3830
public applyBindings(rootContainer: ContentTypeCollectionInterface): Promise<string> {
3931
if (!this.getRenderFrame()) {
40-
debugLog("No render frame");
4132
console.error("No render frame present for Page Builder instance.");
4233
return;
4334
}
4435

4536
return new Promise((resolve, reject) => {
4637
if (this.ready) {
47-
debugLog("Frame is ready, posting data");
48-
try {
49-
this.channel.port1.postMessage({
50-
type: "render",
51-
message: {
52-
stageId: this.stageId,
53-
tree: getSerializedTree(rootContainer),
54-
},
55-
});
56-
} catch (e) {
57-
debugLog("Error in postMessage");
58-
debugLog(e);
59-
}
38+
this.channel.port1.postMessage({
39+
type: "render",
40+
message: {
41+
stageId: this.stageId,
42+
tree: getSerializedTree(rootContainer),
43+
},
44+
});
6045
this.channel.port1.onmessage = (event) => {
6146
if (event.isTrusted) {
6247
if (event.data.type === "render") {
63-
console.log(event.data.message);
64-
debugLog("Render complete, resolving event");
6548
resolve(event.data.message);
6649
}
6750
if (event.data.type === "template") {
68-
debugLog("Requested template " + event.data.message);
6951
this.loadTemplate(event.data.message);
7052
}
7153
} else {
7254
reject();
7355
}
7456
};
7557
} else {
76-
debugLog("Frame not ready, waiting for ready...");
7758
this.readyDeferred.then(() => {
78-
debugLog("Frame became ready");
7959
this.applyBindings(rootContainer).then((rendered: string) => {
8060
resolve(rendered);
8161
}).catch(() => {
@@ -90,17 +70,13 @@ export default class MasterFormatRenderer {
9070
* Create a channel to communicate with our sandboxed iframe
9171
*/
9272
public setupChannel() {
93-
debugLog("Setting up channel");
9473
this.channel = new MessageChannel();
9574
const frame = this.getRenderFrame();
9675
window.addEventListener("message", (event) => {
97-
debugLog("onLoad message called");
98-
debugLog(event);
9976
if (event.data === "PB_RENDER_READY") {
10077
frame.contentWindow.postMessage("PB_RENDER_PORT", "*", [this.channel.port2]);
10178
this.ready = true;
10279
this.readyDeferred.resolve();
103-
debugLog("channel is ready");
10480
}
10581
});
10682
frame.src = Config.getConfig("render_url");
@@ -113,7 +89,6 @@ export default class MasterFormatRenderer {
11389
*/
11490
private loadTemplate(name: string): void {
11591
require(["text!" + name], (template: string) => {
116-
debugLog("Loaded template " + name);
11792
this.channel.port1.postMessage({
11893
type: "template",
11994
message: {

app/code/Magento/PageBuilder/view/adminhtml/web/ts/js/stage.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,6 @@ import buildStage from "./stage-builder";
2020
import {StageUpdateAfterParamsInterface} from "./stage-events.types";
2121
import deferred, {DeferredInterface} from "./utils/promise-deferred";
2222

23-
export function debugLog(message: any) {
24-
if (_.isObject(message) || _.isArray(message)) {
25-
message = JSON.stringify(message);
26-
}
27-
$("[name=debug]").append(message + "\n");
28-
}
29-
3023
export default class Stage {
3124
public static readonly rootContainerName: string = "root-container";
3225
public pageBuilder: PageBuilderInterface;
@@ -54,14 +47,11 @@ export default class Stage {
5447
* @type {(() => void) & _.Cancelable}
5548
*/
5649
private applyBindingsDebounce = _.debounce(() => {
57-
debugLog("------------------------------------------");
58-
debugLog("Lock created");
5950
this.renderingLock = $.Deferred();
6051
this.render.applyBindings(this.rootContainer)
6152
.then((renderedOutput: string) => events.trigger(`stage:${ this.id }:masterFormatRenderAfter`, {
6253
value: renderedOutput,
6354
})).then(() => {
64-
debugLog("Lock resolved");
6555
this.renderingLock.resolve();
6656
}).catch((error: Error) => {
6757
console.error(error);

0 commit comments

Comments
 (0)