2023-07-08 21:15:28 +08:00
|
|
|
/* alt+left/right moves text in prompt */
|
|
|
|
|
2023-05-23 11:23:39 +08:00
|
|
|
function keyupEditOrder(event) {
|
2023-05-26 08:49:28 +08:00
|
|
|
if (!opts.keyedit_move) return;
|
2023-07-08 21:15:28 +08:00
|
|
|
|
2023-05-23 11:23:39 +08:00
|
|
|
let target = event.originalTarget || event.composedPath()[0];
|
|
|
|
if (!target.matches("*:is([id*='_toprow'] [id*='_prompt'], .prompt) textarea")) return;
|
2023-07-08 21:15:28 +08:00
|
|
|
if (!event.altKey) return;
|
2023-07-08 22:53:17 +08:00
|
|
|
event.preventDefault();
|
2023-05-23 10:46:25 +08:00
|
|
|
|
2023-05-23 11:23:39 +08:00
|
|
|
let isLeft = event.key == "ArrowLeft";
|
|
|
|
let isRight = event.key == "ArrowRight";
|
|
|
|
if (!isLeft && !isRight) return;
|
2023-05-23 10:46:25 +08:00
|
|
|
|
2023-05-23 11:23:39 +08:00
|
|
|
let selectionStart = target.selectionStart;
|
|
|
|
let selectionEnd = target.selectionEnd;
|
|
|
|
let text = target.value;
|
|
|
|
let items = text.split(",");
|
|
|
|
let indexStart = (text.slice(0, selectionStart).match(/,/g) || []).length;
|
|
|
|
let indexEnd = (text.slice(0, selectionEnd).match(/,/g) || []).length;
|
|
|
|
let range = indexEnd - indexStart + 1;
|
2023-05-23 10:46:25 +08:00
|
|
|
|
|
|
|
if (isLeft && indexStart > 0) {
|
2023-05-23 11:23:39 +08:00
|
|
|
items.splice(indexStart - 1, 0, ...items.splice(indexStart, range));
|
|
|
|
target.value = items.join();
|
|
|
|
target.selectionStart = items.slice(0, indexStart - 1).join().length + (indexStart == 1 ? 0 : 1);
|
|
|
|
target.selectionEnd = items.slice(0, indexEnd).join().length;
|
2023-05-23 10:46:25 +08:00
|
|
|
} else if (isRight && indexEnd < items.length - 1) {
|
2023-05-23 11:23:39 +08:00
|
|
|
items.splice(indexStart + 1, 0, ...items.splice(indexStart, range));
|
|
|
|
target.value = items.join();
|
|
|
|
target.selectionStart = items.slice(0, indexStart + 1).join().length + 1;
|
|
|
|
target.selectionEnd = items.slice(0, indexEnd + 2).join().length;
|
2023-05-23 10:46:25 +08:00
|
|
|
}
|
2023-05-23 11:23:39 +08:00
|
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
updateInput(target);
|
2023-05-23 10:46:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
addEventListener('keydown', (event) => {
|
|
|
|
keyupEditOrder(event);
|
|
|
|
});
|