Skip to content

Commit 7bb105b

Browse files
committed
Undelete drag-n-drop
1 parent 11af24f commit 7bb105b

1 file changed

Lines changed: 33 additions & 0 deletions

File tree

src/webgl-preview.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,9 @@ export class WebGLPreview {
199199
this.controls = new OrbitControls(this.camera, this.renderer.domElement);
200200
this.initScene();
201201
this.animate();
202+
203+
if (opts.allowDragNDrop) this._enableDropHandler();
204+
202205
this.initStats();
203206
}
204207

@@ -400,6 +403,36 @@ export class WebGLPreview {
400403
this.animationFrameId = undefined;
401404
}
402405

406+
private _enableDropHandler() {
407+
console.warn('Drag and drop is deprecated as a library feature. See the demo how to implement your own.');
408+
this.canvas.addEventListener('dragover', (evt) => {
409+
evt.stopPropagation();
410+
evt.preventDefault();
411+
if (evt.dataTransfer) evt.dataTransfer.dropEffect = 'copy';
412+
this.canvas.classList.add('dragging');
413+
});
414+
415+
this.canvas.addEventListener('dragleave', (evt) => {
416+
evt.stopPropagation();
417+
evt.preventDefault();
418+
this.canvas.classList.remove('dragging');
419+
});
420+
421+
this.canvas.addEventListener('drop', async (evt) => {
422+
evt.stopPropagation();
423+
evt.preventDefault();
424+
this.canvas.classList.remove('dragging');
425+
const files: FileList | [] = evt.dataTransfer?.files ?? [];
426+
const file = files[0];
427+
428+
this.clear();
429+
430+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
431+
await this._readFromStream(file.stream() as unknown as ReadableStream<any>);
432+
this.render();
433+
});
434+
}
435+
403436
private renderLines(travels = this.job.travels(), extrusions = this.job.extrusions()): void {
404437
if (this.renderTravel) {
405438
const material = new LineMaterial({ color: this._travelColor, linewidth: this.lineWidth });

0 commit comments

Comments
 (0)