Skip to content

Commit 2f1eef8

Browse files
committed
Add syntax highlight support for section editor
1 parent 8aa023d commit 2f1eef8

3 files changed

Lines changed: 76 additions & 32 deletions

File tree

admin-power-tools.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ enabled: true
2626

2727
edit_page_enabled: true
2828
edit_section_enabled: true
29-
29+
edit_section_syntax_enabled: true
3030
add_page_enabled: true
3131

3232
reports_enabled: true

admin/templates/edit-section.html.twig

Lines changed: 62 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,20 @@
1010
{% do assets.addJs('plugin://admin-power-tools/assets/ajax_util.js') %}
1111
{% do assets.addJs('plugin://admin-power-tools/assets/fullscreen_util.js') %}
1212

13+
{% if config.plugins['admin-power-tools'].edit_section_syntax_enabled and config.plugins['editor'] and config.plugins['editor'].enabled %}
14+
{# enable CodeMirror if the editor plugin is installed #}
15+
{% do assets.addCss('plugin://editor/lib/codemirror/codemirror.css') %}
16+
{% do assets.addJs('plugin://editor/lib/codemirror/codemirror.js') %}
17+
18+
{% for service in service_list('language') %}
19+
{% if service['id'] == 'md' %}
20+
{% for dep in service.dependencies %}
21+
{% do assets.add(dep) %}
22+
{% endfor %}
23+
{% endif %}
24+
{% endfor %}
25+
{% endif %}
26+
1327
{% block content %}
1428
{{ page.content }}
1529

@@ -98,38 +112,44 @@
98112
{#<h1>{{ _section_name }}</h1>#}
99113

100114
<script>
101-
function _doSave() {
102-
const content = $('#editor').val();
103-
const section_name = $('#heading').val();
104-
_ajaxPost(window.GravAdmin.config.base_url_relative + "/powertools/save-section", {
105-
route: '{{ _route }}',
106-
section: '{{ _section }}',
107-
section_name,
108-
content
109-
})
110-
}
111-
112-
function _doMoveSectionToChildPage() {
113-
const newContent = $('#editor').val();
114-
const newSection = $('#heading').val();
115-
116-
const context = {
117-
"section": "{{ _section }}",
118-
"route": "{{ _route }}",
119-
"new_section": newSection,
120-
"new_content": newContent
121-
};
122-
_nonAjaxAction('move-to-child', JSON.stringify(context), 'Move this section to a new child page?');
115+
function _doSave() {
116+
let $editor = $('#editor');
117+
let content
118+
if ($editor[0].codeMirror) {
119+
content = $editor[0].codeMirror.getValue()
120+
} else {
121+
content = $editor.val();
123122
}
124-
125-
function _doDelete() {
126-
if (confirm("Delete this section from the page?")) {
127-
_ajaxPost(window.GravAdmin.config.base_url_relative + "/powertools/delete-section", {
128-
route: '{{ _route }}',
129-
section: '{{ _section }}'
130-
})
131-
}
123+
const section_name = $('#heading').val();
124+
_ajaxPost(window.GravAdmin.config.base_url_relative + "/powertools/save-section", {
125+
route: '{{ _route }}',
126+
section: '{{ _section }}',
127+
section_name,
128+
content
129+
})
130+
}
131+
132+
function _doMoveSectionToChildPage() {
133+
const newContent = $('#editor').val();
134+
const newSection = $('#heading').val();
135+
136+
const context = {
137+
"section": "{{ _section }}",
138+
"route": "{{ _route }}",
139+
"new_section": newSection,
140+
"new_content": newContent
141+
};
142+
_nonAjaxAction('move-to-child', JSON.stringify(context), 'Move this section to a new child page?');
143+
}
144+
145+
function _doDelete() {
146+
if (confirm("Delete this section from the page?")) {
147+
_ajaxPost(window.GravAdmin.config.base_url_relative + "/powertools/delete-section", {
148+
route: '{{ _route }}',
149+
section: '{{ _section }}'
150+
})
132151
}
152+
}
133153
</script>
134154

135155
<div id="edit-area">
@@ -149,5 +169,17 @@
149169
<textarea id="editor">{{ get_page_section(_route, _section) }}</textarea>
150170
</div>
151171

172+
<script>
173+
if (window.CodeMirror) {
174+
var textarea = document.getElementById("editor");
175+
var editor = CodeMirror.fromTextArea(textarea, {
176+
lineNumbers: false,
177+
mode: "markdown"
178+
});
179+
// This is used in save() to flag the textarea as CodeMirror enabled
180+
textarea.codeMirror = editor;
181+
}
182+
</script>
183+
152184
</div>
153185
{% endblock %}

blueprints.yaml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,19 @@ form:
7676
0: Disabled
7777
validate:
7878
type: bool
79-
79+
80+
edit_section_syntax_enabled:
81+
type: toggle
82+
label: Use CodeMirror if available
83+
description: Enables syntax highlighing for the section editor. The editor plugin must be installed.
84+
highlight: 1
85+
default: 1
86+
options:
87+
1: Enabled
88+
0: Disabled
89+
validate:
90+
type: bool
91+
8092
add_page_enabled:
8193
type: toggle
8294
label: Additional <strong>Add Page</strong> options

0 commit comments

Comments
 (0)