Auto select attention block for editing
This commit is contained in:
parent
d2f459c5cf
commit
7543787a0a
1 changed files with 32 additions and 3 deletions
|
@ -9,9 +9,38 @@ addEventListener('keydown', (event) => {
|
||||||
let minus = "ArrowDown"
|
let minus = "ArrowDown"
|
||||||
if (event.key != plus && event.key != minus) return;
|
if (event.key != plus && event.key != minus) return;
|
||||||
|
|
||||||
selectionStart = target.selectionStart;
|
let selectionStart = target.selectionStart;
|
||||||
selectionEnd = target.selectionEnd;
|
let selectionEnd = target.selectionEnd;
|
||||||
if(selectionStart == selectionEnd) return;
|
// 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);
|
||||||
|
}
|
||||||
|
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue