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
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" >
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 %}
0 commit comments