-
Notifications
You must be signed in to change notification settings - Fork 1
/
renumberTextarea.js
94 lines (81 loc) · 3.6 KB
/
renumberTextarea.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
/*
This file contains functions to select a textarea on a webpage, copy its
text, use renumberReference.js functions on the text, generate popup boxes
based on the text input, and replace the textarea's text with the renumbered
text.
*/
const referenceSorter = require("./renumberReferences");
const { referenceSection, textareaId, buttonName, formatCheckResults } = require("./config");
window.onload = function () {
let button = document.getElementById(buttonName);
button.addEventListener("click", function () {
let textarea = document.getElementById(textareaId);
renumberTextarea(textarea);
});
}
// Perform checkFormat on textarea, renumber its text, and paste it into textarea
function renumberTextarea(textarea) {
const oldText = textarea.value;
const formatCheckPassed = checkFormatWithPopups(oldText);
if (formatCheckPassed == false) {
return;
}
const renumberedText = referenceSorter.renumberReferences(oldText)
const oldRefSectionPosition = oldText.indexOf(referenceSection);
// replaceTextareaContents(textarea, renumberedText, oldRefSectionPosition);
// NOTE: the code in the above function doesn't work, it only works in this code block for whatever reason
const newRefSectionPosition = renumberedText.indexOf(referenceSection);
const refSectionPositionChange = Math.abs(newRefSectionPosition - oldRefSectionPosition);
const originalStartPosition = textarea.selectionStart + refSectionPositionChange;
const originalEndPosition = textarea.selectionEnd + refSectionPositionChange;
textarea.selectionStart = 0;
textarea.selectionEnd = textarea.value.length;
textarea.focus();
document.execCommand("insertText", false, renumberedText);
textarea.selectionStart = originalStartPosition;
textarea.selectionEnd = originalEndPosition;
}
// Checks the format of a textarea's body
// Returns true if no errors are found, false otherwise
function checkFormatWithPopups(oldText) {
const { ALERT, CONFIRM, PASS } = formatCheckResults;
let formatCheckText = "";
let ignoreConfirmCode = 0; // increasing by one bypasses a "CONFIRM" in the formatCheck
while (formatCheckText !== PASS) {
formatCheckText = referenceSorter.checkFormat(oldText, ignoreConfirmCode);
if (formatCheckText.substring(0, 7) === ALERT) {
alert(formatCheckText.substring(7));
return false;
} else if (formatCheckText.substring(0, 7) === CONFIRM) {
if (confirm(formatCheckText.substring(7))) {
ignoreConfirmCode++;
continue;
} else {
return false;
}
}
}
return true;
}
// Replace textarea text with providedText and return cursor to original position
// NOTE: does not work, see commented out code in renumberTextarea function
function replaceTextareaContents(textarea, renumberedText, oldRefSectionPosition) {
/*
Before pasting, store cursor's position in textarea
After pasting, use difference in referenceSection's position between
oldText and renumberedText to reposition cursor near original cursor position
*/
const newRefSectionPosition = renumberedText.indexOf(referenceSection);
const refSectionPositionChange = Math.abs(newRefSectionPosition - oldRefSectionPosition);
const originalStartPosition = textarea.selectionStart + refSectionPositionChange;
const originalEndPosition = textarea.selectionEnd + refSectionPositionChange;
textarea.selectionStart = 0;
textarea.selectionEnd = textarea.value.length;
textarea.focus();
document.execCommand("insertText", false, renumberedText);
textarea.selectionStart = originalStartPosition;
textarea.selectionEnd = originalEndPosition;
}
modules.export = {
renumberTextarea
}