From ff243c1d110b646329fa0b0d8d4f1a52f320d03b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=99emysl=20Eric=20Janouch?=
Date: Sun, 18 Sep 2022 01:46:00 +0200 Subject: [PATCH] xP: implement Readline's M-l, M-u, M-c --- xP/public/xP.js | 47 +++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 43 insertions(+), 4 deletions(-) diff --git a/xP/public/xP.js b/xP/public/xP.js index aa2c39a..1cdb8b0 100644 --- a/xP/public/xP.js +++ b/xP/public/xP.js @@ -742,7 +742,7 @@ let Input = { return true }, - backward: (b, textarea) => { + backward: textarea => { if (textarea.selectionStart !== textarea.selectionEnd) return false @@ -756,7 +756,7 @@ let Input = { return true }, - forward: (b, textarea) => { + forward: textarea => { if (textarea.selectionStart !== textarea.selectionEnd) return false @@ -769,6 +769,42 @@ let Input = { return true }, + modifyWord: (textarea, cb) => { + let start = textarea.selectionStart + let end = textarea.selectionEnd + if (start === end) { + let len = textarea.value.length + while (start < len && /\s/.test(textarea.value.charAt(start))) + start++; + end = start + while (end < len && !/\s/.test(textarea.value.charAt(end))) + end++; + } + if (start === end) + return false + + const text = textarea.value, modified = cb(text.substring(start, end)) + textarea.value = text.slice(0, start) + modified + text.slice(end) + end = start + modified.length + textarea.setSelectionRange(end, end) + return true + }, + + downcase: textarea => { + return Input.modifyWord(textarea, text => text.toLowerCase()) + }, + + upcase: textarea => { + return Input.modifyWord(textarea, text => text.toUpperCase()) + }, + + capitalize: textarea => { + return Input.modifyWord(textarea, text => { + const cps = Array.from(text.toLowerCase()) + return cps[0].toUpperCase() + cps.slice(1).join('') + }) + }, + first: (b, textarea) => { if (b.historyAt <= 0) return false @@ -823,8 +859,11 @@ let Input = { if (hasShortcutModifiers(event)) { handled = true switch (event.key) { - case 'b': success = Input.backward(b, textarea); break - case 'f': success = Input.forward(b, textarea); break + case 'b': success = Input.backward(textarea); break + case 'f': success = Input.forward(textarea); break + case 'l': success = Input.downcase(textarea); break + case 'u': success = Input.upcase(textarea); break + case 'c': success = Input.capitalize(textarea); break case '<': success = Input.first(b, textarea); break case '>': success = Input.last(b, textarea); break case 'p': success = Input.previous(b, textarea); break