Skip to content

Commit 9e59c08

Browse files
committed
Change CodeInput.template to templateObject for better framework compatibility (see my comment in #158)
1 parent 5ff89c3 commit 9e59c08

File tree

1 file changed

+29
-15
lines changed

1 file changed

+29
-15
lines changed

code-input.js

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ var codeInput = {
132132
if (templateName in codeInput.templateNotYetRegisteredQueue) {
133133
for (let i in codeInput.templateNotYetRegisteredQueue[templateName]) {
134134
const elem = codeInput.templateNotYetRegisteredQueue[templateName][i];
135-
elem.template = template;
135+
elem.templateObject = template;
136136
elem.connectedCallback();
137137
// Bind sets elem as first parameter of function
138138
// So innerHTML can be read
@@ -145,7 +145,7 @@ var codeInput = {
145145
if (undefined in codeInput.templateNotYetRegisteredQueue) {
146146
for (let i in codeInput.templateNotYetRegisteredQueue[undefined]) {
147147
const elem = codeInput.templateNotYetRegisteredQueue[undefined][i];
148-
elem.template = template;
148+
elem.templateObject = template;
149149
elem.connectedCallback();
150150
// Bind sets elem as first parameter of function
151151
// So innerHTML can be read
@@ -461,8 +461,8 @@ var codeInput = {
461461
* @param {Array} args - the arguments to pass into the event callback in the template after the code-input element. Normally left empty
462462
*/
463463
pluginEvt(eventName, args) {
464-
for (let i in this.template.plugins) {
465-
let plugin = this.template.plugins[i];
464+
for (let i in this.templateObject.plugins) {
465+
let plugin = this.templateObject.plugins[i];
466466
if (eventName in plugin) {
467467
if (args === undefined) {
468468
plugin[eventName](this);
@@ -517,8 +517,8 @@ var codeInput = {
517517
this.pluginEvt("beforeHighlight");
518518

519519
// Syntax Highlight
520-
if (this.template.includeCodeInputInHighlightFunc) this.template.highlight(resultElement, this);
521-
else this.template.highlight(resultElement);
520+
if (this.templateObject.includeCodeInputInHighlightFunc) this.template.highlight(resultElement, this);
521+
else this.templateObject.highlight(resultElement);
522522

523523
this.syncSize();
524524

@@ -530,7 +530,7 @@ var codeInput = {
530530
*/
531531
syncSize() {
532532
// Synchronise the size of the pre/code and textarea elements
533-
if(this.template.preElementStyled) {
533+
if(this.templateObject.preElementStyled) {
534534
this.style.backgroundColor = getComputedStyle(this.preElement).backgroundColor;
535535
this.textareaElement.style.height = getComputedStyle(this.preElement).height;
536536
this.textareaElement.style.width = getComputedStyle(this.preElement).width;
@@ -605,7 +605,7 @@ var codeInput = {
605605
if(this.textareaElement != null) return; // Already set up
606606

607607
this.classList.add("code-input_registered"); // Remove register message
608-
if (this.template.preElementStyled) this.classList.add("code-input_pre-element-styled");
608+
if (this.templateObject.preElementStyled) this.classList.add("code-input_pre-element-styled");
609609

610610
this.pluginEvt("beforeElementsAdded");
611611

@@ -687,7 +687,7 @@ var codeInput = {
687687
pre.append(code);
688688
this.append(pre);
689689

690-
if (this.template.isCode) {
690+
if (this.templateObject.isCode) {
691691
if (lang != undefined && lang != "") {
692692
code.classList.add("language-" + lang.toLowerCase());
693693
}
@@ -718,18 +718,30 @@ var codeInput = {
718718
}
719719

720720
/**
721-
* @deprecated Please use `codeInput.CodeInput.escapeHtml`
721+
* @deprecated This shouldn't have been accessed as part of the library's public interface (to enable more flexibility in backwards-compatible versions), but is still here just in case it was.
722722
*/
723723
escape_html(text) {
724724
return this.escapeHtml(text);
725725
}
726726

727727
/**
728-
* @deprecated Please use `codeInput.CodeInput.getTemplate`
728+
* @deprecated This shouldn't have been accessed as part of the library's public interface (to enable more flexibility in backwards-compatible versions), but is still here just in case it was.
729729
*/
730730
get_template() {
731731
return this.getTemplate();
732732
}
733+
/**
734+
* @deprecated This shouldn't have been accessed as part of the library's public interface (to enable more flexibility in backwards-compatible versions), but is still here just in case it was.
735+
*/
736+
get template() {
737+
return this.templateObject;
738+
}
739+
/**
740+
* @deprecated This shouldn't have been accessed as part of the library's public interface (to enable more flexibility in backwards-compatible versions), but is still here just in case it was.
741+
*/
742+
set template(value) {
743+
this.templateObject = value;
744+
}
733745

734746

735747
/* ------------------------------------
@@ -743,8 +755,10 @@ var codeInput = {
743755
* find its template and set up the element.
744756
*/
745757
connectedCallback() {
746-
this.template = this.getTemplate();
747-
if (this.template != undefined) {
758+
// Stored in templateObject because some frameworks will override
759+
// template property with the string value of the attribute
760+
this.templateObject = this.getTemplate();
761+
if (this.templateObject != undefined) {
748762
this.classList.add("code-input_registered");
749763
this.setup();
750764
this.classList.add("code-input_loaded");
@@ -793,8 +807,8 @@ var codeInput = {
793807
this.value = newValue;
794808
break;
795809
case "template":
796-
this.template = codeInput.usedTemplates[newValue || codeInput.defaultTemplate];
797-
if (this.template.preElementStyled) this.classList.add("code-input_pre-element-styled");
810+
this.templateObject = codeInput.usedTemplates[newValue || codeInput.defaultTemplate];
811+
if (this.templateObject.preElementStyled) this.classList.add("code-input_pre-element-styled");
798812
else this.classList.remove("code-input_pre-element-styled");
799813
// Syntax Highlight
800814
this.scheduleHighlight();

0 commit comments

Comments
 (0)