forked from tridactyl/tridactyl
/
content_state.ts
61 lines (50 loc) · 1.4 KB
/
content_state.ts
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
import Logger from "./logging"
const logger = new Logger("state")
export type ModeName =
| "normal"
| "insert"
| "hint"
| "ignore"
| "gobble"
| "input"
| "find"
export class PrevInput {
inputId: string
tab: number
jumppos?: number
}
class State {
mode: ModeName = "normal"
cmdHistory: string[] = []
prevInputs: PrevInput[] = [
{
inputId: undefined,
tab: undefined,
jumppos: undefined,
},
]
}
export type ContentStateProperty =
| "mode"
| "cmdHistory"
| "prevInputs"
export type ContentStateChangedCallback =
(property: ContentStateProperty, oldValue: any, newValue: any) => void
const onChangedListeners: ContentStateChangedCallback[] = []
export function addContentStateChangedListener(callback: ContentStateChangedCallback) {
onChangedListeners.push(callback)
}
export const contentState = (new Proxy({"mode": "normal"}, {
get: function(target, property: ContentStateProperty) {
return target[property]
},
set: function(target, property: ContentStateProperty, newValue) {
logger.debug("Content state changed!", property, newValue)
const oldValue = target[property]
target[property] = newValue
for (let listener of onChangedListeners) {
listener(property, oldValue, newValue)
}
return true
},
}) as any) as State