Skip to content

Commit

Permalink
Fix: Reload browser if django CMS core does not resolve inline editab…
Browse files Browse the repository at this point in the history
…le fields upon partial update
  • Loading branch information
fsbraun committed Sep 29, 2024
1 parent 6139b74 commit 1bc7c0d
Showing 1 changed file with 43 additions and 32 deletions.
75 changes: 43 additions & 32 deletions private/js/cms.editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,48 @@ class CMSEditor {
this._global_settings = {};
this._editor_settings = {};

document.addEventListener('DOMContentLoaded', () => this.initAll());
document.addEventListener('DOMContentLoaded', () => {
// Get the CMS object from the parent window
if (window.CMS !== undefined && window.CMS.config !== undefined) {
this.mainWindow = window;
this.CMS = window.CMS;
} else {
this.mainWindow = window.parent;
this.CMS = window.parent.CMS;
}

if (this.CMS) {
// Only needs to happen on the main window.
console.log("Refresh established");
this.CMS.$(window).on('cms-content-refresh', () => {
if (document.querySelector('template.cms-plugin')) {
// django CMS core does not wrap newly inserted inline editable fields
this.CMS.API.Helpers.reloadBrowser();
} else {
this._resetInlineEditors();
}
});
}
this.initAll();
});
}

// CMS Editor: init_all
// Initialize all editors on the page
initAll () {
// Get global options from script element
try {
this._global_settings = JSON.parse(document.getElementById('cms-editor-cfg').textContent);
} catch (e) {
this._global_settings = {};
}

// All textareas with class CMS_Editor: typically on admin site
document.querySelectorAll('textarea.CMS_Editor').forEach(
(el) => this.init(el), this
);
// Register all plugins on the page for inline editing
this.initInlineEditors();
}

// CMS Editor: init
Expand Down Expand Up @@ -112,6 +153,7 @@ class CMSEditor {
// no plugins -> no inline editors
return;
}
console.log("Init inline editors");
const plugins = this.CMS._plugins;

this.observer = this.observer || new IntersectionObserver( (entries) => {
Expand Down Expand Up @@ -269,37 +311,6 @@ class CMSEditor {
return [];
}

// CMS Editor: init_all
initAll () {
// Get the CMS object from the parent window
if (window.CMS !== undefined && window.CMS.config !== undefined) {
this.mainWindow = window;
this.CMS = window.CMS;
} else {
this.mainWindow = window.parent;
this.CMS = window.parent.CMS;
}

if (this.CMS) {
// Only needs to happen on the main window.
this.CMS.$(window).on('cms-content-refresh', () => this._resetInlineEditors());
}

// Get global options from script element
try {
this._global_settings = JSON.parse(document.getElementById('cms-editor-cfg').textContent);
} catch (e) {
this._global_settings = {};
}

// All textareas with class CMS_Editor: typically on admin site
document.querySelectorAll('textarea.CMS_Editor').forEach(
(el) => this.init(el), this
);
// Register all plugins on the page for inline editing
this.initInlineEditors();
}

// CMS Editor: destroy
destroyAll() {
while (this._editors.length) {
Expand Down

0 comments on commit 1bc7c0d

Please sign in to comment.