-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
248 lines (222 loc) · 5.31 KB
/
index.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
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
/**
* @typedef {{
* start:Node;
* start_offset:number;
* end: Node;
* end_offset:number
* }} SelectionInfo
*
* @typedef {{
* target?: Node;
* event_name?: string;
* debounce?: number;
* }} EventOptions
*
* @typedef {{
* stop_it_get_some_help: () => void;
* }} EventCancellationHandle
*/
/**
* @param {() => void} action
* @param {number} timeout
* @returns {{update: {():void}}}
*/
function create_debouncer(action, timeout) {
/** @type {number} **/
let timer_id;
return {
update: () => {
if (timer_id !== undefined) {
clearTimeout(timer_id);
}
timer_id = setTimeout(() => action(), timeout);
},
};
}
/**
* Gets current selection. But, swaps anchor and focus nodes if necessary.
* @returns {SelectionInfo | undefined}
**/
function get_selection() {
const selection = window.getSelection();
if (selection) {
if (!selection.anchorNode || !selection.focusNode) {
return undefined;
}
let node_order;
if (selection.anchorNode === selection.focusNode) {
if (selection.anchorOffset === selection.focusOffset) {
return undefined;
} else {
node_order = selection.anchorOffset < selection.focusOffset ? 4 : 2;
}
} else {
node_order = selection.anchorNode.compareDocumentPosition(
selection.focusNode,
);
}
switch (node_order) {
case 4:
return {
start: selection.anchorNode,
end: selection.focusNode,
start_offset: selection.anchorOffset,
end_offset: selection.focusOffset,
};
case 2:
return {
start: selection.focusNode,
end: selection.anchorNode,
start_offset: selection.focusOffset,
end_offset: selection.anchorOffset,
};
default:
return undefined;
}
} else {
return undefined;
}
}
/**
* Checks if selection is part of the target Node tree.
* @param {Node} target
* @param {SelectionInfo} selection
* @returns {boolean}
*/
function contains(target, selection) {
return (
(target === selection.start || target.contains(selection.start)) &&
(target === selection.end || target.contains(selection.end))
);
}
/**
* Gets current active selection and checks if selection style matches with target user behavior.
* @param {Node} [target]
* @returns {boolean} - Returns true if behavior matches, otherwise returns false.
*/
export function detect_prime(target) {
const selection = get_selection();
if (
!selection ||
selection.start_offset === 0 ||
(target && !contains(target, selection))
) {
return false;
}
const start_token_0 =
selection.start_offset > 1
? selection.start.textContent?.codePointAt(selection.start_offset - 2)
: undefined;
if (!is_terminal_char(start_token_0)) {
return false;
}
const start_token_1 = selection.start.textContent?.codePointAt(
selection.start_offset - 1,
);
if (is_terminal_char(start_token_1)) {
return false;
}
const end_token_0 = selection.end.textContent?.codePointAt(
selection.end_offset,
);
if (is_terminal_char(end_token_0)) {
return false;
}
const end_token_1 = selection.end.textContent?.codePointAt(
selection.end_offset + 1,
);
if (!is_terminal_char(end_token_1)) {
return false;
}
return true;
}
/**
* RegExp? Never heard it?
* @param {number | undefined | null} value
* @returns {boolean}
**/
function is_terminal_char(value) {
switch (value) {
case 0x0009:
case 0x000a:
case 0x000b:
case 0x000c:
case 0x000d:
case 0x002c:
case 0x002e:
case 0x0020:
case 0x0021:
case 0x0022:
case 0x0027:
case 0x0028:
case 0x0029:
case 0x003a:
case 0x003b:
case 0x003f:
case 0x005b:
case 0x005d:
case 0x0060:
case 0x007b:
case 0x007d:
case 0x00a0:
case 0x00b4:
case 0x1680:
case 0x2000:
case 0x2001:
case 0x2002:
case 0x2003:
case 0x2004:
case 0x2005:
case 0x2006:
case 0x2007:
case 0x2008:
case 0x2009:
case 0x200a:
case 0x2018:
case 0x2019:
case 0x201c:
case 0x201d:
case 0x2028:
case 0x2029:
case 0x202f:
case 0x205f:
case 0x3000:
case 0xfeff:
case undefined:
return true;
default:
return false;
}
}
/**
* Initialize detection listener and event dispatcher.
* @param {EventOptions} [options] - Custom event options.
* @returns {EventCancellationHandle} - Remove handle for disabling event listener.
**/
export function init_listener(options) {
const debounce = options?.debounce ?? 1000;
const event_name = options?.event_name ?? "brazil-mentioned";
const event_target = options?.target ?? window;
const target = options?.target;
let debouncer = create_debouncer(() => {
if (detect_prime(target)) {
event_target.dispatchEvent(
new CustomEvent(event_name, { bubbles: true }),
);
}
}, debounce);
document.addEventListener("selectionchange", debouncer.update);
return {
stop_it_get_some_help: () => {
document.removeEventListener(event_name, debouncer.update);
},
};
}
/**
* Calls event cancellation function for you...
* @param {EventCancellationHandle} handle
* @returns {void}
*/
export function remove_listener(handle) {
handle.stop_it_get_some_help();
}