2023-01-21 22:05:21 -07:00
|
|
|
function keyupEditAttention(event) {
|
2022-10-07 17:05:47 -06:00
|
|
|
let target = event.originalTarget || event.composedPath()[0];
|
2023-05-20 13:34:50 -06:00
|
|
|
if (!target.matches("*:is([id*='_toprow'] [id*='_prompt'], .prompt) textarea")) return;
|
2022-10-15 06:51:57 -06:00
|
|
|
if (!(event.metaKey || event.ctrlKey)) return;
|
2023-05-17 06:46:58 -06:00
|
|
|
|
2023-01-21 22:05:21 -07:00
|
|
|
let isPlus = event.key == "ArrowUp";
|
|
|
|
let isMinus = event.key == "ArrowDown";
|
|
|
|
if (!isPlus && !isMinus) return;
|
2023-05-17 06:46:58 -06:00
|
|
|
|
2022-10-17 17:11:44 -06:00
|
|
|
let selectionStart = target.selectionStart;
|
|
|
|
let selectionEnd = target.selectionEnd;
|
2023-01-21 22:05:21 -07:00
|
|
|
let text = target.value;
|
2023-05-17 06:46:58 -06:00
|
|
|
|
2023-01-21 22:05:21 -07:00
|
|
|
function selectCurrentParenthesisBlock(OPEN, CLOSE) {
|
|
|
|
if (selectionStart !== selectionEnd) return false;
|
2023-05-17 06:46:58 -06:00
|
|
|
|
2022-10-17 17:11:44 -06:00
|
|
|
// Find opening parenthesis around current cursor
|
2023-01-21 22:05:21 -07:00
|
|
|
const before = text.substring(0, selectionStart);
|
|
|
|
let beforeParen = before.lastIndexOf(OPEN);
|
2023-04-19 22:04:34 -06:00
|
|
|
if (beforeParen == -1) return false;
|
2023-01-21 22:05:21 -07:00
|
|
|
let beforeParenClose = before.lastIndexOf(CLOSE);
|
2022-10-17 17:11:44 -06:00
|
|
|
while (beforeParenClose !== -1 && beforeParenClose > beforeParen) {
|
2023-01-21 22:05:21 -07:00
|
|
|
beforeParen = before.lastIndexOf(OPEN, beforeParen - 1);
|
|
|
|
beforeParenClose = before.lastIndexOf(CLOSE, beforeParenClose - 1);
|
2022-10-17 17:11:44 -06:00
|
|
|
}
|
2023-05-17 06:46:58 -06:00
|
|
|
|
2022-10-17 17:11:44 -06:00
|
|
|
// Find closing parenthesis around current cursor
|
2023-01-21 22:05:21 -07:00
|
|
|
const after = text.substring(selectionStart);
|
|
|
|
let afterParen = after.indexOf(CLOSE);
|
2023-04-19 22:04:34 -06:00
|
|
|
if (afterParen == -1) return false;
|
2023-01-21 22:05:21 -07:00
|
|
|
let afterParenOpen = after.indexOf(OPEN);
|
2022-10-17 17:11:44 -06:00
|
|
|
while (afterParenOpen !== -1 && afterParen > afterParenOpen) {
|
2023-01-21 22:05:21 -07:00
|
|
|
afterParen = after.indexOf(CLOSE, afterParen + 1);
|
|
|
|
afterParenOpen = after.indexOf(OPEN, afterParenOpen + 1);
|
2022-10-17 17:11:44 -06:00
|
|
|
}
|
2023-01-21 22:05:21 -07:00
|
|
|
if (beforeParen === -1 || afterParen === -1) return false;
|
2023-05-17 06:46:58 -06:00
|
|
|
|
2022-10-17 17:11:44 -06:00
|
|
|
// Set the selection to the text between the parenthesis
|
2023-01-21 22:05:21 -07:00
|
|
|
const parenContent = text.substring(beforeParen + 1, selectionStart + afterParen);
|
2022-10-17 17:11:44 -06:00
|
|
|
const lastColon = parenContent.lastIndexOf(":");
|
|
|
|
selectionStart = beforeParen + 1;
|
|
|
|
selectionEnd = selectionStart + lastColon;
|
|
|
|
target.setSelectionRange(selectionStart, selectionEnd);
|
2023-01-21 22:05:21 -07:00
|
|
|
return true;
|
|
|
|
}
|
2023-05-17 06:46:58 -06:00
|
|
|
|
2023-04-19 22:04:34 -06:00
|
|
|
function selectCurrentWord() {
|
|
|
|
if (selectionStart !== selectionEnd) return false;
|
2023-04-21 20:19:58 -06:00
|
|
|
const delimiters = opts.keyedit_delimiters + " \r\n\t";
|
2023-05-17 06:46:58 -06:00
|
|
|
|
2023-04-20 01:34:13 -06:00
|
|
|
// seek backward until to find beggining
|
|
|
|
while (!delimiters.includes(text[selectionStart - 1]) && selectionStart > 0) {
|
|
|
|
selectionStart--;
|
2023-04-19 22:04:34 -06:00
|
|
|
}
|
2023-05-17 06:46:58 -06:00
|
|
|
|
2023-04-20 01:34:13 -06:00
|
|
|
// seek forward to find end
|
|
|
|
while (!delimiters.includes(text[selectionEnd]) && selectionEnd < text.length) {
|
|
|
|
selectionEnd++;
|
2023-04-19 22:04:34 -06:00
|
|
|
}
|
2023-05-17 06:46:58 -06:00
|
|
|
|
2023-04-19 22:04:34 -06:00
|
|
|
target.setSelectionRange(selectionStart, selectionEnd);
|
|
|
|
return true;
|
|
|
|
}
|
2023-05-17 06:46:58 -06:00
|
|
|
|
2023-04-19 22:04:34 -06:00
|
|
|
// If the user hasn't selected anything, let's select their current parenthesis block or word
|
|
|
|
if (!selectCurrentParenthesisBlock('<', '>') && !selectCurrentParenthesisBlock('(', ')')) {
|
|
|
|
selectCurrentWord();
|
2023-01-21 22:05:21 -07:00
|
|
|
}
|
2023-05-17 06:46:58 -06:00
|
|
|
|
2022-10-06 14:44:54 -06:00
|
|
|
event.preventDefault();
|
2023-05-17 06:46:58 -06:00
|
|
|
|
2023-04-30 13:08:52 -06:00
|
|
|
var closeCharacter = ')';
|
|
|
|
var delta = opts.keyedit_precision_attention;
|
2023-05-17 06:46:58 -06:00
|
|
|
|
2023-01-21 22:05:21 -07:00
|
|
|
if (selectionStart > 0 && text[selectionStart - 1] == '<') {
|
|
|
|
closeCharacter = '>';
|
|
|
|
delta = opts.keyedit_precision_extra;
|
|
|
|
} else if (selectionStart == 0 || text[selectionStart - 1] != "(") {
|
2023-05-17 06:46:58 -06:00
|
|
|
|
2023-01-21 22:05:21 -07:00
|
|
|
// do not include spaces at the end
|
|
|
|
while (selectionEnd > selectionStart && text[selectionEnd - 1] == ' ') {
|
|
|
|
selectionEnd -= 1;
|
|
|
|
}
|
|
|
|
if (selectionStart == selectionEnd) {
|
|
|
|
return;
|
|
|
|
}
|
2023-05-17 06:46:58 -06:00
|
|
|
|
2023-01-21 22:05:21 -07:00
|
|
|
text = text.slice(0, selectionStart) + "(" + text.slice(selectionStart, selectionEnd) + ":1.0)" + text.slice(selectionEnd);
|
2023-05-17 06:46:58 -06:00
|
|
|
|
2023-01-21 22:05:21 -07:00
|
|
|
selectionStart += 1;
|
|
|
|
selectionEnd += 1;
|
|
|
|
}
|
2023-05-17 06:46:58 -06:00
|
|
|
|
2023-04-30 13:08:52 -06:00
|
|
|
var end = text.slice(selectionEnd + 1).indexOf(closeCharacter) + 1;
|
|
|
|
var weight = parseFloat(text.slice(selectionEnd + 1, selectionEnd + 1 + end));
|
2023-01-21 22:05:21 -07:00
|
|
|
if (isNaN(weight)) return;
|
2023-05-17 06:46:58 -06:00
|
|
|
|
2023-01-21 22:05:21 -07:00
|
|
|
weight += isPlus ? delta : -delta;
|
|
|
|
weight = parseFloat(weight.toPrecision(12));
|
|
|
|
if (String(weight).length == 1) weight += ".0";
|
2023-05-17 06:46:58 -06:00
|
|
|
|
2023-04-21 01:37:29 -06:00
|
|
|
if (closeCharacter == ')' && weight == 1) {
|
2023-06-28 17:51:27 -06:00
|
|
|
var endParenPos = text.substring(selectionEnd).indexOf(')');
|
|
|
|
text = text.slice(0, selectionStart - 1) + text.slice(selectionStart, selectionEnd) + text.slice(selectionEnd + endParenPos + 1);
|
2023-04-21 01:37:29 -06:00
|
|
|
selectionStart--;
|
|
|
|
selectionEnd--;
|
|
|
|
} else {
|
2023-06-28 17:51:27 -06:00
|
|
|
text = text.slice(0, selectionEnd + 1) + weight + text.slice(selectionEnd + end);
|
2023-04-21 01:37:29 -06:00
|
|
|
}
|
2023-05-17 06:46:58 -06:00
|
|
|
|
2023-01-21 22:05:21 -07:00
|
|
|
target.focus();
|
|
|
|
target.value = text;
|
|
|
|
target.selectionStart = selectionStart;
|
|
|
|
target.selectionEnd = selectionEnd;
|
2023-05-17 06:46:58 -06:00
|
|
|
|
2023-01-17 04:15:47 -07:00
|
|
|
updateInput(target);
|
2023-01-21 22:05:21 -07:00
|
|
|
}
|
2023-05-17 06:46:58 -06:00
|
|
|
|
2023-01-21 22:05:21 -07:00
|
|
|
addEventListener('keydown', (event) => {
|
|
|
|
keyupEditAttention(event);
|
2023-04-21 01:37:29 -06:00
|
|
|
});
|