Skip to content

Commit

Permalink
fix: Handle block cms plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
fsbraun committed Sep 27, 2024
1 parent aa8f047 commit 4caf9b3
Show file tree
Hide file tree
Showing 13 changed files with 98 additions and 92 deletions.
19 changes: 6 additions & 13 deletions djangocms_text/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ class TextConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"

def ready(self):
register(check_ckeditor_settings)
self.inline_models = discover_inline_editable_models()
register(check_ckeditor_settings)


def discover_inline_editable_models():
Expand All @@ -19,23 +19,17 @@ def discover_inline_editable_models():

registered_inline_fields = ["HTMLFormField", "CharField"]
inline_models = {}
blacklist_apps = [
"auth",
"admin",
"sessions",
"contenttypes",
"sites",
"cms",
"djangocms_text",
"djangocms_alias",
]
blacklist_apps = []

for model, modeladmin in site._registry.items():
if model._meta.app_label in blacklist_apps:
continue

for field_name in getattr(modeladmin, "frontend_editable_fields", []):
try:
form = modeladmin.get_form(request=None, fields=(field_name,)) # Worth a try
form = modeladmin.get_form(
request=None, fields=(field_name,)
) # Worth a try
except Exception:
form = getattr(modeladmin, "form", None)
if form:
Expand All @@ -62,7 +56,6 @@ def discover_inline_editable_models():
return inline_models



def check_ckeditor_settings(app_configs, **kwargs):
from django.conf import settings

Expand Down
21 changes: 8 additions & 13 deletions djangocms_text/cms_toolbars.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from cms.toolbar_pool import toolbar_pool

from . import settings
from .utils import get_url_endpoint, get_render_plugin_url
from .utils import get_url_endpoint, get_render_plugin_url, get_cancel_url
from .widgets import rte_config, TextEditorWidget


Expand Down Expand Up @@ -43,12 +43,8 @@ def media(self):

@cached_property
def inline_editing(self):
inline_editing = self.request.session.get(
"inline_editing", True
) # Activated by default
change = self.request.GET.get(
"inline_editing", None
) # can be changed by query param
inline_editing = self.request.session.get("inline_editing", True) # Activated by default
change = self.request.GET.get("inline_editing", None) # can be changed by query param
if change is not None:
inline_editing = change == "1"
self.request.session["inline_editing"] = inline_editing # store in session
Expand All @@ -60,9 +56,9 @@ def populate(self):
item.add_item(
IconButton(
name=_("Toggle inline editing mode for text plugins"),
url=self.get_full_path_with_param(
"inline_editing", int(not self.inline_editing)
).replace("/structure/", "/edit/"),
url=self.get_full_path_with_param("inline_editing", int(not self.inline_editing)).replace(
"/structure/", "/edit/"
),
active=self.inline_editing,
extra_classes=["cms-icon cms-icon-pencil"],
),
Expand All @@ -72,15 +68,14 @@ def populate(self):
widget = TextEditorWidget(
url_endpoint=get_url_endpoint(),
render_plugin_url=get_render_plugin_url(),
cancel_url=get_cancel_url(),
)
item = TemplateItem(
"cms/toolbar/config.html",
extra_context={
"global_config": widget.get_global_settings(self.current_lang),
"html_field_config": widget.get_editor_settings(self.current_lang),
"allowed_inlines": apps.get_app_config(
"djangocms_text"
).inline_models,
"allowed_inlines": apps.get_app_config("djangocms_text").inline_models,
},
side=self.toolbar.RIGHT,
)
Expand Down
5 changes: 5 additions & 0 deletions djangocms_text/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,8 @@ def get_url_endpoint():
def get_render_plugin_url():
"""Get the url for rendering a text-enabled plugin for the toolbar"""
return admin_reverse("djangocms_text_textplugin_render_plugin")


def get_cancel_url():
"""Get the url for cancelling a plugin edit"""
return admin_reverse("djangocms_text_textplugin_revert_on_cancel")
2 changes: 1 addition & 1 deletion djangocms_text/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,9 @@ def get_global_settings(self, language):
}

