Skip to content

Commit 7b1c88d

Browse files
authored
Merge branch 'main' into esm-support
2 parents 02eabe3 + 56eec90 commit 7b1c88d

24 files changed

+180
-61
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,12 @@ The next step is to set up a `template` to link `code-input` to your syntax-high
6464

6565
- *Highlight.js:*
6666
```js
67-
codeInput.registerTemplate("syntax-highlighted", codeInput.templates.hljs(hljs, [] /* Array of plugins (see below) */));
67+
codeInput.registerTemplate("syntax-highlighted", new codeInput.templates.Hljs(hljs, [] /* Array of plugins (see below) */));
6868
```
6969

7070
- *Prism.js:*
7171
```js
72-
codeInput.registerTemplate("syntax-highlighted", codeInput.templates.prism(Prism, [] /* Array of plugins (see below) */));
72+
codeInput.registerTemplate("syntax-highlighted", new codeInput.templates.Prism(Prism, [] /* Array of plugins (see below) */));
7373
```
7474

7575
- *Custom:*
@@ -106,7 +106,7 @@ The next step is to set up a `template` to link `code-input` to your syntax-high
106106
<!--...-->
107107
<script>
108108
codeInput.registerTemplate("syntax-highlighted",
109-
codeInput.templates.hljs(
109+
new codeInput.templates.Hljs(
110110
hljs,
111111
[
112112
new codeInput.plugins.Autodetect(),

code-input.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ code-input textarea, code-input:not(.code-input_pre-element-styled) pre code, co
4040
border: 0;
4141
min-width: calc(100% - var(--padding) * 2);
4242
min-height: calc(100% - var(--padding) * 2);
43+
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. */
4344
overflow: hidden;
4445
resize: none;
4546
grid-row: 1;

code-input.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,9 @@ export namespace plugins {
103103
class Autocomplete extends Plugin {
104104
/**
105105
* Pass in a function to create a plugin that displays the popup that takes in (popup element, textarea, textarea.selectionEnd).
106-
* @param {(popupElement: HTMLElement, textarea: HTMLTextAreaElement, selectionEnd: number) => void} updatePopupCallback a function to display the popup that takes in (popup element, textarea, textarea.selectionEnd).
106+
* @param {(popupElement: HTMLElement, textarea: HTMLTextAreaElement, selectionEnd: number, selectionStart?: number) => void} updatePopupCallback a function to display the popup that takes in (popup element, textarea, textarea.selectionEnd).
107107
*/
108-
constructor(updatePopupCallback: (popupElem: HTMLElement, textarea: HTMLTextAreaElement, selectionEnd: number) => void);
108+
constructor(updatePopupCallback: (popupElem: HTMLElement, textarea: HTMLTextAreaElement, selectionEnd: number, selectionStart?: number) => void);
109109
}
110110
// ESM-SUPPORT-END-PLUGIN-autocomplete Do not (re)move this - it's needed for ESM generation
111111

code-input.js

Lines changed: 62 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,6 @@ var codeInput = {
478478
* to syntax-highlight it. */
479479

480480
needsHighlight = false; // Just inputted
481-
handleEventsFromTextarea = true; // Turn to false when unusual internal events are called on the textarea
482481
originalAriaDescription;
483482

484483
/**
@@ -519,14 +518,6 @@ var codeInput = {
519518

520519
this.syncSize();
521520

522-
// If editing here, scroll to the caret by focusing, though this shouldn't count as a focus event
523-
if(this.textareaElement === document.activeElement) {
524-
this.handleEventsFromTextarea = false;
525-
this.textareaElement.blur();
526-
this.textareaElement.focus();
527-
this.handleEventsFromTextarea = true;
528-
}
529-
530521
this.pluginEvt("afterHighlight");
531522
}
532523

@@ -642,9 +633,7 @@ var codeInput = {
642633
this.classList.add("code-input_mouse-focused");
643634
});
644635
textarea.addEventListener("blur", () => {
645-
if(this.handleEventsFromTextarea) {
646-
this.classList.remove("code-input_mouse-focused");
647-
}
636+
this.classList.remove("code-input_mouse-focused");
648637
});
649638

650639
this.innerHTML = ""; // Clear Content
@@ -868,22 +857,20 @@ var codeInput = {
868857
this.boundEventCallbacks[listener] = boundCallback;
869858

870859
if (codeInput.textareaSyncEvents.includes(type)) {
871-
// Synchronise with textarea, only when handleEventsFromTextarea is true
872-
// This callback is modified to only run when the handleEventsFromTextarea is set.
873-
let conditionalBoundCallback = function(evt) { if(this.handleEventsFromTextarea) boundCallback(evt); }.bind(this);
874-
this.boundEventCallbacks[listener] = conditionalBoundCallback;
860+
// Synchronise with textarea
861+
this.boundEventCallbacks[listener] = boundCallback;
875862

876863
if (options === undefined) {
877864
if(this.textareaElement == null) {
878865
this.addEventListener("code-input_load", () => { this.textareaElement.addEventListener(type, boundCallback); });
879866
} else {
880-
this.textareaElement.addEventListener(type, conditionalBoundCallback);
867+
this.textareaElement.addEventListener(type, boundCallback);
881868
}
882869
} else {
883870
if(this.textareaElement == null) {
884871
this.addEventListener("code-input_load", () => { this.textareaElement.addEventListener(type, boundCallback, options); });
885872
} else {
886-
this.textareaElement.addEventListener(type, conditionalBoundCallback, options);
873+
this.textareaElement.addEventListener(type, boundCallback, options);
887874
}
888875
}
889876
} else {
@@ -943,10 +930,12 @@ var codeInput = {
943930
if (val === null || val === undefined) {
944931
val = "";
945932
}
933+
946934
// Save in editable textarea element
947935
this.textareaElement.value = val;
948936
// Trigger highlight
949937
this.scheduleHighlight();
938+
950939
return val;
951940
}
952941

@@ -1086,4 +1075,59 @@ var codeInput = {
10861075
}
10871076
// ESM-SUPPORT-END-TEMPLATES-BLOCK-2 Do not (re)move this - it's needed for ESM generation!
10881077

1078+
{
1079+
// Templates are defined here after the codeInput variable is set, because they reference it by extending codeInput.Template.
1080+
1081+
// ESM-SUPPORT-START-TEMPLATE-prism Do not (re)move this - it's needed for ESM generation!
1082+
/**
1083+
* A template that uses Prism.js syntax highlighting (https://prismjs.com/).
1084+
*/
1085+
class Prism extends codeInput.Template { // Dependency: Prism.js (https://prismjs.com/)
1086+
/**
1087+
* Constructor to create a template that uses Prism.js syntax highlighting (https://prismjs.com/)
1088+
* @param {Object} prism Import Prism.js, then after that import pass the `Prism` object as this parameter.
1089+
* @param {codeInput.Plugin[]} plugins - An array of plugin objects to add extra features - see `codeInput.plugins`
1090+
* @returns {codeInput.Template} template object
1091+
*/
1092+
constructor(prism, plugins = []) {
1093+
super(
1094+
prism.highlightElement, // highlight
1095+
true, // preElementStyled
1096+
true, // isCode
1097+
false, // includeCodeInputInHighlightFunc
1098+
plugins
1099+
);
1100+
}
1101+
};
1102+
// ESM-SUPPORT-END-TEMPLATE-prism Do not (re)move this - it's needed for ESM generation!
1103+
codeInput.templates.Prism = Prism;
1104+
1105+
// ESM-SUPPORT-START-TEMPLATE-hljs Do not (re)move this - it's needed for ESM generation!
1106+
/**
1107+
* A template that uses highlight.js syntax highlighting (https://highlightjs.org/).
1108+
*/
1109+
class Hljs extends codeInput.Template { // Dependency: Highlight.js (https://highlightjs.org/)
1110+
/**
1111+
* Constructor to create a template that uses highlight.js syntax highlighting (https://highlightjs.org/)
1112+
* @param {Object} hljs Import highlight.js, then after that import pass the `hljs` object as this parameter.
1113+
* @param {codeInput.Plugin[]} plugins - An array of plugin objects to add extra features - see `codeInput.plugins`
1114+
* @returns {codeInput.Template} template object
1115+
*/
1116+
constructor(hljs, plugins = []) {
1117+
super(
1118+
function(codeElement) {
1119+
codeElement.removeAttribute("data-highlighted");
1120+
hljs.highlightElement(codeElement);
1121+
}, // highlight
1122+
false, // preElementStyled
1123+
true, // isCode
1124+
false, // includeCodeInputInHighlightFunc
1125+
plugins
1126+
);
1127+
}
1128+
};
1129+
// ESM-SUPPORT-END-TEMPLATE-hljs Do not (re)move this - it's needed for ESM generation!
1130+
codeInput.templates.Hljs = Hljs;
1131+
}
1132+
10891133
customElements.define("code-input", codeInput.CodeInput);

code-input.min.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)