@@ -258,17 +258,46 @@ var app = {
258258 } ) ;
259259 } ,
260260 initClipboardPasteToUpload : function ( ) {
261- document . onpaste = function ( event ) {
262- if ( event . clipboardData || event . originalEvent . clipboardData ) {
263- const items = ( event . clipboardData || event . originalEvent . clipboardData ) . items ;
264- items . forEach ( ( item ) => {
265- if ( item . kind === 'file' ) {
266- // Add the file to the dropzone instance.
267- Dropzone . forElement ( '.dropzone' ) . addFile ( item . getAsFile ( ) ) ;
268- }
269- } ) ;
261+ // Attach a paste listener that uploads image/video files from the clipboard
262+ document . addEventListener ( 'paste' , function ( event ) {
263+ // Do not intercept paste if user is typing in an input/textarea/contenteditable
264+ var ae = document . activeElement ;
265+ if ( ae && ( ( ae . tagName && ( ae . tagName . toLowerCase ( ) === 'input' || ae . tagName . toLowerCase ( ) === 'textarea' ) ) || ae . isContentEditable ) ) {
266+ return ;
267+ }
268+
269+ var clipboard = event . clipboardData || ( event . originalEvent && event . originalEvent . clipboardData ) ;
270+ if ( ! clipboard || ! clipboard . items || clipboard . items . length === 0 ) {
271+ return ;
272+ }
273+
274+ // Try to resolve the active Dropzone instance in a safe way
275+ var dz = null ;
276+ try {
277+ dz = Dropzone . forElement ( '#upload-dropzone' ) ;
278+ } catch ( e ) {
279+ if ( Dropzone && Dropzone . instances && Dropzone . instances . length > 0 ) {
280+ dz = Dropzone . instances [ 0 ] ;
281+ }
282+ }
283+ if ( ! dz ) {
284+ return ; // No Dropzone available; nothing to do
285+ }
286+
287+ // Iterate items in a cross-browser way (DataTransferItemList may not support forEach)
288+ for ( var i = 0 ; i < clipboard . items . length ; i ++ ) {
289+ var item = clipboard . items [ i ] ;
290+ if ( ! item ) continue ;
291+ if ( item . kind === 'file' ) {
292+ var file = item . getAsFile ( ) ;
293+ if ( ! file ) continue ;
294+ // Only process images and videos to avoid surprising uploads
295+ if ( ( file . type && file . type . indexOf ( 'image/' ) === 0 ) || ( file . type && file . type . indexOf ( 'video/' ) === 0 ) ) {
296+ dz . addFile ( file ) ;
297+ }
298+ }
270299 }
271- } ;
300+ } ) ;
272301 } ,
273302} ;
274303
0 commit comments