@@ -31,49 +31,44 @@ function clipboardPastedImages(e) {
31
31
32
32
33
33
function insertAtCursor ( field , value ) {
34
- if ( field . selectionStart || field . selectionStart === 0 ) {
35
- const startPos = field . selectionStart ;
36
- const endPos = field . selectionEnd ;
37
- field . value = field . value . substring ( 0 , startPos ) + value + field . value . substring ( endPos , field . value . length ) ;
38
- field . selectionStart = startPos + value . length ;
39
- field . selectionEnd = startPos + value . length ;
34
+ if ( field . getTextArea ( ) . selectionStart || field . getTextArea ( ) . selectionStart === 0 ) {
35
+ const startPos = field . getTextArea ( ) . selectionStart ;
36
+ const endPos = field . getTextArea ( ) . selectionEnd ;
37
+ field . setValue ( field . getValue ( ) . substring ( 0 , startPos ) + value + field . getValue ( ) . substring ( endPos , field . getValue ( ) . length ) ) ;
38
+ field . getTextArea ( ) . electionStart = startPos + value . length ;
39
+ field . getTextArea ( ) . selectionEnd = startPos + value . length ;
40
40
} else {
41
- field . value += value ;
41
+ field . setValue ( field . getValue ( ) + value ) ;
42
42
}
43
43
}
44
44
45
45
function replaceAndKeepCursor ( field , oldval , newval ) {
46
- if ( field . selectionStart || field . selectionStart === 0 ) {
47
- const startPos = field . selectionStart ;
48
- const endPos = field . selectionEnd ;
49
- field . value = field . value . replace ( oldval , newval ) ;
50
- field . selectionStart = startPos + newval . length - oldval . length ;
51
- field . selectionEnd = endPos + newval . length - oldval . length ;
46
+ if ( field . getTextArea ( ) . selectionStart || field . getTextArea ( ) . selectionStart === 0 ) {
47
+ const startPos = field . getTextArea ( ) . selectionStart ;
48
+ const endPos = field . getTextArea ( ) . selectionEnd ;
49
+ field . setValue ( field . getValue ( ) . replace ( oldval , newval ) ) ;
50
+ field . getTextArea ( ) . selectionStart = startPos + newval . length - oldval . length ;
51
+ field . getTextArea ( ) . selectionEnd = endPos + newval . length - oldval . length ;
52
52
} else {
53
- field . value = field . value . replace ( oldval , newval ) ;
53
+ field . setValue ( field . getValue ( ) . replace ( oldval , newval ) ) ;
54
54
}
55
55
}
56
56
57
57
export function initCompImagePaste ( $target ) {
58
- $target . each ( function ( ) {
59
- const dropzone = this . querySelector ( '.dropzone' ) ;
60
- if ( ! dropzone ) {
61
- return ;
62
- }
63
- const uploadUrl = dropzone . getAttribute ( 'data-upload-url' ) ;
64
- const dropzoneFiles = dropzone . querySelector ( '.files' ) ;
65
- for ( const textarea of this . querySelectorAll ( 'textarea' ) ) {
66
- textarea . addEventListener ( 'paste' , async ( e ) => {
67
- for ( const img of clipboardPastedImages ( e ) ) {
68
- const name = img . name . slice ( 0 , img . name . lastIndexOf ( '.' ) ) ;
69
- insertAtCursor ( textarea , `![${ name } ]()` ) ;
70
- const data = await uploadFile ( img , uploadUrl ) ;
71
- replaceAndKeepCursor ( textarea , `![${ name } ]()` , `` ) ;
72
- const input = $ ( `<input id="${ data . uuid } " name="files" type="hidden">` ) . val ( data . uuid ) ;
73
- dropzoneFiles . appendChild ( input [ 0 ] ) ;
74
- }
75
- } , false ) ;
76
- }
58
+ const dropzone = $target [ 0 ] . querySelector ( '.dropzone' ) ;
59
+ if ( ! dropzone ) {
60
+ return ;
61
+ }
62
+ const uploadUrl = dropzone . getAttribute ( 'data-upload-url' ) ;
63
+ const dropzoneFiles = dropzone . querySelector ( '.files' ) ;
64
+ $ ( document ) . on ( 'paste' , '.CodeMirror' , async function ( e ) {
65
+ const img = clipboardPastedImages ( e . originalEvent ) ;
66
+ const name = img [ 0 ] . name . substring ( 0 , img [ 0 ] . name . lastIndexOf ( '.' ) ) ;
67
+ insertAtCursor ( this . CodeMirror , `![${ name } ]()` ) ;
68
+ const data = await uploadFile ( img [ 0 ] , uploadUrl ) ;
69
+ replaceAndKeepCursor ( this . CodeMirror , `![${ name } ]()` , `` ) ;
70
+ const input = $ ( `<input id="${ data . uuid } " name="files" type="hidden">` ) . val ( data . uuid ) ;
71
+ dropzoneFiles . appendChild ( input [ 0 ] ) ;
77
72
} ) ;
78
73
}
79
74
0 commit comments