1
1
import '@github/markdown-toolbar-element' ;
2
+ import $ from 'jquery' ;
2
3
import { attachTribute } from '../tribute.js' ;
3
4
import { hideElem , showElem } from '../../utils/dom.js' ;
4
5
import { initEasyMDEImagePaste , initTextareaImagePaste } from './ImagePaste.js' ;
5
- import $ from 'jquery' ;
6
6
import { initMarkupContent } from '../../markup/content.js' ;
7
7
import { handleGlobalEnterQuickSubmit } from './QuickSubmit.js' ;
8
8
import { attachRefIssueContextPopup } from '../contextpopup.js' ;
@@ -39,31 +39,57 @@ class ComboMarkdownEditor {
39
39
}
40
40
41
41
async init ( ) {
42
+ this . prepareEasyMDEToolbarActions ( ) ;
43
+
44
+ this . setupTab ( ) ;
45
+ this . setupDropzone ( ) ;
46
+
47
+ this . setupTextarea ( ) ;
48
+
49
+ await attachTribute ( this . textarea , { mentions : true , emoji : true } ) ;
50
+
51
+ if ( this . userPreferredEditor === 'easymde' ) {
52
+ await this . switchToEasyMDE ( ) ;
53
+ }
54
+ }
55
+
56
+ applyEditorHeights ( el , heights ) {
57
+ if ( ! heights ) return ;
58
+ if ( heights . minHeight ) el . style . minHeight = heights . minHeight ;
59
+ if ( heights . height ) el . style . height = heights . height ;
60
+ if ( heights . maxHeight ) el . style . maxHeight = heights . maxHeight ;
61
+ }
62
+
63
+ setupTextarea ( ) {
42
64
this . textarea = this . container . querySelector ( '.markdown-text-editor' ) ;
43
65
this . textarea . _giteaComboMarkdownEditor = this ;
44
- this . textarea . id = `_combo_markdown_editor_${ String ( elementIdCounter ) } ` ;
45
- this . textarea . addEventListener ( 'input' , ( e ) => { this . options ?. onContentChanged ?. ( this , e ) } ) ;
66
+ this . textarea . id = `_combo_markdown_editor_${ String ( elementIdCounter ++ ) } ` ;
67
+ this . textarea . addEventListener ( 'input' , ( e ) => {
68
+ this . textareaAutoResize ( ) ;
69
+ this . options ?. onContentChanged ?. ( this , e ) ;
70
+ } ) ;
71
+ this . applyEditorHeights ( this . textarea , this . options . editorHeights ) ;
72
+ this . textareaAutoResize ( ) ;
46
73
this . textareaMarkdownToolbar = this . container . querySelector ( 'markdown-toolbar' ) ;
47
74
this . textareaMarkdownToolbar . setAttribute ( 'for' , this . textarea . id ) ;
48
75
49
- elementIdCounter ++ ;
50
-
51
76
this . switchToEasyMDEButton = this . container . querySelector ( '.markdown-switch-easymde' ) ;
52
77
this . switchToEasyMDEButton ?. addEventListener ( 'click' , async ( e ) => {
53
78
e . preventDefault ( ) ;
79
+ this . userPreferredEditor = 'easymde' ;
54
80
await this . switchToEasyMDE ( ) ;
55
81
} ) ;
56
82
57
- await attachTribute ( this . textarea , { mentions : true , emoji : true } ) ;
83
+ if ( this . dropzone ) {
84
+ initTextareaImagePaste ( this . textarea , this . dropzone ) ;
85
+ }
86
+ }
58
87
88
+ setupDropzone ( ) {
59
89
const dropzoneParentContainer = this . container . getAttribute ( 'data-dropzone-parent-container' ) ;
60
90
if ( dropzoneParentContainer ) {
61
91
this . dropzone = this . container . closest ( this . container . getAttribute ( 'data-dropzone-parent-container' ) ) ?. querySelector ( '.dropzone' ) ;
62
- initTextareaImagePaste ( this . textarea , this . dropzone ) ;
63
92
}
64
-
65
- this . setupTab ( ) ;
66
- this . prepareEasyMDEToolbarActions ( ) ;
67
93
}
68
94
69
95
setupTab ( ) {
@@ -134,7 +160,10 @@ class ComboMarkdownEditor {
134
160
title : 'Add Checkbox (checked)' ,
135
161
} ,
136
162
'gitea-switch-to-textarea' : {
137
- action : this . switchToTextarea . bind ( this ) ,
163
+ action : ( ) => {
164
+ this . userPreferredEditor = 'textarea' ;
165
+ this . switchToTextarea ( ) ;
166
+ } ,
138
167
className : 'fa fa-file' ,
139
168
title : 'Revert to simple textarea' ,
140
169
} ,
@@ -169,7 +198,7 @@ class ComboMarkdownEditor {
169
198
return processed ;
170
199
}
171
200
172
- async switchToTextarea ( ) {
201
+ switchToTextarea ( ) {
173
202
showElem ( this . textareaMarkdownToolbar ) ;
174
203
if ( this . easyMDE ) {
175
204
this . easyMDE . toTextArea ( ) ;
@@ -218,6 +247,7 @@ class ComboMarkdownEditor {
218
247
}
219
248
} ,
220
249
} ) ;
250
+ this . applyEditorHeights ( this . container . querySelector ( '.CodeMirror-scroll' ) , this . options . editorHeights ) ;
221
251
await attachTribute ( this . easyMDE . codemirror . getInputField ( ) , { mentions : true , emoji : true } ) ;
222
252
initEasyMDEImagePaste ( this . easyMDE , this . dropzone ) ;
223
253
hideElem ( this . textareaMarkdownToolbar ) ;
@@ -235,6 +265,7 @@ class ComboMarkdownEditor {
235
265
this . easyMDE . value ( v ) ;
236
266
} else {
237
267
this . textarea . value = v ;
268
+ this . textareaAutoResize ( ) ;
238
269
}
239
270
}
240
271
@@ -246,6 +277,24 @@ class ComboMarkdownEditor {
246
277
}
247
278
}
248
279
280
+ textareaAutoResize ( ) {
281
+ if ( this . textareaInitalHeight === undefined ) {
282
+ this . textareaInitalHeight = this . textarea . offsetHeight ;
283
+ }
284
+ const offset = this . textarea . offsetHeight - this . textarea . clientHeight ;
285
+ if ( ! this . lastValue || Math . abs ( this . lastValue . length - this . textarea . value . length ) > 2 ) {
286
+ // the value has changed a lot, so reset the height to calculate the real scroll height, it might cause UI flickering
287
+ this . textarea . style . height = 'auto' ;
288
+ } else {
289
+ // try to shrink a little to see if a line is deleted (since the value doesn't change much), it won't cause UI flickering
290
+ // the magic number is a general number which fits most line-height styles.
291
+ this . textarea . style . height = `${ this . textarea . scrollHeight + offset - 40 } px` ;
292
+ }
293
+ // make sure the height is not smaller than the initial height
294
+ this . textarea . style . height = `${ Math . max ( this . textareaInitalHeight , this . textarea . scrollHeight + offset ) } px` ;
295
+ this . lastValue = this . textarea . value ;
296
+ }
297
+
249
298
moveCursorToEnd ( ) {
250
299
this . textarea . focus ( ) ;
251
300
this . textarea . setSelectionRange ( this . textarea . value . length , this . textarea . value . length ) ;
@@ -254,6 +303,13 @@ class ComboMarkdownEditor {
254
303
this . easyMDE . codemirror . setCursor ( this . easyMDE . codemirror . lineCount ( ) , 0 ) ;
255
304
}
256
305
}
306
+
307
+ get userPreferredEditor ( ) {
308
+ return window . localStorage . getItem ( `markdown-editor-${ this . options . useScene ?? 'default' } ` ) ;
309
+ }
310
+ set userPreferredEditor ( s ) {
311
+ window . localStorage . setItem ( `markdown-editor-${ this . options . useScene ?? 'default' } ` , s ) ;
312
+ }
257
313
}
258
314
259
315
export function getComboMarkdownEditor ( el ) {
0 commit comments