Skip to content

Commit 457b0a9

Browse files
committed
Make textarea rather than code-input element scroll
1 parent 926fb75 commit 457b0a9

File tree

2 files changed

+61
-38
lines changed

2 files changed

+61
-38
lines changed

code-input.css

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
code-input {
77
/* Allow other elements to be inside */
88
display: block;
9-
overflow-y: auto;
10-
overflow-x: auto;
9+
overflow: hidden;
1110
position: relative;
1211
top: 0;
1312
left: 0;
@@ -38,21 +37,16 @@ code-input textarea, code-input:not(.code-input_pre-element-styled) pre code, co
3837
margin: 0px!important;
3938
padding: var(--padding, 16px)!important;
4039
border: 0;
41-
min-width: calc(100% - var(--padding) * 2);
42-
min-height: calc(100% - var(--padding) * 2);
40+
width: calc(100% - var(--padding) * 2);
41+
height: calc(100% - var(--padding) * 2);
4342
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. */
44-
overflow: hidden;
43+
overflow: auto;
4544
resize: none;
4645
grid-row: 1;
4746
grid-column: 1;
4847
display: block;
4948
}
5049

51-
code-input:not(.code-input_pre-element-styled) pre code, code-input.code-input_pre-element-styled pre {
52-
height: max-content;
53-
width: max-content;
54-
}
55-
5650
code-input:not(.code-input_pre-element-styled) pre, code-input.code-input_pre-element-styled pre code {
5751
/* Remove all margin and padding from others */
5852
margin: 0px!important;
@@ -104,7 +98,7 @@ code-input pre {
10498
code-input textarea {
10599
color: transparent;
106100
background: transparent;
107-
caret-color: inherit!important; /* Or choose your favourite color */
101+
caret-color: inherit!important;
108102
}
109103
code-input textarea::placeholder {
110104
color: lightgrey;
@@ -187,7 +181,11 @@ code-input .code-input_dialog-container .code-input_keyboard-navigation-instruct
187181
position: absolute;
188182
background-color: black;
189183
color: white;
184+
190185
padding: 2px;
186+
line-height: 2em;
187+
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. */
188+
191189
padding-left: 10px;
192190
margin: 0;
193191
text-wrap: balance;
@@ -212,5 +210,6 @@ code-input .code-input_dialog-container .code-input_keyboard-navigation-instruct
212210
code-input:not(:has(.code-input_keyboard-navigation-instructions:empty)):has(textarea:focus):not(.code-input_mouse-focused) textarea,
213211
code-input:not(:has(.code-input_keyboard-navigation-instructions:empty)):has(textarea:focus):not(.code-input_mouse-focused):not(.code-input_pre-element-styled) pre code,
214212
code-input:not(:has(.code-input_keyboard-navigation-instructions:empty)):has(textarea:focus):not(.code-input_mouse-focused).code-input_pre-element-styled pre {
215-
padding-top: calc(var(--padding) + 3em)!important;
213+
padding-top: calc(var(--padding) + 2em + 4px)!important;
214+
height: calc(100% - var(--padding) * 2 - 2em - 4px)!important;
216215
}

code-input.js

Lines changed: 50 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
* **<https://github.com/WebCoder49/code-input>**
77
*/
88

9-
109
var codeInput = {
1110
/**
1211
* A list of attributes that will trigger the
@@ -64,7 +63,8 @@ var codeInput = {
6463
"change",
6564
"selectionchange",
6665
"invalid",
67-
"input"
66+
"input",
67+
"scroll"
6868
],
6969

7070
/* ------------------------------------
@@ -512,27 +512,9 @@ var codeInput = {
512512
if (this.template.includeCodeInputInHighlightFunc) this.template.highlight(resultElement, this);
513513
else this.template.highlight(resultElement);
514514

515-
this.syncSize();
516-
517515
this.pluginEvt("afterHighlight");
518516
}
519517

520-
/**
521-
* Set the size of the textarea element to the size of the pre/code element.
522-
*/
523-
syncSize() {
524-
// Synchronise the size of the pre/code and textarea elements
525-
if(this.template.preElementStyled) {
526-
this.style.backgroundColor = getComputedStyle(this.preElement).backgroundColor;
527-
this.textareaElement.style.height = getComputedStyle(this.preElement).height;
528-
this.textareaElement.style.width = getComputedStyle(this.preElement).width;
529-
} else {
530-
this.style.backgroundColor = getComputedStyle(this.codeElement).backgroundColor;
531-
this.textareaElement.style.height = getComputedStyle(this.codeElement).height;
532-
this.textareaElement.style.width = getComputedStyle(this.codeElement).width;
533-
}
534-
}
535-
536518
/**
537519
* Show some instructions to the user only if they are using keyboard navigation - for example, a prompt on how to navigate with the keyboard if Tab is repurposed.
538520
* @param {string} instructions The instructions to display only if keyboard navigation is being used. If it's blank, no instructions will be shown.
@@ -684,22 +666,64 @@ var codeInput = {
684666
this.value = value;
685667
this.animateFrame();
686668

687-
const resizeObserver = new ResizeObserver((elements) => {
688-
// The only element that could be resized is this code-input element.
689-
this.syncSize();
669+
// Scrolling
670+
this.textareaElement.addEventListener("scroll", () => {
671+
this.syncScrollFromTextarea();
690672
});
691-
resizeObserver.observe(this);
673+
if(this.template.preElementStyled) {
674+
this.preElement.addEventListener("scroll", () => {
675+
this.syncScrollFromHighlighted(this.preElement)
676+
});
677+
} else {
678+
this.codeElement.addEventListener("scroll", () => {
679+
this.syncScrollFromHighlighted(this.codeElement)
680+
});
681+
}
682+
// TODO: Make scrollTop etc. directly accessible from code-input.
683+
}
684+
685+
/**
686+
* Synchronise the scroll position of the textarea element to that of the pre/code element.
687+
* @param {HTMLElement} highlightedElement The pre/code element that has been scrolled (the styled one).
688+
*/
689+
syncScrollFromHighlighted(highlightedElement) {
690+
this.textareaElement.scrollTo(highlightedElement.scrollLeft, highlightedElement.scrollTop);
691+
}
692+
693+
/**
694+
* Synchronise the scroll position of the pre/code element to that of the textarea element.
695+
*/
696+
syncScrollFromTextarea() {
697+
if(this.template.preElementStyled) {
698+
this.preElement.scrollTo(this.textareaElement.scrollLeft, this.textareaElement.scrollTop);
699+
} else {
700+
this.codeElement.scrollTo(this.textareaElement.scrollLeft, this.textareaElement.scrollTop);
701+
}
702+
}
703+
704+
/**
705+
* @deprecated This is internal code used by code-input and it may change/be removed to improve the library. This function with its old name remains, though, because that wasn't made as clear in the past.
706+
*/
707+
syncScroll() {
708+
this.syncScrollFromTextarea();
709+
}
710+
711+
/**
712+
* @deprecated This is internal code used by code-input and it may change/be removed to improve the library. This function with its old name remains, though, because that wasn't made as clear in the past, and will remain until the next major version of code-inp.
713+
*/
714+
sync_scroll() {
715+
this.syncScroll();
692716
}
693717

694718
/**
695-
* @deprecated Please use `codeInput.CodeInput.escapeHtml`
719+
* @deprecated This is internal code used by code-input and it may change/be removed to improve the library. This function with its old name remains, though, because that wasn't made clear in the past.
696720
*/
697721
escape_html(text) {
698722
return this.escapeHtml(text);
699723
}
700724

701725
/**
702-
* @deprecated Please use `codeInput.CodeInput.getTemplate`
726+
* @deprecated This is internal code used by code-input and it may change/be removed to improve the library. This function with its old name remains, though, because that wasn't made clear in the past.
703727
*/
704728
get_template() {
705729
return this.getTemplate();

0 commit comments

Comments
 (0)