@@ -110,6 +110,7 @@ export class ComposeInputModule extends ViewModule<ComposeView> {
110
110
this . squire = new window . Squire ( el , { addLinks } ) ;
111
111
this . initShortcuts ( ) ;
112
112
this . handlePaste ( ) ;
113
+ this . handleDragImages ( ) ;
113
114
this . handlePasteImages ( ) ;
114
115
this . resizeReplyBox ( ) ;
115
116
this . scrollIntoView ( ) ;
@@ -136,35 +137,51 @@ export class ComposeInputModule extends ViewModule<ComposeView> {
136
137
} ) ;
137
138
} ;
138
139
139
- private handlePasteImages = ( ) => {
140
+ private loadImageFromFile = ( file : File , callback : ( result : string ) => void ) => {
141
+ const reader = new FileReader ( ) ;
142
+ reader . onload = ( ) => callback ( reader . result as string ) ;
143
+ reader . readAsDataURL ( file ) ;
144
+ } ;
145
+
146
+ private insertImageIntoSquire = ( imageData : string , name : string ) => {
147
+ try {
148
+ this . squire . insertImage ( imageData , { name, title : name } ) ;
149
+ this . view . draftModule . draftSave ( ) . catch ( Catch . reportErr ) ;
150
+ } catch ( e ) {
151
+ Catch . reportErr ( e ) ;
152
+ }
153
+ } ;
154
+
155
+ private handleDragImages = ( ) => {
140
156
this . squire . addEventListener ( 'drop' , ( ev : DragEvent ) => {
141
- try {
142
- if ( ! this . isRichText ( ) ) {
143
- return ;
144
- }
145
- if ( ! ev . dataTransfer ?. files . length ) {
146
- return ;
147
- }
148
- const file = ev . dataTransfer . files [ 0 ] ;
149
- const reader = new FileReader ( ) ;
150
- reader . onload = ( ) => {
151
- try {
152
- this . squire . insertImage ( reader . result ?. toString ( ) ?? '' , { name : file . name , title : file . name } ) ;
153
- this . view . draftModule . draftSave ( ) . catch ( Catch . reportErr ) ;
154
- } catch ( e ) {
155
- Catch . reportErr ( e ) ;
156
- }
157
- } ;
158
- reader . readAsDataURL ( file ) ;
159
- } catch ( e ) {
160
- Catch . reportErr ( e ) ;
157
+ if ( ! this . isRichText ( ) || ! ev . dataTransfer ?. files . length ) {
158
+ return ;
161
159
}
160
+ const file = ev . dataTransfer . files [ 0 ] ;
161
+ this . loadImageFromFile ( file , imageData => {
162
+ this . insertImageIntoSquire ( imageData , file . name ) ;
163
+ } ) ;
162
164
} ) ;
163
165
this . squire . addEventListener ( 'dragover' , ( e : DragEvent ) => {
164
166
e . preventDefault ( ) ; // this is needed for 'drop' event to fire
165
167
} ) ;
166
168
} ;
167
169
170
+ private handlePasteImages = ( ) => {
171
+ this . squire . addEventListener ( 'pasteImage' , ( ev : Event & { detail : { clipboardData : DataTransfer } } ) => {
172
+ if ( ! this . isRichText ( ) ) return ;
173
+ const items = Array . from ( ev . detail . clipboardData ?. items ?? [ ] ) ;
174
+ const imageItem = items . find ( item => / i m a g e / . test ( item . type ) ) ;
175
+
176
+ const imageFile = imageItem ?. getAsFile ( ) ;
177
+ if ( imageItem && imageFile ) {
178
+ this . loadImageFromFile ( imageFile , imageData => {
179
+ this . insertImageIntoSquire ( imageData , 'Pasted Image' ) ;
180
+ } ) ;
181
+ }
182
+ } ) ;
183
+ } ;
184
+
168
185
private handleRTL = ( ) => {
169
186
const checkRTL = ( ) => {
170
187
let container = $ ( this . squire . getSelection ( ) . commonAncestorContainer ) ;
0 commit comments