Skip to content

Commit 0e5fc93

Browse files
Drag and drop to upload samples (#4345)
* access samplerCanvas and add eventListners to it * add reader to read sample files * add conditions for sample upload * update guide * Refactor: Remove redundant code
1 parent e6857f2 commit 0e5fc93

File tree

2 files changed

+56
-27
lines changed

2 files changed

+56
-27
lines changed

guide/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2347,6 +2347,9 @@ You can import sound samples (.WAV files) and use them with the *Set
23472347
Instrument" block. The *Sampler* widget lets you set the center pitch
23482348
of your sample so that it can be tuned.
23492349

2350+
### How to import sound samples ?
2351+
By clicking the upload samples icon or by perfoming a drag and drop to sample canvas.
2352+
23502353
![widget](./sampler1.svg "Sampler Widget")
23512354

23522355
You can then use the *Sample* block as you would any input to the *Set

js/widgets/sampler.js

Lines changed: 53 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)