-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
318 lines (277 loc) · 9.43 KB
/
Copy pathcontent.js
File metadata and controls
318 lines (277 loc) · 9.43 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
let enabled = true;
chrome.storage.local.get("enabled", function (data) {
enabled = data.enabled !== undefined ? data.enabled : true;
});
chrome.storage.onChanged.addListener(function (changes, areaName) {
if (areaName !== "local" || !changes.enabled) return;
enabled = changes.enabled.newValue;
});
document.addEventListener("keydown", function (e) {
if (!enabled) return;
if (e.key !== "Home" && e.key !== "End") return;
if (e.altKey || e.metaKey) return;
const context = getEditingContext(e);
if (context.kind === "none") return;
if (context.kind === "input" && !supportsSelectionNavigation(context.element)) {
// Keep browser default behavior for input types with no caret selection API.
return;
}
e.preventDefault();
if (context.kind === "input" || context.kind === "textarea") {
handleStandardInput(e, context.element);
} else {
handleContentEditable(e, context.root);
}
}, true);
function getEditingContext(e) {
const target = getEventTarget(e);
const targetEl = getElementFromNode(target);
const deepActive = getDeepActiveElement(document);
const editableRoot = findEditableRoot(target) || findEditableRoot(deepActive);
if (editableRoot) {
return { kind: "contenteditable", root: editableRoot };
}
const control = findNearestTextControl(targetEl) || findNearestTextControl(deepActive);
if (!control) return { kind: "none" };
if (control.tagName === "TEXTAREA") return { kind: "textarea", element: control };
return { kind: "input", element: control };
}
function getEventTarget(e) {
if (typeof e.composedPath === "function") {
const path = e.composedPath();
if (path && path.length) return path[0];
}
return e.target;
}
function getElementFromNode(node) {
if (!node) return null;
return node.nodeType === Node.ELEMENT_NODE ? node : node.parentElement;
}
function getParentElementOrHost(node) {
if (!node) return null;
if (node.parentElement) return node.parentElement;
if (typeof node.getRootNode === "function") {
const root = node.getRootNode();
if (root && root.host) return root.host;
}
return null;
}
function getDeepActiveElement(root) {
let active = root && root.activeElement ? root.activeElement : null;
while (active && active.shadowRoot && active.shadowRoot.activeElement) {
active = active.shadowRoot.activeElement;
}
return active;
}
function findNearestTextControl(node) {
let current = node;
while (current) {
if (current.tagName === "TEXTAREA" || current.tagName === "INPUT") {
return current;
}
current = getParentElementOrHost(current);
}
return null;
}
function supportsSelectionNavigation(input) {
return typeof input.setSelectionRange === "function" && input.selectionStart !== null && input.selectionEnd !== null;
}
// --- Standard input and textarea handling ---
function handleStandardInput(e, el) {
const isHome = e.key === "Home";
const withShift = e.shiftKey;
const withCtrl = e.ctrlKey;
const value = el.value;
const selStart = el.selectionStart;
const selEnd = el.selectionEnd;
const selDir = el.selectionDirection || "none";
// Derive anchor (fixed end) and active (moving end) from direction
const anchor = (selDir === "backward") ? selEnd : selStart;
const active = (selDir === "backward") ? selStart : selEnd;
let target;
if (withCtrl) {
target = isHome ? 0 : value.length;
} else {
// For shift: move the active end to the line boundary
// For non-shift: collapse to the appropriate end, then find line boundary
const cursor = withShift ? active : (isHome ? selStart : selEnd);
if (el.tagName === "TEXTAREA") {
const bounds = getVisualLineBounds(el, cursor);
target = isHome ? bounds.start : bounds.end;
} else {
// Single-line input: the whole value is one line
target = isHome ? 0 : value.length;
}
}
if (withShift) {
const newStart = Math.min(anchor, target);
const newEnd = Math.max(anchor, target);
const newDir = target <= anchor ? "backward" : "forward";
safeSetSelectionRange(el, newStart, newEnd, newDir);
} else {
safeSetSelectionRange(el, target, target);
}
}
function safeSetSelectionRange(el, start, end, dir) {
try {
if (dir) {
el.setSelectionRange(start, end, dir);
} else {
el.setSelectionRange(start, end);
}
} catch (_) {
// Safety guard: avoid throwing if a browser rejects selection updates.
}
}
// --- ContentEditable handling ---
function handleContentEditable(e, editableRoot) {
const isHome = e.key === "Home";
const withShift = e.shiftKey;
const withCtrl = e.ctrlKey;
const sel = window.getSelection();
if (!sel.rangeCount) return;
if (withCtrl) {
const editable = editableRoot || findEditableRoot(sel.anchorNode);
if (!editable) return;
const range = document.createRange();
range.selectNodeContents(editable);
if (withShift) {
// Anchor stays, focus moves to start/end of editable
if (isHome) {
sel.setBaseAndExtent(
sel.anchorNode, sel.anchorOffset,
range.startContainer, range.startOffset
);
} else {
sel.setBaseAndExtent(
sel.anchorNode, sel.anchorOffset,
range.endContainer, range.endOffset
);
}
} else {
if (isHome) {
range.collapse(true);
} else {
range.collapse(false);
}
sel.removeAllRanges();
sel.addRange(range);
}
} else {
// Move/select to start/end of visual line using Selection.modify
const direction = isHome ? "left" : "right";
if (withShift) {
sel.modify("extend", direction, "lineboundary");
} else {
sel.modify("move", direction, "lineboundary");
}
}
}
function findEditableRoot(node) {
let current = getElementFromNode(node);
while (current) {
if (current.isContentEditable) {
const parent = getParentElementOrHost(current);
if (!parent || !parent.isContentEditable) {
return current;
}
}
current = getParentElementOrHost(current);
}
return null;
}
// --- Textarea visual line measurement ---
function getHardLineStart(value, pos) {
const idx = value.lastIndexOf("\n", pos - 1);
return idx === -1 ? 0 : idx + 1;
}
function getHardLineEnd(value, pos) {
const idx = value.indexOf("\n", pos);
return idx === -1 ? value.length : idx;
}
function createMirror(textarea) {
const mirror = document.createElement("div");
const cs = window.getComputedStyle(textarea);
const width = textarea.clientWidth || parseFloat(cs.width) || textarea.offsetWidth || 0;
mirror.style.position = "fixed";
mirror.style.visibility = "hidden";
mirror.style.left = "-9999px";
mirror.style.top = "0";
mirror.style.height = "auto";
mirror.style.width = width + "px";
mirror.style.boxSizing = "border-box";
mirror.style.padding = cs.padding;
mirror.style.border = "0";
mirror.style.overflow = "hidden";
mirror.style.whiteSpace = cs.whiteSpace;
mirror.style.overflowWrap = cs.overflowWrap;
mirror.style.wordWrap = cs.wordWrap;
mirror.style.wordBreak = cs.wordBreak;
mirror.style.fontFamily = cs.fontFamily;
mirror.style.fontSize = cs.fontSize;
mirror.style.fontWeight = cs.fontWeight;
mirror.style.fontStyle = cs.fontStyle;
mirror.style.fontVariant = cs.fontVariant;
mirror.style.lineHeight = cs.lineHeight;
mirror.style.letterSpacing = cs.letterSpacing;
mirror.style.wordSpacing = cs.wordSpacing;
mirror.style.textTransform = cs.textTransform;
mirror.style.textIndent = cs.textIndent;
mirror.style.tabSize = cs.tabSize;
mirror.style.direction = cs.direction;
mirror.style.writingMode = cs.writingMode;
document.body.appendChild(mirror);
return mirror;
}
function getVisualLineBounds(textarea, cursorPos) {
const text = textarea.value;
if (text.length === 0) return { start: 0, end: 0 };
// Isolate the paragraph (between hard newlines) containing the cursor
const paraStart = getHardLineStart(text, cursorPos);
const paraEnd = getHardLineEnd(text, cursorPos);
const paraText = text.substring(paraStart, paraEnd);
if (paraText.length === 0) return { start: paraStart, end: paraEnd };
const mirror = createMirror(textarea);
try {
mirror.textContent = paraText;
const textNode = mirror.firstChild;
const localCursor = cursorPos - paraStart;
function charTop(localPos) {
if (localPos < 0) localPos = 0;
if (localPos >= paraText.length) localPos = paraText.length - 1;
const range = document.createRange();
range.setStart(textNode, localPos);
range.setEnd(textNode, localPos + 1);
return range.getBoundingClientRect().top;
}
// Use the character at cursor, or the last character if cursor is at paragraph end
const measurePos = localCursor < paraText.length ? localCursor : paraText.length - 1;
const cursorTop = charTop(measurePos);
// Binary search: first character on this visual line
let lo = 0, hi = measurePos;
while (lo < hi) {
const mid = (lo + hi) >> 1;
if (charTop(mid) < cursorTop) {
lo = mid + 1;
} else {
hi = mid;
}
}
const lineStart = lo + paraStart;
// Binary search: last character on this visual line
lo = measurePos;
hi = paraText.length - 1;
while (lo < hi) {
const mid2 = Math.ceil((lo + hi) / 2);
if (charTop(mid2) > cursorTop) {
hi = mid2 - 1;
} else {
lo = mid2;
}
}
const lineEnd = Math.min(lo + 1 + paraStart, paraEnd);
return { start: lineStart, end: lineEnd };
} finally {
mirror.remove();
}
}