Skip to content

Commit d4836f4

Browse files
authored
feat: Support djangocms-static-ace to load ace code editor from static files (#156)
1 parent ee7bcbc commit d4836f4

File tree

6 files changed

+49
-6
lines changed

6 files changed

+49
-6
lines changed

CHANGELOG.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
Changelog
33
=========
44

5+
unreleased
6+
==========
7+
8+
* Added support for djangocms-static-ace to serve the ace code editor locally
59

610
2.0.0 (2020-09-02)
711
==================

README.rst

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,13 @@ For a manual install:
8383

8484
* run ``python manage.py migrate``
8585

86+
The Code plugin uses the ace code editor which is loaded from a CDN by default.
87+
If you want the ace code editor to be served from static files, please use
88+
``djangocms-bootstrap4[static-ace]`` instead of ``djangocms-bootstrap4`` in your
89+
requirements or with pip. Make the static files fore the ace code editor available
90+
to your project by adding ``djangocms_static_ace`` to your project's
91+
``INSTALLED_APPS``.
92+
8693

8794
Configuration
8895
-------------
@@ -110,7 +117,8 @@ It provides the following **standard** Bootstrap 4 components:
110117
* `Tabs <https://getbootstrap.com/docs/4.0/components/navs/#tabs>`_
111118
* `Utilities (Spacing) <https://getbootstrap.com/docs/4.0/utilities/>`_
112119

113-
django CMS Bootstrap 4 **does not** add the styles or javascript files to your frontend, these need to be added at your discretion.
120+
django CMS Bootstrap 4 **does not** add the styles or javascript files to your
121+
frontend, these need to be added at your discretion.
114122

115123

116124
Settings

djangocms_bootstrap4/contrib/bootstrap4_content/forms.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
1+
from django.conf import settings as django_settings
12
from django.forms.models import ModelForm
23
from django.forms.widgets import Textarea
34

45

56
class Bootstrap4CodeForm(ModelForm):
7+
class Media:
8+
js = (
9+
"admin/vendor/ace/ace.js"
10+
if "djangocms_static_ace" in django_settings.INSTALLED_APPS
11+
else "https://cdnjs.cloudflare.com/ajax/libs/ace/1.9.6/ace.js",
12+
)
13+
614
class Meta:
715
# When used inside djangocms-text-ckeditor
816
# this causes the label field to be prefilled with the selected text.

djangocms_bootstrap4/contrib/bootstrap4_content/templates/djangocms_bootstrap4/admin/code.html

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
{% extends "admin/change_form.html" %}
22

33
{% block object-tools %}
4-
{{ block.super }}
5-
6-
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.2.9/ace.js"></script>
7-
<script>
4+
{{ block.super }}<script>
85
django.jQuery(function () {
96
// ace editor cannot be attached directly to a textarea
107
var textarea = django.jQuery('textarea').css('display', 'none');
@@ -18,7 +15,19 @@
1815

1916
// init editor with settings
2017
var editor = ace.edit(div[0]);
21-
editor.setTheme('ace/theme/github');
18+
var darkMode;
19+
20+
if (window.parent.CMS.API.Helpers.getColorScheme) {
21+
darkMode = window.parent.CMS.API.Helpers.getColorScheme() === 'dark';
22+
} else {
23+
// django CMS pre-3.11.1
24+
darkMode = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches;
25+
}
26+
if (darkMode) {
27+
editor.setTheme('ace/theme/tomorrow_night');
28+
} else {
29+
editor.setTheme('ace/theme/github');
30+
}
2231
editor.getSession().setValue(textarea.val());
2332
editor.getSession().setMode('ace/mode/html');
2433
editor.setOptions({

setup.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@
1515
]
1616

1717

18+
EXTRA_REQUIREMENTS = {
19+
"static-ace": [
20+
"djangocms-static-ace",
21+
]
22+
}
23+
24+
1825
CLASSIFIERS = [
1926
'Development Status :: 5 - Production/Stable',
2027
'Environment :: Web Environment',
@@ -56,6 +63,7 @@
5663
include_package_data=True,
5764
zip_safe=False,
5865
install_requires=REQUIREMENTS,
66+
extras_require=EXTRA_REQUIREMENTS,
5967
classifiers=CLASSIFIERS,
6068
test_suite='tests.settings.run',
6169
)

tests/test_migrations.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,18 @@
44

55
from django.core.management import call_command
66
from django.test import TestCase, override_settings
7+
from cms import __version__
8+
from distutils.version import LooseVersion
79

810

911
class MigrationTestCase(TestCase):
1012

1113
@override_settings(MIGRATION_MODULES={})
1214
def test_for_missing_migrations(self):
15+
if LooseVersion("3.9") <= LooseVersion(__version__) < LooseVersion("3.10"):
16+
# django-cms 3.9 creates migrations to BigAutoField hence skip this test
17+
return
18+
1319
output = StringIO()
1420
options = {
1521
'interactive': False,

0 commit comments

Comments
 (0)