-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathgo-to-line.js
168 lines (146 loc) · 7.41 KB
/
go-to-line.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
/**
* Add basic Go-To-Line (Ctrl+G by default) functionality to the code editor.
* Files: go-to-line.js / go-to-line.css
*/
codeInput.plugins.GoToLine = class extends codeInput.Plugin {
useCtrlG = false;
/**
* Create a go-to-line command plugin to pass into a template
* @param {boolean} useCtrlG Should Ctrl+G be overriden for go-to-line functionality? If not, you can trigger it yourself using (instance of this plugin)`.showPrompt(code-input element)`.
*/
constructor(useCtrlG = true) {
super([]); // No observed attributes
this.useCtrlG = useCtrlG;
}
/* Add keystroke events */
afterElementsAdded(codeInput) {
const textarea = codeInput.textareaElement;
if(this.useCtrlG) {
textarea.addEventListener('keydown', (event) => { this.checkCtrlG(codeInput, event); });
}
}
/* Called with a dialog box keyup event to check the validity of the line number entered and submit the dialog if Enter is pressed */
checkPrompt(dialog, event) {
// Line number(:column number)
const lines = dialog.textarea.value.split('\n');
const maxLineNo = lines.length;
const lineNo = Number(dialog.input.value.split(':')[0]);
let columnNo = 0; // Means go to start of indented line
let maxColumnNo = 1;
const querySplitByColons = dialog.input.value.split(':');
if(querySplitByColons.length > 2) return dialog.input.classList.add('code-input_go-to-line_error');
if (event.key == 'Escape') return this.cancelPrompt(dialog, event);
if (dialog.input.value) {
if (!/^[0-9:]*$/.test(dialog.input.value) || lineNo < 1 || lineNo > maxLineNo) {
return dialog.input.classList.add('code-input_go-to-line_error');
} else {
// Check if line:column
if(querySplitByColons.length >= 2) {
columnNo = Number(querySplitByColons[1]);
maxColumnNo = lines[lineNo-1].length;
}
if(columnNo < 0 || columnNo > maxColumnNo) {
return dialog.input.classList.add('code-input_go-to-line_error');
} else {
dialog.input.classList.remove('code-input_go-to-line_error');
}
}
}
if (event.key == 'Enter') {
this.goTo(dialog.textarea, lineNo, columnNo);
this.cancelPrompt(dialog, event);
}
}
/* Called with a dialog box keyup event to close and clear the dialog box */
cancelPrompt(dialog, event) {
event.preventDefault();
dialog.codeInput.handleEventsFromTextarea = false;
dialog.textarea.focus();
dialog.codeInput.handleEventsFromTextarea = true;
dialog.setAttribute("inert", true); // Hide from keyboard navigation when closed.
dialog.setAttribute("tabindex", -1); // Hide from keyboard navigation when closed.
dialog.setAttribute("aria-hidden", true); // Hide from screen reader when closed.
// Remove dialog after animation
dialog.classList.add('code-input_go-to-line_hidden-dialog');
dialog.input.value = "";
}
/**
* Show a search-like dialog prompting line number.
* @param {codeInput.CodeInput} codeInput the `<code-input>` element.
*/
showPrompt(codeInput) {
if(codeInput.pluginData.goToLine == undefined || codeInput.pluginData.goToLine.dialog == undefined) {
const textarea = codeInput.textareaElement;
const dialog = document.createElement('div');
const input = document.createElement('input');
const cancel = document.createElement('span');
cancel.setAttribute("tabindex", 0); // Visible to keyboard navigation
cancel.setAttribute("title", "Close Dialog and Return to Editor");
dialog.appendChild(input);
dialog.appendChild(cancel);
dialog.className = 'code-input_go-to-line_dialog';
input.spellcheck = false;
input.placeholder = "Line:Column / Line no. then Enter";
dialog.codeInput = codeInput;
dialog.textarea = textarea;
dialog.input = input;
input.addEventListener('keypress', (event) => {
/* Stop enter from submitting form */
if (event.key == 'Enter') event.preventDefault();
});
input.addEventListener('keyup', (event) => { return this.checkPrompt(dialog, event); });
cancel.addEventListener('click', (event) => { this.cancelPrompt(dialog, event); });
cancel.addEventListener('keypress', (event) => { if (event.key == "Space" || event.key == "Enter") this.cancelPrompt(dialog, event); });
codeInput.dialogContainerElement.appendChild(dialog);
codeInput.pluginData.goToLine = {dialog: dialog};
input.focus();
} else {
codeInput.pluginData.goToLine.dialog.classList.remove("code-input_go-to-line_hidden-dialog");
codeInput.pluginData.goToLine.dialog.removeAttribute("inert"); // Show to keyboard navigation when open.
codeInput.pluginData.goToLine.dialog.setAttribute("tabindex", 0); // Show to keyboard navigation when open.
codeInput.pluginData.goToLine.dialog.removeAttribute("aria-hidden"); // Show to screen reader when open.
codeInput.pluginData.goToLine.dialog.input.focus();
}
}
/* Set the cursor on the first non-space char of textarea's nth line, or to the columnNo-numbered character in the line if it's not 0; and scroll it into view */
goTo(textarea, lineNo, columnNo = 0) {
let fontSize;
let lineHeight;
let scrollAmount;
let topPadding;
let cursorPos = -1;
let lines = textarea.value.split('\n');
if (lineNo > 0 && lineNo <= lines.length) {
if (textarea.computedStyleMap) {
fontSize = textarea.computedStyleMap().get('font-size').value;
lineHeight = fontSize * textarea.computedStyleMap().get('line-height').value;
} else {
fontSize = document.defaultView.getComputedStyle(textarea, null).getPropertyValue('font-size').split('px')[0];
lineHeight = document.defaultView.getComputedStyle(textarea, null).getPropertyValue('line-height').split('px')[0];
}
// scroll amount and initial top padding (3 lines above, if possible)
scrollAmount = (lineNo > 3 ? lineNo - 3 : 1) * lineHeight;
topPadding = (lineHeight - fontSize) / 2;
if (lineNo > 1) {
// cursor positon just after n - 1 full lines
cursorPos = lines.slice(0, lineNo - 1).join('\n').length;
}
// scan first non-space char in nth line
if (columnNo == 0) {
do cursorPos++; while (textarea.value[cursorPos] != '\n' && /\s/.test(textarea.value[cursorPos]));
} else {
cursorPos += 1 + columnNo - 1;
}
textarea.scrollTop = scrollAmount - topPadding;
textarea.setSelectionRange(cursorPos, cursorPos);
textarea.click();
}
}
/* Event handler for keydown event that makes Ctrl+G open go to line dialog */
checkCtrlG(codeInput, event) {
if (event.ctrlKey && event.key == 'g') {
event.preventDefault();
this.showPrompt(codeInput);
}
}
}