From 384927e233a800154d6fab4c6d5e3718dde643e3 Mon Sep 17 00:00:00 2001 From: exyi Date: Fri, 23 Oct 2020 14:40:36 +0000 Subject: [PATCH] Editor: keyboard shortcuts --- frontend/src/Editor.svelte | 40 ++++++++++++++++++++++++++++++-------- frontend/src/helpers.ts | 4 ++++ 2 files changed, 36 insertions(+), 8 deletions(-) diff --git a/frontend/src/Editor.svelte b/frontend/src/Editor.svelte index 5f9661b..4dd4793 100644 --- a/frontend/src/Editor.svelte +++ b/frontend/src/Editor.svelte @@ -2,7 +2,7 @@ import { getContext } from "svelte"; import Graph from "./Graph.svelte"; - import { nonNull, saveToLocalDisk } from "./helpers"; + import { isEditableElement, nonNull, saveToLocalDisk } from "./helpers"; import type { TaskDescriptor, TasksFile } from "./tasks"; import { loadTasks, resetTasks, saveTasks, getCategories, tasksToString } from "./tasks"; import TaskDisplay from "./TaskDisplay.svelte"; @@ -240,6 +240,28 @@ } tasks = tasks; } + + function keydown(e: KeyboardEvent) { + if (isEditableElement(document.activeElement)) { + // another element has focus - ignore our shortcuts + return + } + const shortcuts = { + "z": showSelection, + "s": hideSelection, + "o": () => removeTask(clicked[clicked.length - 1]), + "n": addTask, + "h": addEdge, + "s": saveCurrentState, + "r": () => { showHiddenEdges = !showHiddenEdges }, + } as { [name: string]: () => void } + const keyCode = (e.ctrlKey ? "" : "") + (e.shiftKey ? "" : "") + e.key + if (shortcuts[keyCode]) { + shortcuts[keyCode]() + e.stopPropagation() + e.preventDefault() + } + } + keydown(e)} /> +

Toolbox

- +
- + + on:click={() => removeTask(clicked[clicked.length - 1])}>[O]dstranit {clicked[clicked.length - 1] ?? '???'}
-
@@ -420,8 +444,8 @@
- - + +
diff --git a/frontend/src/helpers.ts b/frontend/src/helpers.ts index 614ea73..124cdf1 100644 --- a/frontend/src/helpers.ts +++ b/frontend/src/helpers.ts @@ -6,6 +6,10 @@ export function copyFieldsThatExist(dest: any, source: any) { } } +export function isEditableElement(e: Element | null | undefined) { + return !!(e && ((e as HTMLElement).isContentEditable || e.tagName == "INPUT" || e.tagName == "TEXTAREA" || e.tagName == "SELECT")) +} + export function saveToLocalDisk(filename: string, text: string) { var element = document.createElement('a'); element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));