Skip to content

Commit ac7828c

Browse files
author
WebCoder49
committed
Fix registering bug
1 parent 8931355 commit ac7828c

File tree

2 files changed

+27
-12
lines changed

2 files changed

+27
-12
lines changed

code-input.css

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,17 @@ code-input {
1313
overflow: hidden;
1414

1515
/* Normal inline styles */
16-
padding: 8px;
17-
margin: 0!important;
18-
width: calc(100% - 16px);
16+
margin: 8px;
17+
--padding: 16px;
1918
height: 250px;
19+
2020
font-size: normal;
2121
font-family: monospace;
2222
line-height: 1.5; /* Inherited to child elements */
2323
tab-size: 2;
2424
caret-color: darkgrey;
2525
white-space: pre;
26+
padding: 0!important; /* Use --padding */
2627
}
2728

2829
code-input textarea, code-input:not(.code-input_pre-element-styled) pre code, code-input.code-input_pre-element-styled pre {
@@ -106,7 +107,7 @@ code-input textarea {
106107
outline: none!important;
107108
}
108109

109-
code-input:not(.code-input_registered)::before {
110+
code-input:not(.code-input_registered)::after {
110111
/* Display message to register */
111112
content: "Use codeInput.registerTemplate to set up.";
112113
display: block;

code-input.js

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -100,25 +100,31 @@ var codeInput = {
100100
throw TypeError(`code-input: Template for "${templateName}" invalid, because the plugin provided at index ${i} is not valid; it is "${template.plugins[i]}". Please make sure you use one of the constructors in codeInput.templates, and that you provide the correct arguments.`);
101101
}
102102
});
103+
103104

104105
codeInput.usedTemplates[templateName] = template;
105106
// Add waiting code-input elements wanting this template from queue
106107
if (templateName in codeInput.templateNotYetRegisteredQueue) {
107108
for (let i in codeInput.templateNotYetRegisteredQueue[templateName]) {
108109
elem = codeInput.templateNotYetRegisteredQueue[templateName][i];
109110
elem.template = template;
110-
codeInput.runOnceWindowLoaded(() => { elem.setup(); }, elem); // So innerHTML can be read
111+
codeInput.runOnceWindowLoaded((function(elem) { elem.connectedCallback(); }).bind(null, elem), elem);
112+
// Bind sets elem in parameter
113+
// So innerHTML can be read
111114
}
112115
console.log(`code-input: template: Added existing elements with template ${templateName}`);
113116
}
117+
114118
if (codeInput.defaultTemplate == undefined) {
115119
codeInput.defaultTemplate = templateName;
116120
// Add elements with default template from queue
117121
if (undefined in codeInput.templateNotYetRegisteredQueue) {
118122
for (let i in codeInput.templateNotYetRegisteredQueue[undefined]) {
119123
elem = codeInput.templateNotYetRegisteredQueue[undefined][i];
120124
elem.template = template;
121-
codeInput.runOnceWindowLoaded(() => { elem.setup(); }, elem); // So innerHTML can be read
125+
codeInput.runOnceWindowLoaded((function(elem) { elem.connectedCallback(); }).bind(null, elem), elem);
126+
// Bind sets elem in parameter
127+
// So innerHTML can be read
122128
}
123129
}
124130
console.log(`code-input: template: Set template ${templateName} as default`);
@@ -471,16 +477,16 @@ var codeInput = {
471477
* @param {boolean} originalUpdate - Whether this update originates from the textarea's content; if so, run it first so custom updates override it.
472478
*/
473479
update(value) {
474-
if(this.textareaElement == null) {
475-
this.addEventListener("code-input_load", () => { this.update(value) }); // Only run when fully loaded
476-
return;
477-
}
478-
479480
// Prevent this from running multiple times on the same input when "value" attribute is changed,
480481
// by not running when value is already equal to the input of this (implying update has already
481482
// been run). Thank you to peterprvy for this.
482483
if (this.ignoreValueUpdate) return;
483484

485+
if(this.textareaElement == null) {
486+
this.addEventListener("code-input_load", () => { this.update(value) }); // Only run when fully loaded
487+
return;
488+
}
489+
484490
this.ignoreValueUpdate = true;
485491
this.value = value;
486492
this.ignoreValueUpdate = false;
@@ -563,6 +569,8 @@ var codeInput = {
563569
* This will be called once the template has been added.
564570
*/
565571
setup() {
572+
if(this.textareaElement != null) return; // Already set up
573+
566574
this.classList.add("code-input_registered"); // Remove register message
567575
if (this.template.preElementStyled) this.classList.add("code-input_pre-element-styled");
568576

@@ -996,13 +1004,19 @@ var codeInput = {
9961004
* has loaded (or now if it has already loaded)
9971005
*/
9981006
runOnceWindowLoaded(callback, codeInputElem) {
999-
if(codeInputElem.textareaElement != null) {
1007+
if(codeInput.windowLoaded) {
10001008
callback(); // Fully loaded
10011009
} else {
10021010
window.addEventListener("load", callback);
10031011
}
10041012
},
1013+
windowLoaded: false
10051014
}
1015+
window.addEventListener("load", function() {
1016+
codeInput.windowLoaded = true;
1017+
});
1018+
1019+
10061020
/**
10071021
* convert wildcards into regex
10081022
*/

0 commit comments

Comments
 (0)