@uiwwsw/virtual-keyboard 0.5.0 → 0.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +36 -21
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -576,6 +576,8 @@ function VirtualInput({
|
|
|
576
576
|
const containerRef = useRef(null);
|
|
577
577
|
const [internalValue, setInternalValue] = useState2(controlledValue ?? defaultValue ?? "");
|
|
578
578
|
const [caretIndex, setCaretIndex] = useState2(() => (controlledValue ?? defaultValue ?? "").length);
|
|
579
|
+
const internalValueRef = useRef(internalValue);
|
|
580
|
+
const caretIndexRef = useRef(caretIndex);
|
|
579
581
|
const [selection, setSelection] = useState2({ start: null, end: null });
|
|
580
582
|
const [showCursor, setShowCursor] = useState2(true);
|
|
581
583
|
const charPositionsRef = useRef([]);
|
|
@@ -596,6 +598,7 @@ function VirtualInput({
|
|
|
596
598
|
useEffect2(() => {
|
|
597
599
|
if (controlledValue !== undefined && controlledValue !== internalValue) {
|
|
598
600
|
setInternalValue(controlledValue);
|
|
601
|
+
internalValueRef.current = controlledValue;
|
|
599
602
|
}
|
|
600
603
|
}, [controlledValue, internalValue]);
|
|
601
604
|
useEffect2(() => {
|
|
@@ -614,16 +617,15 @@ function VirtualInput({
|
|
|
614
617
|
setSelection({ start: null, end: null });
|
|
615
618
|
}, []);
|
|
616
619
|
const deleteSelectedText = useCallback(() => {
|
|
620
|
+
const currentVal = internalValueRef.current;
|
|
617
621
|
if (!hasSelection || selection.start === null || selection.end === null) {
|
|
618
|
-
return { newString:
|
|
622
|
+
return { newString: currentVal, finalCaretIndex: caretIndexRef.current };
|
|
619
623
|
}
|
|
620
624
|
const [start, end] = [selection.start, selection.end].sort((a, b) => a - b);
|
|
621
|
-
const newString =
|
|
625
|
+
const newString = currentVal.slice(0, start) + currentVal.slice(end);
|
|
622
626
|
clearSelection();
|
|
623
627
|
return { newString, finalCaretIndex: start };
|
|
624
628
|
}, [
|
|
625
|
-
internalValue,
|
|
626
|
-
caretIndex,
|
|
627
629
|
hasSelection,
|
|
628
630
|
selection,
|
|
629
631
|
clearSelection
|
|
@@ -708,6 +710,8 @@ function VirtualInput({
|
|
|
708
710
|
return () => cancelAnimationFrame(animationFrameId);
|
|
709
711
|
}, [draw]);
|
|
710
712
|
const updateValue = useCallback((newValue, newCaretIndex) => {
|
|
713
|
+
internalValueRef.current = newValue;
|
|
714
|
+
caretIndexRef.current = newCaretIndex;
|
|
711
715
|
if (controlledValue === undefined) {
|
|
712
716
|
setInternalValue(newValue);
|
|
713
717
|
}
|
|
@@ -751,41 +755,51 @@ function VirtualInput({
|
|
|
751
755
|
}, [internalValue]);
|
|
752
756
|
const handleCanvasClick = useCallback((e) => {
|
|
753
757
|
const index = getCharIndexFromX(e.clientX);
|
|
758
|
+
caretIndexRef.current = index;
|
|
754
759
|
setCaretIndex(index);
|
|
755
760
|
clearSelection();
|
|
756
761
|
onFocus(id);
|
|
757
762
|
}, [getCharIndexFromX, clearSelection, onFocus, id]);
|
|
758
763
|
const handleKeyDown = useCallback((e) => {
|
|
764
|
+
const currentVal = internalValueRef.current;
|
|
765
|
+
const currentCaret = caretIndexRef.current;
|
|
759
766
|
if (e.key === "HangulMode") {
|
|
760
767
|
setHangulMode(!hangulMode);
|
|
761
768
|
return;
|
|
762
769
|
}
|
|
763
770
|
if (e.shiftKey && (e.key === "ArrowLeft" || e.key === "ArrowRight")) {
|
|
764
|
-
const newIndex = e.key === "ArrowLeft" ? Math.max(0,
|
|
771
|
+
const newIndex = e.key === "ArrowLeft" ? Math.max(0, currentCaret - 1) : Math.min(currentVal.length, currentCaret + 1);
|
|
765
772
|
setSelection((prev) => ({
|
|
766
|
-
start: prev.start ??
|
|
773
|
+
start: prev.start ?? currentCaret,
|
|
767
774
|
end: newIndex
|
|
768
775
|
}));
|
|
776
|
+
caretIndexRef.current = newIndex;
|
|
769
777
|
setCaretIndex(newIndex);
|
|
770
778
|
return;
|
|
771
779
|
}
|
|
772
780
|
switch (e.key) {
|
|
773
|
-
case "ArrowLeft":
|
|
774
|
-
|
|
781
|
+
case "ArrowLeft": {
|
|
782
|
+
const newIndex = Math.max(0, currentCaret - 1);
|
|
783
|
+
caretIndexRef.current = newIndex;
|
|
784
|
+
setCaretIndex(newIndex);
|
|
775
785
|
clearSelection();
|
|
776
786
|
return;
|
|
777
|
-
|
|
778
|
-
|
|
787
|
+
}
|
|
788
|
+
case "ArrowRight": {
|
|
789
|
+
const newIndex = Math.min(currentVal.length, currentCaret + 1);
|
|
790
|
+
caretIndexRef.current = newIndex;
|
|
791
|
+
setCaretIndex(newIndex);
|
|
779
792
|
clearSelection();
|
|
780
793
|
return;
|
|
794
|
+
}
|
|
781
795
|
case "Backspace": {
|
|
782
796
|
isCompositionRef.current = false;
|
|
783
797
|
if (hasSelection) {
|
|
784
798
|
const { newString: newString2, finalCaretIndex: finalCaretIndex2 } = deleteSelectedText();
|
|
785
799
|
updateValue(newString2, finalCaretIndex2);
|
|
786
|
-
} else if (
|
|
787
|
-
const newString2 =
|
|
788
|
-
updateValue(newString2,
|
|
800
|
+
} else if (currentCaret > 0) {
|
|
801
|
+
const newString2 = currentVal.slice(0, currentCaret - 1) + currentVal.slice(currentCaret);
|
|
802
|
+
updateValue(newString2, currentCaret - 1);
|
|
789
803
|
}
|
|
790
804
|
return;
|
|
791
805
|
}
|
|
@@ -804,7 +818,7 @@ function VirtualInput({
|
|
|
804
818
|
if (shift && text.length === 1 && text.match(/[a-z]/i)) {
|
|
805
819
|
text = text.toUpperCase();
|
|
806
820
|
}
|
|
807
|
-
const { newString, finalCaretIndex } = hasSelection ? deleteSelectedText() : { newString:
|
|
821
|
+
const { newString, finalCaretIndex } = hasSelection ? deleteSelectedText() : { newString: currentVal, finalCaretIndex: currentCaret };
|
|
808
822
|
const prevChar = newString[finalCaretIndex - 1];
|
|
809
823
|
if (composing && isHangul(prevChar) && isCompositionRef.current) {
|
|
810
824
|
const combined = assemble([prevChar, text]);
|
|
@@ -815,7 +829,7 @@ function VirtualInput({
|
|
|
815
829
|
updateValue(finalString, finalCaretIndex + text.length);
|
|
816
830
|
}
|
|
817
831
|
isCompositionRef.current = composing;
|
|
818
|
-
}, [
|
|
832
|
+
}, [hangulMode, setHangulMode, isCompositionRef, shift, updateValue, hasSelection, deleteSelectedText, clearSelection]);
|
|
819
833
|
useImperativeHandle(inputRef, () => {
|
|
820
834
|
if (isFocused) {
|
|
821
835
|
return { handleKeyDown };
|
|
@@ -825,12 +839,12 @@ function VirtualInput({
|
|
|
825
839
|
return /* @__PURE__ */ jsxDEV2(ShadowWrapper, {
|
|
826
840
|
tagName: "virtual-input-canvas",
|
|
827
841
|
css: `
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
842
|
+
canvas {
|
|
843
|
+
width: 100%;
|
|
844
|
+
height: 100%;
|
|
845
|
+
cursor: text;
|
|
846
|
+
display: block;
|
|
847
|
+
}
|
|
834
848
|
`,
|
|
835
849
|
children: /* @__PURE__ */ jsxDEV2("div", {
|
|
836
850
|
ref: containerRef,
|
|
@@ -1256,6 +1270,7 @@ function VirtualKeypad({
|
|
|
1256
1270
|
},
|
|
1257
1271
|
onBlur,
|
|
1258
1272
|
onContextMenu: (e) => e.preventDefault(),
|
|
1273
|
+
onPointerDown: (e) => e.preventDefault(),
|
|
1259
1274
|
style: {
|
|
1260
1275
|
left: viewport.offsetLeft,
|
|
1261
1276
|
width: viewport.width,
|