Disabled native drag-and-drop globally.

This commit is contained in:
Daan Vanden Bosch 2019-09-14 21:14:38 +02:00
parent 22bf650f04
commit ddf5ff1bef

View File

@ -12,26 +12,21 @@ Logger.useDefaults({
defaultLevel: (Logger as any)[process.env["LOG_LEVEL"] || "OFF"],
});
// Disable native undo/redo.
document.addEventListener("keydown", e => {
const kbe = e as KeyboardEvent;
if (kbe.ctrlKey && !kbe.altKey && kbe.key.toUpperCase() === "Z") {
kbe.preventDefault();
}
});
// This doesn't work in FireFox:
document.addEventListener("beforeinput", e => {
const ie = e as any;
if (ie.inputType === "historyUndo" || ie.inputType === "historyRedo") {
e.preventDefault();
}
});
function initialize(): Disposable {
// Disable native undo/redo.
document.addEventListener("beforeinput", before_input);
// Work-around for FireFox:
document.addEventListener("keydown", keydown);
// Disable native drag-and-drop.
document.addEventListener("dragenter", dragenter);
document.addEventListener("dragover", dragover);
document.addEventListener("drop", drop);
// Initialize view.
const application_view = new ApplicationView();
// Resize the view on window resize.
const resize = throttle(
() => {
application_view.resize(window.innerWidth, window.innerHeight);
@ -44,12 +39,50 @@ function initialize(): Disposable {
document.body.append(application_view.element);
window.addEventListener("resize", resize);
// Dispose view and global event listeners when necessary.
return {
dispose(): void {
window.removeEventListener("beforeinput", before_input);
window.removeEventListener("keydown", keydown);
window.removeEventListener("resize", resize);
window.removeEventListener("dragenter", dragenter);
window.removeEventListener("dragover", dragover);
window.removeEventListener("drop", drop);
application_view.dispose();
},
};
}
function before_input(e: Event): void {
const ie = e as any;
if (ie.inputType === "historyUndo" || ie.inputType === "historyRedo") {
e.preventDefault();
}
}
function keydown(e: Event): void {
const kbe = e as KeyboardEvent;
if (kbe.ctrlKey && !kbe.altKey && kbe.key.toUpperCase() === "Z") {
kbe.preventDefault();
}
}
function dragenter(e: DragEvent): void {
e.preventDefault();
if (e.dataTransfer) {
e.dataTransfer.dropEffect = "none";
}
}
function dragover(e: DragEvent): void {
dragenter(e);
}
function drop(e: DragEvent): void {
dragenter(e);
}
initialize();