@@ -9,6 +9,7 @@ import {emojiString} from '../emoji.js';
9
9
import { renderPreviewPanelContent } from '../repo-editor.js' ;
10
10
import { matchEmoji , matchMention } from '../../utils/match.js' ;
11
11
import { svg } from '../../svg.js' ;
12
+ import { easyMDEToolbarActions } from './EasyMDEToolbarActions.js' ;
12
13
13
14
let elementIdCounter = 0 ;
14
15
@@ -42,6 +43,7 @@ class ComboMarkdownEditor {
42
43
}
43
44
44
45
async init ( ) {
46
+ this . prepareEasyMDEToolbarActions ( ) ;
45
47
this . setupTab ( ) ;
46
48
this . setupDropzone ( ) ;
47
49
this . setupTextarea ( ) ;
@@ -203,177 +205,22 @@ class ComboMarkdownEditor {
203
205
} ) ;
204
206
}
205
207
206
- prepareEasyMDEToolbarActions ( EasyMDE , isWiki ) {
208
+ prepareEasyMDEToolbarActions ( ) {
207
209
this . easyMDEToolbarDefault = [
208
- {
209
- name : 'heading-1' ,
210
- action : EasyMDE . toggleHeading1 ,
211
- icon : svg ( 'octicon-heading' ) ,
212
- title : 'Heading' ,
213
- } ,
214
- {
215
- name : 'heading-2' ,
216
- action : EasyMDE . toggleHeading2 ,
217
- icon : svg ( 'octicon-heading' ) ,
218
- title : 'Heading' ,
219
- } ,
220
- {
221
- name : 'heading-3' ,
222
- action : EasyMDE . toggleHeading3 ,
223
- icon : svg ( 'octicon-heading' ) ,
224
- title : 'Heading' ,
225
- } ,
226
- '|' ,
227
- {
228
- name : 'bold' ,
229
- action : EasyMDE . toggleBold ,
230
- icon : svg ( 'octicon-bold' ) ,
231
- title : 'Bold' ,
232
- } ,
233
- {
234
- name : 'italic' ,
235
- action : EasyMDE . toggleItalic ,
236
- icon : svg ( 'octicon-italic' ) ,
237
- title : 'Italic' ,
238
- } ,
239
- {
240
- name : 'strikethrough' ,
241
- action : EasyMDE . toggleStrikethrough ,
242
- icon : svg ( 'octicon-strikethrough' ) ,
243
- title : 'Strikethrough' ,
244
- } ,
245
- '|' ,
246
- {
247
- name : 'quote' ,
248
- action : EasyMDE . toggleBlockquote ,
249
- icon : svg ( 'octicon-quote' ) ,
250
- title : 'Quote' ,
251
- } ,
252
- isWiki && 'gitea-code-inline' ,
253
- {
254
- name : 'code' ,
255
- action : EasyMDE . toggleCodeBlock ,
256
- icon : svg ( 'octicon-code' ) ,
257
- title : 'Code' ,
258
- } ,
259
- {
260
- name : 'link' ,
261
- action : EasyMDE . drawLink ,
262
- icon : svg ( 'octicon-link' ) ,
263
- title : 'Link' ,
264
- } ,
265
- '|' ,
266
- {
267
- name : 'unordered-list' ,
268
- action : EasyMDE . toggleUnorderedList ,
269
- icon : svg ( 'octicon-list-unordered' ) ,
270
- title : 'Unordered List' ,
271
- } ,
272
- {
273
- name : 'ordered-list' ,
274
- action : EasyMDE . toggleOrderedList ,
275
- icon : svg ( 'octicon-list-ordered' ) ,
276
- title : 'Ordered List' ,
277
- } ,
278
- '|' ,
279
- 'gitea-checkbox-empty' ,
280
- 'gitea-checkbox-checked' ,
281
- '|' ,
282
- {
283
- name : 'image' ,
284
- action : EasyMDE . drawImage ,
285
- icon : svg ( 'octicon-image' ) ,
286
- title : 'Image' ,
287
- } ,
288
- {
289
- name : 'table' ,
290
- action : EasyMDE . drawTable ,
291
- icon : svg ( 'octicon-table' ) ,
292
- title : 'Table' ,
293
- } ,
294
- {
295
- name : 'horizontal-rule' ,
296
- action : EasyMDE . drawHorizontalRule ,
297
- icon : svg ( 'octicon-horizontal-rule' ) ,
298
- title : 'Horizontal Rule' ,
299
- } ,
300
- isWiki && '|' ,
301
- isWiki && {
302
- name : 'preview' ,
303
- action : EasyMDE . togglePreview ,
304
- icon : svg ( 'octicon-eye' ) ,
305
- title : 'Preview' ,
306
- } ,
307
- isWiki && {
308
- name : 'fullscreen' ,
309
- action : EasyMDE . toggleFullScreen ,
310
- icon : svg ( 'octicon-screen-full' ) ,
311
- title : 'Fullscreen' ,
312
- } ,
313
- isWiki && {
314
- name : 'side-by-side' ,
315
- action : EasyMDE . toggleSideBySide ,
316
- icon : svg ( 'octicon-columns' ) ,
317
- title : 'Side by Side' ,
318
- } ,
319
- '|' ,
210
+ 'bold' , 'italic' , 'strikethrough' , '|' , 'heading-1' , 'heading-2' , 'heading-3' , '|' ,
211
+ 'code' , 'quote' , '|' , 'gitea-checkbox-empty' , 'gitea-checkbox-checked' , '|' ,
212
+ 'unordered-list' , 'ordered-list' , '|' , 'link' , 'image' , 'table' , 'horizontal-rule' , '|' ,
320
213
'gitea-switch-to-textarea' ,
321
- ] . filter ( Boolean ) ;
322
-
323
- this . easyMDEToolbarActions = {
324
- 'gitea-checkbox-empty' : {
325
- action ( e ) {
326
- const cm = e . codemirror ;
327
- cm . replaceSelection ( `\n- [ ] ${ cm . getSelection ( ) } ` ) ;
328
- cm . focus ( ) ;
329
- } ,
330
- icon : svg ( 'gitea-empty-checkbox' ) ,
331
- title : 'Add Checkbox (empty)' ,
332
- } ,
333
- 'gitea-checkbox-checked' : {
334
- action ( e ) {
335
- const cm = e . codemirror ;
336
- cm . replaceSelection ( `\n- [x] ${ cm . getSelection ( ) } ` ) ;
337
- cm . focus ( ) ;
338
- } ,
339
- icon : svg ( 'octicon-checkbox' ) ,
340
- title : 'Add Checkbox (checked)' ,
341
- } ,
342
- 'gitea-switch-to-textarea' : {
343
- action : ( ) => {
344
- this . userPreferredEditor = 'textarea' ;
345
- this . switchToTextarea ( ) ;
346
- } ,
347
- icon : svg ( 'octicon-arrow-switch' ) ,
348
- title : 'Revert to simple textarea' ,
349
- } ,
350
- 'gitea-code-inline' : {
351
- action ( e ) {
352
- const cm = e . codemirror ;
353
- const selection = cm . getSelection ( ) ;
354
- cm . replaceSelection ( `\`${ selection } \`` ) ;
355
- if ( ! selection ) {
356
- const cursorPos = cm . getCursor ( ) ;
357
- cm . setCursor ( cursorPos . line , cursorPos . ch - 1 ) ;
358
- }
359
- cm . focus ( ) ;
360
- } ,
361
- icon : svg ( 'octicon-chevron-right' ) ,
362
- title : 'Add Inline Code' ,
363
- }
364
- } ;
214
+ ] ;
365
215
}
366
216
367
- parseEasyMDEToolbar ( actions ) {
217
+ parseEasyMDEToolbar ( EasyMDE , actions ) {
218
+ this . easyMDEToolbarActions = this . easyMDEToolbarActions || easyMDEToolbarActions ( EasyMDE , this ) ;
368
219
const processed = [ ] ;
369
220
for ( const action of actions ) {
370
- if ( typeof action === 'string' && action . startsWith ( 'gitea-' ) ) {
371
- const giteaAction = this . easyMDEToolbarActions [ action ] ;
372
- if ( ! giteaAction ) throw new Error ( `Unknown EasyMDE toolbar action ${ action } ` ) ;
373
- processed . push ( giteaAction ) ;
374
- } else {
375
- processed . push ( action ) ;
376
- }
221
+ const actionButton = this . easyMDEToolbarActions [ action ] ;
222
+ if ( ! actionButton ) throw new Error ( `Unknown EasyMDE toolbar action ${ action } ` ) ;
223
+ processed . push ( actionButton ) ;
377
224
}
378
225
return processed ;
379
226
}
@@ -389,9 +236,6 @@ class ComboMarkdownEditor {
389
236
async switchToEasyMDE ( ) {
390
237
// EasyMDE's CSS should be loaded via webpack config, otherwise our own styles can not overwrite the default styles.
391
238
const { default : EasyMDE } = await import ( /* webpackChunkName: "easymde" */ 'easymde' ) ;
392
-
393
- this . prepareEasyMDEToolbarActions ( EasyMDE , this . options . easyMDEOptions . isWiki ) ;
394
-
395
239
const easyMDEOpt = {
396
240
autoDownloadFontAwesome : false ,
397
241
element : this . textarea ,
@@ -404,7 +248,7 @@ class ComboMarkdownEditor {
404
248
nativeSpellcheck : true ,
405
249
...this . options . easyMDEOptions ,
406
250
} ;
407
- easyMDEOpt . toolbar = this . parseEasyMDEToolbar ( easyMDEOpt . toolbar ?? this . easyMDEToolbarDefault ) ;
251
+ easyMDEOpt . toolbar = this . parseEasyMDEToolbar ( EasyMDE , easyMDEOpt . toolbar ?? this . easyMDEToolbarDefault ) ;
408
252
409
253
this . easyMDE = new EasyMDE ( easyMDEOpt ) ;
410
254
this . easyMDE . codemirror . on ( 'change' , ( ...args ) => { this . options ?. onContentChanged ?. ( this , ...args ) } ) ;
0 commit comments