From f1fcef2a6f9c78ceaf7a831ee07ad355d728cf69 Mon Sep 17 00:00:00 2001 From: Martin Green Date: Sun, 26 Jun 2022 14:16:42 +0100 Subject: [PATCH] Refactorising CodeMirror HTML editor, with an ultimate aim to make it reusable. However more work if required... --- core/views/editor_helpers.py | 3 + core/views/expo.py | 4 +- settings.py | 3 + templates/editexpopage.html | 235 +------------------------ templates/html_editor_scripts_css.html | 166 +++++++++++++++++ templates/widgets/HTMLarea.html | 32 ++++ 6 files changed, 210 insertions(+), 233 deletions(-) create mode 100644 templates/html_editor_scripts_css.html create mode 100644 templates/widgets/HTMLarea.html diff --git a/core/views/editor_helpers.py b/core/views/editor_helpers.py index 1fe30b0..f85d582 100644 --- a/core/views/editor_helpers.py +++ b/core/views/editor_helpers.py @@ -130,3 +130,6 @@ class NewWebImageForm(forms.Form): if full_path.exists(): raise forms.ValidationError("File already exists in %s" % rel_path) return self.cleaned_data['file_'] + +class HTMLarea(forms.Textarea): + template_name = "widgets/HTMLarea.html" diff --git a/core/views/expo.py b/core/views/expo.py index 91919a5..d84aa5f 100644 --- a/core/views/expo.py +++ b/core/views/expo.py @@ -23,6 +23,8 @@ import troggle.settings as settings from troggle.lib import version_control +from troggle.core.views.editor_helpers import HTMLarea + '''Formerly a separate package called 'flatpages' written by Martin Green 2011. This was NOT django.contrib.flatpages which stores HTML in the database, so the name was changed to expopages. @@ -380,5 +382,5 @@ class ExpoPageForm(forms.Form): '''The form used by the editexpopage function ''' title = forms.CharField(widget=forms.TextInput(attrs={'size':'60', 'placeholder': "Enter title (displayed in tab)"})) - html = forms.CharField(widget=forms.Textarea(attrs={"cols":80, "rows":20, 'placeholder': "Enter page content (using HTML)"})) + html = forms.CharField(widget=HTMLarea(attrs={"cols":80, "rows":20, 'placeholder': "Enter page content (using HTML)"})) change_message = forms.CharField(widget=forms.Textarea(attrs={"cols":80, "rows":3, 'placeholder': "Descibe the change made (for git)"})) diff --git a/settings.py b/settings.py index 7c05550..bcb972e 100644 --- a/settings.py +++ b/settings.py @@ -134,10 +134,13 @@ INSTALLED_APPS = ( 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.admindocs', + 'django.forms', #Required to customise widget templates # 'django.contrib.staticfiles', # We put our CSS etc explicitly in the right place so do not need this 'troggle.core', ) +FORM_RENDERER = 'django.forms.renderers.TemplatesSetting' #Required to customise widget templates + # See the recommended order of these in https://docs.djangoproject.com/en/2.2/ref/middleware/ # Note that this is a radically different onion architecture from earlier versions though it looks the same, # see https://docs.djangoproject.com/en/2.0/topics/http/middleware/#upgrading-pre-django-1-10-style-middleware diff --git a/templates/editexpopage.html b/templates/editexpopage.html index b71c91f..22c9106 100644 --- a/templates/editexpopage.html +++ b/templates/editexpopage.html @@ -2,243 +2,14 @@ {% block title %}Edit {{ path }}{% endblock %} {% block extrahead %} - - - - - - - - - - - - - - {% endblock %} {% block body %}

Edit {{ path }}

- - - - -
{% csrf_token %} -{{ form.non_field_errors }} -
- {{ form.title.errors }} - - {{ form.title }} -
-
- {{ form.html.errors }} - - {{ form.html }} -
- - - - - - - - - - -
- {{ form.change_message.errors }} - - {{ form.change_message }} -
-{% include "menu.html" %} +{{ form.as_p }}

- - - - +{% include "menu.html" %} +{% include 'html_editor_scripts_css.html' %} {% endblock %} diff --git a/templates/html_editor_scripts_css.html b/templates/html_editor_scripts_css.html new file mode 100644 index 0000000..5bd56d4 --- /dev/null +++ b/templates/html_editor_scripts_css.html @@ -0,0 +1,166 @@ + + + + + + + + + + + + + + diff --git a/templates/widgets/HTMLarea.html b/templates/widgets/HTMLarea.html new file mode 100644 index 0000000..9837327 --- /dev/null +++ b/templates/widgets/HTMLarea.html @@ -0,0 +1,32 @@ + +
+ + + +{% include "django/forms/widgets/textarea.html" %} + + + + + + + + + + + + +