forked from utags/userscripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext.ts
More file actions
271 lines (243 loc) · 6.68 KB
/
Copy pathtext.ts
File metadata and controls
271 lines (243 loc) · 6.68 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
import { doc } from '../globals/doc'
import {
caretRangeFromPoint,
closestBlockElement,
hasNestedBlock,
isElementVisible,
} from './dom'
export type TextIndex = {
nodes: Text[]
starts: number[]
ends: number[]
text: string
}
export function buildTextIndex(root: Element): TextIndex {
const nodes: Text[] = []
const texts: string[] = []
const tw = doc.createTreeWalker(root, NodeFilter.SHOW_TEXT)
while (tw.nextNode()) {
const t = tw.currentNode as Text
if (hasNestedBlock(root, t)) continue
if (!isElementVisible(t.parentElement || undefined)) continue
nodes.push(t)
texts.push(t.data)
}
const starts: number[] = []
const ends: number[] = []
let acc = 0
for (let i = 0; i < nodes.length; i++) {
starts.push(acc)
acc += texts[i].length
ends.push(acc)
if (i < nodes.length - 1) acc += 1
}
return { nodes, starts, ends, text: texts.join(' ') }
}
const textIndexCache = new WeakMap<
Element,
{ index: TextIndex; textLength: number }
>()
export function getTextIndex(root: Element): TextIndex {
const tl = (root.textContent || '').length
const cached = textIndexCache.get(root)
if (cached && cached.textLength === tl) return cached.index
const idx = buildTextIndex(root)
textIndexCache.set(root, { index: idx, textLength: tl })
return idx
}
export function mapIndexToPosition(
idx: number,
index: TextIndex
): { node: Text; offset: number } | undefined {
for (const [i, node] of index.nodes.entries()) {
if (idx >= index.starts[i] && idx <= index.ends[i]) {
return { node, offset: idx - index.starts[i] }
}
}
return undefined
}
export function adjustIndexToNode(
idx: number,
index: TextIndex,
dir: 'forward' | 'backward'
): number {
const starts = index.starts
const ends = index.ends
let lo = 0
let hi = starts.length - 1
while (lo <= hi) {
const mid = Math.floor((lo + hi) / 2)
if (idx < starts[mid]) hi = mid - 1
else if (idx > ends[mid]) lo = mid + 1
else return idx
}
if (dir === 'forward') {
const j = Math.min(starts.length - 1, Math.max(0, lo))
return starts[j]
}
const j = Math.max(0, Math.min(ends.length - 1, hi))
return ends[j]
}
export function mapPositionToIndex(
node: Node,
offset: number,
index: TextIndex
): number | undefined {
const i = index.nodes.indexOf(node as Text)
if (i !== -1) return index.starts[i] + offset
return undefined
}
export function isSentenceTerminator(ch: string): boolean {
return /[。.。.!?!?…]/.test(ch)
}
export function isClauseTerminator(ch: string): boolean {
return /[,,、;;::.。!?!?]/.test(ch)
}
export function findPrevBoundary(
text: string,
pos: number,
m: 'sentence' | 'clause'
): number {
for (let i = pos - 1; i >= 0; i--) {
const ch = text[i]
const hit =
m === 'sentence' ? isSentenceTerminator(ch) : isClauseTerminator(ch)
if (hit) {
if (
ch === '.' &&
/[A-Za-z\d]/.test(text[i + 1] || '') &&
/[A-Za-z\d]/.test(text[i - 1] || '')
)
continue
return i
}
}
return -1
}
export function findNextBoundary(
text: string,
pos: number,
m: 'sentence' | 'clause'
): number {
for (let i = pos; i < text.length; i++) {
const ch = text[i]
const hit =
m === 'sentence' ? isSentenceTerminator(ch) : isClauseTerminator(ch)
if (hit) {
if (
ch === '.' &&
/[A-Za-z\d]/.test(text[i + 1] || '') &&
/[A-Za-z\d]/.test(text[i - 1] || '')
)
continue
return i
}
}
return text.length
}
export function rangeForParagraph(caret: Range): Range {
const block = closestBlockElement(caret.startContainer)
const r = doc.createRange()
r.selectNodeContents(block)
return r
}
export function rangeForLine(caret: Range): Range | undefined {
const block = closestBlockElement(caret.startContainer)
const caretRect = caret.getBoundingClientRect()
const r = doc.createRange()
r.selectNodeContents(block)
const rects = Array.from(r.getClientRects())
let pick: DOMRect | undefined
for (const rect of rects) {
if (caretRect.top >= rect.top && caretRect.top <= rect.bottom) {
pick = rect
break
}
}
if (!pick) return undefined
if (pick.width <= 2) return undefined
const out = doc.createRange()
out.setStart(block, 0)
out.setEnd(block, block.childNodes.length)
;(out as any).__singleLineRect = pick
return out
}
export function rangeForText(
caret: Range,
m: 'sentence' | 'clause'
): Range | undefined {
const block = closestBlockElement(caret.startContainer)
const idx = getTextIndex(block)
if (idx.nodes.length === 0) return undefined
const startNode = caret.startContainer
const startOffset = caret.startOffset
const caretGlobal = mapPositionToIndex(startNode, startOffset, idx)
if (caretGlobal === undefined) return undefined
const text = idx.text
let s = caretGlobal
let e = caretGlobal
for (let i = caretGlobal - 1; i >= 0; i--) {
const ch = text[i]
const hit =
m === 'sentence' ? isSentenceTerminator(ch) : isClauseTerminator(ch)
if (hit) {
if (
ch === '.' &&
/[A-Za-z\d]/.test(text[i + 1] || '') &&
/[A-Za-z\d]/.test(text[i - 1] || '')
)
continue
s = i + 1
break
}
s = i
}
while (s < text.length && /[,,、;;::.。!?!?…\s\u00A0]/.test(text[s])) s++
for (let i = caretGlobal; i < text.length; i++) {
const ch = text[i]
const hit =
m === 'sentence' ? isSentenceTerminator(ch) : isClauseTerminator(ch)
if (hit) {
if (
ch === '.' &&
/[A-Za-z\d]/.test(text[i + 1] || '') &&
/[A-Za-z\d]/.test(text[i - 1] || '')
)
continue
e = i
break
}
e = i + 1
}
const sAdj = adjustIndexToNode(s, idx, 'forward')
const eAdj = adjustIndexToNode(e, idx, 'backward')
const startPos = mapIndexToPosition(sAdj, idx)
const endPos = mapIndexToPosition(eAdj, idx)
if (!startPos || !endPos) return undefined
const r = doc.createRange()
r.setStart(startPos.node, startPos.offset)
r.setEnd(endPos.node, endPos.offset)
return r
}
export function isPunctuationRect(rect: {
left: number
top: number
width: number
height: number
}): boolean {
if (rect.width > 8) return false
const x = rect.left + Math.max(1, Math.min(rect.width - 1, rect.width * 0.5))
const y = rect.top + rect.height / 2
const cr = caretRangeFromPoint(x, y)
if (!cr) return false
const n = cr.startContainer
const o = cr.startOffset
if (n.nodeType === Node.TEXT_NODE) {
const t = n as Text
const s = t.data
const i = Math.max(0, Math.min(o - 1, s.length - 1))
const ch = s[i] || ''
return /[,,、;;::.。!?!?\s\u00A0]/.test(ch)
}
return false
}