origin commit a3f022d

This commit is contained in:
Paul Spenke
2023-07-02 21:38:11 +08:00
parent 5ef177ea39
commit d7610386e3
5 changed files with 121 additions and 73 deletions
@@ -356,6 +356,21 @@ class PageBuilder implements PageBuilderContract
return [];
}
/**
* Return this page's css in the format passed to GrapesJS.
*
* @param PageContract $page
* @return string
*/
public function getPageStyleCss(PageContract $page)
{
$data = $page->getBuilderData();
if (isset($data['css'])) {
return $data['css'];
}
return '';
}
/**
* Get or set custom css for customizing layout of the page builder.
*
@@ -150,6 +150,7 @@
function saveCurrentTranslationLocally(callback) {
// use timeout to ensure the waiting spinner is fully displayed before the page briefly freezes due to high JS workload
setTimeout(function() {
let existingCss = window.pageData['css'] ? window.pageData['css'] : window.initialCss;
window.pageData = {
html: [],
components: [],
@@ -162,7 +163,7 @@
window.editor.getWrapper().find("[phpb-content-container]").forEach((container, index) => {
let data = getContainerContentInStorageFormat(container);
window.pageData['css'] = data.css;
window.pageData['css'] = mergeCss(existingCss, data.css);
window.pageData['style'] = data.style;
window.pageData['html'][index] = data.html;
window.pageData['components'][index] = data.components;
@@ -177,6 +178,45 @@
}, 200);
}
/**
* Add all selectors from existingCss that are missing in newCss.
* Backwards compatibility fix: losing CSS due to having different block style identifiers for different languages.
*/
function mergeCss(existingCss, newCss) {
if (! existingCss) {
return newCss;
}
let pageBlocksString = JSON.stringify(window.pageBlocks);
let regex = "\\.ID(.*?){(.*?)}"
let matches = existingCss.match(new RegExp(regex, 'g'));
matches.forEach(function(css) {
let selector = css.split('{')[0];
let pageBlocksStringSelector = selector.replace('.', ' ').trim();
if (newCss.indexOf(selector) === -1 && pageBlocksString.indexOf(pageBlocksStringSelector) >= 0) {
newCss += css;
}
});
return newCss;
}
/**
* Remove the style selectors that are no longer present among the current page blocks.
*/
function removeOldStyleSelectors(styleComponents) {
let pageBlocksString = JSON.stringify(window.pageBlocks);
let updatedStyleComponents = [];
styleComponents.forEach(styleComponent => {
if (styleComponent.attributes.selectors.models.length) {
let selector = styleComponent.attributes.selectors.models[0].id;
if (pageBlocksString.includes(selector)) {
updatedStyleComponents.push(styleComponent);
}
}
});
return updatedStyleComponents;
}
/**
* Save the data of all translation variants on the server.
*/
@@ -193,7 +233,7 @@
});
let data = window.pageData;
data.style = removeOldStyleSelectors(data.css, data.style);
data.style = removeOldStyleSelectors(data.style);
data.blocks = window.pageBlocks;
$.ajax({
@@ -222,24 +262,6 @@
});
}
/**
* Remove the style selectors that are not present in the given CSS string.
*/
function removeOldStyleSelectors(css, styleComponents) {
let updatedStyleComponents = [];
styleComponents.forEach(styleComponent => {
if (styleComponent.attributes.selectors.models.length) {
let selector = styleComponent.attributes.selectors.models[0].id;
if (css.includes(selector)) {
updatedStyleComponents.push(styleComponent);
}
}
});
return updatedStyleComponents;
}
/**
* Get the given component in storage format (in context of its container with all siblings removed).
*
@@ -60,6 +60,7 @@ if (window.customConfig !== undefined) {
window.initialComponents = <?= json_encode($pageRenderer->render()) ?>;
window.initialStyle = <?= json_encode($pageBuilder->getPageStyleComponents($page)) ?>;
window.initialCss = <?= json_encode($pageBuilder->getPageStyleCss($page)) ?>;
window.grapesJSTranslations = {
<?= $currentLanguage ?>: {
styleManager: {