Skip to content

Commit 258ef2a

Browse files
authored
Merge pull request #146 from WebCoder49/let-textareas-do-their-job
Return scrolling to the textarea and pre code element, not the code-input
2 parents 728c16c + 2eca69e commit 258ef2a

File tree

5 files changed

+167
-98
lines changed

5 files changed

+167
-98
lines changed

code-input.css

Lines changed: 27 additions & 26 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;
@@ -22,6 +21,10 @@ code-input {
2221
tab-size: 2;
2322
caret-color: darkgrey;
2423
white-space: pre;
24+
word-spacing: normal;
25+
word-break: normal;
26+
word-wrap: normal;
27+
2528
padding: 0!important; /* Use --padding to set the code-input element's padding */
2629
display: grid;
2730
grid-template-columns: 100%;
@@ -38,21 +41,16 @@ code-input textarea, code-input:not(.code-input_pre-element-styled) pre code, co
3841
margin: 0px!important;
3942
padding: var(--padding, 16px)!important;
4043
border: 0;
41-
min-width: calc(100% - var(--padding) * 2);
42-
min-height: calc(100% - var(--padding) * 2);
44+
width: calc(100% - var(--padding) * 2);
45+
height: calc(100% - var(--padding) * 2);
4346
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;
47+
overflow: auto;
4548
resize: none;
4649
grid-row: 1;
4750
grid-column: 1;
4851
display: block;
4952
}
5053

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-
5654
code-input:not(.code-input_pre-element-styled) pre, code-input.code-input_pre-element-styled pre code {
5755
/* Remove all margin and padding from others */
5856
margin: 0px!important;
@@ -68,6 +66,11 @@ code-input textarea, code-input pre, code-input pre * {
6866
line-height: inherit!important;
6967
tab-size: inherit!important;
7068
text-align: inherit!important;
69+
70+
white-space: inherit!important;
71+
word-spacing: inherit!important;
72+
word-break: inherit!important;
73+
word-wrap: inherit!important;
7174
}
7275

7376
/* Make changing the text direction propogate */
@@ -84,7 +87,7 @@ code-input textarea[dir=rtl] + pre {
8487
}
8588

8689

87-
code-input textarea, code-input pre {
90+
code-input textarea, code-input pre, code-input pre code {
8891
/* In the same place */
8992
grid-column: 1;
9093
grid-row: 1;
@@ -104,20 +107,12 @@ code-input pre {
104107
code-input textarea {
105108
color: transparent;
106109
background: transparent;
107-
caret-color: inherit!important; /* Or choose your favourite color */
110+
caret-color: inherit!important;
108111
}
109112
code-input textarea::placeholder {
110113
color: lightgrey;
111114
}
112115

113-
/* Can be scrolled */
114-
code-input textarea, code-input pre {
115-
white-space: inherit;
116-
word-spacing: normal;
117-
word-break: normal;
118-
word-wrap: normal;
119-
}
120-
121116
/* No resize on textarea; transfer outline on focus to code-input element */
122117
code-input textarea {
123118
resize: none;
@@ -158,12 +153,8 @@ Sticks to the top of the code-input element */
158153
code-input .code-input_dialog-container {
159154
z-index: 2;
160155

161-
position: sticky;
162156
grid-row: 1;
163157
grid-column: 1;
164-
165-
top: 0;
166-
left: 0;
167158

168159
margin: 0;
169160
padding: 0;
@@ -173,6 +164,10 @@ code-input .code-input_dialog-container {
173164
/* Dialog boxes' text is based on text-direction */
174165
text-align: inherit;
175166
}
167+
code-input.code-input_pre-element-styled .code-input_dialog-container {
168+
width: calc(100% + 2 * var(--padding));
169+
}
170+
176171
[dir=rtl] code-input .code-input_dialog-container, code-input[dir=rtl] .code-input_dialog-container {
177172
left: unset;
178173
right: 0;
@@ -187,13 +182,18 @@ code-input .code-input_dialog-container .code-input_keyboard-navigation-instruct
187182
position: absolute;
188183
background-color: black;
189184
color: white;
185+
190186
padding: 2px;
187+
line-height: 2em;
188+
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. */
189+
191190
padding-left: 10px;
192191
margin: 0;
193192
text-wrap: balance;
194-
overflow: hidden;
193+
overflow: auto;
195194
text-overflow: ellipsis;
196195
width: calc(100% - 12px);
196+
box-sizing: content-box; /* Make height, width work consistently no matter the box-sizing of ancestors. */
197197
max-height: 3em;
198198
}
199199
code-input:has(pre[dir=rtl]) .code-input_dialog-container .code-input_keyboard-navigation-instructions {
@@ -212,5 +212,6 @@ code-input .code-input_dialog-container .code-input_keyboard-navigation-instruct
212212
code-input:not(:has(.code-input_keyboard-navigation-instructions:empty)):has(textarea:focus):not(.code-input_mouse-focused) textarea,
213213
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,
214214
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;
215+
padding-top: calc(var(--padding) + 2em + 4px)!important;
216+
height: calc(100% - var(--padding) * 2 - 2em - 4px)!important;
216217
}

code-input.d.ts

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -295,11 +295,11 @@ export class Template {
295295
* @param {boolean} preElementStyled - is the `<pre>` element CSS-styled as well as the `<code>` element? If true, `<pre>` element's scrolling is synchronised; if false, `<code>` element's scrolling is synchronised.
296296
* @param {boolean} isCode - is this for writing code? If true, the code-input's lang HTML attribute can be used, and the `<code>` element will be given the class name 'language-[lang attribute's value]'.
297297
* @param {false} includeCodeInputInHighlightFunc - Setting this to true passes the `<code-input>` element as a second argument to the highlight function.
298-
* @param {boolean} autoDisableDuplicateSearching - Leaving this as true uses code-input's default fix for preventing duplicate results in Ctrl+F searching from the input and result elements, and setting this to false indicates your highlighting function implements its own fix. The default fix works by moving text content from elements to CSS `::before` pseudo-elements after highlighting.
299298
* @param {codeInput.Plugin[]} plugins - An array of plugin objects to add extra features - see `codeInput.Plugin`
299+
* @param {boolean} addPlaceholderToLastEmptyLine - Setting this to true adds a space character to the end of the `<code>` element before highlighting when its last line is empty, ensuring that last line is displayed and aligns with the editing.
300300
* @returns template object
301301
*/
302-
constructor(highlight?: (codeElement: HTMLElement) => void, preElementStyled?: boolean, isCode?: boolean, includeCodeInputInHighlightFunc?: false, autoDisableDuplicateSearching?: boolean, plugins?: Plugin[])
302+
constructor(highlight?: (codeElement: HTMLElement) => void, preElementStyled?: boolean, isCode?: boolean, includeCodeInputInHighlightFunc?: false, plugins?: Plugin[], addPlaceholderToLastEmptyLine?: boolean)
303303
/**
304304
* **When `includeCodeInputInHighlightFunc` is `true`, `highlight` takes two parameters: the `<pre><code>` element, and the `<code-input>` element.**
305305
*
@@ -309,25 +309,17 @@ export class Template {
309309
* @param {boolean} preElementStyled - is the `<pre>` element CSS-styled as well as the `<code>` element? If true, `<pre>` element's scrolling is synchronised; if false, `<code>` element's scrolling is synchronised.
310310
* @param {boolean} isCode - is this for writing code? If true, the code-input's lang HTML attribute can be used, and the `<code>` element will be given the class name 'language-[lang attribute's value]'.
311311
* @param {true} includeCodeInputInHighlightFunc - Setting this to true passes the `<code-input>` element as a second argument to the highlight function.
312-
* @param {boolean} autoDisableDuplicateSearching - Leaving this as true uses code-input's default fix for preventing duplicate results in Ctrl+F searching from the input and result elements, and setting this to false indicates your highlighting function implements its own fix. The default fix works by moving text content from elements to CSS `::before` pseudo-elements after highlighting.
313312
* @param {codeInput.Plugin[]} plugins - An array of plugin objects to add extra features - see `codeInput.Plugin`
313+
* @param {boolean} addPlaceholderToLastEmptyLine - Setting this to true adds a space character to the end of the `<code>` element before highlighting when its last line is empty, ensuring that last line is displayed and aligns with the editing.
314314
* @returns template object
315315
*/
316-
constructor(highlight?: (codeElement: HTMLElement, codeInput: CodeInput) => void, preElementStyled?: boolean, isCode?: boolean, includeCodeInputInHighlightFunc?: true, autoDisableDuplicateSearching?: boolean, plugins?: Plugin[])
316+
constructor(highlight?: (codeElement: HTMLElement, codeInput: CodeInput) => void, preElementStyled?: boolean, isCode?: boolean, includeCodeInputInHighlightFunc?: true, plugins?: Plugin[], addPlaceholderToLastEmptyLine?: boolean)
317317
highlight: Function
318318
preElementStyled: boolean
319319
isCode: boolean
320320
includeCodeInputInHighlightFunc: boolean
321-
autoDisableDuplicateSearching: boolean
322321
plugins: Plugin[]
323-
/**
324-
* @deprecated Please give a value for the `autoDisableDuplicateSearching` parameter.
325-
*/
326-
constructor(highlight?: (code: HTMLElement) => void, preElementStyled?: boolean, isCode?: boolean, includeCodeInputInHighlightFunc?: false, plugins?: Plugin[])
327-
/**
328-
* @deprecated Please give a value for the `autoDisableDuplicateSearching` parameter.
329-
*/
330-
constructor(highlight?: (code: HTMLElement, codeInput: CodeInput) => void, preElementStyled?: boolean, isCode?: boolean, includeCodeInputInHighlightFunc?: true, plugins?: Plugin[])
322+
addPlaceholderToLastEmptyLine: boolean
331323
}
332324

333325
/**

0 commit comments

Comments
 (0)