mirror of
https://github.com/DaanVandenBosch/phantasmal-world.git
synced 2025-04-05 15:28:29 +08:00
Disabled native drag-and-drop globally.
This commit is contained in:
parent
22bf650f04
commit
ddf5ff1bef
67
src/index.ts
67
src/index.ts
@ -12,26 +12,21 @@ Logger.useDefaults({
|
|||||||
defaultLevel: (Logger as any)[process.env["LOG_LEVEL"] || "OFF"],
|
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 {
|
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();
|
const application_view = new ApplicationView();
|
||||||
|
|
||||||
|
// Resize the view on window resize.
|
||||||
const resize = throttle(
|
const resize = throttle(
|
||||||
() => {
|
() => {
|
||||||
application_view.resize(window.innerWidth, window.innerHeight);
|
application_view.resize(window.innerWidth, window.innerHeight);
|
||||||
@ -44,12 +39,50 @@ function initialize(): Disposable {
|
|||||||
document.body.append(application_view.element);
|
document.body.append(application_view.element);
|
||||||
window.addEventListener("resize", resize);
|
window.addEventListener("resize", resize);
|
||||||
|
|
||||||
|
// Dispose view and global event listeners when necessary.
|
||||||
return {
|
return {
|
||||||
dispose(): void {
|
dispose(): void {
|
||||||
|
window.removeEventListener("beforeinput", before_input);
|
||||||
|
window.removeEventListener("keydown", keydown);
|
||||||
window.removeEventListener("resize", resize);
|
window.removeEventListener("resize", resize);
|
||||||
|
window.removeEventListener("dragenter", dragenter);
|
||||||
|
window.removeEventListener("dragover", dragover);
|
||||||
|
window.removeEventListener("drop", drop);
|
||||||
application_view.dispose();
|
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();
|
initialize();
|
||||||
|
Loading…
Reference in New Issue
Block a user