Skip to content

Commit cf4c7ee

Browse files
committed
Make size of textarea and pre code be synced when code-input resized
1 parent 72563fc commit cf4c7ee

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

code-input.js

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,21 @@ var codeInput = {
524524
if (this.template.includeCodeInputInHighlightFunc) this.template.highlight(resultElement, this);
525525
else this.template.highlight(resultElement);
526526

527+
this.syncSize();
528+
529+
// Scroll to the caret by focusing, though this shouldn't count as a focus event
530+
this.passEventsToTextarea = false;
531+
this.textareaElement.blur();
532+
this.textareaElement.focus();
533+
this.passEventsToTextarea = true;
534+
535+
this.pluginEvt("afterHighlight");
536+
}
537+
538+
/**
539+
* Set the size of the pre/code element to the size of the textarea element.
540+
*/
541+
syncSize() {
527542
// Synchronise the size of the pre/code and textarea elements
528543
if(this.template.preElementStyled) {
529544
this.style.backgroundColor = getComputedStyle(this.preElement).backgroundColor;
@@ -534,13 +549,6 @@ var codeInput = {
534549
this.textareaElement.style.height = getComputedStyle(this.codeElement).height;
535550
this.textareaElement.style.width = getComputedStyle(this.codeElement).width;
536551
}
537-
// Scroll to the caret by focusing, though this shouldn't count as a focus event
538-
this.passEventsToTextarea = false;
539-
this.textareaElement.blur();
540-
this.textareaElement.focus();
541-
this.passEventsToTextarea = true;
542-
543-
this.pluginEvt("afterHighlight");
544552
}
545553

546554
/**
@@ -659,6 +667,12 @@ var codeInput = {
659667

660668
this.value = value;
661669
this.animateFrame();
670+
671+
const resizeObserver = new ResizeObserver((elements) => {
672+
// The only element that could be resized is this code-input element.
673+
this.syncSize();
674+
});
675+
resizeObserver.observe(this);
662676
}
663677

664678
/**

0 commit comments

Comments
 (0)