Skip to content

Commit 926fb75

Browse files
authored
Merge pull request #144 from WebCoder49/main
Test WebKit AutoCloseBrackets and others with new scrolling
2 parents 9eeca46 + b834cbb commit 926fb75

13 files changed

+79
-22
lines changed

code-input.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ code-input textarea, code-input:not(.code-input_pre-element-styled) pre code, co
4040
border: 0;
4141
min-width: calc(100% - var(--padding) * 2);
4242
min-height: calc(100% - var(--padding) * 2);
43+
box-sizing: content-box; /* Make height, width work consistently no matter the box-sizing of ancestors; dialogs can be styled as wanted so are excluded. */
4344
overflow: hidden;
4445
resize: none;
4546
grid-row: 1;

code-input.min.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@webcoder49/code-input",
3-
"version": "2.5.0",
3+
"version": "2.5.1",
44
"description": "Fully customisable, editable syntax-highlighted textareas.",
55
"browser": "code-input.js",
66
"scripts": {

plugins/auto-close-brackets.js

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,17 @@ codeInput.plugins.AutoCloseBrackets = class extends codeInput.Plugin {
1919

2020
/* Add keystroke events */
2121
afterElementsAdded(codeInput) {
22-
codeInput.textareaElement.addEventListener('keydown', (event) => { this.checkBackspace(codeInput, event) });
23-
codeInput.textareaElement.addEventListener('beforeinput', (event) => { this.checkBrackets(codeInput, event); });
22+
codeInput.pluginData.autoCloseBrackets = { automatedKeypresses: false};
23+
codeInput.textareaElement.addEventListener('keydown', (event) => { this.checkBackspace(codeInput, event); });
24+
codeInput.textareaElement.addEventListener('beforeinput', (event) => { this.checkClosingBracket(codeInput, event); });
25+
codeInput.textareaElement.addEventListener('input', (event) => { this.checkOpeningBracket(codeInput, event); });
2426
}
2527

26-
/* Deal with the automatic creation of closing bracket when opening brackets are typed, and the ability to "retype" a closing
27-
bracket where one has already been placed. */
28-
checkBrackets(codeInput, event) {
28+
/* Deal with the ability to "retype" a closing bracket where one has already
29+
been placed. Runs before input so newly typing a closing bracket can be
30+
prevented.*/
31+
checkClosingBracket(codeInput, event) {
32+
if(codeInput.pluginData.autoCloseBrackets.automatedKeypresses) return;
2933
if(event.data == codeInput.textareaElement.value[codeInput.textareaElement.selectionStart]) {
3034
// Check if a closing bracket is typed
3135
for(let openingBracket in this.bracketPairs) {
@@ -37,18 +41,30 @@ codeInput.plugins.AutoCloseBrackets = class extends codeInput.Plugin {
3741
break;
3842
}
3943
}
40-
} else if(event.data in this.bracketPairs) {
44+
}
45+
}
46+
47+
/* Deal with the automatic creation of closing bracket when opening brackets are typed. Runs after input for consistency between browsers. */
48+
checkOpeningBracket(codeInput, event) {
49+
if(codeInput.pluginData.autoCloseBrackets.automatedKeypresses) return;
50+
if(event.data in this.bracketPairs) {
4151
// Opening bracket typed; Create bracket pair
4252
let closingBracket = this.bracketPairs[event.data];
4353
// Insert the closing bracket
54+
// automatedKeypresses property to prevent keypresses being captured
55+
// by this plugin during automated input as some browsers
56+
// (e.g. GNOME Web) do.
57+
codeInput.pluginData.autoCloseBrackets.automatedKeypresses = true;
4458
document.execCommand("insertText", false, closingBracket);
59+
codeInput.pluginData.autoCloseBrackets.automatedKeypresses = false;
4560
// Move caret before the inserted closing bracket
4661
codeInput.textareaElement.selectionStart = codeInput.textareaElement.selectionEnd -= 1;
4762
}
4863
}
4964

5065
/* Deal with cases where a backspace deleting an opening bracket deletes the closing bracket straight after it as well */
5166
checkBackspace(codeInput, event) {
67+
if(codeInput.pluginData.autoCloseBrackets.automatedKeypresses) return;
5268
if(event.key == "Backspace" && codeInput.textareaElement.selectionStart == codeInput.textareaElement.selectionEnd) {
5369
let closingBracket = this.bracketPairs[codeInput.textareaElement.value[codeInput.textareaElement.selectionStart-1]];
5470
if(closingBracket != undefined && codeInput.textareaElement.value[codeInput.textareaElement.selectionStart] == closingBracket) {
@@ -58,4 +74,4 @@ codeInput.plugins.AutoCloseBrackets = class extends codeInput.Plugin {
5874
}
5975
}
6076
}
61-
}
77+
}

plugins/auto-close-brackets.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugins/find-and-replace.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,11 @@ codeInput.plugins.FindAndReplace = class extends codeInput.Plugin {
204204
const findPreviousButton = document.createElement('button');
205205
const replaceButton = document.createElement('button');
206206
const replaceAllButton = document.createElement('button');
207+
208+
// TODO: Make a button element (semantic HTML for accessibility) in next major version
207209
const cancel = document.createElement('span');
210+
cancel.setAttribute("role", "button");
211+
cancel.setAttribute("aria-label", this.instructions.closeDialog);
208212
cancel.setAttribute("tabindex", 0); // Visible to keyboard navigation
209213
cancel.setAttribute("title", this.instructions.closeDialog);
210214

@@ -243,8 +247,10 @@ codeInput.plugins.FindAndReplace = class extends codeInput.Plugin {
243247
replaceInput.placeholder = this.instructions.replacePlaceholder;
244248
findNextButton.innerText = "↓";
245249
findNextButton.title = this.instructions.findNext;
250+
findNextButton.setAttribute("aria-label", this.instructions.findNext);
246251
findPreviousButton.innerText = "↑";
247252
findPreviousButton.title = this.instructions.findPrevious;
253+
findNextButton.setAttribute("aria-label", this.instructions.findPrevious);
248254
replaceButton.className = 'code-input_find-and-replace_button-hidden';
249255
replaceButton.innerText = this.instructions.replaceActionShort;
250256
replaceButton.title = this.instructions.replaceAction;

0 commit comments

Comments
 (0)