@uiwwsw/virtual-keyboard 0.5.0 → 0.5.2

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.
Files changed (2) hide show
  1. package/dist/index.js +38 -21
  2. 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: internalValue, finalCaretIndex: caretIndex };
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 = internalValue.slice(0, start) + internalValue.slice(end);
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, caretIndex - 1) : Math.min(internalValue.length, caretIndex + 1);
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 ?? caretIndex,
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
- setCaretIndex((prev) => Math.max(0, prev - 1));
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
- case "ArrowRight":
778
- setCaretIndex((prev) => Math.min(internalValue.length, prev + 1));
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 (caretIndex > 0) {
787
- const newString2 = internalValue.slice(0, caretIndex - 1) + internalValue.slice(caretIndex);
788
- updateValue(newString2, caretIndex - 1);
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: internalValue, finalCaretIndex: caretIndex };
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
- }, [internalValue, caretIndex, hangulMode, setHangulMode, isCompositionRef, shift, updateValue, hasSelection, deleteSelectedText, clearSelection]);
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
- canvas {
829
- width: 100%;
830
- height: 100%;
831
- cursor: text;
832
- display: block;
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,
@@ -1029,6 +1043,7 @@ function useKeypadInteraction({
1029
1043
  }
1030
1044
  }, [dispatchKeyEvent, startRepeat, keyBoundsRef]);
1031
1045
  const handlePointerMove = useCallback3((e) => {
1046
+ e.preventDefault();
1032
1047
  const press = activePresses.current.get(e.pointerId);
1033
1048
  if (!press)
1034
1049
  return;
@@ -1059,6 +1074,7 @@ function useKeypadInteraction({
1059
1074
  }
1060
1075
  }, [clearRepeat, dispatchKeyEvent, startRepeat, keyBoundsRef]);
1061
1076
  const handlePointerUp = useCallback3((e) => {
1077
+ e.preventDefault();
1062
1078
  const press = activePresses.current.get(e.pointerId);
1063
1079
  if (press) {
1064
1080
  clearRepeat(press);
@@ -1256,6 +1272,7 @@ function VirtualKeypad({
1256
1272
  },
1257
1273
  onBlur,
1258
1274
  onContextMenu: (e) => e.preventDefault(),
1275
+ onPointerDown: (e) => e.preventDefault(),
1259
1276
  style: {
1260
1277
  left: viewport.offsetLeft,
1261
1278
  width: viewport.width,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@uiwwsw/virtual-keyboard",
3
- "version": "0.5.0",
3
+ "version": "0.5.2",
4
4
  "type": "module",
5
5
  "types": "dist/types/index.d.ts",
6
6
  "main": "dist/index.js",