From 80c093ac4312943a40cdc20c102aa9211e101b63 Mon Sep 17 00:00:00 2001 From: missionfloyd Date: Mon, 10 Apr 2023 22:37:57 -0600 Subject: [PATCH] Add edit-attention from webui --- web/scripts/edit-attention.js | 93 +++++++++++++++++++++++++++++++++++ web/scripts/widgets.js | 5 ++ 2 files changed, 98 insertions(+) create mode 100644 web/scripts/edit-attention.js diff --git a/web/scripts/edit-attention.js b/web/scripts/edit-attention.js new file mode 100644 index 000000000..405ad6e5c --- /dev/null +++ b/web/scripts/edit-attention.js @@ -0,0 +1,93 @@ +export function keyupEditAttention(event){ + let target = event.originalTarget || event.composedPath()[0]; + if (! (event.metaKey || event.ctrlKey)) return; + + let isPlus = event.key == "ArrowUp" + let isMinus = event.key == "ArrowDown" + if (!isPlus && !isMinus) return; + + let selectionStart = target.selectionStart; + let selectionEnd = target.selectionEnd; + let text = target.value; + + function selectCurrentParenthesisBlock(OPEN, CLOSE){ + if (selectionStart !== selectionEnd) return false; + + // Find opening parenthesis around current cursor + const before = text.substring(0, selectionStart); + let beforeParen = before.lastIndexOf(OPEN); + if (beforeParen == -1) return false; + let beforeParenClose = before.lastIndexOf(CLOSE); + while (beforeParenClose !== -1 && beforeParenClose > beforeParen) { + beforeParen = before.lastIndexOf(OPEN, beforeParen - 1); + beforeParenClose = before.lastIndexOf(CLOSE, beforeParenClose - 1); + } + + // Find closing parenthesis around current cursor + const after = text.substring(selectionStart); + let afterParen = after.indexOf(CLOSE); + if (afterParen == -1) return false; + let afterParenOpen = after.indexOf(OPEN); + while (afterParenOpen !== -1 && afterParen > afterParenOpen) { + afterParen = after.indexOf(CLOSE, afterParen + 1); + afterParenOpen = after.indexOf(OPEN, afterParenOpen + 1); + } + if (beforeParen === -1 || afterParen === -1) return false; + + // Set the selection to the text between the parenthesis + const parenContent = text.substring(beforeParen + 1, selectionStart + afterParen); + const lastColon = parenContent.lastIndexOf(":"); + selectionStart = beforeParen + 1; + selectionEnd = selectionStart + lastColon; + target.setSelectionRange(selectionStart, selectionEnd); + return true; + } + + // If the user hasn't selected anything, let's select their current parenthesis block + if(! selectCurrentParenthesisBlock('<', '>')){ + selectCurrentParenthesisBlock('(', ')') + } + + event.preventDefault(); + + let closeCharacter = ')' + let delta = 0.1 + + if (selectionStart > 0 && text[selectionStart - 1] == '<'){ + closeCharacter = '>' + delta = opts.keyedit_precision_extra + } else if (selectionStart == 0 || text[selectionStart - 1] != "(") { + + // do not include spaces at the end + while(selectionEnd > selectionStart && text[selectionEnd-1] == ' '){ + selectionEnd -= 1; + } + if(selectionStart == selectionEnd){ + return + } + + text = text.slice(0, selectionStart) + "(" + text.slice(selectionStart, selectionEnd) + ":1.0)" + text.slice(selectionEnd); + + selectionStart += 1; + selectionEnd += 1; + } + + let end = text.slice(selectionEnd + 1).indexOf(closeCharacter) + 1; + let weight = parseFloat(text.slice(selectionEnd + 1, selectionEnd + 1 + end)); + if (isNaN(weight)) return; + + weight += isPlus ? delta : -delta; + weight = parseFloat(weight.toPrecision(12)); + if(String(weight).length == 1) weight += ".0" + + text = text.slice(0, selectionEnd + 1) + weight + text.slice(selectionEnd + 1 + end - 1); + + target.focus(); + target.value = text; + target.selectionStart = selectionStart; + target.selectionEnd = selectionEnd; + + let e = new Event("input", { bubbles: true }) + Object.defineProperty(e, "target", {value: target}) + target.dispatchEvent(e); +} \ No newline at end of file diff --git a/web/scripts/widgets.js b/web/scripts/widgets.js index d1a9c6c6e..4883208f5 100644 --- a/web/scripts/widgets.js +++ b/web/scripts/widgets.js @@ -1,3 +1,5 @@ +import { keyupEditAttention } from "./edit-attention.js"; + function getNumberDefaults(inputData, defaultStep) { let defaultVal = inputData[1]["default"]; let { min, max, step } = inputData[1]; @@ -136,6 +138,9 @@ function addMultilineWidget(node, name, opts, app) { widget.inputEl.blur(); } }); + widget.inputEl.addEventListener('keydown', (event) => { + keyupEditAttention(event); + }); widget.parent = node; document.body.appendChild(widget.inputEl);