-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontent.js
More file actions
657 lines (640 loc) · 26.3 KB
/
content.js
File metadata and controls
657 lines (640 loc) · 26.3 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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
const MODES = {
NORMAL: 'NORMAL',
INSERT: 'INSERT',
VISUAL: 'VISUAL',
VISUAL_LINE: 'VISUAL_LINE'
};
let modeData = {
currentMode: MODES.NORMAL
};
let modeProxy = new Proxy(modeData, {
set: function (target, key, value) {
target[key] = value;
//holy shit the line above has to come first. took me like 40 min to find this bug ugh
if (key === 'currentMode') {
updateModeIndicator();
}
return true;
}
});
let isShiftHeld = false;
let isAltHeld = false;
let isCmdHeld = false;
let userCursor = null;
let cursorCaret = null;
let lastKeyPressed = null;
function isMacOS() {
return navigator.userAgent.indexOf('Mac') !== -1;
}
/**
* Adjust the width of the cursor caret.
* @param {boolean} isWide - If true, set to wide width; otherwise, set to calculated width.
*/
function setCursorWidth(mode) {
if (cursorCaret) {
isWide = null;
if (mode === 'INSERT') {
isWide = true;
}
else if (mode === 'NORMAL') {
isWide = false;
}
else if (mode === 'VISUAL' || mode === MODES.VISUAL_LINE) {
isWide = false;
};
if (isWide) {
cursorCaret.style.borderWidth = "2px";
} else {
const scaleFactor = 0.416;
const caretHeight = parseFloat(cursorCaret.style.height.slice(0, -2));
const calculatedWidth = scaleFactor * caretHeight;
cursorCaret.style.borderWidth = `${calculatedWidth}px`;
}
}
}
function addModeIndicator() {
const indicator = document.createElement('div');
indicator.id = 'vim-mode-indicator';
indicator.style.position = 'fixed';
indicator.style.bottom = '10px';
indicator.style.left = '10px';
indicator.style.padding = '5px 10px';
indicator.style.background = 'rgba(0, 0, 0, 0.6)';
indicator.style.color = 'white';
indicator.style.borderRadius = '5px';
indicator.style.zIndex = '9999'; // Ensure it's on top
indicator.textContent = modeProxy.currentMode === MODES.VISUAL_LINE ? "VISUAL LINE" : modeProxy.currentMode;
document.body.appendChild(indicator);
}
// This function updates the mode indicator's content
function updateModeIndicator() {
const indicator = document.getElementById('vim-mode-indicator');
if (indicator) {
indicator.textContent = modeProxy.currentMode === MODES.VISUAL_LINE ? "VISUAL LINE" : modeProxy.currentMode;
setCursorWidth(modeProxy.currentMode);
if (modeProxy.currentMode === MODES.VISUAL || modeProxy.currentMode === MODES.VISUAL_LINE) {
isShiftHeld = true;
} else {
isShiftHeld = false;
}
} else {
addModeIndicator();
}
}
// Initialize the mode indicator when the extension loads
addModeIndicator();
let iframe = document.querySelector('.docs-texteventtarget-iframe');
let textTarget = iframe ? iframe.contentDocument.querySelector('[contenteditable="true"]') : null;
function getTextTarget() {
if (!textTarget) {
iframe = document.querySelector('.docs-texteventtarget-iframe');
textTarget = iframe ? iframe.contentDocument.querySelector('[contenteditable="true"]') : null;
}
return textTarget;
}
// This function will be called periodically until the iframe and its content-editable child are found
function checkForTargetElements() {
const target = getTextTarget();
if (!userCursor || !cursorCaret) {
userCursor = document.querySelector(".kix-cursor");
cursorCaret = document.querySelector(".kix-cursor-caret");
if (userCursor && cursorCaret && modeProxy.currentMode === MODES.NORMAL) {
setCursorWidth(modeProxy.currentMode);
}
}
if (textTarget) {
attachKeyListener(textTarget);
clearInterval(intervalId);
}
}
// Periodically check every 500ms until the target elements are found
const intervalId = setInterval(checkForTargetElements, 500);
let replacementPending = false;
let deletionPending = false;
let insideDeletion = false;
function attachKeyListener(element) {
element.addEventListener('keydown', (event) => {
console.log('Key pressed:', event.key);
if (replacementPending || deletionPending) {
// A replacement is pending, don't process in this main handler
return;
}
if (modeProxy.currentMode === MODES.NORMAL) {
event.preventDefault();
switch (event.key) {
case 'i':
modeProxy.currentMode = MODES.INSERT;
break;
case 'a':
simulateKeyPress('ArrowRight');
modeProxy.currentMode = MODES.INSERT;
break;
case 'h':
simulateKeyPress('ArrowLeft')
break;
case 'j':
simulateKeyPress('ArrowDown')
break;
case 'k':
simulateKeyPress('ArrowUp')
break;
case 'l':
simulateKeyPress('ArrowRight')
break;
case 'w':
//next word
if (isMacOS()) {
simulateKeyPress('ArrowRight',false,true);
simulateKeyPress('ArrowRight');
break;
}
performAction('ArrowRight', true);
break;
case 'b':
//prev word
if (isMacOS()) {
simulateKeyPress('ArrowLeft', false, true);
break;
}
performAction('ArrowLeft', true);
break;
case '0':
//need to add if(at beginning of line) do nothing
if (isMacOS()) {
simulateKeyPress('ArrowUp', false, true);
break;
}
performAction('ArrowUp', true)
break;
case '$':
//end of line
//implement if at end of line, don't do anything
if (isMacOS()) {
simulateKeyPress('ArrowDown', false, true);
} else {
performAction('ArrowDown', true);
performAction('ArrowLeft', true);
}
break;
case 'I':
//insert at beginning of line
if (isMacOS()) {
simulateKeyPress('ArrowUp', false, true);
}
else {
performAction('ArrowUp', true);
}
modeProxy.currentMode = MODES.INSERT;
break;
case 'A':
//insert at end of line
if (isMacOS()) {
simulateKeyPress('ArrowDown', false, true);
}
else {
performAction('ArrowLeft',true);
}
modeProxy.currentMode = MODES.INSERT;
break;
case 'g':
//there's a small bug where if you do "Gg" it will go to the beginning of the document when it's not supposed to
if (lastKeyPressed === 'g') {
if (isMacOS()) {
isCmdHeld = true;
simulateKeyPress('ArrowUp');
isCmdHeld = false;
break;
}
simulateKeyPress('Home', true);
break;
} else {
lastKeyPressed = 'g';
}
break;
default:
if (deletionPending || replacementPending) {
event.stopPropagation();
event.preventDefault();
}
break;
case 'G':
if (isMacOS()) {
isCmdHeld = true;
simulateKeyPress('ArrowDown');
isCmdHeld = false;
break;
}
simulateKeyPress('End', true);
break;
case 'v':
isShiftHeld = true;
simulateKeyPress('ArrowRight');
modeProxy.currentMode = MODES.VISUAL;
break;
case 'V':
if (isMacOS()) {
isCmdHeld = true;
simulateKeyPress('ArrowLeft');
isCmdHeld = false;
} else {
simulateKeyPress('Home');
}
isShiftHeld = true; // Keep shift held for selection
if (isMacOS()) {
isCmdHeld = true;
simulateKeyPress('ArrowRight');
isCmdHeld = false;
} else {
simulateKeyPress('End');
}
modeProxy.currentMode = MODES.VISUAL_LINE;
break;
case 'x':
simulateKeyPress('ArrowRight');
simulateKeyPress('Backspace');
break;
case 'r':
replacementPending = true;
const charListener = (charEvent) => {
if (charEvent.key === "Shift") {
return;
}
replacementPending = false;
charEvent.stopPropagation(); // Stop propagation of the event
charEvent.preventDefault(); // Prevent default behavior
element.removeEventListener('keydown', charListener);
simulateKeyPress('ArrowRight');
simulateKeyPress('Backspace');
simulateCharacter(charEvent.key);
simulateKeyPress('ArrowLeft');
};
element.addEventListener('keydown', charListener);
break;
case 'd':
deletionPending = true;
const deletionListener = (deletionEvent) => {
if (insideDeletion) {
return;
}
insideDeletion = true;
deletionPending = false;
deletionEvent.stopPropagation();
deletionEvent.preventDefault();
if (deletionEvent.key === 'd') {
if (isMacOS()) {
//doesn't work if at beginning of line
simulateKeyPress('ArrowUp',false,true);
isShiftHeld = true;
simulateKeyPress('ArrowDown',false,true);
isShiftHeld = false;
simulateKeyPress('Backspace');
}
else {
simulateKeyPress('ArrowDown', true);
simulateKeyPress('ArrowLeft');
isShiftHeld = true;
simulateKeyPress('ArrowUp', true);
simulateKeyPress('Backspace');
isShiftHeld = false;
}
}
else if (deletionEvent.key === 'w') {
if (isMacOS()) {
isShiftHeld = true;
simulateKeyPress('ArrowRight',false,true);
isShiftHeld = false;
simulateKeyPress('Backspace');
}
else {
isShiftHeld = true;
simulateKeyPress('ArrowRight', true);
isShiftHeld = false;
simulateKeyPress('Backspace');
}
}
insideDeletion = false;
element.removeEventListener('keydown', deletionListener);
};
element.addEventListener('keydown', deletionListener);
break;
case 'D':
if (!isAtEndOfLine()) {
if (isMacOS()) {
isShiftHeld = true;
simulateKeyPress('ArrowDown', false, true);
isShiftHeld = false;
} else {
isShiftHeld = true;
simulateKeyPress('End');
isShiftHeld = false;
}
simulateKeyPress('Backspace');
}
break;
case 'c':
deletionPending = true;
const deletionListener1 = (deletionEvent) => {
if (insideDeletion) {
return;
}
insideDeletion = true;
deletionPending = false;
deletionEvent.stopPropagation();
deletionEvent.preventDefault();
if (deletionEvent.key === 'w') {
if (isMacOS()) {
isShiftHeld = true;
simulateKeyPress('ArrowRight',false,true);
isShiftHeld = false;
simulateKeyPress('Backspace');
modeProxy.currentMode = MODES.INSERT;
}
else {
isShiftHeld = true;
simulateKeyPress('ArrowRight', true);
isShiftHeld = false;
simulateKeyPress('Backspace');
modeProxy.currentMode = MODES.INSERT;
}
}
insideDeletion = false;
element.removeEventListener('keydown', deletionListener1);
};
element.addEventListener('keydown', deletionListener1);
break;
case 'v':
isShiftHeld = true;
simulateKeyPress('ArrowRight');
modeProxy.currentMode = MODES.VISUAL;
break;
case 'o':
//doesn't work at the end of a line
simulateKeyPress('$');
simulateKeyPress('Enter');
modeProxy.currentMode = MODES.INSERT;
break;
case 'O':
//doesn't work at beginning of line because of '0' bug
simulateKeyPress('0');
simulateKeyPress('Enter');
simulateKeyPress('ArrowUp');
modeProxy.currentMode = MODES.INSERT;
break;
case 'e':
simulateKeyPress('w');
simulateKeyPress('ArrowLeft');
simulateKeyPress('ArrowLeft');
break;
case 'u':
if (isMacOS()) {
isCmdHeld = true;
simulateKeyPress('z');
isCmdHeld = false;
break;
}
simulateKeyPress('z', true);
break;
}
}
else if (modeProxy.currentMode === MODES.VISUAL) {
event.preventDefault();
switch (event.key) {
case 'Escape':
isShiftHeld = false;
simulateKeyPress('ArrowRight');
simulateKeyPress('ArrowLeft');
modeProxy.currentMode = MODES.NORMAL;
break;
case 'h':
simulateKeyPress('ArrowLeft');
break;
case 'j':
simulateKeyPress('ArrowDown')
break;
case 'k':
simulateKeyPress('ArrowUp')
break;
case 'l':
simulateKeyPress('ArrowRight')
break;
case 'w':
//next word
if (isMacOS()) {
simulateKeyPress('ArrowRight',false,true);
simulateKeyPress('ArrowRight')
}
else {
performAction('ArrowRight', true);
}
break;
case 'b':
//prev word
if (isMacOS()) {
simulateKeyPress('ArrowLeft',false,true);
}
else {
performAction('ArrowLeft', true);
}
break;
case '0':
//need to add if(at beginning of line) do nothing
if (isMacOS()) {
simulateKeyPress('ArrowUp',false,true);
}
else {
performAction('ArrowUp', true);
}
break;
case '$':
//end of line
if (isMacOS()) {
simulateKeyPress('ArrowDown',false,true);
}
else {
performAction('ArrowDown', true);
}
break;
case 'g':
//there's a small bug where if you do "Gg" it will go to the beginning of the document when it's not supposed to
if (lastKeyPressed === 'g') {
if (isMacOS()) {
isCmdHeld = true;
simulateKeyPress('ArrowUp');
isCmdHeld = false;
break;
}
simulateKeyPress('Home', true);
break;
} else {
lastKeyPressed = 'g';
}
break;
case 'd':
simulateKeyPress('Backspace');
case 'c':
default:
if (deletionPending || replacementPending) {
event.stopPropagation();
event.preventDefault();
}
break;
case 'G':
if (isMacOS()) {
isCmdHeld = true;
simulateKeyPress('ArrowDown');
isCmdHeld = false;
break;
}
simulateKeyPress('End', true);
break;
case 'y':
if (iframe && iframe.contentDocument) {
try {
const success = iframe.contentDocument.execCommand('copy');
if (success) {
console.log('Content copied to clipboard from VISUAL mode');
} else {
console.warn('Copy command was not successful in VISUAL mode. Ensure text is selected.');
}
} catch (err) {
console.error('Failed to execute copy command in VISUAL mode:', err);
}
} else {
console.warn('Iframe or contentDocument not found for copy operation in VISUAL mode.');
}
modeProxy.currentMode = MODES.NORMAL;
break;
}
}
else if (modeProxy.currentMode === MODES.VISUAL_LINE) {
event.preventDefault();
switch (event.key) {
case 'j':
simulateKeyPress('ArrowDown');
break;
case 'k':
// isShiftHeld is true because we are in VISUAL_LINE mode.
if (isMacOS()) {
// Simulate Cmd+ArrowLeft to go to the beginning of the current visual line.
// Note: The existing '0' command uses simulateKeyPress('ArrowUp', false, true) for macOS,
// which might be "go to top of paragraph/document". We need true "start of line".
// Let's assume for now that 'ArrowLeft' with Cmd is the correct "start of line" for selection purposes.
// If textTarget.dispatchKeyEvent for 'moveFocusToStartOfLine' or similar exists, that'd be better,
// but we're using existing simulateKeyPress.
// We need to ensure 'ArrowLeft' with Cmd is what we want for "start of visual line".
// The existing '0' command for macOS is: simulateKeyPress('ArrowUp', false, true);
// The existing '$' command for macOS is: simulateKeyPress('ArrowDown', false, true);
// These might be more like "go to start/end of paragraph/block" rather than visual line.
// Let's try to use the Mac standard "Command + Left Arrow" for start of line.
// This means we need to set isCmdHeld = true temporarily.
isCmdHeld = true;
simulateKeyPress('ArrowLeft'); // Cmd+ArrowLeft
isCmdHeld = false;
simulateKeyPress('ArrowUp'); // Shift+ArrowUp (isShiftHeld is true globally for VISUAL_LINE)
} else {
simulateKeyPress('Home'); // Home key for non-MacOS (go to start of line)
simulateKeyPress('ArrowUp'); // Shift+ArrowUp
}
break;
case 'y':
if (iframe && iframe.contentDocument) {
try {
const success = iframe.contentDocument.execCommand('copy');
if (success) {
console.log('Content copied to clipboard');
} else {
console.warn('Copy command was not successful. Ensure text is selected.');
}
} catch (err) {
console.error('Failed to execute copy command:', err);
}
} else {
console.warn('Iframe or contentDocument not found for copy operation.');
}
modeProxy.currentMode = MODES.NORMAL;
break;
case 'Escape':
isShiftHeld = false; // Crucial: do this *before* simulating keys
// Simulate a slight cursor movement to ensure selection is cleared.
// ArrowLeft might be safer if we want to stay on the same line, near the start.
simulateKeyPress('ArrowLeft');
modeProxy.currentMode = MODES.NORMAL;
break;
default:
break;
}
}
else if (modeProxy.currentMode === MODES.INSERT && event.key === 'Escape') {
modeProxy.currentMode = MODES.NORMAL;
}
});
//element.addEventListener('blur', () => {
// modeProxy.currentMode = MODES.NORMAL;
// updateModeIndicator();
//});
}
const KEY_CODES = {
'End': 35,
'Home': 36,
'ArrowLeft': 37,
'ArrowUp': 38,
'ArrowRight': 39,
'ArrowDown': 40,
'Backspace': 8,
'Enter': 13,
'Shift': 16,
'Cmd': 91,
'Option': 18
};
function simulateKeyPress(keyval, ctrlval = false, optionval = false) {
const target = getTextTarget();
if (target) {
const keyCode = KEY_CODES[keyval] || keyval.keyCode;
const simulatedEvent = new KeyboardEvent('keydown', {
key: keyval,
keyCode: keyCode, // KeyCode for 'Home' key
which: keyCode, // 'which' for compatibility
ctrlKey: ctrlval,
shiftKey: isShiftHeld,
altKey: optionval, // Option key pressed if optionval is true
metaKey: isCmdHeld,
bubbles: true,
cancelable: true
});
target.dispatchEvent(simulatedEvent);
}
}
function performAction(key, ctrlval = false) {
if (isMacOS()) {
isAltHeld = true;
simulateKeyPress(key);
isAltHeld = false;
} else {
simulateKeyPress(key, ctrlval);
console.log(key, ctrlval);
}
}
function simulateCharacter(letter) {
const target = getTextTarget();
if (target) {
const simulatedEvent = new KeyboardEvent('keypress', {
key: letter,
charCode: letter.charCodeAt(0),
bubbles: true,
cancelable: true
});
target.dispatchEvent(simulatedEvent);
}
}
function isAtEndOfLine() {
const selection = window.getSelection();
if (!selection.rangeCount) return false;
const range = selection.getRangeAt(0);
if (!range.collapsed) return false; // Ensure the range is a cursor (collapsed range)
// Create a temporary range to select all content from the cursor to the end of the node
const tempRange = range.cloneRange();
tempRange.selectNodeContents(range.endContainer);
tempRange.setStart(range.endOffset, 0);
// Check if the temporary range's content is empty (i.e., cursor is at the end)
return tempRange.toString() === '';
}