Skip to content

Commit cbff022

Browse files
committed
Update to ace 1.9.6, load ace editor from static files if djangocms_static_ace is installed, add dark mode (#123)
Fix: Load ace editor from static files if `djangocms-static-ace` is installed Fix: Respect if user has set dark mode Add: Weak dependency on djangocms-static-ace Doc: optional static-ace dependency
1 parent a26c443 commit cbff022

File tree

5 files changed

+36
-4
lines changed

5 files changed

+36
-4
lines changed

CHANGELOG.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ Unreleased
6363
* feat: djangocms-versioning support added, including model restructure and configuration
6464
* feat: django-cms v4.0.x support added
6565

66+
* Add support for ace editor loaded from static files through djangocms-static-ace
67+
* Add dark mode support
6668

6769
3.0.0 (2020-09-02)
6870
==================

README.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ For a manual install:
6262
* add ``djangocms_snippet`` to your ``INSTALLED_APPS``
6363
* run ``python manage.py migrate djangocms_snippet``
6464

65+
Djangocms-snippet uses the ace code editor which normally is loaded from a CDN.
66+
If you prefer your application to provide the editor locally, you can change
67+
the requirement from `djangocms_snippet` to `djangocms_snippet[static-ace]` and
68+
add `djangocms_static_ace` to your project's `INSTALLED_APPS`.
69+
6570

6671
Configuration
6772
-------------

djangocms_snippet/admin.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@
3232

3333
@admin.register(Snippet)
3434
class SnippetAdmin(*snippet_admin_classes):
35+
class Media:
36+
js = (
37+
"admin/vendor/ace/ace.js"
38+
if "djangocms_static_ace" in settings.INSTALLED_APPS
39+
else "https://cdnjs.cloudflare.com/ajax/libs/ace/1.9.6/ace.js",
40+
)
41+
3542
list_display = ('name',)
3643
search_fields = ['name']
3744
change_form_template = 'djangocms_snippet/admin/change_form.html'

djangocms_snippet/templates/djangocms_snippet/admin/change_form.html

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33

44
{% block object-tools %}
55
{{ block.super }}
6-
7-
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.2.5/ace.js"></script>
86
<script>
97
django.jQuery(function () {
108
// ace editor cannot be attached directly to a textarea
@@ -19,14 +17,29 @@
1917

2018
// init editor with settings
2119
var editor = ace.edit(div[0]);
22-
editor.setTheme('ace/theme/' + settings.theme);
20+
var darkMode;
21+
if (window.CMS) {
22+
if (CMS.API.Helpers.getColorScheme) {
23+
darkMode = CMS.API.Helpers.getColorScheme() === 'dark';
24+
} else {
25+
// django CMS pre-3.11.1
26+
darkMode = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches;
27+
}
28+
} else if (window.localStorage) {
29+
// CMS not loaded: set color scheme for admin site / popup window according to settings
30+
darkMode = JSON.parse(localStorage.getItem('cms_cookie') || '{}').color_scheme === 'dark';
31+
}
32+
if (darkMode) {
33+
editor.setTheme('ace/theme/tomorrow_night');
34+
} else {
35+
editor.setTheme(settings.theme ? 'ace/theme/' + settings.theme : 'ace/theme/github');
36+
}
2337
editor.getSession().setValue(textarea.val());
2438
editor.getSession().setMode('ace/mode/' + settings.mode);
2539
editor.setOptions({
2640
fontSize: '14px',
2741
cursorStyle: 'smooth'
2842
});
29-
editor.renderer.setScrollMargin(5, 5);
3043

3144
// send data back to textarea when submitting
3245
textarea.closest('form').submit(function () {

setup.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
'django-treebeard>=4.3',
1515
]
1616

17+
EXTRA_REQUIREMENTS = {
18+
'static-ace': ['djangocms-static-ace', ],
19+
}
20+
1721

1822
CLASSIFIERS = [
1923
'Development Status :: 5 - Production/Stable',
@@ -53,6 +57,7 @@
5357
include_package_data=True,
5458
zip_safe=False,
5559
install_requires=REQUIREMENTS,
60+
extras_require=EXTRA_REQUIREMENTS,
5661
classifiers=CLASSIFIERS,
5762
test_suite='tests.settings.run',
5863
)

0 commit comments

Comments
 (0)