return {
"add_plugin_url": admin_reverse(cms_placeholder_add_plugin),
"url_endpoint": self.url_endpoint or get_url_endpoint(),
"static_url": settings.STATIC_URL + "djangocms_text",
"add_plugin_url": admin_reverse(cms_placeholder_add_plugin),
"lang": toolbar_setting,
"lang_alt": {
"toolbar": gettext("CMS Plugins"),
Expand Down
1 change: 0 additions & 1 deletion private/js/ckeditor4_plugins/cmsplugins/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ import CmsDialog from "../../cms.dialog";
this.unsaved_child_plugins = [];

var settings = CMS_Editor.getSettings(editor.name);
console.log(settings);
this.setupCancelCleanupCallback(settings);

// don't do anything if there are no plugins defined
Expand Down
2 changes: 0 additions & 2 deletions private/js/cms.dialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,6 @@ class CmsForm {
if (options && options.x && options.y) {
const el_pos = this.el.getBoundingClientRect();
if (options.x > window.innerWidth / 2) {
console.log("options.x", options.x);
console.log("el_pos.width", el_pos.width);
this.dialog.classList.add("right");
this.dialog.style.right = ( el_pos.x + el_pos.width - options.x - 28) + 'px';
} else {
Expand Down
36 changes: 17 additions & 19 deletions private/js/cms.editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,7 @@ class CMSEditor {
this._global_settings = {};
this._editor_settings = {};

// 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());
}
document.addEventListener('DOMContentLoaded', () => this.initAll());
}

// CMS Editor: init
Expand Down Expand Up @@ -284,6 +272,20 @@ class CMSEditor {

// 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);
Expand Down Expand Up @@ -615,11 +617,7 @@ class CMSEditor {
}
}

// Create global editor object
document.addEventListener('DOMContentLoaded', () => {
"use strict";

window.CMS_Editor = new CMSEditor();
window.CMS_Editor.initAll();
});
// Create global editor object
window.CMS_Editor = new CMSEditor();

4 changes: 0 additions & 4 deletions private/js/cms.linkfield.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@ import "../css/cms.linkfield.css";

class LinkField {
constructor(element, options) {
console.log("LinkField constructor", element.id + '_select');
this.options = options;
this.urlElement = element;
this.form = element.closest("form");
this.selectElement = this.form.querySelector(`input[name="${this.urlElement.id + '_select'}"]`);
console.log(this.urlElement, this.selectElement);
if (this.selectElement) {
this.prepareField();
this.registerEvents();
Expand Down Expand Up @@ -108,7 +106,6 @@ class LinkField {
handleSelection(event) {
event.stopPropagation();
event.preventDefault();
console.log("handleSelection", event.target.textContent, event.target.getAttribute('data-href'));
this.inputElement.value = event.target.getAttribute('data-text') || event.target.textContent;
this.inputElement.classList.add('cms-linkfield-selected');
this.urlElement.value = event.target.getAttribute('data-href');
Expand All @@ -118,7 +115,6 @@ class LinkField {
}

handleChange(event) {
console.log("handleChange", event.target);
if (this.selectElement.value) {
fetch(this.options.url + '?g=' + encodeURIComponent(this.selectElement.value))
.then(response => response.json())
Expand Down
3 changes: 0 additions & 3 deletions private/js/cms.texteditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,7 @@ class CmsTextEditor {

_blur (e) {
this.save(e.target, (el, response) => {
console.log("timer set");
console.log(response);
setTimeout(() => {
console.log(e.target);
if (e.target.dataset.changed === 'true') {
e.target.innerText = this.options.undo;
e.target.dataset.changed = 'false';
Expand Down
3 changes: 2 additions & 1 deletion private/js/cms.tiptap.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import TableCell from '@tiptap/extension-table-cell'
import TableHeader from '@tiptap/extension-table-header'
import TableRow from '@tiptap/extension-table-row'
import { TextAlign, TextAlignOptions } from '@tiptap/extension-text-align';
import CmsPluginNode from './tiptap_plugins/cms.plugin';
import { CmsPluginNode, CmsBlockPluginNode } from './tiptap_plugins/cms.plugin';
import TiptapToolbar from "./tiptap_plugins/cms.tiptap.toolbar";
import {StarterKit} from "@tiptap/starter-kit";

Expand Down Expand Up @@ -55,6 +55,7 @@ class CMSTipTapPlugin {
}),
Small, Var, Kbd, Samp,
CmsPluginNode,
CmsBlockPluginNode,
TextAlign.configure({
types: ['heading', 'paragraph'],
}),
Expand Down
3 changes: 1 addition & 2 deletions private/js/tiptap_plugins/cms.balloon-toolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ export default class CmsBalloonToolbar {

this.toolbar.style.insetBlockStart = `${pos.top - ref.top}px`;
// TODO: Set the size of the balloon according to the fontsize
// this.toolbar.style.setProperty('--size', this.editor.view. ...)
// this.toolbar.style.setProperty('--size', this.editor.view. ...)
}

_getResolvedPos() {
Expand All @@ -142,7 +142,6 @@ export default class CmsBalloonToolbar {
if (type in this._node_icons) {
this.toolbar.innerHTML = this._node_icons[type];
} else {
console.log(type);
this.toolbar.innerHTML = this._menu_icon;
}
}
Expand Down
Loading

0 comments on commit 4caf9b3

Please sign in to comment.