diff --git a/.gitignore b/.gitignore index ac77be6..f3e6155 100644 --- a/.gitignore +++ b/.gitignore @@ -21,6 +21,7 @@ extensions/dev_* .vscode/* !.vscode/extensions.json .idea +.junie .DS_Store *.suo *.ntvs* diff --git a/extensions/.gitkeep b/extensions/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/plugi-core/build.js b/plugi-core/build.js index db66585..b78d448 100644 --- a/plugi-core/build.js +++ b/plugi-core/build.js @@ -1,4 +1,9 @@ import { build } from 'vite'; +import path from 'path'; +import { fileURLToPath } from 'url'; +import fs from 'fs'; + +const __dirname = path.dirname(fileURLToPath(import.meta.url)); // libraries const libraries = [ @@ -19,13 +24,76 @@ const libraries = [ }, ]; +// GrapesJS ships a pre-bundled Webpack bundle as its ESM entry (grapes.mjs). +// Vite cannot re-bundle it correctly because the internal __webpack_require__ +// calls break when Vite replaces the module system. Use a custom plugin to +// inject the raw GrapesJS UMD bundle and resolve the import to it. +function grapesjsBundlePlugin() { + const grapesMinPath = path.resolve(__dirname, 'node_modules/grapesjs/dist/grapes.min.js'); + const grapesMinContent = fs.readFileSync(grapesMinPath, 'utf-8'); + + return [ + { + name: 'grapesjs-resolve', + resolveId(id, importer) { + if (id === 'grapesjs') { + return { id: '\0grapesjs-umd', moduleSideEffects: true }; + } + return null; + }, + load(id) { + if (id === '\0grapesjs-umd') { + // Return the UMD bundle as a raw module that sets window.grapesjs + return `window.grapesjs = undefined; +${grapesMinContent} +var _grapesjs = window.grapesjs; +export default _grapesjs;`; + } + return null; + }, + // Prevent Rollup from trying to scan/transform this module + moduleParsed(info) { + if (info.id === '\0grapesjs-umd') { + info.isEntry = false; + } + }, + }, + { + name: 'grapesjs-output', + renderChunk(code) { + // The GrapesJS UMD bundle uses __webpack_modules__ which Rollup might rename. + // We need to ensure the output preserves the Webpack bundle structure. + return null; + }, + }, + ]; +} + + // build libraries.forEach(async (libItem) => { await build({ configFile: false, + plugins: [grapesjsBundlePlugin()], build: { lib: libItem, emptyOutDir: false, + minify: 'terser', + terserOptions: { + mangle: { + // Reserve names that are used as globals in pagebuilder.php to prevent + // the minifier from generating them as mangled variable names + reserved: ['config', 'editor', 'window', 'document', 'console', 'CKEDITOR'], + }, + compress: { + // Preserve __webpack_* variable names used by the GrapesJS Webpack bundle + keep_fargs: false, + }, + format: { + // Preserve __webpack_* variable names from being mangled + keep_quoted_props: true, + }, + }, rollupOptions: { output: { assetFileNames: (assetInfo) => { diff --git a/plugi-core/dist/PageBuilderBody.js b/plugi-core/dist/PageBuilderBody.js index 69a6c6c..e8959ec 100644 --- a/plugi-core/dist/PageBuilderBody.js +++ b/plugi-core/dist/PageBuilderBody.js @@ -2,360 +2,611 @@ $(".gjs-editor").append($("#toggle-sidebar")); $(".gjs-pn-panels").prepend($("#sidebar-header")); $(".gjs-pn-panels").append($("#sidebar-bottom-buttons")); $("#toggle-sidebar").click(function() { - $("#gjs").toggleClass("sidebar-collapsed"), S(); + $("#gjs").toggleClass("sidebar-collapsed"); + triggerEditorResize(); }); -y(); -window.editor.on("run:open-sm", function(t) { - $(".gjs-trt-traits").parent().parent().css("display", "none"), $(".gjs-sm-sectors").parent().parent().css("display", "block"), $("#gjs-sm-advanced .gjs-sm-properties").append($(".gjs-clm-tags")); +autoCollapseSidebar(); +window.editor.on("run:open-sm", function(editor2) { + $(".gjs-trt-traits").parent().parent().css("display", "none"); + $(".gjs-sm-sectors").parent().parent().css("display", "block"); + $("#gjs-sm-advanced .gjs-sm-properties").append($(".gjs-clm-tags")); }); -window.editor.on("run:open-tm", function(t) { - $(".gjs-sm-sectors").parent().parent().css("display", "none"), $(".gjs-trt-traits").parent().parent().css("display", "block"); +window.editor.on("run:open-tm", function(editor2) { + $(".gjs-sm-sectors").parent().parent().css("display", "none"); + $(".gjs-trt-traits").parent().parent().css("display", "block"); }); -window.editor.on("block:drag:start", function(t) { - y(); +window.editor.on("block:drag:start", function(block) { + autoCollapseSidebar(); }); -window.editor.on("rteToolbarPosUpdate", function(t) { - !window.editor || !window.editor.getSelected() || !window.editor.getSelected().getEl() || setTimeout(function() { - let i = $(".gjs-rte-toolbar").first(); - if (i.offset().top < t.elementTop + 0.5 * t.elementHeight) { - let a = t.elementTop - i.height(); - a > 0 && i.css("top", a + "px"); +window.editor.on("rteToolbarPosUpdate", function(toolbarPosition) { + if (!window.editor || !window.editor.getSelected()) + return; + let selectedDomComponent = window.editor.getSelected().getEl(); + if (!selectedDomComponent) + return; + setTimeout(function() { + let $toolbar = $(".gjs-rte-toolbar").first(); + let toolbarInUpperHalf = $toolbar.offset().top < toolbarPosition.elementTop + 0.5 * toolbarPosition.elementHeight; + if (toolbarInUpperHalf) { + let newTop = toolbarPosition.elementTop - $toolbar.height(); + if (newTop > 0) { + $toolbar.css("top", newTop + "px"); + } } }, 0); }); -function y() { - $(window).width() < 1e3 && ($("#gjs").addClass("sidebar-collapsed"), S()); +function autoCollapseSidebar() { + if ($(window).width() < 1e3) { + $("#gjs").addClass("sidebar-collapsed"); + triggerEditorResize(); + } } -function S() { +function triggerEditorResize() { window.editor.trigger("change:canvasOffset canvasScroll"); } -let g = !1; -$(document).keydown(function(t) { - t.which === 8 && (g = !0); -}).keyup(function(t) { - t.which === 8 && (g = !1); +let backspaceIsPressed = false; +$(document).keydown(function(event) { + if (event.which === 8) + backspaceIsPressed = true; +}).keyup(function(event) { + if (event.which === 8) + backspaceIsPressed = false; }); -$(window).on("beforeunload", function(t) { - g && t.preventDefault(); +$(window).on("beforeunload", function(event) { + if (backspaceIsPressed) + event.preventDefault(); }); -function A() { +function addBlockSearch() { $(".gjs-blocks-cs").prepend($("#block-search")); } -window.addEventListener("message", V, !1); -function V(t) { - t.data === "page-loaded" ? ($("#phpb-loading").addClass("loaded"), A(), window.isLoaded = !0, $(window).trigger("pagebuilder-page-loaded")) : t.data === "touch-start" && window.touchStart(); +window.addEventListener("message", onMessage, false); +function onMessage(event) { + if (event.data === "page-loaded") { + $("#phpb-loading").addClass("loaded"); + addBlockSearch(); + window.isLoaded = true; + $(window).trigger("pagebuilder-page-loaded"); + } else if (event.data === "touch-start") { + window.touchStart(); + } } $(document).on("input", "#block-search input", function() { - let t = $(this).val().toLowerCase(); + let term = $(this).val().toLowerCase(); $(".gjs-block-category").each(function() { - let e = !1; + let atLeastOneMatch = false; $(this).find(".gjs-block").each(function() { - $(this).data("original-html") || $(this).data("original-html", $(this).html()); - let i = $(this).text(); - if (i.toLowerCase().includes(t)) { - $(this).removeClass("d-none"), e = !0; - let n = new RegExp("(" + t + ")", "gi"), a = i.replace(n, "$1"); + if (!$(this).data("original-html")) { + $(this).data("original-html", $(this).html()); + } + let label = $(this).text(); + if (label.toLowerCase().includes(term)) { + $(this).removeClass("d-none"); + atLeastOneMatch = true; + let regEx = new RegExp("(" + term + ")", "gi"); + let highlightedText = label.replace(regEx, "$1"); $(this).find(".gjs-block-label").html( - $(this).data("original-html").replace(i.trim(), a) + $(this).data("original-html").replace(label.trim(), highlightedText) ); - } else + } else { $(this).addClass("d-none"); - }), $(this).removeClass("d-none"), e || $(this).addClass("d-none"); + } + }); + $(this).removeClass("d-none"); + if (!atLeastOneMatch) { + $(this).addClass("d-none"); + } }); }); window.customBuilderScripts = {}; -window.editor.on("component:create", (t) => { - if (t.components().length) { - let e = t.components().models[t.components().length - 1]; - if (e.attributes.type === "script") { - let i = t.attributes.attributes["block-id"]; - i === void 0 && (i = t.attributes.attributes.id); - let n = e, a = !1; - for (; n.parent(); ) - if (n = n.parent(), n.attributes.attributes["phpb-content-container"]) { - a = !0; +window.editor.on("component:create", (component) => { + if (component.components().length) { + let lastChild = component.components().models[component.components().length - 1]; + if (lastChild.attributes.type === "script") { + let blockId = component.attributes.attributes["block-id"]; + if (blockId === void 0) { + blockId = component.attributes.attributes["id"]; + } + let rootComponent = lastChild; + let insideEditablePartOfPage = false; + while (rootComponent.parent()) { + rootComponent = rootComponent.parent(); + if (rootComponent.attributes.attributes["phpb-content-container"]) { + insideEditablePartOfPage = true; break; } - a && (window.customBuilderScripts[i] = e.toHTML(), e.remove()); + } + if (insideEditablePartOfPage) { + window.customBuilderScripts[blockId] = lastChild.toHTML(); + lastChild.remove(); + } } } }); -window.editor.on("component:add", function(t) { - if (t.attributes["run-builder-script"] !== void 0) { - let e = customBuilderScripts; - window.customBuilderScripts[t.attributes["block-id"]] = customBuilderScripts[t.attributes["run-builder-script"]], runScriptsOfComponentAndChildren(t), window.customBuilderScripts = e, delete t.attributes["run-builder-script"]; +window.editor.on("component:add", function(component) { + if (component.attributes["run-builder-script"] !== void 0) { + let originalCustomBuilderScripts = customBuilderScripts; + window.customBuilderScripts[component.attributes["block-id"]] = customBuilderScripts[component.attributes["run-builder-script"]]; + runScriptsOfComponentAndChildren(component); + window.customBuilderScripts = originalCustomBuilderScripts; + delete component.attributes["run-builder-script"]; } }); -window.editor.on("sorter:drag:end", function(t) { - let e = t.modelToDrop; - e && e.attributes && (e.attributes["block-id"] || e.attributes.id) && window.runScriptsOfComponentAndChildren(e); - for (let i in CKEDITOR.instances) - CKEDITOR.instances[i].destroy(!0); +window.editor.on("sorter:drag:end", function(event) { + let component = event.modelToDrop; + if (component && component.attributes && (component.attributes["block-id"] || component.attributes["id"])) { + window.runScriptsOfComponentAndChildren(component); + } + for (let instanceName in CKEDITOR.instances) { + CKEDITOR.instances[instanceName].destroy(true); + } }); -window.runScriptsOfComponentAndChildren = function(t) { - x(t), t.components().each(function(e) { - runScriptsOfComponentAndChildren(e); +window.runScriptsOfComponentAndChildren = function(component) { + runComponentScript(component); + component.components().each(function(child) { + runScriptsOfComponentAndChildren(child); }); }; -function x(t) { - let e = t.attributes["block-id"]; - if (e === void 0 && (e = t.attributes.attributes.id), e && window.customBuilderScripts[e] !== void 0) { - let i = t.attributes["style-identifier"], n = $("").append(window.customBuilderScripts[e]); - n.find("script").prepend("let inPageBuilder = true;"), n.find("script").prepend('let blockSelector = ".' + i + '";'), n.find("script").prepend('let block = document.getElementsByClassName("' + i + '")[0];'), n.find("script").prepend("(function(){"), n.find("script").append("})();"); - let a = document.createElement("script"); - a.type = "text/javascript", a.innerHTML = n.find("script").html(), window.editor.Canvas.getDocument().body.appendChild(a); +function runComponentScript(component) { + let blockId = component.attributes["block-id"]; + if (blockId === void 0) { + blockId = component.attributes.attributes["id"]; + } + if (blockId && window.customBuilderScripts[blockId] !== void 0) { + let styleIdentifier = component.attributes["style-identifier"]; + let $scriptTag = $("").append(window.customBuilderScripts[blockId]); + $scriptTag.find("script").prepend("let inPageBuilder = true;"); + $scriptTag.find("script").prepend('let blockSelector = ".' + styleIdentifier + '";'); + $scriptTag.find("script").prepend('let block = document.getElementsByClassName("' + styleIdentifier + '")[0];'); + $scriptTag.find("script").prepend("(function(){"); + $scriptTag.find("script").append("})();"); + let scriptTag = document.createElement("script"); + scriptTag.type = "text/javascript"; + scriptTag.innerHTML = $scriptTag.find("script").html(); + window.editor.Canvas.getDocument().body.appendChild(scriptTag); } } -function H() { - R(); - let t = document.createElement("script"); - t.type = "text/javascript", t.src = window.injectionScriptUrl; - let e = t.outerHTML + " + - + +