-
Notifications
You must be signed in to change notification settings - Fork 95
Expand file tree
/
Copy pathuseKeyboardShortcuts.ts
More file actions
62 lines (52 loc) · 1.96 KB
/
useKeyboardShortcuts.ts
File metadata and controls
62 lines (52 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import { onKeyDown, onKeyStroke } from '@vueuse/core';
import useHistoryStore from '@/src/store/history';
import { useCurrentImage } from '@/src/composables/useCurrentImage';
import { isDarwin } from '@/src/constants';
import { DEFAULT_KEYMAP } from '../config';
import { useToolStore } from '../store/tools';
import { Tools } from '../store/tools/types';
import { useRectangleStore } from '../store/tools/rectangles';
import { useRulerStore } from '../store/tools/rulers';
import { usePolygonStore } from '../store/tools/polygons';
const Keymap = DEFAULT_KEYMAP;
const isCtrlOrCmd = (ev: KeyboardEvent) => {
return isDarwin ? ev.metaKey : ev.ctrlKey;
};
const applyLabelOffset = (offset: number) => {
const toolToStore = {
[Tools.Rectangle]: useRectangleStore(),
[Tools.Ruler]: useRulerStore(),
[Tools.Polygon]: usePolygonStore(),
};
const toolStore = useToolStore();
// @ts-ignore - map may not have all keys of tools
const activeToolStore = toolToStore[toolStore.currentTool];
if (!activeToolStore) return;
const labels = Object.entries(activeToolStore.labels);
const activeLabelIndex = labels.findIndex(
([name]) => name === activeToolStore.activeLabel
);
const [nextLabel] = labels.at((activeLabelIndex + offset) % labels.length)!;
activeToolStore.setActiveLabel(nextLabel);
};
const undo = () => {
const { currentImageID } = useCurrentImage();
if (!currentImageID.value) return;
useHistoryStore().undo({ datasetID: currentImageID.value });
};
const redo = () => {
const { currentImageID } = useCurrentImage();
if (!currentImageID.value) return;
useHistoryStore().redo({ datasetID: currentImageID.value });
};
export function useKeyboardShortcuts() {
onKeyDown(Keymap.DecrementLabel, () => applyLabelOffset(-1));
onKeyDown(Keymap.IncrementLabel, () => applyLabelOffset(1));
onKeyStroke(Keymap.UndoRedo, (ev) => {
if (isCtrlOrCmd(ev)) {
ev.preventDefault();
if (ev.shiftKey) redo();
else undo();
}
});
}