2023-01-22 05:05:21 +00:00
|
|
|
function keyupEditAttention(event) {
|
2022-10-07 23:05:47 +00:00
|
|
|
let target = event.originalTarget || event.composedPath()[0];
|
2023-05-20 19:34:50 +00:00
|
|
|
if (!target.matches("*:is([id*='_toprow'] [id*='_prompt'], .prompt) textarea")) return;
|
2022-10-15 12:51:57 +00:00
|
|
|
if (!(event.metaKey || event.ctrlKey)) return;
|
2023-05-17 12:46:58 +00:00
|
|
|
|
2023-01-22 05:05:21 +00:00
|
|
|
let isPlus = event.key == "ArrowUp";
|
|
|
|
let isMinus = event.key == "ArrowDown";
|
|
|
|
if (!isPlus && !isMinus) return;
|
2023-05-17 12:46:58 +00:00
|
|
|
|
2022-10-17 23:11:44 +00:00
|
|
|
let selectionStart = target.selectionStart;
|
|
|
|
let selectionEnd = target.selectionEnd;
|
2023-01-22 05:05:21 +00:00
|
|
|
let text = target.value;
|
2023-05-17 12:46:58 +00:00
|
|
|
|
2023-01-22 05:05:21 +00:00
|
|
|
function selectCurrentParenthesisBlock(OPEN, CLOSE) {
|
|
|
|
if (selectionStart !== selectionEnd) return false;
|
2023-05-17 12:46:58 +00:00
|
|
|
|
2022-10-17 23:11:44 +00:00
|
|
|
// Find opening parenthesis around current cursor
|
2023-01-22 05:05:21 +00:00
|
|
|
const before = text.substring(0, selectionStart);
|
|
|
|
let beforeParen = before.lastIndexOf(OPEN);
|
2023-04-20 04:04:34 +00:00
|
|
|
if (beforeParen == -1) return false;
|
2023-05-17 12:46:58 +00:00
|
|
|
|
2023-10-21 06:09:32 +00:00
|
|
|
let beforeClosingParen = before.lastIndexOf(CLOSE);
|
|
|
|
if (beforeClosingParen != -1 && beforeClosingParen > beforeParen) return false;
|
|
|
|
|
2022-10-17 23:11:44 +00:00
|
|
|
// Find closing parenthesis around current cursor
|
2023-01-22 05:05:21 +00:00
|
|
|
const after = text.substring(selectionStart);
|
|
|
|
let afterParen = after.indexOf(CLOSE);
|
2023-04-20 04:04:34 +00:00
|
|
|
if (afterParen == -1) return false;
|
2023-05-17 12:46:58 +00:00
|
|
|
|
2023-10-21 06:09:32 +00:00
|
|
|
let afterOpeningParen = after.indexOf(OPEN);
|
2023-11-03 06:59:19 +00:00
|
|
|
if (afterOpeningParen != -1 && afterOpeningParen < afterParen) return false;
|
2023-10-21 06:09:32 +00:00
|
|
|
|
2022-10-17 23:11:44 +00:00
|
|
|
// Set the selection to the text between the parenthesis
|
2023-01-22 05:05:21 +00:00
|
|
|
const parenContent = text.substring(beforeParen + 1, selectionStart + afterParen);
|
2023-10-21 05:45:51 +00:00
|
|
|
if (/.*:-?[\d.]+/s.test(parenContent)) {
|
|
|
|
const lastColon = parenContent.lastIndexOf(":");
|
|
|
|
selectionStart = beforeParen + 1;
|
|
|
|
selectionEnd = selectionStart + lastColon;
|
|
|
|
} else {
|
|
|
|
selectionStart = beforeParen + 1;
|
|
|
|
selectionEnd = selectionStart + parenContent.length;
|
|
|
|
}
|
|
|
|
|
2022-10-17 23:11:44 +00:00
|
|
|
target.setSelectionRange(selectionStart, selectionEnd);
|
2023-01-22 05:05:21 +00:00
|
|
|
return true;
|
|
|
|
}
|
2023-05-17 12:46:58 +00:00
|
|
|
|
2023-04-20 04:04:34 +00:00
|
|
|
function selectCurrentWord() {
|
|
|
|
if (selectionStart !== selectionEnd) return false;
|
2023-10-01 00:37:44 +00:00
|
|
|
const whitespace_delimiters = {"Tab": "\t", "Carriage Return": "\r", "Line Feed": "\n"};
|
|
|
|
let delimiters = opts.keyedit_delimiters;
|
|
|
|
|
|
|
|
for (let i of opts.keyedit_delimiters_whitespace) {
|
|
|
|
delimiters += whitespace_delimiters[i];
|
|
|
|
}
|
2023-05-17 12:46:58 +00:00
|
|
|
|
2023-10-01 03:20:58 +00:00
|
|
|
// seek backward to find beginning
|
2023-04-20 07:34:13 +00:00
|
|
|
while (!delimiters.includes(text[selectionStart - 1]) && selectionStart > 0) {
|
|
|
|
selectionStart--;
|
2023-04-20 04:04:34 +00:00
|
|
|
}
|
2023-05-17 12:46:58 +00:00
|
|
|
|
2023-04-20 07:34:13 +00:00
|
|
|
// seek forward to find end
|
|
|
|
while (!delimiters.includes(text[selectionEnd]) && selectionEnd < text.length) {
|
|
|
|
selectionEnd++;
|
2023-04-20 04:04:34 +00:00
|
|
|
}
|
2023-05-17 12:46:58 +00:00
|
|
|
|
2023-04-20 04:04:34 +00:00
|
|
|
target.setSelectionRange(selectionStart, selectionEnd);
|
|
|
|
return true;
|
|
|
|
}
|
2023-05-17 12:46:58 +00:00
|
|
|
|
2023-04-20 04:04:34 +00:00
|
|
|
// If the user hasn't selected anything, let's select their current parenthesis block or word
|
2023-10-21 05:45:51 +00:00
|
|
|
if (!selectCurrentParenthesisBlock('<', '>') && !selectCurrentParenthesisBlock('(', ')') && !selectCurrentParenthesisBlock('[', ']')) {
|
2023-04-20 04:04:34 +00:00
|
|
|
selectCurrentWord();
|
2023-01-22 05:05:21 +00:00
|
|
|
}
|
2023-05-17 12:46:58 +00:00
|
|
|
|
2022-10-06 20:44:54 +00:00
|
|
|
event.preventDefault();
|
2023-05-17 12:46:58 +00:00
|
|
|
|
2023-04-30 19:08:52 +00:00
|
|
|
var closeCharacter = ')';
|
|
|
|
var delta = opts.keyedit_precision_attention;
|
2023-10-21 05:45:51 +00:00
|
|
|
var start = selectionStart > 0 ? text[selectionStart - 1] : "";
|
|
|
|
var end = text[selectionEnd];
|
2023-05-17 12:46:58 +00:00
|
|
|
|
2023-10-21 05:45:51 +00:00
|
|
|
if (start == '<') {
|
2023-01-22 05:05:21 +00:00
|
|
|
closeCharacter = '>';
|
|
|
|
delta = opts.keyedit_precision_extra;
|
2023-10-21 05:45:51 +00:00
|
|
|
} else if (start == '(' && end == ')' || start == '[' && end == ']') { // convert old-style (((emphasis)))
|
|
|
|
let numParen = 0;
|
|
|
|
|
|
|
|
while (text[selectionStart - numParen - 1] == start && text[selectionEnd + numParen] == end) {
|
|
|
|
numParen++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (start == "[") {
|
|
|
|
weight = (1 / 1.1) ** numParen;
|
2023-10-07 04:49:33 +00:00
|
|
|
} else {
|
2023-10-21 05:45:51 +00:00
|
|
|
weight = 1.1 ** numParen;
|
2023-10-07 04:49:33 +00:00
|
|
|
}
|
2023-10-21 05:45:51 +00:00
|
|
|
|
|
|
|
weight = Math.round(weight / opts.keyedit_precision_attention) * opts.keyedit_precision_attention;
|
|
|
|
|
|
|
|
text = text.slice(0, selectionStart - numParen) + "(" + text.slice(selectionStart, selectionEnd) + ":" + weight + ")" + text.slice(selectionEnd + numParen);
|
|
|
|
selectionStart -= numParen - 1;
|
|
|
|
selectionEnd -= numParen - 1;
|
|
|
|
} else if (start != '(') {
|
2023-01-22 05:05:21 +00:00
|
|
|
// do not include spaces at the end
|
|
|
|
while (selectionEnd > selectionStart && text[selectionEnd - 1] == ' ') {
|
2023-10-07 04:49:33 +00:00
|
|
|
selectionEnd--;
|
2023-01-22 05:05:21 +00:00
|
|
|
}
|
2023-10-07 04:49:33 +00:00
|
|
|
|
2023-01-22 05:05:21 +00:00
|
|
|
if (selectionStart == selectionEnd) {
|
|
|
|
return;
|
|
|
|
}
|
2023-05-17 12:46:58 +00:00
|
|
|
|
2023-01-22 05:05:21 +00:00
|
|
|
text = text.slice(0, selectionStart) + "(" + text.slice(selectionStart, selectionEnd) + ":1.0)" + text.slice(selectionEnd);
|
2023-05-17 12:46:58 +00:00
|
|
|
|
2023-10-07 04:49:33 +00:00
|
|
|
selectionStart++;
|
|
|
|
selectionEnd++;
|
2023-01-22 05:05:21 +00:00
|
|
|
}
|
2023-05-17 12:46:58 +00:00
|
|
|
|
2023-10-21 05:45:51 +00:00
|
|
|
if (text[selectionEnd] != ':') return;
|
|
|
|
var weightLength = text.slice(selectionEnd + 1).indexOf(closeCharacter) + 1;
|
|
|
|
var weight = parseFloat(text.slice(selectionEnd + 1, selectionEnd + weightLength));
|
|
|
|
if (isNaN(weight)) return;
|
|
|
|
|
|
|
|
weight += isPlus ? delta : -delta;
|
|
|
|
weight = parseFloat(weight.toPrecision(12));
|
|
|
|
if (Number.isInteger(weight)) weight += ".0";
|
|
|
|
|
|
|
|
if (closeCharacter == ')' && weight == 1) {
|
|
|
|
var endParenPos = text.substring(selectionEnd).indexOf(')');
|
|
|
|
text = text.slice(0, selectionStart - 1) + text.slice(selectionStart, selectionEnd) + text.slice(selectionEnd + endParenPos + 1);
|
|
|
|
selectionStart--;
|
|
|
|
selectionEnd--;
|
|
|
|
} else {
|
|
|
|
text = text.slice(0, selectionEnd + 1) + weight + text.slice(selectionEnd + weightLength);
|
2023-04-21 07:37:29 +00:00
|
|
|
}
|
2023-05-17 12:46:58 +00:00
|
|
|
|
2023-01-22 05:05:21 +00:00
|
|
|
target.focus();
|
|
|
|
target.value = text;
|
|
|
|
target.selectionStart = selectionStart;
|
|
|
|
target.selectionEnd = selectionEnd;
|
2023-05-17 12:46:58 +00:00
|
|
|
|
2023-01-17 11:15:47 +00:00
|
|
|
updateInput(target);
|
2023-01-22 05:05:21 +00:00
|
|
|
}
|
2023-05-17 12:46:58 +00:00
|
|
|
|
2023-01-22 05:05:21 +00:00
|
|
|
addEventListener('keydown', (event) => {
|
|
|
|
keyupEditAttention(event);
|
2023-04-21 07:37:29 +00:00
|
|
|
});
|