Skip to content

Commit 0bbaaef

Browse files
committed
Add final compatibility (fixes #211) for autogrow plugin; Fully tested
1 parent 085f5b5 commit 0bbaaef

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

code-input.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -555,13 +555,13 @@ var codeInput = {
555555
*/
556556
syncSize() {
557557
// Synchronise the size of the pre/code and textarea elements
558+
// Set directly as well as via the variable so precedence
559+
// not lowered, breaking CSS backwards compatibility.
558560
const height = getComputedStyle(this.getStyledHighlightingElement()).height;
559-
this.textareaElement.style.height = 0;
560561
this.textareaElement.style.height = height;
561562
this.internalStyle.setProperty("--code-input_synced-height", height);
562563

563564
const width = getComputedStyle(this.getStyledHighlightingElement()).width;
564-
this.textareaElement.style.width = 0;
565565
this.textareaElement.style.width = width;
566566
this.internalStyle.setProperty("--code-input_synced-width", width);
567567
}
@@ -860,6 +860,10 @@ var codeInput = {
860860
this.syncSize();
861861
});
862862
resizeObserver.observe(this);
863+
// Must resize when this content resizes, for autogrow plugin
864+
// support.
865+
resizeObserver.observe(this.preElement);
866+
resizeObserver.observe(this.codeElement);
863867

864868
// Synchronise colors
865869
const preColorChangeCallback = (evt) => {

plugins/autocomplete.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ codeInput.plugins.Autocomplete = class extends codeInput.Plugin {
6565
// Hasn't saved text in test pre to find pos
6666
// Need to regenerate text in test pre
6767
return this.getCaretCoordinates(codeInput, textarea, charIndex, false);
68-
}
68+
}
6969
afterSpan = spans[1];
7070
} else {
7171
/* Inspired by https://github.com/component/textarea-caret-position */
@@ -74,7 +74,8 @@ codeInput.plugins.Autocomplete = class extends codeInput.Plugin {
7474
let beforeSpan = document.createElement("span");
7575
beforeSpan.textContent = textarea.value.substring(0, charIndex);
7676
afterSpan = document.createElement("span");
77-
afterSpan.textContent = "."; // Placeholder
77+
afterSpan.textContent = ""; // Text here would potentially make the
78+
// invisible pre and thus the autogrowing code-input element wider
7879

7980
// Clear test pre - https://developer.mozilla.org/en-US/docs/Web/API/Node/removeChild
8081
while (testPosElem.firstChild) {

plugins/prism-line-numbers.css

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ code-input.line-numbers.code-input_autogrow_width textarea, .line-numbers code-i
3939
width: calc(var(--code-input_autogrow_min-width) - max(3.8em, var(--padding-left, 16px)) - var(--padding-right, 16px))!important; /* So minimum width possible while containing highlighted code */
4040
min-width: max(var(--code-input_synced-width), calc(100% - max(3.8em, var(--padding-left, 16px)) - var(--padding-right, 16px)));
4141
}
42-
code-input.code-input_autogrow_width pre code,
43-
code-input.code-input_autogrow_width pre {
42+
code-input.line-numbers.code-input_autogrow_width pre code,
43+
code-input.line-numbers.code-input_autogrow_width pre,
44+
.line-numbers code-input.code-input_autogrow_width pre code,
45+
.line-numbers code-input.code-input_autogrow_width pre {
4446
min-width: calc(var(--code-input_autogrow_min-width) - max(3.8em, var(--padding-left, 16px)) - var(--padding-right, 16px));
4547
}

0 commit comments

Comments
 (0)