|
1 | 1 | (function($) {
|
2 | 2 | $.fn.setAsEditor = function(selector) {
|
3 |
| - selector = selector || 'textarea#Form_Body'; |
| 3 | + selector = selector || '.BodyBox,.js-bodybox'; |
4 | 4 |
|
5 | 5 | // If editor can be loaded, add class to body
|
6 | 6 | $('body').addClass('topcodereditor-active');
|
|
11 | 11 | var editor,
|
12 | 12 | editorCacheBreakValue = Math.random(),
|
13 | 13 | editorVersion = gdn.definition('editorVersion', editorCacheBreakValue),
|
14 |
| - formatOriginal = gdn.definition('editorInputFormat', 'Markdown'), |
| 14 | + defaultInputFormat = gdn.definition('defaultInputFormat', 'Markdown'), |
| 15 | + defaultMobileInputFormat = gdn.definition('defaultMobileInputFormat', 'Markdown'), |
| 16 | + editorInputFormat = gdn.definition('editorInputFormat', 'Markdown'), |
15 | 17 | topcoderEditorToolbar = gdn.definition('topcoderEditorToolbar'),
|
16 |
| - debug = true; |
| 18 | + debug = false; |
17 | 19 |
|
18 | 20 | var toolbarCustomActions = [{
|
19 | 21 | name: "mentions",
|
|
45 | 47 | }
|
46 | 48 |
|
47 | 49 | function logMessage(message) {
|
48 |
| - console.log('TopcoderPlugin::'+ message); |
| 50 | + if (debug) { |
| 51 | + console.log('TopcoderPlugin::'+ message); |
| 52 | + } |
49 | 53 | }
|
50 | 54 |
|
51 | 55 | function topcoderHandles(cm, option) {
|
|
123 | 127 | * Initialize editor on the page.
|
124 | 128 | *
|
125 | 129 | */
|
126 |
| - var editorInit = function(textareaObj) { |
127 |
| - var $currentEditableTextarea = $(textareaObj); |
128 |
| - |
129 |
| - // if found, perform operation |
130 |
| - if ($currentEditableTextarea.length) { |
131 |
| - // instantiate new editor |
132 |
| - var editor = new EasyMDE({ |
133 |
| - shortcuts: { |
134 |
| - "mentions":"Ctrl-Space", |
135 |
| - }, |
136 |
| - autofocus: false, |
137 |
| - forceSync: true, // true, force text changes made in EasyMDE to be immediately stored in original text area. |
138 |
| - placeholder: '', |
139 |
| - element: $currentEditableTextarea[0], |
140 |
| - hintOptions: {hint: topcoderHandles}, |
141 |
| - // toolbar: topcoderEditorToolbar, |
142 |
| - toolbar: ["bold", "italic", "strikethrough", "|", |
143 |
| - "heading-1", "heading-2", "heading-3", "|", "code", "quote", "|", "unordered-list", |
144 |
| - "ordered-list", "clean-block", "|", { |
145 |
| - name: "mentions", |
146 |
| - action: function mentions(editor) { |
147 |
| - completeAfter(editor.codemirror); |
148 |
| - }, |
149 |
| - className: "fa fa-at", |
150 |
| - title: "Mention a Topcoder User", |
151 |
| - |
152 |
| - }, "link", "image", "table", "horizontal-rule", "|", "fullscreen", "|", "guide"], |
153 |
| - hideIcons: ["guide", "heading", "preview", "side-by-side"], |
154 |
| - insertTexts: { |
155 |
| - horizontalRule: ["", "\n\n-----\n\n"], |
156 |
| - image: [""], |
157 |
| - link: ["[", "](https://)"], |
158 |
| - table: ["", "\n\n| Column 1 | Column 2 | Column 3 |\n| -------- | -------- | -------- |\n| Text | Text | Text |\n\n"], |
159 |
| - }, |
160 |
| - // uploadImage: false by default, If set to true, enables the image upload functionality, which can be triggered by drag&drop, copy-paste and through the browse-file window (opened when the user click on the upload-image icon). Defaults to false. |
161 |
| - // imageMaxSize: Maximum image size in bytes, checked before upload (note: never trust client, always check image size at server-side). Defaults to 1024*1024*2 (2Mb). |
162 |
| - // imageAccept: A comma-separated list of mime-types used to check image type before upload (note: never trust client, always check file types at server-side). Defaults to image/png, image/jpeg. |
163 |
| - // imageUploadEndpoint: The endpoint where the images data will be sent, via an asynchronous POST request |
164 |
| - // imageTexts: |
165 |
| - // errorMessages: Errors displayed to the user, using the errorCallback option, |
166 |
| - // errorCallback: A callback function used to define how to display an error message. |
167 |
| - // renderingConfig: Adjust settings for parsing the Markdown during previewing (not editing) |
168 |
| - // showIcons: An array of icon names to show. Can be used to show specific icons hidden by default without completely customizing the toolbar. |
169 |
| - // sideBySideFullscreen: If set to false, allows side-by-side editing without going into fullscreen. Defaults to true. |
170 |
| - //theme: Override the theme. Defaults to easymde. |
171 |
| - }); |
172 |
| - |
173 |
| - // forceSync = true, need to clear form after async requests |
174 |
| - $currentEditableTextarea.closest('form').on('complete', function(frm, btn) { |
175 |
| - editor.codemirror.setValue(''); |
176 |
| - }); |
| 130 | + var editorInit = function (textareaObj) { |
| 131 | + var $currentEditableTextarea = $(textareaObj); |
| 132 | + var $postForm = $(textareaObj).closest('form'); |
| 133 | + var currentFormFormat = $postForm.find('input[name="Format"]'); |
| 134 | + var currentTextBoxWrapper; // div wrapper |
| 135 | + |
| 136 | + // TODO: how many formats |
| 137 | + if (currentFormFormat.length) { |
| 138 | + // TODO: |
| 139 | + // there might be different formats if there are several comments |
| 140 | + currentFormFormat = currentFormFormat[0].value.toLowerCase(); |
| 141 | + } |
| 142 | + |
| 143 | + logMessage('The default format is '+ editorInputFormat); |
| 144 | + logMessage('The form format is '+ JSON.stringify(currentFormFormat)); |
| 145 | + |
| 146 | + currentTextBoxWrapper = $currentEditableTextarea.parent('.TextBoxWrapper'); |
| 147 | + // If singleInstance is false, then odds are the editor is being |
| 148 | + // loaded inline and there are other instances on page. |
| 149 | + var singleInstance = true; |
| 150 | + |
| 151 | + // Determine if editing a comment, or not. When editing a comment, |
| 152 | + // it has a comment id, while adding a new comment has an empty |
| 153 | + // comment id. The value is a hidden input. |
| 154 | + var commentId = $postForm.find('#Form_CommentID').val(); |
| 155 | + logMessage('CommentID='+commentId); |
| 156 | + |
| 157 | + if (typeof commentId != 'undefined' && commentId != '') { |
| 158 | + singleInstance = false; |
| 159 | + } |
| 160 | + |
| 161 | + logMessage('isSingleInstance='+singleInstance); |
| 162 | + |
| 163 | + if ($currentEditableTextarea.length) { |
| 164 | + // instantiate new editor |
| 165 | + var editor = new EasyMDE({ |
| 166 | + shortcuts: { |
| 167 | + "mentions": "Ctrl-Space", |
| 168 | + }, |
| 169 | + autofocus: false, |
| 170 | + forceSync: true, // true, force text changes made in EasyMDE to be immediately stored in original text area. |
| 171 | + placeholder: '', |
| 172 | + element: $currentEditableTextarea[0], |
| 173 | + hintOptions: { hint: topcoderHandles }, |
| 174 | + // toolbar: topcoderEditorToolbar, |
| 175 | + toolbar: ["bold", "italic", "strikethrough", "|", |
| 176 | + "heading-1", "heading-2", "heading-3", "|", "code", "quote", "|", "unordered-list", |
| 177 | + "ordered-list", "clean-block", "|", { |
| 178 | + name: "mentions", |
| 179 | + action: function mentions (editor) { |
| 180 | + completeAfter(editor.codemirror); |
| 181 | + }, |
| 182 | + className: "fa fa-at", |
| 183 | + title: "Mention a Topcoder User", |
| 184 | + |
| 185 | + }, "link", "image", "table", "horizontal-rule", "|", "fullscreen", "|", "guide"], |
| 186 | + hideIcons: ["guide", "heading", "preview", "side-by-side"], |
| 187 | + insertTexts: { |
| 188 | + horizontalRule: ["", "\n\n-----\n\n"], |
| 189 | + image: [""], |
| 190 | + link: ["[", "](https://)"], |
| 191 | + table: ["", "\n\n| Column 1 | Column 2 | Column 3 |\n| -------- | -------- | -------- |\n| Text | Text | Text |\n\n"], |
| 192 | + }, |
| 193 | + // uploadImage: false by default, If set to true, enables the image upload functionality, which can be triggered by drag&drop, copy-paste and through the browse-file window (opened when the user click on the upload-image icon). Defaults to false. |
| 194 | + // imageMaxSize: Maximum image size in bytes, checked before upload (note: never trust client, always check image size at server-side). Defaults to 1024*1024*2 (2Mb). |
| 195 | + // imageAccept: A comma-separated list of mime-types used to check image type before upload (note: never trust client, always check file types at server-side). Defaults to image/png, image/jpeg. |
| 196 | + // imageUploadEndpoint: The endpoint where the images data will be sent, via an asynchronous POST request |
| 197 | + // imageTexts: |
| 198 | + // errorMessages: Errors displayed to the user, using the errorCallback option, |
| 199 | + // errorCallback: A callback function used to define how to display an error message. |
| 200 | + // renderingConfig: Adjust settings for parsing the Markdown during previewing (not editing) |
| 201 | + // showIcons: An array of icon names to show. Can be used to show specific icons hidden by default without completely customizing the toolbar. |
| 202 | + // sideBySideFullscreen: If set to false, allows side-by-side editing without going into fullscreen. Defaults to true. |
| 203 | + //theme: Override the theme. Defaults to easymde. |
| 204 | + }); |
| 205 | + |
| 206 | + // forceSync = true, need to clear form after async requests |
| 207 | + $currentEditableTextarea.closest('form').on('complete', function (frm, btn) { |
| 208 | + editor.codemirror.setValue(''); |
| 209 | + }); |
177 | 210 |
|
178 | 211 | editor.codemirror.on('change', function (cm, changeObj){
|
179 | 212 | // logMessage('onChange:'+cm.getCursor().ch);
|
180 | 213 | });
|
181 | 214 |
|
182 |
| - editor.codemirror.on('keydown', function (cm, event){ |
183 |
| - if (!cm.state.completionActive /*Enables keyboard navigation in autocomplete list*/) { |
184 |
| - if(event.key == '@') { |
185 |
| - var currentCursorPosition = cm.getCursor(); |
186 |
| - if(currentCursorPosition.ch === 0) { |
187 |
| - cm.showHint({ completeSingle: false, alignWithWord: true }); |
188 |
| - return; |
189 |
| - } |
190 |
| - |
191 |
| - var backwardCursorPosition = { |
192 |
| - line: currentCursorPosition.line, |
193 |
| - ch: currentCursorPosition.ch - 1 |
194 |
| - }; |
195 |
| - var backwardCharacter = cm.getRange(backwardCursorPosition, currentCursorPosition); |
196 |
| - if (backwardCharacter === ' ') { // space |
197 |
| - cm.showHint({ completeSingle: false, alignWithWord: true }); |
| 215 | + editor.codemirror.on('keydown', function (cm, event) { |
| 216 | + if (!cm.state.completionActive /*Enables keyboard navigation in autocomplete list*/) { |
| 217 | + if (event.key == '@') { |
| 218 | + var currentCursorPosition = cm.getCursor(); |
| 219 | + if (currentCursorPosition.ch === 0) { |
| 220 | + cm.showHint({ completeSingle: false, alignWithWord: true }); |
| 221 | + return; |
| 222 | + } |
| 223 | + |
| 224 | + var backwardCursorPosition = { |
| 225 | + line: currentCursorPosition.line, |
| 226 | + ch: currentCursorPosition.ch - 1 |
| 227 | + }; |
| 228 | + var backwardCharacter = cm.getRange(backwardCursorPosition, currentCursorPosition); |
| 229 | + if (backwardCharacter === ' ') { // space |
| 230 | + cm.showHint({ completeSingle: false, alignWithWord: true }); |
| 231 | + } |
198 | 232 | }
|
199 | 233 | }
|
200 |
| - } |
201 |
| - }); |
202 |
| - } |
203 |
| - } //editorInit |
| 234 | + }); |
| 235 | + } |
204 | 236 |
|
205 |
| - editorInit(this); |
| 237 | + }; //editorInit |
206 | 238 |
|
| 239 | + editorInit(this); |
207 | 240 | // jQuery chaining
|
208 | 241 | return this;
|
209 | 242 | };
|
210 | 243 |
|
211 | 244 | $(document).on('contentLoad', function(e) {
|
212 |
| - if ($('textarea#Form_Body', e.target).length === 0) { |
213 |
| - console.log('Couldn\'t load EasyMDE: missing #Form_Body'); |
| 245 | + if ($('.BodyBox[format="Markdown"], .BodyBox[format="wysiwyg"],.js-bodybox[format="Markdown"], .js-bodybox[format="wysiwyg"]', e.target).length === 0) { |
| 246 | + console.log('Supported only [format="Markdown"][format="wysiwyg"]'); |
214 | 247 | return;
|
215 | 248 | }
|
216 |
| - // Vanilla Form |
217 |
| - $('textarea#Form_Body', e.target).setAsEditor(); |
| 249 | + // Multiple editors are supported on a page |
| 250 | + $('.BodyBox[format="Markdown"], .BodyBox[format="wysiwyg"],.js-bodybox[format="Markdown"], .js-bodybox[format="wysiwyg"]', e.target).setAsEditor(); |
218 | 251 | });
|
219 | 252 | }(jQuery));
|
0 commit comments