2022-10-06 14:44:54 -06:00
|
|
|
addEventListener('keydown', (event) => {
|
2022-10-07 17:05:47 -06:00
|
|
|
let target = event.originalTarget || event.composedPath()[0];
|
2022-10-06 14:44:54 -06:00
|
|
|
if (!target.hasAttribute("placeholder")) return;
|
|
|
|
if (!target.placeholder.toLowerCase().includes("prompt")) return;
|
|
|
|
|
|
|
|
let plus = "ArrowUp"
|
|
|
|
let minus = "ArrowDown"
|
|
|
|
if (event.key != plus && event.key != minus) return;
|
|
|
|
|
|
|
|
selectionStart = target.selectionStart;
|
|
|
|
selectionEnd = target.selectionEnd;
|
|
|
|
if(selectionStart == selectionEnd) return;
|
|
|
|
|
|
|
|
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-11 17:19:28 -06:00
|
|
|
if (isNaN(weight)) return;
|
2022-10-06 14:44:54 -06: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-09 18:37:35 -06: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-06 14:44:54 -06:00
|
|
|
});
|