forked from tarnas14/vimflowy
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathstate.js
108 lines (97 loc) · 2.84 KB
/
state.js
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
104
105
106
107
108
const stateClosure = (initialState = {}, stateChanged = () => {}) => {
let s = initialState
return {
set: stateReducer => {
s = Object.assign({}, s, stateReducer(s))
stateChanged()
},
get: () => Object.assign({}, s)
}
}
const Mode = {
NORMAL: 'NORMAL',
INSERT: 'INSERT',
VISUAL: 'VISUAL',
REPLACE: 'REPLACE',
FIND: 'FIND',
INNER: 'INNER',
AFTER: 'AFTER'
}
const state = stateClosure({
mode: Mode.NORMAL,
anchorOffset: 0,
debug: false
}
)
window.toggleDebugging = () => state.set(s => ({
debug: !s.debug
}))
const debug = (...args) => state.get().debug && console.log(...args)
const modeClosure = (getState, setState) => {
const indicatorElement = document.createElement('div')
indicatorElement.setAttribute('style', 'position: fixed; z-index:9001; bottom:0; left: 0; background-color: grey; color: white; padding: .3em; font-family: sans-serif;')
indicatorElement.innerHTML = 'NORMAL'
document.querySelector('body').append(indicatorElement)
let timerId = null
const setMode = modeText => {
clearTimeout(timerId)
indicatorElement.innerHTML = modeText
}
return {
flashMode: (temporaryMode, duration = 1000) =>
{
setMode(temporaryMode)
timerId = setTimeout(() => { indicatorElement.innerHTML = getState().mode }, duration)
},
goToInsertMode: (cursorRight = false) =>
{
setState(s => ({mode: Mode.INSERT}))
setMode(Mode.INSERT)
document.getSelection().modify('extend', 'left', 'character')
if (cursorRight)
{
document.getSelection().modify('move', 'right', 'character')
}
},
goToNormalMode: () =>
{
setState(s => ({mode: Mode.NORMAL}))
setMode(Mode.NORMAL)
},
goToVisualMode: () =>
{
setState(s => ({mode: Mode.VISUAL}))
setMode(Mode.VISUAL)
},
goToReplaceMode: () =>
{
setState(s => ({mode: Mode.REPLACE}))
setMode(Mode.REPLACE)
},
goToFindMode: () =>
{
setState(s => ({mode: Mode.FIND}))
setMode(Mode.FIND)
},
goToInnerMode: () =>
{
setState(s => ({mode: Mode.INNER}))
setMode(Mode.INNER)
// copy over the buffer in case it gets closed
// (which we usually do when dealing with sequences)
// @TODO: move this to actual 'ci' binding if chrome
// starts throwing errors...
keyBufferTempCopy = keyBuffer;
},
goToAfterMode: () =>
{
setState(s => ({mode: Mode.AFTER}))
setMode(Mode.AFTER)
// copy over the buffer in case it gets closed
// (which we usually do when dealing with sequences)
// @TODO: move this to actual 'di' binding if chrome
// starts throwing errors...
keyBufferTempCopy = keyBuffer;
}
}
}