2022-10-07 04:44:54 +08:00
|
|
|
addEventListener('keydown', (event) => {
|
2022-10-08 07:05:47 +08:00
|
|
|
let target = event.originalTarget || event.composedPath()[0];
|
2022-10-07 04:44:54 +08:00
|
|
|
if (!target.hasAttribute("placeholder")) return;
|
|
|
|
if (!target.placeholder.toLowerCase().includes("prompt")) return;
|
2022-10-15 20:51:57 +08:00
|
|
|
if (! (event.metaKey || event.ctrlKey)) return;
|
|
|
|
|
2022-10-07 04:44:54 +08:00
|
|
|
|
|
|
|
let plus = "ArrowUp"
|
|
|
|
let minus = "ArrowDown"
|
|
|
|
if (event.key != plus && event.key != minus) return;
|
|
|
|
|
2022-10-18 07:11:44 +08:00
|
|
|
let selectionStart = target.selectionStart;
|
|
|
|
let selectionEnd = target.selectionEnd;
|
|
|
|
// If the user hasn't selected anything, let's select their current parenthesis block
|
|
|
|
if (selectionStart === selectionEnd) {
|
|
|
|
// Find opening parenthesis around current cursor
|
|
|
|
const before = target.value.substring(0, selectionStart);
|
|
|
|
let beforeParen = before.lastIndexOf("(");
|
|
|
|
if (beforeParen == -1) return;
|
|
|
|
let beforeParenClose = before.lastIndexOf(")");
|
|
|
|
while (beforeParenClose !== -1 && beforeParenClose > beforeParen) {
|
|
|
|
beforeParen = before.lastIndexOf("(", beforeParen - 1);
|
|
|
|
beforeParenClose = before.lastIndexOf(")", beforeParenClose - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find closing parenthesis around current cursor
|
|
|
|
const after = target.value.substring(selectionStart);
|
|
|
|
let afterParen = after.indexOf(")");
|
|
|
|
if (afterParen == -1) return;
|
|
|
|
let afterParenOpen = after.indexOf("(");
|
|
|
|
while (afterParenOpen !== -1 && afterParen > afterParenOpen) {
|
|
|
|
afterParen = after.indexOf(")", afterParen + 1);
|
|
|
|
afterParenOpen = after.indexOf("(", afterParenOpen + 1);
|
|
|
|
}
|
|
|
|
if (beforeParen === -1 || afterParen === -1) return;
|
|
|
|
|
|
|
|
// Set the selection to the text between the parenthesis
|
|
|
|
const parenContent = target.value.substring(beforeParen + 1, selectionStart + afterParen);
|
|
|
|
const lastColon = parenContent.lastIndexOf(":");
|
|
|
|
selectionStart = beforeParen + 1;
|
|
|
|
selectionEnd = selectionStart + lastColon;
|
|
|
|
target.setSelectionRange(selectionStart, selectionEnd);
|
|
|
|
}
|
2022-10-07 04:44:54 +08:00
|
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
if (selectionStart == 0 || target.value[selectionStart - 1] != "(") {
|
|
|
|
target.value = target.value.slice(0, selectionStart) +
|
|
|
|
"(" + target.value.slice(selectionStart, selectionEnd) + ":1.0)" +
|
|
|
|
target.value.slice(selectionEnd);
|
|
|
|
|
|
|
|
target.focus();
|
|
|
|
target.selectionStart = selectionStart + 1;
|
|
|
|
target.selectionEnd = selectionEnd + 1;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
end = target.value.slice(selectionEnd + 1).indexOf(")") + 1;
|
|
|
|
weight = parseFloat(target.value.slice(selectionEnd + 1, selectionEnd + 1 + end));
|
2022-10-12 07:19:28 +08:00
|
|
|
if (isNaN(weight)) return;
|
2022-10-07 04:44:54 +08:00
|
|
|
if (event.key == minus) weight -= 0.1;
|
|
|
|
if (event.key == plus) weight += 0.1;
|
|
|
|
|
|
|
|
weight = parseFloat(weight.toPrecision(12));
|
|
|
|
|
|
|
|
target.value = target.value.slice(0, selectionEnd + 1) +
|
|
|
|
weight +
|
|
|
|
target.value.slice(selectionEnd + 1 + end - 1);
|
|
|
|
|
|
|
|
target.focus();
|
|
|
|
target.selectionStart = selectionStart;
|
|
|
|
target.selectionEnd = selectionEnd;
|
|
|
|
}
|
2022-10-10 08:37:35 +08:00
|
|
|
// Since we've modified a Gradio Textbox component manually, we need to simulate an `input` DOM event to ensure its
|
|
|
|
// internal Svelte data binding remains in sync.
|
|
|
|
target.dispatchEvent(new Event("input", { bubbles: true }));
|
2022-10-07 04:44:54 +08:00
|
|
|
});
|