Replace textarea.value calls
This commit is contained in:
+30
-19
@@ -6,44 +6,55 @@
|
||||
|
||||
// --- DOM helpers ---
|
||||
|
||||
// Route every edit through execCommand so the browser's native undo/redo
|
||||
// stack is preserved. Direct assignment to textarea.value would wipe it.
|
||||
function replaceRange(start, end, text) {
|
||||
textarea.focus();
|
||||
textarea.selectionStart = start;
|
||||
textarea.selectionEnd = end;
|
||||
document.execCommand('insertText', false, text);
|
||||
}
|
||||
|
||||
function wrap(before, after, placeholder) {
|
||||
var start = textarea.selectionStart;
|
||||
var end = textarea.selectionEnd;
|
||||
var selected = textarea.value.slice(start, end) || placeholder;
|
||||
var replacement = before + selected + after;
|
||||
textarea.value = textarea.value.slice(0, start) + replacement + textarea.value.slice(end);
|
||||
if (selected === placeholder) {
|
||||
var hadSelection = end > start;
|
||||
var selected = hadSelection ? textarea.value.slice(start, end) : placeholder;
|
||||
replaceRange(start, end, before + selected + after);
|
||||
if (!hadSelection) {
|
||||
textarea.selectionStart = start + before.length;
|
||||
textarea.selectionEnd = start + before.length + placeholder.length;
|
||||
} else {
|
||||
textarea.selectionStart = start + replacement.length;
|
||||
textarea.selectionEnd = start + replacement.length;
|
||||
}
|
||||
textarea.focus();
|
||||
}
|
||||
|
||||
function linePrefix(prefix) {
|
||||
var start = textarea.selectionStart;
|
||||
var lineStart = textarea.value.lastIndexOf('\n', start - 1) + 1;
|
||||
textarea.value = textarea.value.slice(0, lineStart) + prefix + textarea.value.slice(lineStart);
|
||||
replaceRange(lineStart, lineStart, prefix);
|
||||
textarea.selectionStart = textarea.selectionEnd = start + prefix.length;
|
||||
textarea.focus();
|
||||
}
|
||||
|
||||
function insertAtCursor(s) {
|
||||
var start = textarea.selectionStart;
|
||||
var end = textarea.selectionEnd;
|
||||
textarea.value = textarea.value.slice(0, start) + s + textarea.value.slice(end);
|
||||
textarea.selectionStart = textarea.selectionEnd = start + s.length;
|
||||
textarea.dispatchEvent(new Event('input'));
|
||||
textarea.focus();
|
||||
replaceRange(textarea.selectionStart, textarea.selectionEnd, s);
|
||||
}
|
||||
|
||||
function applyResult(result) {
|
||||
textarea.value = result.text;
|
||||
var oldText = textarea.value;
|
||||
var newText = result.text;
|
||||
var prefixLen = 0;
|
||||
var maxPrefix = Math.min(oldText.length, newText.length);
|
||||
while (prefixLen < maxPrefix && oldText.charCodeAt(prefixLen) === newText.charCodeAt(prefixLen)) {
|
||||
prefixLen++;
|
||||
}
|
||||
var oldEnd = oldText.length;
|
||||
var newEnd = newText.length;
|
||||
while (oldEnd > prefixLen && newEnd > prefixLen
|
||||
&& oldText.charCodeAt(oldEnd - 1) === newText.charCodeAt(newEnd - 1)) {
|
||||
oldEnd--;
|
||||
newEnd--;
|
||||
}
|
||||
replaceRange(prefixLen, oldEnd, newText.slice(prefixLen, newEnd));
|
||||
textarea.selectionStart = textarea.selectionEnd = result.cursor;
|
||||
textarea.dispatchEvent(new Event('input'));
|
||||
textarea.focus();
|
||||
}
|
||||
|
||||
function applyTableOp(fn, arg) {
|
||||
|
||||
Reference in New Issue
Block a user