Skip to content

Commit b62612a

Browse files
committed
Make code-input element only be set up when innerHTML written (fixes #176)
1 parent 1e292e9 commit b62612a

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

code-input.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515

1616
var codeInput = {
1717
/**
18-
* A list of attributes that will trigger the
19-
* `codeInput.CodeInput.attributeChangedCallback`
18+
* A list of attributes that will trigger the
19+
* `codeInput.CodeInput.attributeChangedCallback`
2020
* when modified in a code-input element. This
2121
* does not include events, which are handled in
2222
* `codeInput.CodeInput.addEventListener` and
@@ -133,9 +133,7 @@ var codeInput = {
133133
for (let i in codeInput.templateNotYetRegisteredQueue[templateName]) {
134134
const elem = codeInput.templateNotYetRegisteredQueue[templateName][i];
135135
elem.templateObject = template;
136-
elem.connectedCallback();
137-
// Bind sets elem as first parameter of function
138-
// So innerHTML can be read
136+
elem.setup();
139137
}
140138
}
141139

@@ -146,9 +144,7 @@ var codeInput = {
146144
for (let i in codeInput.templateNotYetRegisteredQueue[undefined]) {
147145
const elem = codeInput.templateNotYetRegisteredQueue[undefined][i];
148146
elem.templateObject = template;
149-
elem.connectedCallback();
150-
// Bind sets elem as first parameter of function
151-
// So innerHTML can be read
147+
elem.setup();
152148
}
153149
}
154150
}
@@ -611,6 +607,8 @@ var codeInput = {
611607
setup() {
612608
if(this.textareaElement != null) return; // Already set up
613609

610+
this.classList.add("code-input_registered");
611+
614612
this.mutationObserver = new MutationObserver(this.mutationObserverCallback.bind(this));
615613
this.mutationObserver.observe(this, {
616614
attributes: true,
@@ -728,6 +726,8 @@ var codeInput = {
728726
this.syncSize();
729727
});
730728
resizeObserver.observe(this);
729+
730+
this.classList.add("code-input_loaded");
731731
}
732732

733733
/**
@@ -775,9 +775,10 @@ var codeInput = {
775775
// template property with the string value of the attribute
776776
this.templateObject = this.getTemplate();
777777
if (this.templateObject != undefined) {
778+
// Template registered before loading
778779
this.classList.add("code-input_registered");
779-
this.setup();
780-
this.classList.add("code-input_loaded");
780+
// Children not yet present - wait until they are
781+
window.addEventListener("DOMContentLoaded", this.setup.bind(this))
781782
}
782783
}
783784

docs/_index.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ console.log("Hello, World!");</textarea></code-input>
8080
<script src="https://cdn.jsdelivr.net/gh/WebCoder49/[email protected]/plugins/indent.min.js"></script>
8181
8282
<!--Register code-input template-->
83+
<!--This can be before the code-input element is created, in which case it will defer the registration of all code-input
84+
elements until the page is loaded, or after it is created, in which case registration will occur immediately, but not inside
85+
code-input elements.-->
8386
<script>
8487
codeInput.registerTemplate("syntax-highlighted",
8588
new codeInput.templates.Prism(
@@ -137,6 +140,9 @@ console.log("Hello, World!");</textarea></code-input>
137140
<script src="https://cdn.jsdelivr.net/gh/WebCoder49/[email protected]/plugins/indent.min.js"></script>
138141
139142
<!--Register code-input template-->
143+
<!--This can be before the code-input element is created, in which case it will defer the registration of all code-input
144+
elements until the page is loaded, or after it is created, in which case registration will occur immediately, but not inside
145+
code-input elements.-->
140146
<script>
141147
codeInput.registerTemplate("syntax-highlighted",
142148
new codeInput.templates.Hljs(
@@ -182,6 +188,9 @@ console.log("Hello, World!");</textarea></code-input>
182188
<script src="https://cdn.jsdelivr.net/gh/WebCoder49/[email protected]/plugins/indent.min.js"></script>
183189
184190
<!--Register code-input template-->
191+
<!--This can be before the code-input element is created, in which case it will defer the registration of all code-input
192+
elements until the page is loaded, or after it is created, in which case registration will occur immediately, but not inside
193+
code-input elements.-->
185194
<script>
186195
codeInput.registerTemplate("syntax-highlighted",
187196
new codeInput.Template(
@@ -305,4 +314,4 @@ something like [CodeMirror](https://codemirror.net/),
305314

306315
🎉 code-input.js is collaboratively developed by many people, which is what keeps it going strong. By version 2.6.0, many have reported bugs and suggestions, and [9 people (see them on GitHub)](https://github.com/WebCoder49/code-input/graphs/contributors) have contributed code or documentation directly. If you have found a bug, would like to help with the code or documentation, or have additional suggestions, for plugins or core functionality, [please look at GitHub](https://github.com/WebCoder49/code-input/tree/main/CONTRIBUTING.md) or [get in touch via email so I can add it for you](mailto:[email protected])!
307316

308-
*We're looking into mirroring code-input.js onto Codeberg as well as GitHub for more flexibility and freedom - if you have ideas for this please get in touch!*
317+
*I'm looking into mirroring code-input.js onto Codeberg as well as GitHub for more flexibility and freedom - if you have ideas for this please get in touch!*

0 commit comments

Comments
 (0)