update
This commit is contained in:
+34
-17
@@ -116,18 +116,21 @@
|
|||||||
].filter((layout) => layout.chars.length > 0);
|
].filter((layout) => layout.chars.length > 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
function setGematriaInputValue(nextValue) {
|
function setGematriaInputValue(nextValue, options = {}) {
|
||||||
const { inputEl } = getElements();
|
const { inputEl } = getElements();
|
||||||
if (!(inputEl instanceof HTMLTextAreaElement)) {
|
if (!(inputEl instanceof HTMLTextAreaElement)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const shouldFocus = options?.focus !== false;
|
||||||
inputEl.value = String(nextValue || "");
|
inputEl.value = String(nextValue || "");
|
||||||
inputEl.dispatchEvent(new Event("input", { bubbles: true }));
|
inputEl.dispatchEvent(new Event("input", { bubbles: true }));
|
||||||
|
if (shouldFocus) {
|
||||||
inputEl.focus({ preventScroll: true });
|
inputEl.focus({ preventScroll: true });
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function insertGematriaText(insertValue) {
|
function insertGematriaText(insertValue, options = {}) {
|
||||||
const { inputEl } = getElements();
|
const { inputEl } = getElements();
|
||||||
if (!(inputEl instanceof HTMLTextAreaElement)) {
|
if (!(inputEl instanceof HTMLTextAreaElement)) {
|
||||||
return;
|
return;
|
||||||
@@ -138,23 +141,29 @@
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const start = Number.isFinite(inputEl.selectionStart) ? inputEl.selectionStart : inputEl.value.length;
|
const isInputFocused = document.activeElement === inputEl;
|
||||||
const end = Number.isFinite(inputEl.selectionEnd) ? inputEl.selectionEnd : start;
|
const useSelection = options?.preserveSelection !== false && isInputFocused;
|
||||||
|
const start = useSelection && Number.isFinite(inputEl.selectionStart) ? inputEl.selectionStart : inputEl.value.length;
|
||||||
|
const end = useSelection && Number.isFinite(inputEl.selectionEnd) ? inputEl.selectionEnd : start;
|
||||||
const nextValue = `${inputEl.value.slice(0, start)}${text}${inputEl.value.slice(end)}`;
|
const nextValue = `${inputEl.value.slice(0, start)}${text}${inputEl.value.slice(end)}`;
|
||||||
setGematriaInputValue(nextValue);
|
setGematriaInputValue(nextValue, { focus: options?.focus });
|
||||||
|
|
||||||
const nextCaret = start + text.length;
|
const nextCaret = start + text.length;
|
||||||
|
if (useSelection) {
|
||||||
inputEl.setSelectionRange(nextCaret, nextCaret);
|
inputEl.setSelectionRange(nextCaret, nextCaret);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function backspaceGematriaText() {
|
function backspaceGematriaText(options = {}) {
|
||||||
const { inputEl } = getElements();
|
const { inputEl } = getElements();
|
||||||
if (!(inputEl instanceof HTMLTextAreaElement)) {
|
if (!(inputEl instanceof HTMLTextAreaElement)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const start = Number.isFinite(inputEl.selectionStart) ? inputEl.selectionStart : inputEl.value.length;
|
const isInputFocused = document.activeElement === inputEl;
|
||||||
const end = Number.isFinite(inputEl.selectionEnd) ? inputEl.selectionEnd : start;
|
const useSelection = options?.preserveSelection !== false && isInputFocused;
|
||||||
|
const start = useSelection && Number.isFinite(inputEl.selectionStart) ? inputEl.selectionStart : inputEl.value.length;
|
||||||
|
const end = useSelection && Number.isFinite(inputEl.selectionEnd) ? inputEl.selectionEnd : start;
|
||||||
if (start === 0 && end === 0) {
|
if (start === 0 && end === 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -166,9 +175,11 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
const nextValue = `${inputEl.value.slice(0, nextStart)}${inputEl.value.slice(nextEnd)}`;
|
const nextValue = `${inputEl.value.slice(0, nextStart)}${inputEl.value.slice(nextEnd)}`;
|
||||||
setGematriaInputValue(nextValue);
|
setGematriaInputValue(nextValue, { focus: options?.focus });
|
||||||
|
if (useSelection) {
|
||||||
inputEl.setSelectionRange(nextStart, nextStart);
|
inputEl.setSelectionRange(nextStart, nextStart);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function renderKeyboardLayout() {
|
function renderKeyboardLayout() {
|
||||||
const { keyboardScriptEl, keyboardGridEl } = getElements();
|
const { keyboardScriptEl, keyboardGridEl } = getElements();
|
||||||
@@ -209,34 +220,34 @@
|
|||||||
keyboardGridEl.setAttribute("lang", state.keyboardScriptId);
|
keyboardGridEl.setAttribute("lang", state.keyboardScriptId);
|
||||||
}
|
}
|
||||||
|
|
||||||
function handleKeyboardAction(action) {
|
function handleKeyboardAction(action, options = {}) {
|
||||||
const normalizedAction = String(action || "").trim().toLowerCase();
|
const normalizedAction = String(action || "").trim().toLowerCase();
|
||||||
if (!normalizedAction) {
|
if (!normalizedAction) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (normalizedAction === "backspace") {
|
if (normalizedAction === "backspace") {
|
||||||
backspaceGematriaText();
|
backspaceGematriaText(options);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (normalizedAction === "space") {
|
if (normalizedAction === "space") {
|
||||||
insertGematriaText(" ");
|
insertGematriaText(" ", options);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (normalizedAction === "star") {
|
if (normalizedAction === "star") {
|
||||||
insertGematriaText("*");
|
insertGematriaText("*", options);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (normalizedAction === "question") {
|
if (normalizedAction === "question") {
|
||||||
insertGematriaText("?");
|
insertGematriaText("?", options);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (normalizedAction === "clear") {
|
if (normalizedAction === "clear") {
|
||||||
setGematriaInputValue("");
|
setGematriaInputValue("", { focus: options?.focus });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1345,7 +1356,10 @@
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
insertGematriaText(String(keyButton.dataset.keyboardInsert || ""));
|
insertGematriaText(String(keyButton.dataset.keyboardInsert || ""), {
|
||||||
|
focus: false,
|
||||||
|
preserveSelection: false
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
keyboardActionsEl?.addEventListener("click", (event) => {
|
keyboardActionsEl?.addEventListener("click", (event) => {
|
||||||
@@ -1359,7 +1373,10 @@
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
handleKeyboardAction(actionButton.dataset.keyboardAction);
|
handleKeyboardAction(actionButton.dataset.keyboardAction, {
|
||||||
|
focus: false,
|
||||||
|
preserveSelection: false
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
state.listenersBound = true;
|
state.listenersBound = true;
|
||||||
|
|||||||
Reference in New Issue
Block a user