-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseTextSelection.ts
More file actions
189 lines (164 loc) · 4.39 KB
/
Copy pathuseTextSelection.ts
File metadata and controls
189 lines (164 loc) · 4.39 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
import { ref, onMounted, onBeforeUnmount } from "vue";
const MIN_TEXT_LENGTH = 1;
const MAX_TEXT_LENGTH = 200;
const DEBOUNCE_MS = 50;
const BLOCK_TAGS = new Set([
"P",
"LI",
"BLOCKQUOTE",
"H1",
"H2",
"H3",
"H4",
"H5",
"H6",
"ARTICLE",
"SECTION",
"TD",
"DD",
"DT",
"FIGCAPTION",
"DIV",
]);
const IGNORED_TAGS = new Set([
"INPUT",
"TEXTAREA",
"CODE",
"PRE",
"SCRIPT",
"STYLE",
]);
const NIBBLE_ROOT_ID = "subturtle-nibble-root";
function isInsideIgnored(node: Node | null): boolean {
let el: Element | null =
node?.nodeType === Node.ELEMENT_NODE
? (node as Element)
: node?.parentElement ?? null;
while (el) {
if (el.id === NIBBLE_ROOT_ID) return true;
if (IGNORED_TAGS.has(el.tagName)) return true;
if (el.getAttribute && el.getAttribute("contenteditable") === "true")
return true;
el = el.parentElement;
}
return false;
}
function closestBlockText(node: Node | null): string {
let el: Element | null =
node?.nodeType === Node.ELEMENT_NODE
? (node as Element)
: node?.parentElement ?? null;
while (el) {
if (BLOCK_TAGS.has(el.tagName)) {
const text = (el.textContent || "").replace(/\s+/g, " ").trim();
if (text.length > 0) return text;
}
el = el.parentElement;
}
return "";
}
export function useTextSelection() {
const text = ref<string>("");
const rect = ref<DOMRect | null>(null);
const contextText = ref<string>("");
const isVisible = ref<boolean>(false);
let debounceTimer: number | null = null;
let lastFingerprint = "";
function clear() {
text.value = "";
rect.value = null;
contextText.value = "";
isVisible.value = false;
lastFingerprint = "";
}
function evaluateSelection() {
const selection = window.getSelection();
if (!selection || selection.isCollapsed || selection.rangeCount === 0) {
isVisible.value = false;
return;
}
const selected = selection.toString().trim();
if (
selected.length < MIN_TEXT_LENGTH ||
selected.length > MAX_TEXT_LENGTH
) {
isVisible.value = false;
return;
}
if (isInsideIgnored(selection.anchorNode)) {
isVisible.value = false;
return;
}
const range = selection.getRangeAt(0);
const boundingRect = range.getBoundingClientRect();
if (boundingRect.width === 0 && boundingRect.height === 0) {
isVisible.value = false;
return;
}
const fingerprint = `${selected}|${Math.round(boundingRect.top)}|${Math.round(
boundingRect.left
)}`;
if (fingerprint === lastFingerprint && isVisible.value) return;
lastFingerprint = fingerprint;
text.value = selected;
rect.value = boundingRect;
contextText.value = closestBlockText(selection.anchorNode) || selected;
isVisible.value = true;
}
function scheduleEvaluate() {
if (debounceTimer !== null) {
window.clearTimeout(debounceTimer);
}
debounceTimer = window.setTimeout(() => {
debounceTimer = null;
evaluateSelection();
}, DEBOUNCE_MS);
}
function onSelectionChange() {
const selection = window.getSelection();
if (!selection || selection.isCollapsed) {
isVisible.value = false;
lastFingerprint = "";
return;
}
scheduleEvaluate();
}
function onMouseUp() {
scheduleEvaluate();
}
function onKeyDown(e: KeyboardEvent) {
if (e.key === "Escape") clear();
}
function onDocClick(e: MouseEvent) {
const target = e.target;
const el =
target instanceof Element
? target
: target instanceof Node
? target.parentElement
: null;
if (!el) return;
if (el.closest(`#${NIBBLE_ROOT_ID}`)) return;
if (window.getSelection()?.isCollapsed !== false) return;
}
onMounted(() => {
document.addEventListener("selectionchange", onSelectionChange);
document.addEventListener("mouseup", onMouseUp);
document.addEventListener("keydown", onKeyDown);
document.addEventListener("mousedown", onDocClick);
});
onBeforeUnmount(() => {
document.removeEventListener("selectionchange", onSelectionChange);
document.removeEventListener("mouseup", onMouseUp);
document.removeEventListener("keydown", onKeyDown);
document.removeEventListener("mousedown", onDocClick);
if (debounceTimer !== null) window.clearTimeout(debounceTimer);
});
return {
text,
rect,
contextText,
isVisible,
clear,
};
}