edit-attention.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. function keyupEditAttention(event) {
  2. let target = event.originalTarget || event.composedPath()[0];
  3. if (!target.matches("*:is([id*='_toprow'] [id*='_prompt'], .prompt) textarea")) return;
  4. if (!(event.metaKey || event.ctrlKey)) return;
  5. let isPlus = event.key == "ArrowUp";
  6. let isMinus = event.key == "ArrowDown";
  7. if (!isPlus && !isMinus) return;
  8. let selectionStart = target.selectionStart;
  9. let selectionEnd = target.selectionEnd;
  10. let text = target.value;
  11. function selectCurrentParenthesisBlock(OPEN, CLOSE) {
  12. if (selectionStart !== selectionEnd) return false;
  13. // Find opening parenthesis around current cursor
  14. const before = text.substring(0, selectionStart);
  15. let beforeParen = before.lastIndexOf(OPEN);
  16. if (beforeParen == -1) return false;
  17. // Find closing parenthesis around current cursor
  18. const after = text.substring(selectionStart);
  19. let afterParen = after.indexOf(CLOSE);
  20. if (afterParen == -1) return false;
  21. // Set the selection to the text between the parenthesis
  22. const parenContent = text.substring(beforeParen + 1, selectionStart + afterParen);
  23. const lastColon = parenContent.lastIndexOf(":");
  24. selectionStart = beforeParen + 1;
  25. selectionEnd = selectionStart + lastColon;
  26. target.setSelectionRange(selectionStart, selectionEnd);
  27. return true;
  28. }
  29. function selectCurrentWord() {
  30. if (selectionStart !== selectionEnd) return false;
  31. const whitespace_delimiters = {"Tab": "\t", "Carriage Return": "\r", "Line Feed": "\n"};
  32. let delimiters = opts.keyedit_delimiters;
  33. for (let i of opts.keyedit_delimiters_whitespace) {
  34. delimiters += whitespace_delimiters[i];
  35. }
  36. // seek backward to find beginning
  37. while (!delimiters.includes(text[selectionStart - 1]) && selectionStart > 0) {
  38. selectionStart--;
  39. }
  40. // seek forward to find end
  41. while (!delimiters.includes(text[selectionEnd]) && selectionEnd < text.length) {
  42. selectionEnd++;
  43. }
  44. target.setSelectionRange(selectionStart, selectionEnd);
  45. return true;
  46. }
  47. // If the user hasn't selected anything, let's select their current parenthesis block or word
  48. if (!selectCurrentParenthesisBlock('<', '>') && !selectCurrentParenthesisBlock('(', ')')) {
  49. selectCurrentWord();
  50. }
  51. event.preventDefault();
  52. var closeCharacter = ')';
  53. var delta = opts.keyedit_precision_attention;
  54. if (selectionStart > 0 && text[selectionStart - 1] == '<') {
  55. closeCharacter = '>';
  56. delta = opts.keyedit_precision_extra;
  57. } else if (selectionStart == 0 || text[selectionStart - 1] != "(") {
  58. // do not include spaces at the end
  59. while (selectionEnd > selectionStart && text[selectionEnd - 1] == ' ') {
  60. selectionEnd -= 1;
  61. }
  62. if (selectionStart == selectionEnd) {
  63. return;
  64. }
  65. text = text.slice(0, selectionStart) + "(" + text.slice(selectionStart, selectionEnd) + ":1.0)" + text.slice(selectionEnd);
  66. selectionStart += 1;
  67. selectionEnd += 1;
  68. }
  69. var end = text.slice(selectionEnd + 1).indexOf(closeCharacter) + 1;
  70. var weight = parseFloat(text.slice(selectionEnd + 1, selectionEnd + end));
  71. if (isNaN(weight)) return;
  72. weight += isPlus ? delta : -delta;
  73. weight = parseFloat(weight.toPrecision(12));
  74. if (String(weight).length == 1) weight += ".0";
  75. if (closeCharacter == ')' && weight == 1) {
  76. var endParenPos = text.substring(selectionEnd).indexOf(')');
  77. text = text.slice(0, selectionStart - 1) + text.slice(selectionStart, selectionEnd) + text.slice(selectionEnd + endParenPos + 1);
  78. selectionStart--;
  79. selectionEnd--;
  80. } else {
  81. text = text.slice(0, selectionEnd + 1) + weight + text.slice(selectionEnd + end);
  82. }
  83. target.focus();
  84. target.value = text;
  85. target.selectionStart = selectionStart;
  86. target.selectionEnd = selectionEnd;
  87. updateInput(target);
  88. }
  89. addEventListener('keydown', (event) => {
  90. keyupEditAttention(event);
  91. });