Skip to content

Commit b8ceb7d

Browse files
committed
Optimise + add plugins README
1 parent bf44e14 commit b8ceb7d

File tree

4 files changed

+72
-15
lines changed

4 files changed

+72
-15
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ The next step is to set up a `template` to link `code-input` to your syntax-high
8484
<!--...-->
8585
<script src="plugins/autodetect.js"></script>
8686
<script src="plugins/indent.js"></script>
87-
<script src="plugins/autocomplete.js"></script>
8887
<!--...-->
8988
<script>
9089
codeInput.registerTemplate("syntax-highlighted",
@@ -99,6 +98,8 @@ The next step is to set up a `template` to link `code-input` to your syntax-high
9998
</script>
10099
```
101100

101+
To see a full list of plugins and their functions, please see [plugins/README.md](./plugins/README.md).
102+
102103
### Using the component
103104
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 `lang` attribute will add a `language-{value}` class to the `pre code` block. You can now use HTML attributes and events to make your element as simple or interactive as you like!
104105
```HTML

code-input.css

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,17 @@ code-input textarea, code-input:not(.code-input_pre-element-styled) pre code, co
3131
margin: 0px!important;
3232
padding: var(--padding, 16px)!important;
3333
border: 0;
34+
width: calc(100% - var(--padding, 16px) * 2);
35+
height: calc(100% - var(--padding, 16px) * 2);
36+
}
37+
code-input:not(.code-input_pre-element-styled) pre, code-input.code-input_pre-element-styled pre code {
38+
/* Remove all margin and padding from others */
39+
margin: 0px!important;
40+
padding: 0px!important;
3441
width: 100%;
3542
height: 100%;
3643
}
44+
3745
code-input textarea, code-input pre, code-input pre * {
3846
/* Also add text styles to highlighing tokens */
3947
font-size: inherit!important;

code-input.js

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,14 @@ var codeInput = {
6868

6969
/* Syntax-highlighting functions */
7070
update(text) {
71-
console.log("Update", text);
72-
7371
// Prevent this from running multiple times on the same input when "value" attribute is changed,
74-
// by saving the value currently updated as _inUpdateNewText. Thank you to peterprvy for this.
75-
if(this.value != text) {
76-
this._inUpdateNewText = text;
77-
this.value = text; // Change value attribute if necessary.
78-
this._inUpdateNewText = undefined;
79-
}
72+
// by not running when value is already equal to the input of this (implying update has already
73+
// been run). Thank you to peterprvy for this.
74+
if(this.value == text) return;
75+
76+
console.log("Update", text);
77+
78+
this.value = text; // Change value attribute if necessary.
8079
if(this.querySelector("textarea").value != text) this.querySelector("textarea").value = text;
8180

8281

@@ -206,11 +205,7 @@ var codeInput = {
206205
switch (name) {
207206

208207
case "value":
209-
210-
// Update code, only if this text has not already been updated.
211-
if(this._inUpdateNewText === undefined || newValue !== this._inUpdateNewText) {
212-
this.update(newValue);
213-
}
208+
this.update(newValue);
214209

215210
break;
216211

@@ -227,12 +222,20 @@ var codeInput = {
227222
break;
228223

229224
case "lang":
225+
230226
let code = this.querySelector("pre code");
231227
let main_textarea = this.querySelector("textarea");
232228

229+
// Check not already updated
230+
if(newValue != null) {
231+
newValue = newValue.toLowerCase();
232+
233+
if(code.classList.contains(`language-${newValue}`)) break; // Already updated
234+
}
235+
236+
233237
// Case insensitive
234238
oldValue = oldValue.toLowerCase();
235-
newValue = newValue.toLowerCase();
236239

237240
// Remove old language class and add new
238241
console.log("code-input: Language: REMOVE", "language-" + oldValue);

plugins/README.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Code-input: Plugins
2+
## List Of Plugins
3+
4+
### Autocomplete
5+
Display a popup under the caret using the text in the code-input element. This works well with autocomplete suggestions.
6+
Files: [autocomplete.js](./autocomplete.js) / [autocomplete.css](./autocomplete.css)
7+
[🚀 *CodePen Demo*](https://codepen.io/WebCoder49/pen/xxapjXB)
8+
### Autodetect
9+
Autodetect the language live and change the `lang` attribute using the syntax highlighter's autodetect capabilities. Works with highlight.js.
10+
Files: [autodetect.js](./autodetect.js)
11+
[🚀 *CodePen Demo*](https://codepen.io/WebCoder49/pen/eYLyMae)
12+
### Debounce Update
13+
Debounce the update and highlighting function ([What is Debouncing?](https://medium.com/@jamischarles/what-is-debouncing-2505c0648ff1))
14+
Files: [debounce-update.js](./debounce-update.js)
15+
[🚀 *CodePen Demo*](https://codepen.io/WebCoder49/pen/GRXyxzV)
16+
### Indent
17+
Adds indentation using the `Tab` key, and auto-indents after a newline, as well as making it possible to indent/unindent multiple lines using Tab/Shift+Tab
18+
Files: [indent.js](./indent.js)
19+
[🚀 *CodePen Demo*](https://codepen.io/WebCoder49/pen/WNgdzar)
20+
### Prism Line Numbers
21+
Allows code-input elements to be used with the Prism.js line-numbers plugin, as long as the code-input element or a parent element of it has the CSS class `line-numbers`. [Prism.js Plugin Docs](https://prismjs.com/plugins/line-numbers/)
22+
Files: [prism-line-numbers.css](./prism-line-numbers.css) (NO JS FILE)
23+
[🚀 *CodePen Demo*](https://codepen.io/WebCoder49/pen/XWPVrWv)
24+
## Using Plugins
25+
Plugins allow you to add extra features to a template, like [automatic indentation](./indent.js) or [support for highlight.js's language autodetection](./autodetect.js). To use them, just:
26+
- Import the plugins' JS/CSS files (there may only be one of these; import all of the files that exist) after you have imported `code-input` and before registering the template.
27+
- If a JavaScript file is present, Place an instance of each plugin in the array of plugins argument when registering, like this:
28+
```html
29+
<script src="code-input.js"></script>
30+
<!--...-->
31+
<script src="plugins/autodetect.js"></script>
32+
<script src="plugins/indent.js"></script>
33+
<!--...-->
34+
<script>
35+
codeInput.registerTemplate("syntax-highlighted",
36+
codeInput.templates.hljs(
37+
hljs,
38+
[
39+
new codeInput.plugins.Autodetect(),
40+
new codeInput.plugins.Indent()
41+
]
42+
)
43+
);
44+
</script>
45+
```

0 commit comments

Comments
 (0)