Allow ckeditor customization in HTMLField
This commit is contained in:
parent
f20f1aa810
commit
c35290ca45
13
README.rst
13
README.rst
|
@ -94,6 +94,7 @@ For an overview of all the available settings have a look here:
|
|||
|
||||
http://docs.ckeditor.com/#!/api/CKEDITOR.config
|
||||
|
||||
|
||||
Drag & Drop Images
|
||||
------------------
|
||||
|
||||
|
@ -133,6 +134,18 @@ And use it in your models, just like a ``TextField``::
|
|||
class MyModel(models.Model):
|
||||
myfield = HTMLField(blank=True)
|
||||
|
||||
Configurable settings
|
||||
*********************
|
||||
|
||||
When using ``HTMLField``, you can customize the ckeditor configuration for each
|
||||
field::
|
||||
|
||||
class MyModel(models.Model):
|
||||
myfield = HTMLField(blank=True, configuration='CKEDITOR_SETTINGS_MYMODEL')
|
||||
|
||||
and define a ``CKEDITOR_SETTINGS_MYMODEL`` parameter in project settings; values
|
||||
not specified in your custom configuration will be taken from the global ``CKEDITOR_SETTINGS``.
|
||||
|
||||
Extending the plugin
|
||||
--------------------
|
||||
|
||||
|
|
|
@ -10,13 +10,26 @@ except ImportError:
|
|||
|
||||
|
||||
class HTMLField(models.TextField):
|
||||
configuration = None
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
# This allow widget configuration customization from the model definition
|
||||
if kwargs.get('configuration', False):
|
||||
self.configuration = kwargs['configuration']
|
||||
del(kwargs['configuration'])
|
||||
super(HTMLField, self).__init__(*args, **kwargs)
|
||||
|
||||
def formfield(self, **kwargs):
|
||||
defaults = {'widget': TextEditorWidget}
|
||||
if self.configuration:
|
||||
text_editor_widget = TextEditorWidget(configuration=self.configuration)
|
||||
else:
|
||||
text_editor_widget = TextEditorWidget
|
||||
defaults = {'widget': text_editor_widget}
|
||||
defaults.update(kwargs)
|
||||
|
||||
# override the admin widget
|
||||
if defaults['widget'] == admin_widgets.AdminTextareaWidget:
|
||||
defaults['widget'] = TextEditorWidget
|
||||
defaults['widget'] = text_editor_widget
|
||||
|
||||
return super(HTMLField, self).formfield(**defaults)
|
||||
|
||||
|
|
|
@ -50,10 +50,9 @@ $(document).ready(function () {
|
|||
init: function (container, options, settings) {
|
||||
if ($('#' + container).length > 0) {
|
||||
this.container = $('#' + container);
|
||||
|
||||
// add additional settings to options
|
||||
this.options.toolbar = settings.toolbar;
|
||||
this.options = $.extend(true, {
|
||||
this.options = $.extend(false, {
|
||||
'settings': settings
|
||||
}, this.options, options);
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import json
|
||||
|
||||
from copy import deepcopy
|
||||
from django.conf import settings
|
||||
from django.forms import Textarea
|
||||
from django.template.loader import render_to_string
|
||||
|
@ -10,7 +10,8 @@ import djangocms_text_ckeditor.settings as text_settings
|
|||
|
||||
|
||||
class TextEditorWidget(Textarea):
|
||||
def __init__(self, attrs=None, installed_plugins=None, pk=None, placeholder=None, plugin_language=None):
|
||||
def __init__(self, attrs=None, installed_plugins=None, pk=None,
|
||||
placeholder=None, plugin_language=None, configuration=None):
|
||||
"""
|
||||
Create a widget for editing text + plugins.
|
||||
|
||||
|
@ -31,6 +32,12 @@ class TextEditorWidget(Textarea):
|
|||
self.pk = pk
|
||||
self.placeholder = placeholder
|
||||
self.plugin_language = plugin_language
|
||||
if configuration and getattr(settings, configuration, False):
|
||||
conf = deepcopy(text_settings.CKEDITOR_SETTINGS)
|
||||
conf.update(getattr(settings, configuration))
|
||||
self.configuration = conf
|
||||
else:
|
||||
self.configuration = text_settings.CKEDITOR_SETTINGS
|
||||
|
||||
def render_textarea(self, name, value, attrs=None):
|
||||
return super(TextEditorWidget, self).render(name, value, attrs)
|
||||
|
@ -41,7 +48,7 @@ class TextEditorWidget(Textarea):
|
|||
'ckeditor_class': self.ckeditor_class,
|
||||
'name': name,
|
||||
'language': language,
|
||||
'settings': language.join(json.dumps(text_settings.CKEDITOR_SETTINGS).split("{{ language }}")),
|
||||
'settings': language.join(json.dumps(self.configuration).split("{{ language }}")),
|
||||
'STATIC_URL': settings.STATIC_URL,
|
||||
'installed_plugins': self.installed_plugins,
|
||||
'plugin_pk': self.pk,
|
||||
|
|
Reference in New Issue