-
Notifications
You must be signed in to change notification settings - Fork 7
fix: Support django CMS 5 data bridge for text-enabled plugins #87
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -449,24 +449,13 @@ class CMSEditor { | |
} | ||
|
||
} | ||
const script = dom.querySelector('script#data-bridge'); | ||
el.dataset.changed = 'false'; | ||
if (script && script.textContent.length > 2) { | ||
this.CMS.API.Helpers.dataBridge = JSON.parse(script.textContent); | ||
} else { | ||
const regex1 = /^\s*Window\.CMS\.API\.Helpers\.dataBridge\s=\s(.*?);$/gmu.exec(body); | ||
const regex2 = /^\s*Window\.CMS\.API\.Helpers\.dataBridge\.structure\s=\s(.*?);$/gmu.exec(body); | ||
if (regex1 && regex2 && this.CMS) { | ||
this.CMS.API.Helpers.dataBridge = JSON.parse(regex1[1]); | ||
this.CMS.API.Helpers.dataBridge.structure = JSON.parse(regex2[1]); | ||
} else { | ||
// No databridge found: reload | ||
this.CMS.API.Helpers.reloadBrowser('REFRESH_PAGE'); | ||
return; | ||
} | ||
this.processDataBridge(dom); | ||
if (!this.CMS.API.Helpers.dataBridge) { | ||
// No databridge found | ||
this.CMS.API.Helpers.reloadBrowser('REFRESH_PAGE'); | ||
return; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. issue (bug_risk): Undefined variable Pass the HTML string (e.g., dom.innerHTML) into processDataBridge or rename the variable so that body is defined. |
||
} | ||
// Additional content for the page disrupts inline editing and needs to be removed | ||
delete this.CMS.API.Helpers.dataBridge.structure?.content; | ||
|
||
if (this.CMS.settings.version.startsWith('3.')) { | ||
/* Reflect dirty flag in django CMS < 4 */ | ||
|
@@ -497,6 +486,27 @@ class CMSEditor { | |
} | ||
} | ||
|
||
processDataBridge(dom) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. issue (complexity): Consider refactoring the dataBridge extraction logic into a single helper function that handles all cases, removes duplication, and restores immediate reload on failure. Here’s one way to collapse the three “find‐then‐fallback” branches, kill the duplicated regex, remove the stray logs, and restore the original “reload on missing bridge” behavior—all while keeping every code‐path intact: // call site
el.dataset.changed = 'false';
const dataBridge = this._fetchDataBridge(dom);
if (!dataBridge) {
this.CMS.API.Helpers.reloadBrowser('REFRESH_PAGE');
return;
}
delete dataBridge.structure?.content;
this.CMS.API.Helpers.dataBridge = dataBridge;
… // new helper
_fetchDataBridge(dom, body = dom.innerHTML) {
// 1) inline JSON <script id="data-bridge">…</script>
const script = dom.querySelector('script#data-bridge');
const txt = script?.textContent?.trim();
if (txt) {
return JSON.parse(txt);
}
// 2) legacy JS assignments in page HTML
const rx1 = /^\s*Window\.CMS\.API\.Helpers\.dataBridge\s*=\s*(.*?);/gm.exec(body);
const rx2 = /^\s*Window\.CMS\.API\.Helpers\.dataBridge\.structure\s*=\s*(.*?);/gm.exec(body);
if (rx1 && rx2) {
const bridge = JSON.parse(rx1[1]);
bridge.structure = JSON.parse(rx2[1]);
return bridge;
}
// 3) nothing found
return null;
} —This
|
||
const script = dom.querySelector('script#data-bridge'); | ||
|
||
if (script && script.textContent.length > 2) { | ||
this.CMS.API.Helpers.dataBridge = JSON.parse(script.textContent); | ||
} else { | ||
const regex1 = /^\s*Window\.CMS\.API\.Helpers\.dataBridge\s=\s(.*?);$/gmu.exec(dom.innerHTML); | ||
const regex2 = /^\s*Window\.CMS\.API\.Helpers\.dataBridge\.structure\s=\s(.*?);$/gmu.exec(dom.innerHTML); | ||
|
||
if (regex1 && regex2 && this.CMS) { | ||
this.CMS.API.Helpers.dataBridge = JSON.parse(regex1[1]); | ||
this.CMS.API.Helpers.dataBridge.structure = JSON.parse(regex2[1]); | ||
} else { | ||
// No databridge found | ||
this.CMS.API.Helpers.dataBridge = null; | ||
} | ||
} | ||
// Additional content for the page disrupts inline editing and needs to be removed | ||
delete this.CMS.API.Helpers.dataBridge.structure?.content; | ||
fsbraun marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
// CMS Editor: addPluginForm | ||
// Get form for a new child plugin | ||
addPluginForm (plugin_type, iframe, el , onLoad, onSave) { | ||
|
@@ -554,19 +564,13 @@ class CMSEditor { | |
el.dataset.changed = 'true'; | ||
// Hook into the django CMS dataBridge to get the details of the newly created or saved | ||
// plugin. For new plugins we need their id to get the content. | ||
|
||
if (!this.CMS.API.Helpers.dataBridge) { | ||
// The dataBridge sets a timer, so typically it will not yet be present | ||
setTimeout(() => { | ||
// Needed to update StructureBoard | ||
if (onSave) { | ||
onSave(el, form, this.CMS.API.Helpers.dataBridge); | ||
} | ||
}, 100); | ||
} else { | ||
// Needed to update StructureBoard | ||
if (onSave) { | ||
onSave(el, form, this.CMS.API.Helpers.dataBridge); | ||
} | ||
this.processDataBridge(form); | ||
} | ||
// Needed to update StructureBoard | ||
if (onSave && this.CMS.API.Helpers.dataBridge) { | ||
onSave(el, form, this.CMS.API.Helpers.dataBridge); | ||
} | ||
// Do callback | ||
} else if (onLoad) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue (bug_risk):
dataBridge
fallback sets empty object, preventing reloadAssigning
{}
makesdataBridge
truthy, so theif (!dataBridge)
check never fires. Return a success flag or setdataBridge
tonull
/undefined
on failure to allow the reload path to run.