Skip to content

Commit 085f5b5

Browse files
committed
Add almost-perfect (#211) cross-browser Autogrow plugin (fixes #206); make Autogrow fit dialogs by default; make dialogs fully disappear when closed (#210)
1 parent 37fdd10 commit 085f5b5

File tree

7 files changed

+58
-38
lines changed

7 files changed

+58
-38
lines changed

code-input.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ code-input textarea, code-input pre, code-input pre * {
9999
tab-size: inherit!important;
100100
text-align: inherit!important;
101101
}
102-
code-input textarea, code-input pre, code-input pre code {
102+
code-input pre, code-input pre code {
103103
overflow: visible!important;
104104
}
105105

code-input.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -558,12 +558,12 @@ var codeInput = {
558558
const height = getComputedStyle(this.getStyledHighlightingElement()).height;
559559
this.textareaElement.style.height = 0;
560560
this.textareaElement.style.height = height;
561-
this.textareaElement.style.setProperty("--code-input_synced-height", height);
561+
this.internalStyle.setProperty("--code-input_synced-height", height);
562562

563563
const width = getComputedStyle(this.getStyledHighlightingElement()).width;
564564
this.textareaElement.style.width = 0;
565565
this.textareaElement.style.width = width;
566-
this.textareaElement.style.setProperty("--code-input_synced-width", width);
566+
this.internalStyle.setProperty("--code-input_synced-width", width);
567567
}
568568

569569
/**
@@ -785,6 +785,14 @@ var codeInput = {
785785

786786
this.innerHTML = ""; // Clear Content
787787

788+
// Add internal style as non-externally-overridable alternative to style attribute for e.g. syncing color
789+
this.classList.add("code-input_styles_" + codeInput.stylesheetI);
790+
const stylesheet = document.createElement("style");
791+
stylesheet.innerHTML = "code-input.code-input_styles_" + codeInput.stylesheetI + " {}";
792+
this.appendChild(stylesheet);
793+
this.internalStyle = stylesheet.sheet.cssRules[0].style;
794+
codeInput.stylesheetI++;
795+
788796
// Synchronise attributes to textarea
789797
for(let i = 0; i < this.attributes.length; i++) {
790798
let attribute = this.attributes[i].name;
@@ -853,15 +861,6 @@ var codeInput = {
853861
});
854862
resizeObserver.observe(this);
855863

856-
857-
// Add internal style as non-externally-overridable alternative to style attribute for e.g. syncing color
858-
this.classList.add("code-input_styles_" + codeInput.stylesheetI);
859-
const stylesheet = document.createElement("style");
860-
stylesheet.innerHTML = "code-input.code-input_styles_" + codeInput.stylesheetI + " {}";
861-
this.appendChild(stylesheet);
862-
this.internalStyle = stylesheet.sheet.cssRules[0].style;
863-
codeInput.stylesheetI++;
864-
865864
// Synchronise colors
866865
const preColorChangeCallback = (evt) => {
867866
if(evt.propertyName == "color") {

plugins/autogrow.css

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,36 @@ code-input.code-input_autogrow_height textarea {
1818
height: calc(var(--code-input_autogrow_min-height) - var(--padding-top, 16px) - var(--padding-bottom, 16px))!important; /* So minimum height possible while containing highlighted code */
1919
min-height: max(var(--code-input_synced-height), calc(100% - var(--padding-top, 16px) - var(--padding-bottom, 16px)));
2020
}
21+
code-input.code-input_autogrow_height > pre,
22+
code-input.code-input_autogrow_height > pre code {
23+
min-height: calc(var(--code-input_autogrow_min-height) - var(--padding-top, 16px) - var(--padding-bottom, 16px));
24+
}
2125

2226
code-input.code-input_autogrow_width {
2327
--code-input_autogrow_min-width: 100px;
2428
--code-input_autogrow_max-width: 100%;
2529
width: max-content; /* Using unset rather than max-content makes always 100% width. */
2630
max-width: var(--code-input_autogrow_max-width);
2731
}
28-
2932
code-input.code-input_autogrow_width textarea {
3033
width: calc(var(--code-input_autogrow_min-width) - var(--padding-left, 16px) - var(--padding-right, 16px))!important; /* So minimum width possible while containing highlighted code */
3134
min-width: max(var(--code-input_synced-width), calc(100% - var(--padding-left, 16px) - var(--padding-right, 16px)));
3235
}
36+
code-input.code-input_autogrow_width pre code,
37+
code-input.code-input_autogrow_width pre {
38+
min-width: calc(var(--code-input_autogrow_min-width) - var(--padding-left, 16px) - var(--padding-right, 16px));
39+
}
40+
41+
/* FindAndReplace / GoToLine dialog visibility */
42+
code-input.code-input_autogrow_width:has(.code-input_go-to-line_dialog:not(.code-input_go-to-line_hidden-dialog)) {
43+
--code-input_autogrow_min-width: 300px;
44+
}
45+
code-input.code-input_autogrow_height:has(.code-input_go-to-line_dialog:not(.code-input_go-to-line_hidden-dialog)) {
46+
--code-input_autogrow_min-height: 150px;
47+
}
48+
code-input.code-input_autogrow_width:has(.code-input_find-and-replace_dialog:not(.code-input_find-and-replace_hidden-dialog)) {
49+
--code-input_autogrow_min-width: 500px;
50+
}
51+
code-input.code-input_autogrow_height:has(.code-input_find-and-replace_dialog:not(.code-input_find-and-replace_hidden-dialog)) {
52+
--code-input_autogrow_min-height: 170px;
53+
}

plugins/find-and-replace.css

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@
1515
/* Find-and-replace dialog */
1616

1717
@keyframes code-input_find-and-replace_roll-in {
18-
0% {opacity: 0; transform: translateY(-34px);}
19-
100% {opacity: 1; transform: translateY(0px);}
20-
}
21-
22-
@keyframes code-input_find-and-replace_roll-out {
23-
0% {opacity: 1;top: 0;}
24-
100% {opacity: 0;top: -34px;}
18+
0% {
19+
opacity: 0;
20+
transform: translateY(-34px);
21+
}
22+
100% {
23+
opacity: 1;
24+
transform: translateY(0px);
25+
}
2526
}
2627

2728
.code-input_find-and-replace_dialog {
@@ -38,14 +39,11 @@
3839

3940
.code-input_find-and-replace_dialog:not(.code-input_find-and-replace_hidden-dialog) {
4041
animation: code-input_find-and-replace_roll-in .2s;
41-
opacity: 1;
42-
pointer-events: all;
42+
display: block;
4343
}
4444

4545
.code-input_find-and-replace_dialog.code-input_find-and-replace_hidden-dialog {
46-
animation: code-input_find-and-replace_roll-out .2s;
47-
opacity: 0;
48-
pointer-events: none;
46+
display: none;
4947
}
5048

5149
.code-input_find-and-replace_dialog input::placeholder {

plugins/go-to-line.css

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
@keyframes code-input_go-to-line_roll-in {
2-
0% {opacity: 0; transform: translateY(-34px);}
3-
100% {opacity: 1; transform: translateY(0px);}
4-
}
5-
6-
@keyframes code-input_go-to-line_roll-out {
7-
0% {opacity: 1; transform: translateY(0px);}
8-
100% {opacity: 0; transform: translateY(-34px);}
2+
0% {
3+
opacity: 0;
4+
transform: translateY(-34px);
5+
}
6+
100% {
7+
opacity: 1;
8+
transform: translateY(0px);
9+
}
910
}
1011

1112
.code-input_go-to-line_dialog {
@@ -21,14 +22,11 @@
2122

2223
.code-input_go-to-line_dialog:not(.code-input_go-to-line_hidden-dialog) {
2324
animation: code-input_go-to-line_roll-in .2s;
24-
opacity: 1;
25-
pointer-events: all;
25+
display: block;
2626
}
2727

2828
.code-input_go-to-line_dialog.code-input_go-to-line_hidden-dialog {
29-
animation: code-input_go-to-line_roll-out .2s;
30-
opacity: 0;
31-
pointer-events: none;
29+
display: none;
3230
}
3331

3432
.code-input_go-to-line_dialog input::placeholder {

plugins/prism-line-numbers.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,7 @@ 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 {
44+
min-width: calc(var(--code-input_autogrow_min-width) - max(3.8em, var(--padding-left, 16px)) - var(--padding-right, 16px));
45+
}

tests/tester.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,7 @@ console.log("I've got another line!", 2 &lt; 3, "should be true.");
520520
await waitAsync(100); // Wait for width to update
521521

522522
const emptyWidth = textarea.parentElement.clientWidth;
523+
textarea.parentElement.style.setProperty("--code-input_autogrow_max-width", "calc(infinity * 1px)"); // So can grow beyond width of viewport - default CSS limits to width
523524
addText(textarea, "// A very very very very extremely vastly very very very very long line of code is written here in this very comment; yes, this very comment!");
524525
await waitAsync(100); // Wait for width to update
525526

@@ -529,7 +530,6 @@ console.log("I've got another line!", 2 &lt; 3, "should be true.");
529530
await waitAsync(200); // Wait for width to update
530531

531532
testAssertion("Autogrow", "font-size Decrease Decreases Width", textarea.parentElement.clientWidth < fullWidth, `${textarea.parentElement.clientWidth} should be < ${fullWidth}`);
532-
textarea.parentElement.style.setProperty("font-size", "100%");
533533
textarea.parentElement.style.removeProperty("font-size");
534534
textarea.parentElement.style.setProperty("--code-input_autogrow_min-width", (fullWidth + 10) + "px");
535535
await waitAsync(200); // Wait for width to update

0 commit comments

Comments
 (0)