From 7a3cff10ec79cd7681aca38453c48b26459d3ff7 Mon Sep 17 00:00:00 2001 From: Will Scullin Date: Thu, 9 Jul 2020 06:11:54 -0700 Subject: [PATCH] Add drag and drop support. --- apple1js.html | 4 +++- js/apple1.js | 34 ++++++++++++++++++++++++++++++---- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/apple1js.html b/apple1js.html index 1203b98..7699258 100644 --- a/apple1js.html +++ b/apple1js.html @@ -58,7 +58,9 @@ var tapes = {}; - +

Apple 1js

An Apple 1 Emulator in JavaScript

diff --git a/js/apple1.js b/js/apple1.js index 22385a4..3753a5b 100644 --- a/js/apple1.js +++ b/js/apple1.js @@ -80,9 +80,9 @@ if (typeof window.webkitAudioContext !== 'undefined') { context = new window.AudioContext(); } -export function doLoadLocal() { +export function doLoadLocal(files) { context.resume(); - var files = document.querySelector('#local_file').files; + files = files || document.querySelector('#local_file').files; if (files.length == 1) { var file = files[0]; var fileReader = new FileReader(); @@ -106,8 +106,8 @@ export function doLoadLocal() { aci.buffer = buf; MicroModal.close('local-modal'); }, - function(error) { - window.alert(error.message); + function() { + window.alert('Unable to read tape file: ' + file.name); } ); }; @@ -352,6 +352,32 @@ export function doLoadText() { MicroModal.close('input-modal'); } +export function handleDragOver(event) { + event.preventDefault(); + event.dataTransfer.dropEffect = 'copy'; +} + +export function handleDrop(event) { + event.preventDefault(); + event.stopPropagation(); + + var dt = event.dataTransfer; + if (dt.files.length > 0) { + doLoadLocal(dt.files); + } +} + +export function handleDragEnd(event) { + var dt = event.dataTransfer; + if (dt.items) { + for (var i = 0; i < dt.items.length; i++) { + dt.items.remove(i); + } + } else { + event.dataTransfer.clearData(); + } +} + MicroModal.init(); document.addEventListener('DOMContentLoaded', function() {