Skip to content

Commit d0789d6

Browse files
authored
Merge branch 'main' into pre-or-code-styled
2 parents 71a4829 + 6a53600 commit d0789d6

File tree

12 files changed

+108
-56
lines changed

12 files changed

+108
-56
lines changed

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# code-input
22

3+
> **:warning: This document is a work in progress for the next release. [Read latest released instructions.](https://github.com/WebCoder49/code-input/tree/v2.5.1/README.md)**
4+
35
![Click to Switch](https://img.shields.io/static/v1?label=&message=Click%20to%20Switch:%20&color=grey&style=for-the-badge)[![GitHub](https://img.shields.io/static/v1?label=&message=GitHub&color=navy&style=for-the-badge&logo=github)](https://github.com/WebCoder49/code-input)[![NPM](https://img.shields.io/static/v1?label=&message=NPM&color=red&style=for-the-badge&logo=npm)](https://www.npmjs.com/package/@webcoder49/code-input)
46

57
[![View License](https://img.shields.io/github/license/webcoder49/code-input?style=for-the-badge)](LICENSE) [![View Releases](https://img.sHields.io/github/v/release/webcoder49/code-input?style=for-the-badge)](https://github.com/WebCoder49/code-input/releases) [![View the demo on CodePen](https://img.shields.io/static/v1?label=Demo&message=on%20CodePen&color=orange&logo=codepen&style=for-the-badge)](https://codepen.io/WebCoder49/full/jOypJOx)
@@ -117,18 +119,18 @@ The next step is to set up a `template` to link `code-input` to your syntax-high
117119
</script>
118120
```
119121

120-
> ⚠️ Unfortunately placing multiple plugins of the same type in a template can currently cause errors and undefined behaviour, even if such a configuration makes logical sense. [This is issue #118](https://github.com/WebCoder49/code-input/issues/118) and will be fixed as soon as possible - if you'd like to help and have the time you're welcome, but it's also at the top of the maintainer's To-Do list.
122+
> ⚠️ Unfortunately placing multiple plugins of the same type in a template can currently cause errors and undefined behaviour, even if such a configuration makes logical sense. [This is issue #118](https://github.com/WebCoder49/code-input/issues/118) ~~and will be fixed as soon as possible - if you'd like to help and have the time you're welcome, but it's also at the top of the maintainer's To-Do list.~~ and should be fixed in major version 3 - right now, I don't believe there's much demand or use, and workarounds are not too complex.
121123
122124
To see a full list of plugins and their functions, please see [plugins/README.md](./plugins/README.md).
123125

124126
### 4. Using the component
125-
Now that you have registered a template, you can use the custom `<code-input>` element in HTML. If you have more than one template registered, you need to add the template name as the `template` attribute. With the element, using the `language` attribute will add a `language-{value}` class to the `pre code` block. You can now use HTML attributes and events, as well as CSS styles, to make your element as simple or interactive as you like, as if it were a `textarea` element!
127+
Now that you have registered a template, you can use the custom `<code-input>` element in HTML. I recommend it surrounds a fallback `<textarea code-input-fallback>` element which will be used instead when JavaScript support is absent, and will pass its attributes to the `<code-input>` element otherwise, as shown below. If you have more than one template registered, you need to add the template name as the `template` attribute. With the element, using the `language` attribute will add a `language-{value}` class to the `pre code` block. You can now use HTML attributes and events, as well as CSS styles, to make your element as simple or interactive as you like, as if it were a `textarea` element!
126128
```HTML
127-
<code-input language="HTML"></code-input>
129+
<code-input language="HTML"><textarea code-input-fallback></textarea></code-input>
128130
```
129131
*or*
130132
```HTML
131-
<code-input language="HTML" placeholder="Type code here" template="syntax-highlighted" onchange="console.log('Your code is', this.value)">&lt; href='https://github.com/WebCoder49/code-input'>code-input&lt;/a></code-input>
133+
<code-input language="HTML" template="syntax-highlighted" onchange="console.log('Your code is', this.value)"><textarea code-input-fallback placeholder="Type code here">&lt; href='https://github.com/WebCoder49/code-input'>code-input&lt;/a></textarea></code-input>
132134
```
133135

134136
> ⚠️ At the moment, you need to set the `--padding` property rather than `padding` for a `code-input` element's CSS. All other properties should work as normal.

code-input.css

Lines changed: 49 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ code-input {
1212
top: 0;
1313
left: 0;
1414

15+
color: black;
16+
background-color: white;
17+
1518
/* Normal inline styles */
1619
margin: 8px;
1720
--padding: 16px;
@@ -38,8 +41,8 @@ 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+
min-width: calc(100% - var(--padding, 16px) * 2);
45+
min-height: calc(100% - var(--padding, 16px) * 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. */
4447
overflow: hidden;
4548
resize: none;
@@ -58,9 +61,6 @@ code-input:not(.code-input_pre-element-styled) pre, code-input.code-input_pre-el
5861
margin: 0!important;
5962
padding: 0!important;
6063
border: 0!important;
61-
62-
width: 100%;
63-
height: 100%;
6464
}
6565

6666
code-input textarea, code-input pre, code-input pre * {
@@ -94,7 +94,7 @@ code-input textarea, code-input pre {
9494

9595
/* Move the textarea in front of the result */
9696

97-
code-input textarea {
97+
code-input textarea:not([code-input-fallback]) {
9898
z-index: 1;
9999
}
100100
code-input pre {
@@ -103,7 +103,7 @@ code-input pre {
103103

104104
/* Make textarea almost completely transparent, except for caret and placeholder */
105105

106-
code-input textarea {
106+
code-input textarea:not([code-input-fallback]) {
107107
color: transparent;
108108
background: transparent;
109109
caret-color: inherit!important; /* Or choose your favourite color */
@@ -126,7 +126,7 @@ code-input textarea {
126126
outline: none!important;
127127
}
128128
code-input:has(textarea:focus):not(.code-input_mouse-focused) {
129-
outline: 2px solid black;
129+
outline: 2px solid currentColor;
130130
}
131131

132132
/* Before registering give a hint about how to register. */
@@ -138,19 +138,30 @@ code-input:not(.code-input_registered) {
138138

139139
code-input:not(.code-input_registered)::after {
140140
/* Display message to register */
141-
content: "Use codeInput.registerTemplate to set up.";
141+
content: "No-JavaScript fallback. For highlighting and plugins: as a user use a newer browser/enable JavaScript support; as a developer use codeInput.registerTemplate.";
142142
display: block;
143143
position: absolute;
144-
bottom: var(--padding);
145-
left: var(--padding);
146-
width: calc(100% - 2 * var(--padding));
144+
bottom: 0;
145+
left: var(--padding, 16px);
146+
width: calc(100% - 2 * var(--padding, 16px));
147+
overflow-x: auto;
147148

148-
border-top: 1px solid grey;
149-
outline: var(--padding) solid white;
150-
background-color: white;
149+
border-top: 1px solid currentColor;
150+
outline-top: 0;
151+
background-color: inherit;
152+
color: inherit;
153+
154+
margin: 0;
155+
padding: 0;
156+
height: 2em;
151157
}
152158

153-
code-input:not(.code-input_loaded) pre, code-input:not(.code-input_loaded) textarea {
159+
code-input:not(.code-input_registered) textarea {
160+
/* Don't overlap with message */
161+
min-height: calc(100% - var(--padding, 16px) * 2 - 2em);
162+
}
163+
164+
code-input:not(.code-input_loaded) pre, code-input:not(.code-input_loaded) textarea:not([code-input-fallback]) {
154165
opacity: 0;
155166
}
156167

@@ -169,12 +180,16 @@ code-input .code-input_dialog-container {
169180

170181
margin: 0;
171182
padding: 0;
172-
width: 100%;
173183
height: 0;
184+
width: 100%;
174185

175186
/* Dialog boxes' text is based on text-direction */
176187
text-align: inherit;
177188
}
189+
code-input.code-input_pre-element-styled .code-input_dialog-container {
190+
width: calc(100% + 2 * var(--padding, 16px) - 2px);
191+
}
192+
178193
[dir=rtl] code-input .code-input_dialog-container, code-input[dir=rtl] .code-input_dialog-container {
179194
left: unset;
180195
right: 0;
@@ -203,16 +218,28 @@ code-input:has(pre[dir=rtl]) .code-input_dialog-container .code-input_keyboard-n
203218
right: 0;
204219
}
205220

206-
code-input:not(:has(textarea:focus)) .code-input_dialog-container .code-input_keyboard-navigation-instructions,
221+
code-input:not(:has(textarea:not([code-input-fallback]):focus)) .code-input_dialog-container .code-input_keyboard-navigation-instructions,
207222
code-input.code-input_mouse-focused .code-input_dialog-container .code-input_keyboard-navigation-instructions,
208223
code-input .code-input_dialog-container .code-input_keyboard-navigation-instructions:empty {
209224
/* When not keyboard-focused / no instructions don't show instructions */
210225
display: none;
211226
}
212227

213228
/* Things with padding when instructions are present */
214-
code-input:not(:has(.code-input_keyboard-navigation-instructions:empty)):has(textarea:focus):not(.code-input_mouse-focused) textarea,
215-
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,
216-
code-input:not(:has(.code-input_keyboard-navigation-instructions:empty)):has(textarea:focus):not(.code-input_mouse-focused).code-input_pre-element-styled pre {
217-
padding-top: calc(var(--padding) + 3em)!important;
229+
code-input:not(:has(.code-input_keyboard-navigation-instructions:empty)):has(textarea:not([code-input-fallback]):focus):not(.code-input_mouse-focused) textarea,
230+
code-input:not(:has(.code-input_keyboard-navigation-instructions:empty)):has(textarea:not([code-input-fallback]):focus):not(.code-input_mouse-focused):not(.code-input_pre-element-styled) pre code,
231+
code-input:not(:has(.code-input_keyboard-navigation-instructions:empty)):has(textarea:not([code-input-fallback]):focus):not(.code-input_mouse-focused).code-input_pre-element-styled pre {
232+
padding-top: calc(var(--padding, 16px) + 3em)!important;
233+
}
234+
code-input:not(:has(.code-input_keyboard-navigation-instructions:empty)):has(textarea:not([code-input-fallback]):focus):not(.code-input_mouse-focused) textarea, code-input:not(:has(.code-input_keyboard-navigation-instructions:empty)):has(textarea:not([code-input-fallback]):focus):not(.code-input_mouse-focused):not(.code-input_pre-element-styled) pre code, code-input:not(:has(.code-input_keyboard-navigation-instructions:empty)):has(textarea:not([code-input-fallback]):focus):not(.code-input_mouse-focused).code-input_pre-element-styled pre {
235+
min-height: calc(100% - var(--padding, 16px) * 2 - 3em);
236+
}
237+
238+
/* No JavaScript fallback - styles to override all previous */
239+
240+
code-input textarea[code-input-fallback] {
241+
overflow: auto;
242+
background-color: inherit;
243+
color: inherit;
244+
height: max-content;
218245
}

code-input.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,11 @@ export namespace plugins {
178178
instructionTranslations?: {
179179
closeDialog?: string;
180180
input?: string;
181+
guidanceFormat?: string;
182+
guidanceLineRange?: (current:Number, max: Number) => string;
183+
guidanceColumnRange?: (line: Number, current: Number, max: Number) => string;
184+
guidanceValidLine?: (line: Number) => string;
185+
guidanceValidColumn?: (line: Number, column: Number) => string;
181186
});
182187
/**
183188
* Show a search-like dialog prompting line number.

code-input.js

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -605,16 +605,34 @@ var codeInput = {
605605

606606
this.pluginEvt("beforeElementsAdded");
607607

608+
const fallbackTextarea = this.querySelector("textarea[code-input-fallback]");
609+
let value;
610+
if(fallbackTextarea) {
611+
// Fallback textarea exists
612+
// Sync attributes; existing code-input attributes take priority
613+
let textareaAttributeNames = fallbackTextarea.getAttributeNames();
614+
for(let i = 0; i < textareaAttributeNames.length; i++) {
615+
const attr = textareaAttributeNames[i];
616+
if(!this.hasAttribute(attr)) {
617+
this.setAttribute(attr, fallbackTextarea.getAttribute(attr));
618+
}
619+
}
620+
// Sync value
621+
value = fallbackTextarea.value;
622+
} else {
623+
value = this.unescapeHtml(this.innerHTML);
624+
}
625+
value = value || this.getAttribute("value") || "";
626+
608627
// First-time attribute sync
609-
let lang = this.getAttribute("language") || this.getAttribute("lang");
610-
let placeholder = this.getAttribute("placeholder") || this.getAttribute("language") || this.getAttribute("lang") || "";
611-
let value = this.unescapeHtml(this.innerHTML) || this.getAttribute("value") || "";
612-
// Value attribute deprecated, but included for compatibility
628+
const lang = this.getAttribute("language") || this.getAttribute("lang");
629+
const placeholder = this.getAttribute("placeholder") || lang || "";
630+
613631

614632
this.initialValue = value; // For form reset
615633

616634
// Create textarea
617-
let textarea = document.createElement("textarea");
635+
const textarea = document.createElement("textarea");
618636
textarea.placeholder = placeholder;
619637
if(value != "") {
620638
textarea.value = value;

0 commit comments

Comments
 (0)