diff --git a/mdeditor/configs.py b/mdeditor/configs.py new file mode 100644 index 0000000..62cdc5a --- /dev/null +++ b/mdeditor/configs.py @@ -0,0 +1,65 @@ +# -*- coding:utf-8 -*- +from django.conf import settings +from django.core.exceptions import ImproperlyConfigured + + +DEFAULT_CONFIG = { + 'width': '90%', + 'heigth': 500, + 'toolbar': ["undo", "redo", "|", + "bold", "del", "italic", "quote", "ucwords", "uppercase", "lowercase", "|", + "h1", "h2", "h3", "h5", "h6", "|", + "list-ul", "list-ol", "hr", "|", + "link", "reference-link", "image", "code", "preformatted-text", "code-block", "table", "datetime", + "emoji", "html-entities", "pagebreak", "goto-line", "|", + "help", "info", + "||", "preview", "watch", "fullscreen"], + 'upload_image_formats': ["jpg", "JPG", "jpeg", "JPEG", "gif", "GIF", "png", + "PNG", "bmp", "BMP", "webp", "WEBP"], + 'image_floder': 'editor', + 'theme': 'default', # dark / default + 'preview_theme': 'default', # dark / default + 'editor_theme': 'default', # pastel-on-dark / default + 'toolbar_autofixed': True, + 'search_replace': True, + 'emoji': True, + 'tex': True, + 'flow_chart': True, + 'sequence': True +} + + +class MDConfig(dict): + + def __init__(self, config_name='default'): + self.update(DEFAULT_CONFIG) + self.set_configs(config_name) + + def set_configs(self, config_name='default'): + """ + set config item + :param config_name: + :return: + """ + # Try to get valid config from settings. + configs = getattr(settings, 'MDEDITOR_CONFIGS', None) + if configs: + if isinstance(configs, dict): + # Make sure the config_name exists. + if config_name in configs: + config = configs[config_name] + # Make sure the configuration is a dictionary. + if not isinstance(config, dict): + raise ImproperlyConfigured('MDEDITOR_CONFIGS["%s"] \ + setting must be a dictionary type.' % + config_name) + # Override defaults with settings config. + self.update(config) + else: + raise ImproperlyConfigured("No configuration named '%s' \ + found in your CKEDITOR_CONFIGS setting." % + config_name) + else: + raise ImproperlyConfigured('MDEDITOR_CONFIGS setting must be a\ + dictionary type.') + diff --git a/mdeditor/views.py b/mdeditor/views.py index da9968d..3e7cb59 100644 --- a/mdeditor/views.py +++ b/mdeditor/views.py @@ -8,10 +8,10 @@ from django.http import HttpResponse from django.views.decorators.csrf import csrf_exempt from django.utils.decorators import method_decorator -from .widgets import DEFAULT_CONFIG +from .configs import MDConfig # TODO 此处获取default配置,当用户设置了其他配置时,此处无效,需要进一步完善 -MDEDITOR_CONFIGS = settings.MDEDITOR_CONFIGS['default'] if hasattr(settings, 'MDEDITOR_CONFIGS') else DEFAULT_CONFIG +MDEDITOR_CONFIGS = MDConfig('default') class UploadView(generic.View): diff --git a/mdeditor/widgets.py b/mdeditor/widgets.py index 424f108..6301e25 100644 --- a/mdeditor/widgets.py +++ b/mdeditor/widgets.py @@ -2,8 +2,6 @@ from __future__ import absolute_import from django import forms -from django.conf import settings -from django.core.exceptions import ImproperlyConfigured from django.template.loader import render_to_string from django.utils.encoding import force_text from django.utils.html import conditional_escape @@ -16,31 +14,7 @@ # Django <1.7 from django.forms.util import flatatt - -DEFAULT_CONFIG = { - 'width': '90%', - 'heigth': 500, - 'toolbar': ["undo", "redo", "|", - "bold", "del", "italic", "quote", "ucwords", "uppercase", "lowercase", "|", - "h1", "h2", "h3", "h5", "h6", "|", - "list-ul", "list-ol", "hr", "|", - "link", "reference-link", "image", "code", "preformatted-text", "code-block", "table", "datetime", - "emoji", "html-entities", "pagebreak", "goto-line", "|", - "help", "info", - "||", "preview", "watch", "fullscreen"], - 'upload_image_formats': ["jpg", "JPG", "jpeg", "JPEG", "gif", "GIF", "png", - "PNG", "bmp", "BMP", "webp", "WEBP"], - 'image_floder': 'editor', - 'theme': 'default', # dark / default - 'preview_theme': 'default', # dark / default - 'editor_theme': 'default', # pastel-on-dark / default - 'toolbar_autofixed': True, - 'search_replace': True, - 'emoji': True, - 'tex': True, - 'flow_chart': True, - 'sequence': True -} +from .configs import MDConfig class MDEditorWidget(forms.Textarea): @@ -51,29 +25,7 @@ class MDEditorWidget(forms.Textarea): def __init__(self, config_name='default', *args, **kwargs): super(MDEditorWidget, self).__init__(*args, **kwargs) # Setup config from defaults. - self.config = DEFAULT_CONFIG.copy() - - # Try to get valid config from settings. - configs = getattr(settings, 'MDEDITOR_CONFIGS', None) - if configs: - if isinstance(configs, dict): - # Make sure the config_name exists. - if config_name in configs: - config = configs[config_name] - # Make sure the configuration is a dictionary. - if not isinstance(config, dict): - raise ImproperlyConfigured('MDEDITOR_CONFIGS["%s"] \ - setting must be a dictionary type.' % - config_name) - # Override defaults with settings config. - self.config.update(config) - else: - raise ImproperlyConfigured("No configuration named '%s' \ - found in your CKEDITOR_CONFIGS setting." % - config_name) - else: - raise ImproperlyConfigured('CKEDITOR_CONFIGS setting must be a\ - dictionary type.') + self.config =MDConfig(config_name) def render(self, name, value, renderer=None, attrs=None): """