Skip to content

Commit 4e8ae4b

Browse files
committed
Make CodeInput's interface clearer in d.ts file, and add readonly template (silent on set) to work with Vue better while being backwards compatible.
1 parent 4887984 commit 4e8ae4b

File tree

4 files changed

+59
-8
lines changed

4 files changed

+59
-8
lines changed

code-input.d.ts

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,9 @@ export namespace plugins {
285285
// ESM-SUPPORT-END-NAMESPACE-1 Do not (re)move this - it's needed for ESM generation
286286

287287
/**
288-
* Please see `codeInput.templates.prism` or `codeInput.templates.hljs`.
288+
* If you're using one of the two named highlighters, please see
289+
* `codeInput.templates.prism` or `codeInput.templates.hljs`.
290+
* Otherwise please see this class' constructor.
289291
* Templates are used in `<code-input>` elements and once registered with
290292
* `codeInput.registerTemplate` will be in charge of the highlighting
291293
* algorithm and settings for all code-inputs with a `template` attribute
@@ -397,9 +399,35 @@ export namespace templates {
397399

398400
/**
399401
* A `<code-input>` element, an instance of an `HTMLElement`, and the result
400-
* of `document.createElement("code-input")`.
402+
* of `document.createElement("code-input")`. Attributes are only set when
403+
* the element's template has been registered, and before this are null.
401404
*/
402-
export class CodeInput extends HTMLElement { }
405+
export class CodeInput extends HTMLElement {
406+
/**
407+
* When the code-input's template is registered, this contains its codeInput.Template object.
408+
*/
409+
templateObject?: readonly Template
410+
/**
411+
* Exposed child textarea element for user to input code in; in this version of code-input you shouldn't need to access
412+
* it because most textarea functionality is present on the code-input element itself.
413+
*/
414+
textareaElement?: HTMLTextAreaElement
415+
/**
416+
* Exposed child pre element where syntax-highlighted code is outputted.
417+
* Contains this.codeElement as its only child.
418+
*/
419+
preElement?: HTMLPreElement
420+
/**
421+
* Exposed child pre element's child code element where syntax-highlighted code is outputted.
422+
* Has this.preElement as its parent.
423+
*/
424+
codeElement?: HTMLElement
425+
/**
426+
* Exposed non-scrolling element designed to contain dialog boxes etc. from plugins,
427+
* that shouldn't scroll with the code-input element.
428+
*/
429+
dialogContainerElement?: HTMLElement
430+
}
403431

404432
/**
405433
* Register a template so code-input elements with a template attribute that equals the templateName will use the template.

code-input.js

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -412,13 +412,20 @@ var codeInput = {
412412
* ------------------------------------ */
413413

414414
/**
415-
* A code-input element.
415+
* A `<code-input>` element, an instance of an `HTMLElement`, and the result
416+
* of `document.createElement("code-input")`. Attributes are only set when
417+
* the element's template has been registered, and before this are null.
416418
*/
417419
CodeInput: class extends HTMLElement {
418420
constructor() {
419421
super(); // Element
420422
}
421423

424+
/**
425+
* When the code-input's template is registered, this contains its codeInput.Template object.
426+
* Should be treated as read-only by external code.
427+
*/
428+
templateObject = null;
422429
/**
423430
* Exposed child textarea element for user to input code in
424431
*/
@@ -435,8 +442,8 @@ var codeInput = {
435442
codeElement = null;
436443

437444
/**
438-
* Exposed non-scrolling element designed to contain dialog boxes etc. that shouldn't scroll
439-
* with the code-input element.
445+
* Exposed non-scrolling element designed to contain dialog boxes etc. from plugins,
446+
* that shouldn't scroll with the code-input element.
440447
*/
441448
dialogContainerElement = null;
442449

@@ -737,6 +744,22 @@ var codeInput = {
737744
return this.getTemplate();
738745
}
739746

747+
/**
748+
* @deprecated Present for backwards compatibility; use CodeInput.templateObject.
749+
*/
750+
get template() {
751+
return this.templateObject;
752+
}
753+
754+
/**
755+
* @deprecated The Vue framework may try to set the template
756+
* property to the value of the template attribute, a string.
757+
* This should not happen. Intentional use of this should
758+
* also not happen since templates are changed by changing
759+
* the template attribute to the name of one registered.
760+
*/
761+
set template(value) { }
762+
740763
/* ------------------------------------
741764
* -----------Callbacks----------------
742765
* ------------------------------------

plugins/autocomplete.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ codeInput.plugins.Autocomplete = class extends codeInput.Plugin {
3333

3434
let testPosPre = document.createElement("pre");
3535
testPosPre.setAttribute("aria-hidden", true); // Hide for screen readers
36-
if(codeInput.template.preElementStyled) {
36+
if(codeInput.templateObject.preElementStyled) {
3737
testPosPre.classList.add("code-input_autocomplete_testpos");
3838
codeInput.appendChild(testPosPre); // Styled like first pre, but first pre found to update
3939
} else {

plugins/indent.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ codeInput.plugins.Indent = class extends codeInput.Plugin {
6767
let testIndentationWidthPre = document.createElement("pre");
6868
testIndentationWidthPre.setAttribute("aria-hidden", "true"); // Hide for screen readers
6969
let testIndentationWidthSpan = document.createElement("span");
70-
if(codeInput.template.preElementStyled) {
70+
if(codeInput.templateObject.preElementStyled) {
7171
testIndentationWidthPre.appendChild(testIndentationWidthSpan);
7272
testIndentationWidthPre.classList.add("code-input_autocomplete_test-indentation-width");
7373
codeInput.appendChild(testIndentationWidthPre); // Styled like first pre, but first pre found to update

0 commit comments

Comments
 (0)