-
Notifications
You must be signed in to change notification settings - Fork 0
/
bundle.js
209 lines (166 loc) · 4.57 KB
/
bundle.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
'use strict';
// console.log('VIM typer!');
class Mode {
deactivate() {
}
activate() {
}
}
class ModeManager {
constructor() {
this.currentMode = new EmptyMode();
}
changeMode( newMode ) {
if ( this.currentMode.constructor === newMode.constructor ) {
return;
}
if ( this.currentMode ) {
this.currentMode.deactivate();
}
this.currentMode = newMode;
this.currentMode.activate();
}
anyMode() {
return !( this.currentMode instanceof EmptyMode );
}
}
class InsertMode extends Mode {
constructor( moveAfter ) {
super();
if ( !getCarretStart( document.activeElement ) ) {
return;
}
let currentPos = getCarretStart( document.activeElement );
if ( moveAfter ) {
currentPos++;
}
setSelection( currentPos, currentPos, document.activeElement );
// console.log('constructor insertu');
}
activate() {
super.activate();
// console.log('activate insertu');
}
deactivate() {
super.deactivate();
// console.log('deactiv inseru');
}
}
class NavigationMode extends Mode {
constructor() {
super();
// console.log('navmode constructor');
}
activate() {
if ( !getCarretStart( document.activeElement ) ) {
return;
}
super.activate();
// console.log('activate nav mode');
this.keyListener = ( e ) => { this.handleKeys( e ); };
document.addEventListener( 'keydown', this.keyListener );
let pos = getCarretStart( document.activeElement );
const content = getContent( document.activeElement );
this.leftOffset = RecalculateLeftOffset( pos, content );
const [ start, end ] = InitializeCarret( pos, content );
setSelection( start, end, document.activeElement );
}
deactivate() {
super.deactivate();
// console.log('deactivate nav mode');
document.removeEventListener( 'keydown', this.keyListener );
}
handleKeys( e ) {
const content = getContent( document.activeElement );
const pos = getCarretStart( document.activeElement );
this.leftOffset = RecalculateLeftOffset( pos, content );
let start, end = -1;
if ( e.key === 'e' ) {
// Move carrate to the end of next word
const endOfWordPosition = FindEndOfWord( pos, content );
start = endOfWordPosition;
end = start + 1;
}
if ( [ 'h', 'j', 'k', 'l' ].includes( e.key ) ) {
if ( e.key === 'h' ) {
[ start, end ] = MoveCarret( pos, pos - 1, content );
this.leftOffset = RecalculateLeftOffset( start, content );
}
if ( e.key === 'l' ) {
[ start, end ] = MoveCarret( pos, pos + 1, content );
this.leftOffset = RecalculateLeftOffset( start, content );
}
if ( e.key === 'k' ) {
[ start, end ] = CalculateHorizontal( pos, -1, this.leftOffset, content );
}
if ( e.key === 'j' ) {
[ start, end ] = CalculateHorizontal( pos, 1, this.leftOffset, content );
}
}
setSelection( start, end, document.activeElement );
e.preventDefault();
}
}
class EmptyMode extends Mode {
deactivate() {
super.deactivate();
// console.log('deactiv empty');
}
activate() {
super.activate();
// console.log('active empty');
const pos = getCarretStart( document.activeElement );
setSelection( pos, pos, document.activeElement );
}
}
const modeMgr = new ModeManager();
document.addEventListener( 'keydown', HandlePluginToggle );
function HandlePluginToggle( e ) {
if (
// Alt + v
( e.altKey && e.key === 'v' ) ||
// On mac: Cmd key + option key
( e.altKey && ( e.key === 'Meta' && e.code === 'MetaLeft' ) )
) {
if ( modeMgr.anyMode() ) {
modeMgr.changeMode( new EmptyMode() );
setIndicator( false );
//unpin all listeners etc....
} else {
modeMgr.changeMode( new NavigationMode() );
setIndicator( true );
}
return;
}
if ( modeMgr.anyMode() ) {
if ( e.key === 'Escape' && ! ( modeMgr.currentMode instanceof NavigationMode ) ) {
modeMgr.changeMode( new NavigationMode() );
}
if ( modeMgr.currentMode instanceof NavigationMode && ( e.key === 'i' || e.key === 'a' ) ) {
modeMgr.changeMode( new InsertMode( e.key === 'a' ) );
e.preventDefault();
}
}
}
function getContent( element ) {
if ( element.tagName === 'TEXTAREA' ) {
return element.value;
} else {
return element.innerHTML;
}
}
let indicator;
function createIndicator() {
indicator = document.createElement( 'div' );
indicator.style.width = indicator.style.height = '5px';
indicator.style.position = 'fixed';
indicator.style.left = indicator.style.top = '0';
indicator.style.background = 'red';
document.body.appendChild( indicator );
}
function setIndicator( isActive ) {
indicator.style.background = isActive ? 'green' : 'red';
}
window.addEventListener( 'DOMContentLoaded', function( event ) {
createIndicator();
} );