Skip to content

Commit 4a1ae85

Browse files
committed
fix: Support django CMS 5 data bridge for text-enabled plugins
1 parent ffa847f commit 4a1ae85

File tree

1 file changed

+34
-28
lines changed

1 file changed

+34
-28
lines changed

private/js/cms.editor.js

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -449,24 +449,13 @@ class CMSEditor {
449449
}
450450

451451
}
452-
const script = dom.querySelector('script#data-bridge');
453452
el.dataset.changed = 'false';
454-
if (script && script.textContent.length > 2) {
455-
this.CMS.API.Helpers.dataBridge = JSON.parse(script.textContent);
456-
} else {
457-
const regex1 = /^\s*Window\.CMS\.API\.Helpers\.dataBridge\s=\s(.*?);$/gmu.exec(body);
458-
const regex2 = /^\s*Window\.CMS\.API\.Helpers\.dataBridge\.structure\s=\s(.*?);$/gmu.exec(body);
459-
if (regex1 && regex2 && this.CMS) {
460-
this.CMS.API.Helpers.dataBridge = JSON.parse(regex1[1]);
461-
this.CMS.API.Helpers.dataBridge.structure = JSON.parse(regex2[1]);
462-
} else {
463-
// No databridge found: reload
464-
this.CMS.API.Helpers.reloadBrowser('REFRESH_PAGE');
465-
return;
466-
}
453+
this.processDataBridge(dom);
454+
if (!this.CMS.API.Helpers.dataBridge) {
455+
// No databridge found
456+
this.CMS.API.Helpers.reloadBrowser('REFRESH_PAGE');
457+
return;
467458
}
468-
// Additional content for the page disrupts inline editing and needs to be removed
469-
delete this.CMS.API.Helpers.dataBridge.structure?.content;
470459

471460
if (this.CMS.settings.version.startsWith('3.')) {
472461
/* Reflect dirty flag in django CMS < 4 */
@@ -497,6 +486,29 @@ class CMSEditor {
497486
}
498487
}
499488

489+
processDataBridge(dom) {
490+
const script = dom.querySelector('script#data-bridge');
491+
492+
if (script && script.textContent.length > 2) {
493+
this.CMS.API.Helpers.dataBridge = JSON.parse(script.textContent);
494+
console.log("CMS.API.Helpers.dataBridge", this.CMS.API.Helpers.dataBridge);
495+
} else {
496+
const regex1 = /^\s*Window\.CMS\.API\.Helpers\.dataBridge\s=\s(.*?);$/gmu.exec(body);
497+
const regex2 = /^\s*Window\.CMS\.API\.Helpers\.dataBridge\.structure\s=\s(.*?);$/gmu.exec(body);
498+
499+
if (regex1 && regex2 && this.CMS) {
500+
this.CMS.API.Helpers.dataBridge = JSON.parse(regex1[1]);
501+
this.CMS.API.Helpers.dataBridge.structure = JSON.parse(regex2[1]);
502+
console.log("CMS.API.Helpers.dataBridge by REGEX", this.CMS.API.Helpers.dataBridge);
503+
} else {
504+
// No databridge found
505+
this.CMS.API.Helpers.dataBridge = {};
506+
}
507+
}
508+
// Additional content for the page disrupts inline editing and needs to be removed
509+
delete this.CMS.API.Helpers.dataBridge.structure?.content;
510+
}
511+
500512
// CMS Editor: addPluginForm
501513
// Get form for a new child plugin
502514
addPluginForm (plugin_type, iframe, el , onLoad, onSave) {
@@ -554,19 +566,13 @@ class CMSEditor {
554566
el.dataset.changed = 'true';
555567
// Hook into the django CMS dataBridge to get the details of the newly created or saved
556568
// plugin. For new plugins we need their id to get the content.
569+
557570
if (!this.CMS.API.Helpers.dataBridge) {
558-
// The dataBridge sets a timer, so typically it will not yet be present
559-
setTimeout(() => {
560-
// Needed to update StructureBoard
561-
if (onSave) {
562-
onSave(el, form, this.CMS.API.Helpers.dataBridge);
563-
}
564-
}, 100);
565-
} else {
566-
// Needed to update StructureBoard
567-
if (onSave) {
568-
onSave(el, form, this.CMS.API.Helpers.dataBridge);
569-
}
571+
this.processDataBridge(form);
572+
}
573+
// Needed to update StructureBoard
574+
if (onSave && this.CMS.API.Helpers.dataBridge) {
575+
onSave(el, form, this.CMS.API.Helpers.dataBridge);
570576
}
571577
// Do callback
572578
} else if (onLoad) {

0 commit comments

Comments
 (0)