Skip to content

Commit 0764195

Browse files
authored
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 068f6d3 commit 0764195

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
@@ -5,6 +5,8 @@ Changelog
55
unreleased
66
==========
77

8+
* Add support for ace editor loaded from static files through djangocms-static-ace
9+
* Add dark mode support
810

911
3.0.0 (2020-09-02)
1012
==================

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
@@ -7,6 +7,13 @@
77

88

99
class SnippetAdmin(admin.ModelAdmin):
10+
class Media:
11+
js = (
12+
"admin/vendor/ace/ace.js"
13+
if "djangocms_static_ace" in settings.INSTALLED_APPS
14+
else "https://cdnjs.cloudflare.com/ajax/libs/ace/1.9.6/ace.js",
15+
)
16+
1017
list_display = ('slug', 'name')
1118
search_fields = ['slug', 'name']
1219
prepopulated_fields = {'slug': ('name',)}

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
@@ -11,6 +11,10 @@
1111
'django-treebeard>=4.3,<4.5',
1212
]
1313

14+
EXTRA_REQUIREMENTS = {
15+
'static-ace': ['djangocms-static-ace', ],
16+
}
17+
1418

1519
CLASSIFIERS = [
1620
'Development Status :: 5 - Production/Stable',
@@ -57,6 +61,7 @@
5761
include_package_data=True,
5862
zip_safe=False,
5963
install_requires=REQUIREMENTS,
64+
extras_require=EXTRA_REQUIREMENTS,
6065
classifiers=CLASSIFIERS,
6166
test_suite='tests.settings.run',
6267
)

0 commit comments

Comments
 (0)