edit-attention.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. function keyupEditAttention(event) {
  2. let target = event.originalTarget || event.composedPath()[0];
  3. if (!target.matches("[id*='_toprow'] [id*='_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. let beforeParenClose = before.lastIndexOf(CLOSE);
  18. while (beforeParenClose !== -1 && beforeParenClose > beforeParen) {
  19. beforeParen = before.lastIndexOf(OPEN, beforeParen - 1);
  20. beforeParenClose = before.lastIndexOf(CLOSE, beforeParenClose - 1);
  21. }
  22. // Find closing parenthesis around current cursor
  23. const after = text.substring(selectionStart);
  24. let afterParen = after.indexOf(CLOSE);
  25. if (afterParen == -1) return false;
  26. let afterParenOpen = after.indexOf(OPEN);
  27. while (afterParenOpen !== -1 && afterParen > afterParenOpen) {
  28. afterParen = after.indexOf(CLOSE, afterParen + 1);
  29. afterParenOpen = after.indexOf(OPEN, afterParenOpen + 1);
  30. }
  31. if (beforeParen === -1 || afterParen === -1) return false;
  32. // Set the selection to the text between the parenthesis
  33. const parenContent = text.substring(beforeParen + 1, selectionStart + afterParen);
  34. const lastColon = parenContent.lastIndexOf(":");
  35. selectionStart = beforeParen + 1;
  36. selectionEnd = selectionStart + lastColon;
  37. target.setSelectionRange(selectionStart, selectionEnd);
  38. return true;
  39. }
  40. function selectCurrentWord() {
  41. if (selectionStart !== selectionEnd) return false;
  42. const delimiters = opts.keyedit_delimiters + " \r\n\t";
  43. // seek backward until to find beggining
  44. while (!delimiters.includes(text[selectionStart - 1]) && selectionStart > 0) {
  45. selectionStart--;
  46. }
  47. // seek forward to find end
  48. while (!delimiters.includes(text[selectionEnd]) && selectionEnd < text.length) {
  49. selectionEnd++;
  50. }
  51. target.setSelectionRange(selectionStart, selectionEnd);
  52. return true;
  53. }
  54. // If the user hasn't selected anything, let's select their current parenthesis block or word
  55. if (!selectCurrentParenthesisBlock('<', '>') && !selectCurrentParenthesisBlock('(', ')')) {
  56. selectCurrentWord();
  57. }
  58. event.preventDefault();
  59. var closeCharacter = ')';
  60. var delta = opts.keyedit_precision_attention;
  61. if (selectionStart > 0 && text[selectionStart - 1] == '<') {
  62. closeCharacter = '>';
  63. delta = opts.keyedit_precision_extra;
  64. } else if (selectionStart == 0 || text[selectionStart - 1] != "(") {
  65. // do not include spaces at the end
  66. while (selectionEnd > selectionStart && text[selectionEnd - 1] == ' ') {
  67. selectionEnd -= 1;
  68. }
  69. if (selectionStart == selectionEnd) {
  70. return;
  71. }
  72. text = text.slice(0, selectionStart) + "(" + text.slice(selectionStart, selectionEnd) + ":1.0)" + text.slice(selectionEnd);
  73. selectionStart += 1;
  74. selectionEnd += 1;
  75. }
  76. var end = text.slice(selectionEnd + 1).indexOf(closeCharacter) + 1;
  77. var weight = parseFloat(text.slice(selectionEnd + 1, selectionEnd + 1 + end));
  78. if (isNaN(weight)) return;
  79. weight += isPlus ? delta : -delta;
  80. weight = parseFloat(weight.toPrecision(12));
  81. if (String(weight).length == 1) weight += ".0";
  82. if (closeCharacter == ')' && weight == 1) {
  83. text = text.slice(0, selectionStart - 1) + text.slice(selectionStart, selectionEnd) + text.slice(selectionEnd + 5);
  84. selectionStart--;
  85. selectionEnd--;
  86. } else {
  87. text = text.slice(0, selectionEnd + 1) + weight + text.slice(selectionEnd + 1 + end - 1);
  88. }
  89. target.focus();
  90. target.value = text;
  91. target.selectionStart = selectionStart;
  92. target.selectionEnd = selectionEnd;
  93. updateInput(target);
  94. }
  95. addEventListener('keydown', (event) => {
  96. keyupEditAttention(event);
  97. });