-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathactionUtils.ts
More file actions
103 lines (82 loc) · 2.77 KB
/
actionUtils.ts
File metadata and controls
103 lines (82 loc) · 2.77 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import { KeyBindingMap } from 'tinykeys';
import { rootParentActionId } from '../constants';
import { ActionId, Actions, ActionsContext, StoreMethods, WrappedAction, WrappedActionList } from '../types';
import { DeepReadonly } from 'solid-js/store';
type RunStoreMethods = {
selectParentAction: StoreMethods['selectParentAction'];
closePalette: StoreMethods['closePalette'];
openPalette: StoreMethods['openPalette'];
};
function getActionContext(action: WrappedAction, actionsContext: ActionsContext) {
const rootContext = actionsContext.root;
const dynamicContext = actionsContext.dynamic[action.id] || {};
return {
rootContext,
dynamicContext,
};
}
export function checkActionAllowed(action: WrappedAction, actionsContext: ActionsContext) {
if (!action.cond) {
return true;
}
const { rootContext, dynamicContext } = getActionContext(action, actionsContext);
const isAllowed = action.cond({ actionId: action.id, rootContext, dynamicContext });
return isAllowed;
}
export function runAction(
action: WrappedAction,
actionsContext: ActionsContext,
storeMethods: RunStoreMethods,
invokedBy: 'shortcut' | 'palette'
) {
const { id, run } = action;
if (!run) {
storeMethods.selectParentAction(id);
if (invokedBy === 'shortcut') {
storeMethods.openPalette();
}
return;
}
const { rootContext, dynamicContext } = getActionContext(action, actionsContext);
run({ actionId: id, rootContext, dynamicContext });
storeMethods.closePalette();
}
export function getShortcutHandlersMap(
actionsList: WrappedActionList,
actionsContext: ActionsContext,
storeMethods: StoreMethods
) {
const shortcutMap: KeyBindingMap = {};
actionsList.forEach((action) => {
const actionHandler = (event: KeyboardEvent) => {
const targetElem = event.target as HTMLElement;
const shortcutsAttr = targetElem.dataset.cpKbdShortcuts;
if (shortcutsAttr === 'disabled') {
return;
}
const isAllowed = checkActionAllowed(action, actionsContext);
if (!isAllowed) {
return;
}
event.preventDefault();
runAction(action, actionsContext, storeMethods, 'shortcut');
};
const shortcut = action.shortcut;
if (shortcut) {
shortcutMap[shortcut] = actionHandler;
}
});
return shortcutMap;
}
type ActiveParentActionIdListArg = Readonly<Array<ActionId>>;
export function getParentAction(action: WrappedAction, actions: DeepReadonly<Actions>) {
return Object.values(actions).filter(({id}) => id === action.parentActionId)[0]
}
export function getActiveParentAction(activeParentActionIdList: ActiveParentActionIdListArg) {
const activeId = activeParentActionIdList.at(-1) || rootParentActionId;
const isRoot = activeId === rootParentActionId;
return {
activeId,
isRoot,
};
}