-
Notifications
You must be signed in to change notification settings - Fork 3
/
handleSelection.js
43 lines (31 loc) · 1.22 KB
/
handleSelection.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
// when text selected, handle it
function handleSelection(windowHref, selectedText, precedingText) {
// find if selected text is a method/figure/reference
// convert to lowercase
let selectedTextLower = selectedText.toLowerCase();
let precedingTextLower = precedingText.toLowerCase();
// initialize context as an empty string
let context = '';
// check for 'method', 'fig', and 'ref'
if (selectedTextLower.includes('method')) {
context = 'method';
showMethods(windowHref, precedingText)
} else if (selectedTextLower.includes('fig') || precedingTextLower.includes('fig')) {
context = 'fig';
showFig(windowHref, selectedText)
} else if (extractLastNumber(selectedText) !== null) {
context = 'ref';
showRef(windowHref, extractLastNumber(selectedText));
}
}
function extractLastNumber(selectedText) {
const hasNumber = /\d/.test(selectedText);
if (hasNumber) {
// This regular expression matches the last sequence of digits in the string
const lastNumberMatch = selectedText.match(/(\d+)(?!.*\d)/);
if (lastNumberMatch) {
return Number(lastNumberMatch[0]);
}
}
return null;
}