@witchcraft/spellcraft 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +579 -0
- package/dist/core/EmulatedEvent.d.ts +10 -0
- package/dist/core/EmulatedEvent.js +19 -0
- package/dist/core/Emulator.d.ts +74 -0
- package/dist/core/Emulator.js +153 -0
- package/dist/core/ShortcutManagerManager.d.ts +103 -0
- package/dist/core/ShortcutManagerManager.js +267 -0
- package/dist/core/addCommand.d.ts +7 -0
- package/dist/core/addCommand.js +7 -0
- package/dist/core/addKey.d.ts +7 -0
- package/dist/core/addKey.js +7 -0
- package/dist/core/addShortcut.d.ts +7 -0
- package/dist/core/addShortcut.js +7 -0
- package/dist/core/attach.d.ts +10 -0
- package/dist/core/attach.js +13 -0
- package/dist/core/createCommand.d.ts +4 -0
- package/dist/core/createCommand.js +11 -0
- package/dist/core/createCommands.d.ts +11 -0
- package/dist/core/createCommands.js +20 -0
- package/dist/core/createCondition.d.ts +7 -0
- package/dist/core/createCondition.js +11 -0
- package/dist/core/createContext.d.ts +2 -0
- package/dist/core/createContext.js +8 -0
- package/dist/core/createKey.d.ts +10 -0
- package/dist/core/createKey.js +35 -0
- package/dist/core/createKeys.d.ts +5 -0
- package/dist/core/createKeys.js +36 -0
- package/dist/core/createManager.d.ts +81 -0
- package/dist/core/createManager.js +83 -0
- package/dist/core/createManagerEventListeners.d.ts +4 -0
- package/dist/core/createManagerEventListeners.js +130 -0
- package/dist/core/createManagerOptions.d.ts +8 -0
- package/dist/core/createManagerOptions.js +17 -0
- package/dist/core/createShortcut.d.ts +8 -0
- package/dist/core/createShortcut.js +24 -0
- package/dist/core/createShortcuts.d.ts +13 -0
- package/dist/core/createShortcuts.js +20 -0
- package/dist/core/detach.d.ts +10 -0
- package/dist/core/detach.js +6 -0
- package/dist/core/index.d.ts +32 -0
- package/dist/core/index.js +32 -0
- package/dist/core/removeCommand.d.ts +7 -0
- package/dist/core/removeCommand.js +7 -0
- package/dist/core/removeKey.d.ts +7 -0
- package/dist/core/removeKey.js +7 -0
- package/dist/core/removeShortcut.d.ts +7 -0
- package/dist/core/removeShortcut.js +7 -0
- package/dist/core/setCommandProp.d.ts +17 -0
- package/dist/core/setCommandProp.js +96 -0
- package/dist/core/setCommandsProp.d.ts +15 -0
- package/dist/core/setCommandsProp.js +94 -0
- package/dist/core/setKeyProp.d.ts +16 -0
- package/dist/core/setKeyProp.js +47 -0
- package/dist/core/setKeysProp.d.ts +10 -0
- package/dist/core/setKeysProp.js +166 -0
- package/dist/core/setManagerProp.d.ts +34 -0
- package/dist/core/setManagerProp.js +54 -0
- package/dist/core/setShortcutProp.d.ts +9 -0
- package/dist/core/setShortcutProp.js +50 -0
- package/dist/core/setShortcutsProp.d.ts +12 -0
- package/dist/core/setShortcutsProp.js +93 -0
- package/dist/defaults/KeysSorter.d.ts +35 -0
- package/dist/defaults/KeysSorter.js +20 -0
- package/dist/defaults/Stringifier.d.ts +66 -0
- package/dist/defaults/Stringifier.js +119 -0
- package/dist/defaults/defaultConditionEquals.d.ts +19 -0
- package/dist/defaults/defaultConditionEquals.js +5 -0
- package/dist/defaults/defaultManagerCallback.d.ts +7 -0
- package/dist/defaults/defaultManagerCallback.js +5 -0
- package/dist/helpers/KnownError.d.ts +8 -0
- package/dist/helpers/KnownError.js +3 -0
- package/dist/helpers/calculateAndSetPositionAndWidth.d.ts +13 -0
- package/dist/helpers/calculateAndSetPositionAndWidth.js +18 -0
- package/dist/helpers/calculateLayoutSize.d.ts +9 -0
- package/dist/helpers/calculateLayoutSize.js +13 -0
- package/dist/helpers/doesShortcutConflict.d.ts +18 -0
- package/dist/helpers/doesShortcutConflict.js +42 -0
- package/dist/helpers/equalsCommand.d.ts +7 -0
- package/dist/helpers/equalsCommand.js +4 -0
- package/dist/helpers/equalsContext.d.ts +7 -0
- package/dist/helpers/equalsContext.js +19 -0
- package/dist/helpers/equalsShortcut.d.ts +9 -0
- package/dist/helpers/equalsShortcut.js +7 -0
- package/dist/helpers/forceClear.d.ts +12 -0
- package/dist/helpers/forceClear.js +16 -0
- package/dist/helpers/forceUpdateNativeKeysState.d.ts +5 -0
- package/dist/helpers/forceUpdateNativeKeysState.js +4 -0
- package/dist/helpers/generateKeyShortcutMap.d.ts +23 -0
- package/dist/helpers/generateKeyShortcutMap.js +64 -0
- package/dist/helpers/getKeyFromEventCode.d.ts +15 -0
- package/dist/helpers/getKeyFromEventCode.js +36 -0
- package/dist/helpers/getKeyFromIdOrVariant.d.ts +4 -0
- package/dist/helpers/getKeyFromIdOrVariant.js +26 -0
- package/dist/helpers/getKeyboardLayoutMap.d.ts +5 -0
- package/dist/helpers/getKeyboardLayoutMap.js +7 -0
- package/dist/helpers/getLabel.d.ts +9 -0
- package/dist/helpers/getLabel.js +6 -0
- package/dist/helpers/getTriggerableShortcut.d.ts +6 -0
- package/dist/helpers/getTriggerableShortcut.js +25 -0
- package/dist/helpers/index.d.ts +28 -0
- package/dist/helpers/index.js +28 -0
- package/dist/helpers/isValidManager.d.ts +3 -0
- package/dist/helpers/isValidManager.js +20 -0
- package/dist/helpers/isValidShortcut.d.ts +3 -0
- package/dist/helpers/isValidShortcut.js +10 -0
- package/dist/helpers/labelWithEvent.d.ts +13 -0
- package/dist/helpers/labelWithEvent.js +20 -0
- package/dist/helpers/labelWithKeyboardMap.d.ts +15 -0
- package/dist/helpers/labelWithKeyboardMap.js +17 -0
- package/dist/helpers/managerToStorableClone.d.ts +12 -0
- package/dist/helpers/managerToStorableClone.js +13 -0
- package/dist/helpers/onKeyboardLayoutChange.d.ts +10 -0
- package/dist/helpers/onKeyboardLayoutChange.js +7 -0
- package/dist/helpers/safeSetManagerChain.d.ts +20 -0
- package/dist/helpers/safeSetManagerChain.js +37 -0
- package/dist/helpers/shortcutCanExecuteIn.d.ts +4 -0
- package/dist/helpers/shortcutCanExecuteIn.js +9 -0
- package/dist/helpers/shortcutIsTriggerableBy.d.ts +2 -0
- package/dist/helpers/shortcutIsTriggerableBy.js +5 -0
- package/dist/helpers/shortcutSwapChords.d.ts +75 -0
- package/dist/helpers/shortcutSwapChords.js +118 -0
- package/dist/helpers/virtualPress.d.ts +13 -0
- package/dist/helpers/virtualPress.js +15 -0
- package/dist/helpers/virtualRelease.d.ts +5 -0
- package/dist/helpers/virtualRelease.js +15 -0
- package/dist/helpers/virtualToggle.d.ts +7 -0
- package/dist/helpers/virtualToggle.js +16 -0
- package/dist/internal/addToChain.d.ts +3 -0
- package/dist/internal/addToChain.js +27 -0
- package/dist/internal/areValidKeys.d.ts +7 -0
- package/dist/internal/areValidKeys.js +28 -0
- package/dist/internal/areValidVariants.d.ts +4 -0
- package/dist/internal/areValidVariants.js +45 -0
- package/dist/internal/checkTrigger.d.ts +2 -0
- package/dist/internal/checkTrigger.js +47 -0
- package/dist/internal/checkUntrigger.d.ts +2 -0
- package/dist/internal/checkUntrigger.js +18 -0
- package/dist/internal/cloneLastChord.d.ts +4 -0
- package/dist/internal/cloneLastChord.js +5 -0
- package/dist/internal/containsPossibleToggleChords.d.ts +13 -0
- package/dist/internal/containsPossibleToggleChords.js +65 -0
- package/dist/internal/errorTextAdd.d.ts +1 -0
- package/dist/internal/errorTextAdd.js +11 -0
- package/dist/internal/errorTextInUse.d.ts +1 -0
- package/dist/internal/errorTextInUse.js +8 -0
- package/dist/internal/errorTextRemove.d.ts +1 -0
- package/dist/internal/errorTextRemove.js +8 -0
- package/dist/internal/getModifierState.d.ts +2 -0
- package/dist/internal/getModifierState.js +7 -0
- package/dist/internal/getPressedKeys.d.ts +7 -0
- package/dist/internal/getPressedKeys.js +18 -0
- package/dist/internal/getPressedModifierKeys.d.ts +2 -0
- package/dist/internal/getPressedModifierKeys.js +10 -0
- package/dist/internal/getPressedNonModifierKeys.d.ts +7 -0
- package/dist/internal/getPressedNonModifierKeys.js +11 -0
- package/dist/internal/inChain.d.ts +5 -0
- package/dist/internal/inChain.js +14 -0
- package/dist/internal/isValidChain.d.ts +10 -0
- package/dist/internal/isValidChain.js +22 -0
- package/dist/internal/isValidChord.d.ts +11 -0
- package/dist/internal/isValidChord.js +59 -0
- package/dist/internal/isValidCommand.d.ts +12 -0
- package/dist/internal/isValidCommand.js +20 -0
- package/dist/internal/keyOrder.d.ts +6 -0
- package/dist/internal/keyOrder.js +14 -0
- package/dist/internal/removeFromChain.d.ts +3 -0
- package/dist/internal/removeFromChain.js +32 -0
- package/dist/internal/safeSetEmulatedToggleState.d.ts +4 -0
- package/dist/internal/safeSetEmulatedToggleState.js +13 -0
- package/dist/internal/setKeysState.d.ts +8 -0
- package/dist/internal/setKeysState.js +42 -0
- package/dist/internal/updateNativeKeysState.d.ts +14 -0
- package/dist/internal/updateNativeKeysState.js +58 -0
- package/dist/layouts/createLayout.d.ts +25 -0
- package/dist/layouts/createLayout.js +221 -0
- package/dist/module.d.mts +13 -0
- package/dist/module.json +9 -0
- package/dist/module.mjs +40 -0
- package/dist/runtime/composables/useLabeledByKeyboardLayoutMap.d.ts +9 -0
- package/dist/runtime/composables/useLabeledByKeyboardLayoutMap.js +19 -0
- package/dist/runtime/composables/usePointerCoords.d.ts +32 -0
- package/dist/runtime/composables/usePointerCoords.js +17 -0
- package/dist/runtime/composables/useShortcutManagerContextCount.d.ts +14 -0
- package/dist/runtime/composables/useShortcutManagerContextCount.js +61 -0
- package/dist/runtime/composables/useShortcutManagerKeysLayout.d.ts +18 -0
- package/dist/runtime/composables/useShortcutManagerKeysLayout.js +22 -0
- package/dist/runtime/composables/useShortcutManagerVirtualPress.d.ts +6 -0
- package/dist/runtime/composables/useShortcutManagerVirtualPress.js +24 -0
- package/dist/runtime/types.d.ts +10 -0
- package/dist/runtime/types.js +1 -0
- package/dist/runtime/utils/shortcutToId.d.ts +5 -0
- package/dist/runtime/utils/shortcutToId.js +6 -0
- package/dist/types/commands.d.ts +113 -0
- package/dist/types/commands.js +0 -0
- package/dist/types/condition.d.ts +29 -0
- package/dist/types/condition.js +0 -0
- package/dist/types/context.d.ts +18 -0
- package/dist/types/context.js +0 -0
- package/dist/types/enums.d.ts +186 -0
- package/dist/types/enums.js +70 -0
- package/dist/types/general.d.ts +92 -0
- package/dist/types/general.js +0 -0
- package/dist/types/index.d.ts +8 -0
- package/dist/types/index.js +8 -0
- package/dist/types/keys.d.ts +332 -0
- package/dist/types/keys.js +0 -0
- package/dist/types/manager.d.ts +249 -0
- package/dist/types/manager.js +0 -0
- package/dist/types/plugins.d.ts +1 -0
- package/dist/types/plugins.js +0 -0
- package/dist/types/shortcuts.d.ts +144 -0
- package/dist/types/shortcuts.js +0 -0
- package/dist/types/utils.d.ts +1 -0
- package/dist/types/utils.js +0 -0
- package/dist/types.d.mts +3 -0
- package/dist/utils/chainContainsSubset.d.ts +27 -0
- package/dist/utils/chainContainsSubset.js +45 -0
- package/dist/utils/cloneChain.d.ts +2 -0
- package/dist/utils/cloneChain.js +11 -0
- package/dist/utils/cloneKey.d.ts +3 -0
- package/dist/utils/cloneKey.js +26 -0
- package/dist/utils/containsKey.d.ts +7 -0
- package/dist/utils/containsKey.js +4 -0
- package/dist/utils/dedupeKeys.d.ts +9 -0
- package/dist/utils/dedupeKeys.js +9 -0
- package/dist/utils/equalsKey.d.ts +12 -0
- package/dist/utils/equalsKey.js +13 -0
- package/dist/utils/equalsKeys.d.ts +24 -0
- package/dist/utils/equalsKeys.js +15 -0
- package/dist/utils/index.d.ts +14 -0
- package/dist/utils/index.js +14 -0
- package/dist/utils/isAnyKey.d.ts +5 -0
- package/dist/utils/isAnyKey.js +5 -0
- package/dist/utils/isMouseKey.d.ts +5 -0
- package/dist/utils/isMouseKey.js +20 -0
- package/dist/utils/isNormalKey.d.ts +5 -0
- package/dist/utils/isNormalKey.js +5 -0
- package/dist/utils/isTriggerKey.d.ts +5 -0
- package/dist/utils/isTriggerKey.js +3 -0
- package/dist/utils/isWheelKey.d.ts +5 -0
- package/dist/utils/isWheelKey.js +11 -0
- package/dist/utils/mapKeys.d.ts +8 -0
- package/dist/utils/mapKeys.js +5 -0
- package/dist/utils/removeKeys.d.ts +7 -0
- package/dist/utils/removeKeys.js +4 -0
- package/package.json +156 -0
- package/src/core/EmulatedEvent.ts +29 -0
- package/src/core/Emulator.ts +185 -0
- package/src/core/ShortcutManagerManager.ts +380 -0
- package/src/core/addCommand.ts +24 -0
- package/src/core/addKey.ts +25 -0
- package/src/core/addShortcut.ts +24 -0
- package/src/core/attach.ts +27 -0
- package/src/core/createCommand.ts +24 -0
- package/src/core/createCommands.ts +45 -0
- package/src/core/createCondition.ts +21 -0
- package/src/core/createContext.ts +14 -0
- package/src/core/createKey.ts +59 -0
- package/src/core/createKeys.ts +68 -0
- package/src/core/createManager.ts +209 -0
- package/src/core/createManagerEventListeners.ts +139 -0
- package/src/core/createManagerOptions.ts +29 -0
- package/src/core/createShortcut.ts +49 -0
- package/src/core/createShortcuts.ts +51 -0
- package/src/core/detach.ts +21 -0
- package/src/core/index.ts +35 -0
- package/src/core/removeCommand.ts +25 -0
- package/src/core/removeKey.ts +25 -0
- package/src/core/removeShortcut.ts +24 -0
- package/src/core/setCommandProp.ts +140 -0
- package/src/core/setCommandsProp.ts +128 -0
- package/src/core/setKeyProp.ts +80 -0
- package/src/core/setKeysProp.ts +205 -0
- package/src/core/setManagerProp.ts +111 -0
- package/src/core/setShortcutProp.ts +89 -0
- package/src/core/setShortcutsProp.ts +124 -0
- package/src/defaults/KeysSorter.ts +55 -0
- package/src/defaults/Stringifier.ts +234 -0
- package/src/defaults/defaultConditionEquals.ts +29 -0
- package/src/defaults/defaultManagerCallback.ts +14 -0
- package/src/helpers/KnownError.ts +13 -0
- package/src/helpers/calculateAndSetPositionAndWidth.ts +30 -0
- package/src/helpers/calculateLayoutSize.ts +22 -0
- package/src/helpers/doesShortcutConflict.ts +72 -0
- package/src/helpers/equalsCommand.ts +18 -0
- package/src/helpers/equalsContext.ts +29 -0
- package/src/helpers/equalsShortcut.ts +34 -0
- package/src/helpers/forceClear.ts +31 -0
- package/src/helpers/forceUpdateNativeKeysState.ts +9 -0
- package/src/helpers/generateKeyShortcutMap.ts +113 -0
- package/src/helpers/getKeyFromEventCode.ts +67 -0
- package/src/helpers/getKeyFromIdOrVariant.ts +33 -0
- package/src/helpers/getKeyboardLayoutMap.ts +13 -0
- package/src/helpers/getLabel.ts +15 -0
- package/src/helpers/getTriggerableShortcut.ts +37 -0
- package/src/helpers/index.ts +30 -0
- package/src/helpers/isValidManager.ts +29 -0
- package/src/helpers/isValidShortcut.ts +20 -0
- package/src/helpers/labelWithEvent.ts +41 -0
- package/src/helpers/labelWithKeyboardMap.ts +37 -0
- package/src/helpers/managerToStorableClone.ts +26 -0
- package/src/helpers/onKeyboardLayoutChange.ts +17 -0
- package/src/helpers/safeSetManagerChain.ts +66 -0
- package/src/helpers/shortcutCanExecuteIn.ts +24 -0
- package/src/helpers/shortcutIsTriggerableBy.ts +15 -0
- package/src/helpers/shortcutSwapChords.ts +240 -0
- package/src/helpers/virtualPress.ts +34 -0
- package/src/helpers/virtualRelease.ts +25 -0
- package/src/helpers/virtualToggle.ts +28 -0
- package/src/internal/addToChain.ts +40 -0
- package/src/internal/areValidKeys.ts +40 -0
- package/src/internal/areValidVariants.ts +59 -0
- package/src/internal/checkTrigger.ts +55 -0
- package/src/internal/checkUntrigger.ts +26 -0
- package/src/internal/cloneLastChord.ts +10 -0
- package/src/internal/containsPossibleToggleChords.ts +91 -0
- package/src/internal/errorTextAdd.ts +18 -0
- package/src/internal/errorTextInUse.ts +10 -0
- package/src/internal/errorTextRemove.ts +10 -0
- package/src/internal/getModifierState.ts +15 -0
- package/src/internal/getPressedKeys.ts +26 -0
- package/src/internal/getPressedModifierKeys.ts +14 -0
- package/src/internal/getPressedNonModifierKeys.ts +19 -0
- package/src/internal/inChain.ts +25 -0
- package/src/internal/isValidChain.ts +42 -0
- package/src/internal/isValidChord.ts +87 -0
- package/src/internal/isValidCommand.ts +35 -0
- package/src/internal/keyOrder.ts +24 -0
- package/src/internal/removeFromChain.ts +46 -0
- package/src/internal/safeSetEmulatedToggleState.ts +23 -0
- package/src/internal/setKeysState.ts +71 -0
- package/src/internal/updateNativeKeysState.ts +84 -0
- package/src/layouts/createLayout.ts +328 -0
- package/src/module.ts +62 -0
- package/src/runtime/composables/useLabeledByKeyboardLayoutMap.ts +28 -0
- package/src/runtime/composables/usePointerCoords.ts +40 -0
- package/src/runtime/composables/useShortcutManagerContextCount.ts +81 -0
- package/src/runtime/composables/useShortcutManagerKeysLayout.ts +42 -0
- package/src/runtime/composables/useShortcutManagerVirtualPress.ts +30 -0
- package/src/runtime/types.ts +10 -0
- package/src/runtime/utils/shortcutToId.ts +14 -0
- package/src/types/commands.ts +148 -0
- package/src/types/condition.ts +35 -0
- package/src/types/context.ts +19 -0
- package/src/types/enums.ts +236 -0
- package/src/types/general.ts +117 -0
- package/src/types/index.ts +10 -0
- package/src/types/keys.ts +385 -0
- package/src/types/manager.ts +374 -0
- package/src/types/plugins.ts +32 -0
- package/src/types/shortcuts.ts +204 -0
- package/src/types/utils.ts +40 -0
- package/src/utils/chainContainsSubset.ts +97 -0
- package/src/utils/cloneChain.ts +13 -0
- package/src/utils/cloneKey.ts +33 -0
- package/src/utils/containsKey.ts +17 -0
- package/src/utils/dedupeKeys.ts +23 -0
- package/src/utils/equalsKey.ts +32 -0
- package/src/utils/equalsKeys.ts +50 -0
- package/src/utils/index.ts +16 -0
- package/src/utils/isAnyKey.ts +12 -0
- package/src/utils/isMouseKey.ts +27 -0
- package/src/utils/isNormalKey.ts +15 -0
- package/src/utils/isTriggerKey.ts +7 -0
- package/src/utils/isWheelKey.ts +18 -0
- package/src/utils/mapKeys.ts +21 -0
- package/src/utils/removeKeys.ts +16 -0
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { type Result } from "@alanscodelog/utils/Result";
|
|
2
|
+
import { type Key, type MultipleErrors, type RawKey, SHORTCUT_ERROR } from "../types/index.js";
|
|
3
|
+
/**
|
|
4
|
+
* Creates a key.
|
|
5
|
+
*
|
|
6
|
+
* @template TId **@internal** See {@link ./README.md Collection Entries}
|
|
7
|
+
* @param id See {@link Key.id}
|
|
8
|
+
* @param opts Set {@link Key}.
|
|
9
|
+
*/
|
|
10
|
+
export declare function createKey<TId extends string = string, TKey extends RawKey<TId> = RawKey<TId>>(id: TId, rawKey?: Omit<TKey, "id">): Result<Key<TId>, MultipleErrors<typeof SHORTCUT_ERROR.INVALID_VARIANT>>;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { Err, Ok } from "@alanscodelog/utils/Result";
|
|
2
|
+
import { KnownError } from "../helpers/KnownError.js";
|
|
3
|
+
import { SHORTCUT_ERROR } from "../types/index.js";
|
|
4
|
+
export function createKey(id, rawKey = {}) {
|
|
5
|
+
const k = rawKey;
|
|
6
|
+
const key = {
|
|
7
|
+
type: "key",
|
|
8
|
+
id,
|
|
9
|
+
label: k.label ?? id,
|
|
10
|
+
classes: [...k.classes ?? []],
|
|
11
|
+
x: k.x ?? 0,
|
|
12
|
+
y: k.y ?? 0,
|
|
13
|
+
width: k.width ?? 0,
|
|
14
|
+
height: k.height ?? 0,
|
|
15
|
+
enabled: k.enabled ?? true,
|
|
16
|
+
pressed: false,
|
|
17
|
+
isModifier: k.isModifier ?? false,
|
|
18
|
+
isToggle: k.isToggle ?? false,
|
|
19
|
+
...k.isToggle ? {
|
|
20
|
+
toggleOnId: `${id}On`,
|
|
21
|
+
toggleOffId: `${id}Off`,
|
|
22
|
+
toggleOnPressed: false,
|
|
23
|
+
toggleOffPressed: false
|
|
24
|
+
} : {},
|
|
25
|
+
variants: k.variants ?? [],
|
|
26
|
+
render: k.render ?? true,
|
|
27
|
+
updateStateOnAllEvents: k.updateStateOnAllEvents ?? true
|
|
28
|
+
};
|
|
29
|
+
if (key.variants?.includes(key.id)) {
|
|
30
|
+
return Err(
|
|
31
|
+
new KnownError(SHORTCUT_ERROR.INVALID_VARIANT, `A key variant cannot be the key id itself. Attempted to use "${key.id}" in variants:[ ${key.variants.join(",")} ]`, { variants: key.variants, id: key.id })
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
return Ok(key);
|
|
35
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { type Result } from "@alanscodelog/utils/Result";
|
|
2
|
+
import type { Key, Keys, MultipleErrors, PickManager, SHORTCUT_ERROR } from "../types/index.js";
|
|
3
|
+
export declare function createKeys<TKey extends Key = Key, TKeys extends (TKey)[] = (TKey)[], TEntries extends Record<TKeys[number]["id"], Key> = Record<TKeys[number]["id"], Key>, TCheck extends boolean | "only" = true>(entries: TKeys, manager?: PickManager<"options", "stringifier">, rawKeys?: Partial<Pick<Keys, "autoManageLayout" | "layout">>, { check }?: {
|
|
4
|
+
check?: TCheck;
|
|
5
|
+
}): Result<TCheck extends "only" ? true : Keys<TEntries>, MultipleErrors<typeof SHORTCUT_ERROR.DUPLICATE_KEY>>;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { Ok } from "@alanscodelog/utils/Result";
|
|
2
|
+
import { addKey } from "./addKey.js";
|
|
3
|
+
import { defaultStringifier } from "../defaults/Stringifier.js";
|
|
4
|
+
import { calculateLayoutSize } from "../helpers/calculateLayoutSize.js";
|
|
5
|
+
export function createKeys(entries, manager = {
|
|
6
|
+
options: { stringifier: defaultStringifier }
|
|
7
|
+
}, rawKeys = {}, {
|
|
8
|
+
check = true
|
|
9
|
+
} = {}) {
|
|
10
|
+
const keysList = entries;
|
|
11
|
+
const keys = {
|
|
12
|
+
type: "keys",
|
|
13
|
+
// prevent multiple calculations when initially adding keys
|
|
14
|
+
autoManageLayout: false,
|
|
15
|
+
...rawKeys,
|
|
16
|
+
layout: { y: 0, x: 0, ...rawKeys.layout ?? {} },
|
|
17
|
+
entries: {},
|
|
18
|
+
nativeToggleKeys: [],
|
|
19
|
+
nativeModifierKeys: [],
|
|
20
|
+
variants: {},
|
|
21
|
+
toggles: {}
|
|
22
|
+
};
|
|
23
|
+
const managerClone = { ...manager, keys };
|
|
24
|
+
if (check) {
|
|
25
|
+
for (const key of keysList) {
|
|
26
|
+
const res = addKey(key, managerClone);
|
|
27
|
+
if (res.isError) return res;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
if (check === "only") return Ok(true);
|
|
31
|
+
if (rawKeys?.autoManageLayout) {
|
|
32
|
+
keys.autoManageLayout = true;
|
|
33
|
+
keys.layout = calculateLayoutSize(keys);
|
|
34
|
+
}
|
|
35
|
+
return Ok(keys);
|
|
36
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { type Result } from "@alanscodelog/utils/Result";
|
|
2
|
+
import type { RecordFromArray } from "@alanscodelog/utils/types";
|
|
3
|
+
import type { CanHookErrors, CanHooks, Command, Commands, CommandsSetEntries, Context, Hooks, Key, Keys, KeysSetEntries, Manager, ManagerListener, MultipleErrors, RawCommand, RawKey, RawShortcut, SHORTCUT_ERROR, Shortcuts, ShortcutsSetEntries } from "../types/index.js";
|
|
4
|
+
/**
|
|
5
|
+
* Create a manager which can track key states, layouts, and trigger shortcuts. Basically the brains of the operation.
|
|
6
|
+
*
|
|
7
|
+
* First, all the elements of a manager need to be created.
|
|
8
|
+
* Then you can create the listeners for the manager with {@link createManagerEventListeners}.
|
|
9
|
+
*
|
|
10
|
+
* Then these listeners can be attached/removed from elements (or the {@link Emulator})
|
|
11
|
+
*
|
|
12
|
+
* You could make several managers for different areas of your application or use single manager and different contexts to differ between them.
|
|
13
|
+
*
|
|
14
|
+
* It is easiest to build a manager like so, but the manager also takes in full {@link Keys}/{@link Commands}/{@link Shortcuts} instances, though it's a bit harder to build those first then pass to the manager:
|
|
15
|
+
* ```ts
|
|
16
|
+
*
|
|
17
|
+
* const manager = createManager(
|
|
18
|
+
* {
|
|
19
|
+
* keys: [
|
|
20
|
+
* createKey("a", ),
|
|
21
|
+
* // or using a raw key
|
|
22
|
+
* {id: "b"}
|
|
23
|
+
* ],
|
|
24
|
+
* commands: [
|
|
25
|
+
* createCommand("a"),
|
|
26
|
+
* {name: "b", execute() {....}}
|
|
27
|
+
* ],
|
|
28
|
+
* shortcuts: [
|
|
29
|
+
* { chain: [["a"]], command: "a" }
|
|
30
|
+
* ]
|
|
31
|
+
* context: createContext({...}),
|
|
32
|
+
* options: {
|
|
33
|
+
* // required
|
|
34
|
+
* evaluateCondition() {
|
|
35
|
+
* ...
|
|
36
|
+
* }
|
|
37
|
+
* }
|
|
38
|
+
* }, {
|
|
39
|
+
* // if constructing from raw entries like above, createKeys/Shortcuts can take additional options
|
|
40
|
+
* ...,
|
|
41
|
+
* keys: {...},
|
|
42
|
+
* shortcuts:{...}
|
|
43
|
+
* }).unwrap()
|
|
44
|
+
*
|
|
45
|
+
* const listeners = createManagerEventListeners(manager)
|
|
46
|
+
* attach(document, listeners)
|
|
47
|
+
* // to remove:
|
|
48
|
+
* dettach(document, listeners)
|
|
49
|
+
* ```
|
|
50
|
+
*
|
|
51
|
+
* You will then not need to do much else. It will take care of setting the pressed state of keys, the state of the current chord chain (see {@link Manager._chain}), finding a matching shortcut if it exists, and triggering it's command.
|
|
52
|
+
*
|
|
53
|
+
* The way this works is that when a key is pressed it's added to the current chord in the chain. If the chain matches a shortcut's chain that can be triggered (it must be enabled, it must have a command and a function to execute, and it's condition and it's command's condition must evaluate to true), the command's execute function is triggered with `isKeydown = true`, see {@link Command.execute}.
|
|
54
|
+
*
|
|
55
|
+
* If there are no shortcuts to trigger but there are "potential shortcuts" that start with the current chain and could trigger and current chord contains any non-modifier keys (see {@link Manager.pressedNonModifierKeys}), an empty chord will be added to the chain on the next key pressed and the process repeats.
|
|
56
|
+
*
|
|
57
|
+
* If there are no shortcuts and no potential shortcuts, the callback will be called with {@link SHORTCUT_ERROR.NO_MATCHING_SHORTCUT} and you will probably want to clear the chain. When the chain is cleared (see {@link Manager._chain}), the manager will not add/remove keys from the chain until all non-modifier keys are unpressed. Pressed state is still set/tracked though.
|
|
58
|
+
*
|
|
59
|
+
* The moment a key is released after a shortcut is triggered, the command's execute function is fired again with `isKeydown = false`.
|
|
60
|
+
*
|
|
61
|
+
* If {@link Manager.options.updateStateOnAllEvents} is not disabled and a mouseeneter listener was created, the manager is able to keep the most accurate state of the modifier keys. See {@link Manager.options.updateStateOnAllEvents} for more info.
|
|
62
|
+
*
|
|
63
|
+
* For a detailed usage example, see the demo.
|
|
64
|
+
*
|
|
65
|
+
* ### Other
|
|
66
|
+
*
|
|
67
|
+
* - If you need to emulate keypresses for testing see {@link Emulator}.
|
|
68
|
+
*/
|
|
69
|
+
export declare function createManager<THooks extends Hooks, TKeys extends Keys | RawKey[], TShortcuts extends Shortcuts | RawShortcut[], TCommands extends Commands | RawCommand[], TContext extends Context, TListener extends ManagerListener>(rawManager: {
|
|
70
|
+
name?: string;
|
|
71
|
+
options: Partial<Omit<Manager["options"], "evaluateCondition">> & Pick<Manager<any, any, any, any, TContext>["options"], "evaluateCondition">;
|
|
72
|
+
context?: TContext;
|
|
73
|
+
keys?: TKeys;
|
|
74
|
+
shortcuts?: TShortcuts;
|
|
75
|
+
commands?: TCommands;
|
|
76
|
+
hooks?: Partial<THooks>;
|
|
77
|
+
listener?: TListener;
|
|
78
|
+
}, additionalOpts?: Partial<{
|
|
79
|
+
keys?: Partial<Pick<Keys, "autoManageLayout" | "layout">>;
|
|
80
|
+
shortcuts?: Partial<Pick<Shortcuts, "ignoreModifierConflicts" | "ignoreChainConflicts">>;
|
|
81
|
+
}>): Result<Manager<THooks, TKeys extends Keys ? Extract<TKeys, Keys> : TKeys extends RawKey[] ? Keys<RecordFromArray<TKeys, "id", Key>> : never, Shortcuts, TCommands extends Commands ? Extract<TCommands, Commands> : TCommands extends RawCommand[] ? Commands<RecordFromArray<TCommands, "name", Command>> : never, TContext>, MultipleErrors<CommandsSetEntries["entries@add"]["error"] | KeysSetEntries["entries@add"]["error"] | ShortcutsSetEntries["entries@add"]["error"] | typeof SHORTCUT_ERROR.INVALID_VARIANT> | CanHookErrors<THooks extends never ? never : THooks, keyof CanHooks>>;
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { castType } from "@alanscodelog/utils/castType";
|
|
2
|
+
import { isArray } from "@alanscodelog/utils/isArray";
|
|
3
|
+
import { Ok } from "@alanscodelog/utils/Result";
|
|
4
|
+
import { createCommand } from "./createCommand.js";
|
|
5
|
+
import { createCommands } from "./createCommands.js";
|
|
6
|
+
import { createKey } from "./createKey.js";
|
|
7
|
+
import { createKeys } from "./createKeys.js";
|
|
8
|
+
import { createManagerOptions } from "./createManagerOptions.js";
|
|
9
|
+
import { createShortcut } from "./createShortcut.js";
|
|
10
|
+
import { createShortcuts } from "./createShortcuts.js";
|
|
11
|
+
export function createManager(rawManager, additionalOpts = {}) {
|
|
12
|
+
const options = createManagerOptions(rawManager.options);
|
|
13
|
+
const m = { options };
|
|
14
|
+
const state = {
|
|
15
|
+
chain: [],
|
|
16
|
+
isAwaitingKeyup: false,
|
|
17
|
+
isRecording: false,
|
|
18
|
+
untrigger: false,
|
|
19
|
+
nextIsChord: false
|
|
20
|
+
};
|
|
21
|
+
let keys = rawManager.keys;
|
|
22
|
+
if (!keys || isArray(keys)) {
|
|
23
|
+
castType(keys);
|
|
24
|
+
const keysList = [];
|
|
25
|
+
if (keys) {
|
|
26
|
+
for (const key of keys) {
|
|
27
|
+
const res2 = createKey(key.id, key);
|
|
28
|
+
if (res2.isError) return res2;
|
|
29
|
+
keysList.push(res2.value);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
const res = createKeys(keysList, m, additionalOpts?.keys);
|
|
33
|
+
if (res.isError) return res;
|
|
34
|
+
keys = res.value;
|
|
35
|
+
}
|
|
36
|
+
let commands = rawManager.commands;
|
|
37
|
+
if (!commands || isArray(commands)) {
|
|
38
|
+
castType(commands);
|
|
39
|
+
const commandsList = [];
|
|
40
|
+
if (commands) {
|
|
41
|
+
for (const command of commands) {
|
|
42
|
+
const res2 = createCommand(command.name, command);
|
|
43
|
+
commandsList.push(res2);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
const res = createCommands(commandsList, { options });
|
|
47
|
+
if (res.isError) return res;
|
|
48
|
+
commands = res.value;
|
|
49
|
+
}
|
|
50
|
+
let shortcuts = rawManager.shortcuts;
|
|
51
|
+
if (!shortcuts || isArray(shortcuts)) {
|
|
52
|
+
castType(shortcuts);
|
|
53
|
+
const shortcutsList = [];
|
|
54
|
+
const m2 = { ...m, commands, keys };
|
|
55
|
+
if (shortcuts) {
|
|
56
|
+
for (const shortcut of shortcuts) {
|
|
57
|
+
const res2 = createShortcut(shortcut, m2);
|
|
58
|
+
if (res2.isError) return res2;
|
|
59
|
+
shortcutsList.push(res2.value);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
const res = createShortcuts(shortcutsList, {
|
|
63
|
+
keys,
|
|
64
|
+
commands,
|
|
65
|
+
options
|
|
66
|
+
}, additionalOpts?.shortcuts);
|
|
67
|
+
if (res.isError) return res;
|
|
68
|
+
shortcuts = res.value;
|
|
69
|
+
}
|
|
70
|
+
const manager = {
|
|
71
|
+
...rawManager,
|
|
72
|
+
name: rawManager.name ?? "manager",
|
|
73
|
+
type: "manager",
|
|
74
|
+
keys,
|
|
75
|
+
shortcuts,
|
|
76
|
+
commands,
|
|
77
|
+
options,
|
|
78
|
+
state,
|
|
79
|
+
context: rawManager.context,
|
|
80
|
+
hooks: rawManager?.hooks
|
|
81
|
+
};
|
|
82
|
+
return Ok(manager);
|
|
83
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { EventListenerTypes, EventTypes, Manager } from "../types/index.js";
|
|
2
|
+
export declare function createManagerEventListeners<T extends EventTypes, TTypes extends T[]>(manager: Manager, types?: TTypes, { debug }?: {
|
|
3
|
+
debug?: true | ((type: EventTypes, e: KeyboardEvent | MouseEvent) => void);
|
|
4
|
+
}): EventListenerTypes<TTypes[number]>;
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import { getKeyFromEventCode } from "../helpers/getKeyFromEventCode.js";
|
|
2
|
+
import { addToChain } from "../internal/addToChain.js";
|
|
3
|
+
import { removeFromChain } from "../internal/removeFromChain.js";
|
|
4
|
+
import { setKeysState } from "../internal/setKeysState.js";
|
|
5
|
+
import { updateNativeKeysState } from "../internal/updateNativeKeysState.js";
|
|
6
|
+
export function createManagerEventListeners(manager, types = ["keydown", "keyup", "keypress", "wheel", "mousedown", "mouseup", "mouseenter"], { debug } = {}) {
|
|
7
|
+
const debugFn = debug === true ? console.log : void 0;
|
|
8
|
+
const listeners = {};
|
|
9
|
+
for (const type of types) {
|
|
10
|
+
switch (type) {
|
|
11
|
+
case "keydown": {
|
|
12
|
+
listeners.keydown = (e) => {
|
|
13
|
+
debugFn?.("keydown", e);
|
|
14
|
+
if (!manager.options.enableListeners) return;
|
|
15
|
+
const keyIds = getKeyFromEventCode(e.code, e, manager.keys.entries);
|
|
16
|
+
if (keyIds.isError) {
|
|
17
|
+
manager.options.cb(manager, keyIds.error, e);
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
const ids = keyIds.value;
|
|
21
|
+
manager.listener?.({ event: e, isKeydown: true, keys: ids, manager });
|
|
22
|
+
setKeysState(ids, manager, true);
|
|
23
|
+
updateNativeKeysState(manager, e, ids);
|
|
24
|
+
const res = addToChain(manager, ids, e);
|
|
25
|
+
if (res.isError) {
|
|
26
|
+
manager.options.cb(manager, res.error, e);
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
break;
|
|
30
|
+
}
|
|
31
|
+
case "keyup": {
|
|
32
|
+
listeners.keyup = (e) => {
|
|
33
|
+
debugFn?.("keyup", e);
|
|
34
|
+
if (!manager.options.enableListeners) return;
|
|
35
|
+
const keyIds = getKeyFromEventCode(e.code, e, manager.keys.entries);
|
|
36
|
+
if (keyIds.isError) {
|
|
37
|
+
manager.options.cb(manager, keyIds.error, e);
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
const ids = keyIds.value;
|
|
41
|
+
manager.listener?.({ event: e, isKeydown: false, keys: ids, manager });
|
|
42
|
+
setKeysState(ids, manager, false);
|
|
43
|
+
updateNativeKeysState(manager, e, ids);
|
|
44
|
+
const res = removeFromChain(manager, ids, e);
|
|
45
|
+
if (res.isError) {
|
|
46
|
+
manager.options.cb(manager, res.error, e);
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
break;
|
|
50
|
+
}
|
|
51
|
+
case "wheel": {
|
|
52
|
+
listeners.wheel = (e) => {
|
|
53
|
+
debugFn?.("wheel", e);
|
|
54
|
+
if (!manager.options.enableListeners) return;
|
|
55
|
+
const dir = e.deltaY < 0 ? "Up" : "Down";
|
|
56
|
+
const code = `Wheel${dir}`;
|
|
57
|
+
const keyIds = getKeyFromEventCode(code, e, manager.keys.entries, { pressedState: false });
|
|
58
|
+
if (keyIds.isError) {
|
|
59
|
+
manager.options.cb(manager, keyIds.error, e);
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
const ids = keyIds.value;
|
|
63
|
+
manager.listener?.({ event: e, isKeydown: true, keys: ids, manager });
|
|
64
|
+
setKeysState(ids, manager, true);
|
|
65
|
+
updateNativeKeysState(manager, e, ids);
|
|
66
|
+
addToChain(manager, ids, e);
|
|
67
|
+
manager.listener?.({ event: e, isKeydown: false, keys: ids, manager });
|
|
68
|
+
setKeysState(ids, manager, false);
|
|
69
|
+
const res = removeFromChain(manager, ids, e);
|
|
70
|
+
if (res.isError) {
|
|
71
|
+
manager.options.cb(manager, res.error, e);
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
break;
|
|
75
|
+
}
|
|
76
|
+
case "mousedown": {
|
|
77
|
+
listeners.mousedown = (e) => {
|
|
78
|
+
debugFn?.("mousedown", e);
|
|
79
|
+
if (!manager.options.enableListeners) return;
|
|
80
|
+
const button = e.button.toString();
|
|
81
|
+
const keyIds = getKeyFromEventCode(button, e, manager.keys.entries, { pressedState: false });
|
|
82
|
+
if (keyIds.isError) {
|
|
83
|
+
manager.options.cb(manager, keyIds.error, e);
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
const ids = keyIds.value;
|
|
87
|
+
manager.listener?.({ event: e, isKeydown: true, keys: ids, manager });
|
|
88
|
+
setKeysState(ids, manager, true);
|
|
89
|
+
updateNativeKeysState(manager, e, ids);
|
|
90
|
+
const res = addToChain(manager, ids, e);
|
|
91
|
+
if (res.isError) {
|
|
92
|
+
manager.options.cb(manager, res.error, e);
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
break;
|
|
96
|
+
}
|
|
97
|
+
case "mouseup": {
|
|
98
|
+
listeners.mouseup = (e) => {
|
|
99
|
+
debugFn?.("mouseup", e);
|
|
100
|
+
if (!manager.options.enableListeners) return;
|
|
101
|
+
const button = e.button.toString();
|
|
102
|
+
const keyIds = getKeyFromEventCode(button, e, manager.keys.entries, { pressedState: true });
|
|
103
|
+
if (keyIds.isError) {
|
|
104
|
+
manager.options.cb(manager, keyIds.error, e);
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
const ids = keyIds.value;
|
|
108
|
+
manager.listener?.({ event: e, isKeydown: false, keys: ids, manager });
|
|
109
|
+
setKeysState(ids, manager, false);
|
|
110
|
+
updateNativeKeysState(manager, e, ids);
|
|
111
|
+
const res = removeFromChain(manager, ids, e);
|
|
112
|
+
if (res.isError) {
|
|
113
|
+
manager.options.cb(manager, res.error, e);
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
break;
|
|
117
|
+
}
|
|
118
|
+
case "mouseenter": {
|
|
119
|
+
listeners.mouseenter = (e) => {
|
|
120
|
+
debugFn?.("mouseenter", e);
|
|
121
|
+
if (!manager.options.enableListeners) return;
|
|
122
|
+
manager.listener?.({ event: e, isKeydown: true, keys: [], manager });
|
|
123
|
+
updateNativeKeysState(manager, e, []);
|
|
124
|
+
};
|
|
125
|
+
break;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
return listeners;
|
|
130
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { createManager } from "./createManager.js";
|
|
2
|
+
import type { Manager } from "../types/index.js";
|
|
3
|
+
/**
|
|
4
|
+
* Many methods require some of the manager's options, but not the entire manager. Also it's easier to create a manager from scratch by first creating it's options
|
|
5
|
+
*
|
|
6
|
+
* This can be used to create the base manager options.
|
|
7
|
+
*/
|
|
8
|
+
export declare function createManagerOptions(rawOpts: Parameters<typeof createManager>[0]["options"]): Manager["options"];
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { defaultConditionEquals } from "../defaults/defaultConditionEquals.js";
|
|
2
|
+
import { defaultManagerCallback } from "../defaults/defaultManagerCallback.js";
|
|
3
|
+
import { defaultSorter } from "../defaults/KeysSorter.js";
|
|
4
|
+
import { defaultStringifier } from "../defaults/Stringifier.js";
|
|
5
|
+
export function createManagerOptions(rawOpts) {
|
|
6
|
+
const options = {
|
|
7
|
+
sorter: defaultSorter,
|
|
8
|
+
stringifier: defaultStringifier,
|
|
9
|
+
conditionEquals: defaultConditionEquals,
|
|
10
|
+
cb: defaultManagerCallback,
|
|
11
|
+
enableShortcuts: true,
|
|
12
|
+
enableListeners: true,
|
|
13
|
+
updateStateOnAllEvents: true,
|
|
14
|
+
...rawOpts
|
|
15
|
+
};
|
|
16
|
+
return options;
|
|
17
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { type Result } from "@alanscodelog/utils/Result";
|
|
2
|
+
import type { ChainError, Command, Condition, Manager, MultipleErrors, PickManager, RawShortcut, Shortcut, SHORTCUT_ERROR } from "../types/index.js";
|
|
3
|
+
/**
|
|
4
|
+
* Creates a {@link Shortcut} object from a {@link RawShortcut}.
|
|
5
|
+
*
|
|
6
|
+
* If the raw shortcut has no condition, a new blank empty condition is created. If you will be following the suggestion of not having any conditions equal eachother (see {@link ConditionComparer}), you can fallback to passing the same instance of an empty condition without issues.
|
|
7
|
+
*/
|
|
8
|
+
export declare function createShortcut<TRawCommand extends Command["name"] | Command = Command["name"] | Command, TCommand extends TRawCommand extends string ? Command<TRawCommand> : TRawCommand = TRawCommand extends string ? Command<TRawCommand> : TRawCommand, TCondition extends Condition = Condition>(rawShortcut: RawShortcut<TRawCommand, TCommand, TCondition>, manager: Pick<Manager, "keys" | "commands"> & PickManager<"options", "stringifier" | "sorter">): Result<Shortcut<TCommand["name"], TCondition>, MultipleErrors<ChainError | typeof SHORTCUT_ERROR.UNKNOWN_COMMAND>>;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { Ok } from "@alanscodelog/utils/Result";
|
|
2
|
+
import { isValidShortcut } from "../helpers/isValidShortcut.js";
|
|
3
|
+
export function createShortcut(rawShortcut, manager) {
|
|
4
|
+
const sorter = manager.options.sorter;
|
|
5
|
+
const finalChain = [];
|
|
6
|
+
for (const chord of rawShortcut.chain) {
|
|
7
|
+
const stringChord = chord.map((key) => typeof key === "object" ? key.id : key);
|
|
8
|
+
sorter.sort(stringChord, manager.keys);
|
|
9
|
+
finalChain.push(stringChord);
|
|
10
|
+
}
|
|
11
|
+
const command = typeof rawShortcut.command === "string" ? rawShortcut.command : rawShortcut.command?.name;
|
|
12
|
+
const shortcut = {
|
|
13
|
+
...rawShortcut,
|
|
14
|
+
type: "shortcut",
|
|
15
|
+
enabled: rawShortcut.enabled ?? true,
|
|
16
|
+
command,
|
|
17
|
+
chain: finalChain,
|
|
18
|
+
condition: rawShortcut.condition ?? { type: "condition", text: "" },
|
|
19
|
+
forceUnequal: false
|
|
20
|
+
};
|
|
21
|
+
const res = isValidShortcut(shortcut, manager);
|
|
22
|
+
if (res.isError) return res;
|
|
23
|
+
return Ok(shortcut);
|
|
24
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { type Result } from "@alanscodelog/utils/Result";
|
|
2
|
+
import type { CanHookErrors, Manager, MultipleErrors, PickManager, Shortcut, Shortcuts, ShortcutsSetEntries } from "../types/index.js";
|
|
3
|
+
/**
|
|
4
|
+
* # Shortcut
|
|
5
|
+
*
|
|
6
|
+
* Creates a set of shortcuts.
|
|
7
|
+
*
|
|
8
|
+
*/
|
|
9
|
+
export declare function createShortcuts<THooks extends Manager["hooks"], TRawShortcuts extends Shortcut[], TCheck extends boolean | "only" = true>(shortcutsList: TRawShortcuts, manager: Pick<Manager, "keys" | "commands"> & PickManager<"options", "evaluateCondition" | "conditionEquals" | "stringifier" | "sorter"> & {
|
|
10
|
+
hooks?: THooks;
|
|
11
|
+
}, opts?: Partial<Pick<Shortcuts, "ignoreModifierConflicts" | "ignoreChainConflicts">>, { check }?: {
|
|
12
|
+
check?: boolean | "only";
|
|
13
|
+
}): Result<TCheck extends "only" ? true : Shortcuts, MultipleErrors<ShortcutsSetEntries["entries@add"]["error"]> | CanHookErrors<THooks extends never ? never : THooks, "canSetShortcutsProp">>;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { Ok } from "@alanscodelog/utils/Result";
|
|
2
|
+
import { addShortcut } from "./addShortcut.js";
|
|
3
|
+
export function createShortcuts(shortcutsList, manager, opts, {
|
|
4
|
+
check = true
|
|
5
|
+
} = {}) {
|
|
6
|
+
const shortcuts = {
|
|
7
|
+
entries: [],
|
|
8
|
+
ignoreModifierConflicts: opts?.ignoreModifierConflicts ?? false,
|
|
9
|
+
ignoreChainConflicts: opts?.ignoreChainConflicts ?? false
|
|
10
|
+
};
|
|
11
|
+
const managerClone = { ...manager, shortcuts };
|
|
12
|
+
if (check) {
|
|
13
|
+
for (const shortcut of shortcutsList) {
|
|
14
|
+
const res = addShortcut(shortcut, managerClone);
|
|
15
|
+
if (res.isError) return res;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
if (check === "only") return Ok(true);
|
|
19
|
+
return Ok(shortcuts);
|
|
20
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { AnyFunction } from "@alanscodelog/utils/types";
|
|
2
|
+
import type { AttachTarget, EventTypes } from "../types/index.js";
|
|
3
|
+
/**
|
|
4
|
+
* Detach the listeners from an element.
|
|
5
|
+
*
|
|
6
|
+
* Or you can just use the abort controller returned by {@link attach}.
|
|
7
|
+
*/
|
|
8
|
+
export declare function detach(el: AttachTarget, listeners: Record<EventTypes, AnyFunction>,
|
|
9
|
+
/** Listeners need ot be detached with the same options they were attached with. */
|
|
10
|
+
opts?: Partial<Record<EventTypes, AddEventListenerOptions>>): void;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
export * from "../types/index.js";
|
|
2
|
+
export * as helpers from "../helpers/index.js";
|
|
3
|
+
export * as utils from "../helpers/index.js";
|
|
4
|
+
export { addCommand } from "./addCommand.js";
|
|
5
|
+
export { addKey } from "./addKey.js";
|
|
6
|
+
export { addShortcut } from "./addShortcut.js";
|
|
7
|
+
export { attach } from "./attach.js";
|
|
8
|
+
export { createCommand } from "./createCommand.js";
|
|
9
|
+
export { createCommands } from "./createCommands.js";
|
|
10
|
+
export { createCondition } from "./createCondition.js";
|
|
11
|
+
export { createContext } from "./createContext.js";
|
|
12
|
+
export { createKey } from "./createKey.js";
|
|
13
|
+
export { createKeys } from "./createKeys.js";
|
|
14
|
+
export { createManager } from "./createManager.js";
|
|
15
|
+
export { createManagerEventListeners } from "./createManagerEventListeners.js";
|
|
16
|
+
export { createManagerOptions } from "./createManagerOptions.js";
|
|
17
|
+
export { createShortcut } from "./createShortcut.js";
|
|
18
|
+
export { createShortcuts } from "./createShortcuts.js";
|
|
19
|
+
export { detach } from "./detach.js";
|
|
20
|
+
export { EmulatedEvent } from "./EmulatedEvent.js";
|
|
21
|
+
export { Emulator } from "./Emulator.js";
|
|
22
|
+
export { removeCommand } from "./removeCommand.js";
|
|
23
|
+
export { removeKey } from "./removeKey.js";
|
|
24
|
+
export { removeShortcut } from "./removeShortcut.js";
|
|
25
|
+
export { setCommandProp } from "./setCommandProp.js";
|
|
26
|
+
export { setCommandsProp } from "./setCommandsProp.js";
|
|
27
|
+
export { setKeyProp } from "./setKeyProp.js";
|
|
28
|
+
export { setKeysProp } from "./setKeysProp.js";
|
|
29
|
+
export { setManagerProp } from "./setManagerProp.js";
|
|
30
|
+
export { setShortcutProp } from "./setShortcutProp.js";
|
|
31
|
+
export { setShortcutsProp } from "./setShortcutsProp.js";
|
|
32
|
+
export { ShortcutManagerManager } from "./ShortcutManagerManager.js";
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
export * from "../types/index.js";
|
|
2
|
+
export * as helpers from "../helpers/index.js";
|
|
3
|
+
export * as utils from "../helpers/index.js";
|
|
4
|
+
export { addCommand } from "./addCommand.js";
|
|
5
|
+
export { addKey } from "./addKey.js";
|
|
6
|
+
export { addShortcut } from "./addShortcut.js";
|
|
7
|
+
export { attach } from "./attach.js";
|
|
8
|
+
export { createCommand } from "./createCommand.js";
|
|
9
|
+
export { createCommands } from "./createCommands.js";
|
|
10
|
+
export { createCondition } from "./createCondition.js";
|
|
11
|
+
export { createContext } from "./createContext.js";
|
|
12
|
+
export { createKey } from "./createKey.js";
|
|
13
|
+
export { createKeys } from "./createKeys.js";
|
|
14
|
+
export { createManager } from "./createManager.js";
|
|
15
|
+
export { createManagerEventListeners } from "./createManagerEventListeners.js";
|
|
16
|
+
export { createManagerOptions } from "./createManagerOptions.js";
|
|
17
|
+
export { createShortcut } from "./createShortcut.js";
|
|
18
|
+
export { createShortcuts } from "./createShortcuts.js";
|
|
19
|
+
export { detach } from "./detach.js";
|
|
20
|
+
export { EmulatedEvent } from "./EmulatedEvent.js";
|
|
21
|
+
export { Emulator } from "./Emulator.js";
|
|
22
|
+
export { removeCommand } from "./removeCommand.js";
|
|
23
|
+
export { removeKey } from "./removeKey.js";
|
|
24
|
+
export { removeShortcut } from "./removeShortcut.js";
|
|
25
|
+
export { setCommandProp } from "./setCommandProp.js";
|
|
26
|
+
export { setCommandsProp } from "./setCommandsProp.js";
|
|
27
|
+
export { setKeyProp } from "./setKeyProp.js";
|
|
28
|
+
export { setKeysProp } from "./setKeysProp.js";
|
|
29
|
+
export { setManagerProp } from "./setManagerProp.js";
|
|
30
|
+
export { setShortcutProp } from "./setShortcutProp.js";
|
|
31
|
+
export { setShortcutsProp } from "./setShortcutsProp.js";
|
|
32
|
+
export { ShortcutManagerManager } from "./ShortcutManagerManager.js";
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { type Result } from "@alanscodelog/utils/Result";
|
|
2
|
+
import type { CanHookErrors, Command, CommandsSetEntries, Manager, MultipleErrors } from "../types/index.js";
|
|
3
|
+
export declare function removeCommand<THooks extends Manager["hooks"], TCheck extends boolean | "only" = true>(command: Command, manager: CommandsSetEntries["entries@remove"]["manager"] & {
|
|
4
|
+
hooks?: THooks;
|
|
5
|
+
}, opts?: {
|
|
6
|
+
check?: TCheck;
|
|
7
|
+
}): Result<TCheck extends "only" ? true : Command, MultipleErrors<CommandsSetEntries["entries@remove"]["error"]> | CanHookErrors<THooks extends never ? never : THooks, "canSetCommandsProp">>;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { Ok } from "@alanscodelog/utils/Result";
|
|
2
|
+
import { setCommandsProp } from "./setCommandsProp.js";
|
|
3
|
+
export function removeCommand(command, manager, opts = {}) {
|
|
4
|
+
const res = setCommandsProp("entries@remove", command, manager, opts);
|
|
5
|
+
if (res.isError) return res;
|
|
6
|
+
return Ok(command);
|
|
7
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { type Result } from "@alanscodelog/utils/Result";
|
|
2
|
+
import type { CanHookErrors, Key, KeysSetEntries, Manager, MultipleErrors } from "../types/index.js";
|
|
3
|
+
export declare function removeKey<THooks extends Manager["hooks"], TCheck extends boolean | "only" = true>(key: Key, manager: KeysSetEntries["entries@remove"]["manager"] & {
|
|
4
|
+
hooks?: THooks;
|
|
5
|
+
}, opts?: {
|
|
6
|
+
check?: TCheck;
|
|
7
|
+
}): Result<TCheck extends "only" ? true : Key, MultipleErrors<KeysSetEntries["entries@remove"]["error"]> | CanHookErrors<THooks extends never ? never : THooks, "canSetKeysProp">>;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { Ok } from "@alanscodelog/utils/Result";
|
|
2
|
+
import { setKeysProp } from "./setKeysProp.js";
|
|
3
|
+
export function removeKey(key, manager, opts = {}) {
|
|
4
|
+
const res = setKeysProp("entries@remove", key, manager, opts);
|
|
5
|
+
if (res.isError) return res;
|
|
6
|
+
return Ok(key);
|
|
7
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { type Result } from "@alanscodelog/utils/Result";
|
|
2
|
+
import type { CanHookErrors, Manager, MultipleErrors, Shortcut, ShortcutsSetEntries } from "../types/index.js";
|
|
3
|
+
export declare function removeShortcut<THooks extends Manager["hooks"], TCheck extends boolean | "only" = true>(shortcut: Shortcut, manager: ShortcutsSetEntries["entries@remove"]["manager"] & {
|
|
4
|
+
hooks?: THooks;
|
|
5
|
+
}, opts?: {
|
|
6
|
+
check?: TCheck;
|
|
7
|
+
}): Result<TCheck extends "only" ? true : Shortcut, MultipleErrors<ShortcutsSetEntries["entries@remove"]["error"]> | CanHookErrors<THooks extends never ? never : THooks, "canSetShortcutsProp">>;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { Ok } from "@alanscodelog/utils/Result";
|
|
2
|
+
import { setShortcutsProp } from "./setShortcutsProp.js";
|
|
3
|
+
export function removeShortcut(shortcut, manager, opts = {}) {
|
|
4
|
+
const res = setShortcutsProp("entries@remove", shortcut, manager, opts);
|
|
5
|
+
if (res.isError) return res;
|
|
6
|
+
return Ok(shortcut);
|
|
7
|
+
}
|