Skip to content

Commit b914908

Browse files
committed
Make textarea functions work on code-input elements (fixes #179)
1 parent 1fc5e4c commit b914908

File tree

1 file changed

+34
-8
lines changed

1 file changed

+34
-8
lines changed

code-input.js

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -732,7 +732,7 @@ var codeInput = {
732732
// The only element that could be resized is this code-input element.
733733
this.syncSize();
734734
});
735-
resizeObserver.observe(this);
735+
resizeObserver.observe(this.textareaElement);
736736

737737
this.classList.add("code-input_loaded");
738738
}
@@ -1128,14 +1128,40 @@ var codeInput = {
11281128
get wrap() { return this.getAttribute("wrap") || ""; }
11291129
set wrap(val) { this.setAttribute("wrap", val); }
11301130

1131+
/**
1132+
* Get the JavaScript method from the internal textarea
1133+
* element, throwing an error when no textarea is present.
1134+
* The method is bound to the textarea as `this`.
1135+
*
1136+
* For internal use - treat the code-input element as a
1137+
* textarea for the standard functions (e.g. document.
1138+
* querySelector("code-input").focus()).
1139+
*/
1140+
getTextareaMethod(name) {
1141+
if(this.textareaElement) {
1142+
return this.textareaElement[name].bind(this.textareaElement);
1143+
} else {
1144+
// Unregistered
1145+
const fallbackTextarea = this.querySelector("textarea[data-code-input-fallback]");
1146+
if(fallbackTextarea) {
1147+
return fallbackTextarea[name].bind(fallbackTextarea);
1148+
} else {
1149+
throw new Error("Cannot call "+name+" on an unregistered code-input element without a data-code-input-fallback textarea.");
1150+
}
1151+
}
1152+
}
11311153

1132-
blur(options={}) { return this.textareaElement.blur(options); }
1133-
checkValidity() { return this.textareaElement.checkValidity(); }
1134-
focus(options={}) { return this.textareaElement.focus(options); }
1135-
reportValidity() { return this.textareaElement.reportValidity(); }
1136-
setCustomValidity(error) { this.textareaElement.setCustomValidity(error); }
1137-
setRangeText(replacement, selectionStart=this.selectionStart, selectionEnd=this.selectionEnd, selectMode="preserve") { this.getTextareaProperty("setRangeText")(replacement, selectionStart, selectionEnd, selectMode); }
1138-
setSelectionRange(selectionStart, selectionEnd, selectionDirection="none") { this.getTextareaProperty("setSelectionRange")(selectionStart, selectionEnd, selectionDirection); }
1154+
blur(options={}) { this.getTextareaMethod("blur")(options); }
1155+
checkValidity() { return this.getTextareaMethod("checkValidity")(); }
1156+
focus(options={}) { this.getTextareaMethod("focus")(options); }
1157+
reportValidity() { return this.getTextareaMethod("reportValidity")(); }
1158+
setCustomValidity(error) { this.getTextareaMethod("setCustomValidity")(error); }
1159+
setRangeText(replacement, selectionStart=this.selectionStart, selectionEnd=this.selectionEnd, selectMode="preserve") {
1160+
this.getTextareaMethod("setRangeText")(replacement, selectionStart, selectionEnd, selectMode);
1161+
// Reflect that value updated
1162+
if(this.textareaElement) this.scheduleHighlight();
1163+
}
1164+
setSelectionRange(selectionStart, selectionEnd, selectionDirection="none") { this.getTextareaMethod("setSelectionRange")(selectionStart, selectionEnd, selectionDirection); }
11391165

11401166
/**
11411167
* Allows plugins to store data in the scope of a single element.

0 commit comments

Comments
 (0)