Skip to content

Commit 3ab457c

Browse files
committed
Make scrolling horizontally work correctly for indentation
1 parent 78d08c7 commit 3ab457c

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

plugins/indent.js

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,33 @@ codeInput.plugins.Indent = class extends codeInput.Plugin {
2828
}
2929
}
3030

31-
/* Add keystroke events */
31+
/* Add keystroke events, and get the width of the indentation in pixels. */
3232
afterElementsAdded(codeInput) {
3333
let textarea = codeInput.textareaElement;
3434
textarea.addEventListener('keydown', (event) => { this.checkTab(codeInput, event); this.checkEnter(codeInput, event); this.checkBackspace(codeInput, event); });
3535
textarea.addEventListener('beforeinput', (event) => { this.checkCloseBracket(codeInput, event); });
36+
37+
// Get the width of the indentation in pixels
38+
let testIndentationWidthPre = document.createElement("pre");
39+
testIndentationWidthPre.setAttribute("aria-hidden", "true"); // Hide for screen readers
40+
let testIndentationWidthSpan = document.createElement("span");
41+
if(codeInput.template.preElementStyled) {
42+
testIndentationWidthPre.appendChild(testIndentationWidthSpan);
43+
testIndentationWidthPre.classList.add("code-input_autocomplete_test-indentation-width");
44+
codeInput.appendChild(testIndentationWidthPre); // Styled like first pre, but first pre found to update
45+
} else {
46+
let testIndentationWidthCode = document.createElement("code");
47+
testIndentationWidthCode.appendChild(testIndentationWidthSpan);
48+
testIndentationWidthCode.classList.add("code-input_autocomplete_test-indentation-width");
49+
testIndentationWidthPre.appendChild(testIndentationWidthCode);
50+
codeInput.appendChild(testIndentationWidthPre); // Styled like first pre, but first pre found to update
51+
}
52+
53+
testIndentationWidthSpan.innerHTML = codeInput.escapeHtml(this.indentation);
54+
let indentationWidthPx = testIndentationWidthSpan.offsetWidth;
55+
codeInput.removeChild(testIndentationWidthPre);
56+
57+
codeInput.pluginData.indent = {indentationWidthPx: indentationWidthPx};
3658
}
3759

3860
/* Deal with the Tab key causing indentation, and Tab+Selection indenting / Shift+Tab+Selection unindenting lines */
@@ -93,6 +115,15 @@ codeInput.plugins.Indent = class extends codeInput.Plugin {
93115
// move cursor
94116
inputElement.selectionStart = selectionStartI;
95117
inputElement.selectionEnd = selectionEndI;
118+
119+
// move scroll position to follow code
120+
console.log("indw", codeInput.pluginData.indent.indentationWidthPx);
121+
if(event.shiftKey) {
122+
console.log("shift");
123+
codeInput.scrollBy(-codeInput.pluginData.indent.indentationWidthPx, 0);
124+
} else {
125+
codeInput.scrollBy(codeInput.pluginData.indent.indentationWidthPx, 0);
126+
}
96127
}
97128

98129
codeInput.value = inputElement.value;

0 commit comments

Comments
 (0)