@@ -300,6 +300,57 @@ function SampleWidget() {
300300 return this . _save_lock ;
301301 } ;
302302
303+ //To handle sample files
304+ this . handleFiles = ( sampleFile ) => {
305+ const reader = new FileReader ( ) ;
306+ reader . readAsDataURL ( sampleFile ) ;
307+
308+ reader . onload = ( ) => {
309+ // if the file is of .wav type, save it
310+ if ( reader . result . substring ( reader . result . indexOf ( ":" ) + 1 , reader . result . indexOf ( ";" ) ) === "audio/wav" ) {
311+ if ( reader . result . length <= 1333333 ) {
312+ this . sampleData = reader . result ;
313+ this . sampleName = sampleFile . name ;
314+ this . _addSample ( ) ;
315+ }
316+ else {
317+ this . activity . errorMsg ( _ ( "Warning: Your sample cannot be loaded because it is >1MB." ) , this . timbreBlock ) ;
318+ }
319+ }
320+ // otherwise, output error message
321+ else {
322+ this . showSampleTypeError ( ) ;
323+ }
324+ }
325+ reader . onloadend = ( ) => {
326+ if ( reader . result ) {
327+ const value = [ sampleFile . name , reader . result ] ;
328+ }
329+ } ;
330+ }
331+
332+ //Drag-and-Drop sample files
333+ this . drag_and_drop = ( ) => {
334+ const dropZone = document . getElementsByClassName ( "samplerCanvas" ) [ 0 ] ;
335+
336+ dropZone . addEventListener ( "dragover" , ( e ) => {
337+ e . preventDefault ( ) ;
338+ dropZone . classList . add ( "dragover" ) ;
339+ } )
340+
341+ dropZone . addEventListener ( "dragleave" , ( ) => {
342+ dropZone . classList . remove ( "dragover" ) ;
343+ } )
344+
345+ dropZone . addEventListener ( "drop" , ( e ) => {
346+ e . preventDefault ( ) ;
347+ dropZone . classList . remove ( "dragover" ) ;
348+
349+ const sampleFiles = e . dataTransfer . files [ 0 ] ;
350+ this . handleFiles ( sampleFiles ) ;
351+ } )
352+ }
353+
303354 /**
304355 * Initializes the Sample Widget.
305356 * @param {object } activity - The activity object.
@@ -382,33 +433,7 @@ function SampleWidget() {
382433 const __readerAction = function ( event ) {
383434 window . scroll ( 0 , 0 ) ;
384435 const sampleFile = fileChooser . files [ 0 ] ;
385- const reader = new FileReader ( ) ;
386- reader . readAsDataURL ( sampleFile ) ;
387-
388- // eslint-disable-next-line no-unused-vars
389- reader . onload = function ( event ) {
390- // if the file is of .wav type, save it
391- if ( reader . result . substring ( reader . result . indexOf ( ":" ) + 1 , reader . result . indexOf ( ";" ) ) === "audio/wav" ) {
392- if ( reader . result . length <= 1333333 ) {
393- that . sampleData = reader . result ;
394- that . sampleName = fileChooser . files [ 0 ] . name ;
395- that . _addSample ( ) ;
396- } else {
397- that . activity . errorMsg ( _ ( "Warning: Your sample cannot be loaded because it is >1MB." ) , that . timbreBlock ) ;
398- }
399- }
400- // otherwise, output error message
401- else {
402- that . showSampleTypeError ( ) ;
403- }
404- } ;
405-
406- reader . onloadend = function ( ) {
407- if ( reader . result ) {
408- // eslint-disable-next-line no-unused-vars
409- const value = [ fileChooser . files [ 0 ] . name , reader . result ] ;
410- }
411- } ;
436+ that . handleFiles ( sampleFile ) ;
412437 fileChooser . removeEventListener ( "change" , __readerAction ) ;
413438 } ;
414439
@@ -502,6 +527,7 @@ function SampleWidget() {
502527 this . pause ( ) ;
503528
504529 widgetWindow . sendToCenter ( ) ;
530+ this . drag_and_drop ( ) ;
505531 } ;
506532
507533 /**
0 commit comments