|
1 | 1 | import { ViewStoreAction } from 'src/stores/view/view-store-actions'; |
2 | 2 |
|
3 | | -type ActionType = ViewStoreAction['type']; |
4 | | -const navigationEvents = new Set<ActionType>([ |
5 | | - 'view/set-active-node/history/select-previous', |
6 | | - 'view/set-active-node/history/select-next', |
7 | | - 'view/set-active-node/sequential/select-next', |
8 | | -]); |
9 | | - |
10 | | -const searchEvents = new Set<ActionType>([ |
11 | | - 'view/search/set-query', |
12 | | - 'view/search/set-results', |
13 | | - 'view/search/toggle-input', |
14 | | -]); |
15 | | - |
16 | | -const stateEvents = new Set<ActionType>([ |
17 | | - 'view/set-active-node/document', |
18 | | - 'view/set-active-node/mouse', |
19 | | - 'view/set-active-node/mouse-silent', |
20 | | - 'view/set-active-node/search', |
21 | | - 'view/set-active-node/keyboard', |
22 | | - 'view/set-active-node/keyboard-jump', |
23 | | - 'view/selection/select-all', |
24 | | -]); |
25 | | - |
26 | | -const editMainSplitEvents = new Set<ActionType>([ |
27 | | - 'view/editor/enable-main-editor', |
28 | | - 'view/editor/disable-main-editor', |
29 | | -]); |
30 | | -const editSidebarEvents = new Set<ActionType>([ |
31 | | - 'view/editor/enable-sidebar-editor', |
32 | | - 'view/editor/disable-sidebar-editor', |
33 | | -]); |
34 | | - |
35 | 3 | export type ViewEventType = { |
36 | 4 | activeNodeHistory?: boolean; |
37 | 5 | activeNode?: boolean; |
38 | 6 | search?: boolean; |
39 | | - editMainSplit?: boolean; |
40 | | - editSidebar?: boolean; |
| 7 | + mainEditor?: boolean; |
| 8 | + sidebarEditor?: boolean; |
41 | 9 | }; |
42 | | -const cachedResults: { [key: string]: ViewEventType } = {}; |
43 | 10 |
|
44 | | -export const getViewEventType = (type: ActionType): ViewEventType => { |
45 | | - if (cachedResults[type]) { |
46 | | - return cachedResults[type]; |
47 | | - } |
48 | | - |
49 | | - let result: ViewEventType | null = null; |
50 | | - |
51 | | - if (navigationEvents.has(type)) result = { activeNodeHistory: true }; |
52 | | - else if (stateEvents.has(type)) result = { activeNode: true }; |
53 | | - else if (searchEvents.has(type)) result = { search: true }; |
54 | | - else if (editMainSplitEvents.has(type)) result = { editMainSplit: true }; |
55 | | - else if (editSidebarEvents.has(type)) result = { editSidebar: true }; |
56 | | - if (!result) result = {}; |
| 11 | +type ActionType = ViewStoreAction['type']; |
57 | 12 |
|
58 | | - cachedResults[type] = result; |
| 13 | +const eventTypesDictionary: Partial<Record<ActionType, ViewEventType>> = { |
| 14 | + 'view/set-active-node/history/select-previous': { |
| 15 | + activeNodeHistory: true, |
| 16 | + }, |
| 17 | + 'view/set-active-node/history/select-next': { activeNodeHistory: true }, |
| 18 | + 'view/set-active-node/sequential/select-next': { |
| 19 | + activeNodeHistory: true, |
| 20 | + }, |
| 21 | + 'view/search/set-query': { search: true }, |
| 22 | + 'view/search/set-results': { search: true }, |
| 23 | + 'view/search/toggle-input': { search: true }, |
| 24 | + 'view/set-active-node/document': { activeNode: true }, |
| 25 | + 'view/set-active-node/mouse': { activeNode: true }, |
| 26 | + 'view/set-active-node/mouse-silent': { activeNode: true }, |
| 27 | + 'view/set-active-node/search': { activeNode: true }, |
| 28 | + 'view/set-active-node/keyboard': { activeNode: true }, |
| 29 | + 'view/set-active-node/keyboard-jump': { activeNode: true }, |
| 30 | + 'view/selection/select-all': { activeNode: true }, |
| 31 | + 'view/editor/enable-main-editor': { mainEditor: true }, |
| 32 | + 'view/editor/disable-main-editor': { mainEditor: true }, |
| 33 | + 'view/editor/enable-sidebar-editor': { sidebarEditor: true }, |
| 34 | + 'view/editor/disable-sidebar-editor': { sidebarEditor: true }, |
| 35 | +} as const; |
| 36 | + |
| 37 | +const viewEventTypes = new Map(Object.entries(eventTypesDictionary)) as Map< |
| 38 | + ActionType, |
| 39 | + ViewEventType |
| 40 | +>; |
| 41 | + |
| 42 | +const none = {}; |
59 | 43 |
|
60 | | - return result; |
| 44 | +export const getViewEventType = (type: ActionType): ViewEventType => { |
| 45 | + return viewEventTypes.get(type) || none; |
61 | 46 | }; |
0 commit comments