@uiwwsw/virtual-keyboard 0.5.7 → 0.5.8

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 CHANGED
@@ -581,6 +581,14 @@ function VirtualInput({
581
581
  const caretIndexRef = useRef(caretIndex);
582
582
  const [selection, setSelection] = useState2({ start: null, end: null });
583
583
  const [showCursor, setShowCursor] = useState2(true);
584
+ const showCursorRef = useRef(showCursor);
585
+ useEffect2(() => {
586
+ showCursorRef.current = showCursor;
587
+ }, [showCursor]);
588
+ const longPressTimerRef = useRef(null);
589
+ const [showCopyFeedback, setShowCopyFeedback] = useState2(false);
590
+ const pointerStartedRef = useRef(false);
591
+ const pointerStartPositionRef = useRef(null);
584
592
  const charPositionsRef = useRef([]);
585
593
  const scrollXRef = useRef(0);
586
594
  useEffect2(() => {
@@ -678,16 +686,20 @@ function VirtualInput({
678
686
  return;
679
687
  }
680
688
  ctx.fillStyle = color;
681
- if (charPositionsRef.current.length !== internalValue.length + 1) {
689
+ const text = internalValueRef.current;
690
+ const cIndex = caretIndexRef.current;
691
+ const currentScrollX = scrollXRef.current;
692
+ const isCursorVisible = showCursorRef.current;
693
+ if (charPositionsRef.current.length !== text.length + 1) {
682
694
  const positions = [x];
683
- for (let i = 0;i < internalValue.length; i++) {
684
- const w = ctx.measureText(internalValue.slice(0, i + 1)).width;
695
+ for (let i = 0;i < text.length; i++) {
696
+ const w = ctx.measureText(text.slice(0, i + 1)).width;
685
697
  positions.push(x + w);
686
698
  }
687
699
  charPositionsRef.current = positions;
688
700
  }
689
701
  const charX = charPositionsRef.current;
690
- const caretPos = charX[caretIndex] ?? x;
702
+ const caretPos = charX[cIndex] ?? x;
691
703
  const viewLeft = paddingLeft;
692
704
  const viewRight = width - paddingRight - borderLeft - borderRight;
693
705
  const safety = 2;
@@ -718,9 +730,9 @@ function VirtualInput({
718
730
  ctx.fillRect(startX, 0, endX - startX, height);
719
731
  ctx.fillStyle = color;
720
732
  }
721
- ctx.fillText(internalValue, x, y);
722
- if (isFocused && showCursor && !hasSelection) {
723
- const caretX = charX[caretIndex] ?? x;
733
+ ctx.fillText(text, x, y);
734
+ if (isFocused && isCursorVisible && !hasSelection) {
735
+ const caretX = charX[cIndex] ?? x;
724
736
  ctx.beginPath();
725
737
  ctx.moveTo(caretX, y - fontSize / 2);
726
738
  ctx.lineTo(caretX, y + fontSize / 2);
@@ -728,8 +740,36 @@ function VirtualInput({
728
740
  ctx.lineWidth = 1.5;
729
741
  ctx.stroke();
730
742
  }
743
+ if (showCopyFeedback) {
744
+ ctx.save();
745
+ ctx.fillStyle = "rgba(0, 0, 0, 0.7)";
746
+ const msg = "Copied!";
747
+ const msgWidth = ctx.measureText(msg).width + 20;
748
+ const msgHeight = fontSize + 10;
749
+ ctx.translate(scrollXRef.current, 0);
750
+ ctx.restore();
751
+ ctx.save();
752
+ const cx = width / 2;
753
+ const cy = height / 2;
754
+ ctx.fillStyle = "rgba(30, 41, 59, 0.9)";
755
+ const r = 6;
756
+ const rw = msgWidth;
757
+ const rh = msgHeight;
758
+ const rx = cx - rw / 2;
759
+ const ry = cy - rh / 2;
760
+ ctx.beginPath();
761
+ ctx.roundRect(rx, ry, rw, rh, r);
762
+ ctx.fill();
763
+ ctx.fillStyle = "#ffffff";
764
+ ctx.font = `bold ${fontSize * 0.8}px system-ui`;
765
+ ctx.textAlign = "center";
766
+ ctx.textBaseline = "middle";
767
+ ctx.fillText(msg, cx, cy);
768
+ ctx.restore();
769
+ return;
770
+ }
731
771
  ctx.restore();
732
- }, [internalValue, caretIndex, isFocused, showCursor, placeholder, hasSelection, selection]);
772
+ }, [isFocused, placeholder, hasSelection, selection, showCopyFeedback]);
733
773
  useEffect2(() => {
734
774
  let animationFrameId;
735
775
  const loop = () => {
@@ -784,12 +824,57 @@ function VirtualInput({
784
824
  return internalValue.length;
785
825
  }, [internalValue]);
786
826
  const handleCanvasClick = useCallback((e) => {
827
+ if (showCopyFeedback)
828
+ return;
787
829
  const index = getCharIndexFromX(e.clientX);
788
830
  caretIndexRef.current = index;
789
831
  setCaretIndex(index);
790
832
  clearSelection();
791
833
  onFocus(id);
792
- }, [getCharIndexFromX, clearSelection, onFocus, id]);
834
+ }, [getCharIndexFromX, clearSelection, onFocus, id, showCopyFeedback]);
835
+ const cancelLongPress = useCallback(() => {
836
+ if (longPressTimerRef.current) {
837
+ clearTimeout(longPressTimerRef.current);
838
+ longPressTimerRef.current = null;
839
+ }
840
+ pointerStartedRef.current = false;
841
+ pointerStartPositionRef.current = null;
842
+ }, []);
843
+ const handlePointerDown = useCallback((e) => {
844
+ if (e.button !== 0)
845
+ return;
846
+ cancelLongPress();
847
+ pointerStartedRef.current = true;
848
+ pointerStartPositionRef.current = { x: e.clientX, y: e.clientY };
849
+ longPressTimerRef.current = window.setTimeout(async () => {
850
+ if (!pointerStartedRef.current)
851
+ return;
852
+ pointerStartedRef.current = false;
853
+ try {
854
+ if (internalValueRef.current) {
855
+ await navigator.clipboard.writeText(internalValueRef.current);
856
+ if (navigator.vibrate)
857
+ navigator.vibrate(50);
858
+ setShowCopyFeedback(true);
859
+ setTimeout(() => setShowCopyFeedback(false), 1500);
860
+ }
861
+ } catch (err) {
862
+ console.error("Failed to copy", err);
863
+ }
864
+ }, 600);
865
+ }, [cancelLongPress]);
866
+ const handlePointerMove = useCallback((e) => {
867
+ if (!pointerStartedRef.current || !pointerStartPositionRef.current)
868
+ return;
869
+ const dx = Math.abs(e.clientX - pointerStartPositionRef.current.x);
870
+ const dy = Math.abs(e.clientY - pointerStartPositionRef.current.y);
871
+ if (dx > 10 || dy > 10) {
872
+ cancelLongPress();
873
+ }
874
+ }, [cancelLongPress]);
875
+ const handlePointerUp = useCallback(() => {
876
+ cancelLongPress();
877
+ }, [cancelLongPress]);
793
878
  const handleKeyDown = useCallback((e) => {
794
879
  const currentVal = internalValueRef.current;
795
880
  const currentCaret = caretIndexRef.current;
@@ -797,6 +882,20 @@ function VirtualInput({
797
882
  setHangulMode(!hangulMode);
798
883
  return;
799
884
  }
885
+ if ((e.ctrlKey || e.metaKey) && e.key.toLowerCase() === "c") {
886
+ if (hasSelection && selection.start !== null && selection.end !== null) {
887
+ const [start, end] = [selection.start, selection.end].sort((a, b) => a - b);
888
+ const textToCopy = currentVal.slice(start, end);
889
+ navigator.clipboard.writeText(textToCopy).then(() => {
890
+ if (navigator.vibrate)
891
+ navigator.vibrate(50);
892
+ setShowCopyFeedback(true);
893
+ setTimeout(() => setShowCopyFeedback(false), 1500);
894
+ }).catch(console.error);
895
+ e.preventDefault();
896
+ return;
897
+ }
898
+ }
800
899
  if (e.shiftKey && (e.key === "ArrowLeft" || e.key === "ArrowRight")) {
801
900
  const newIndex = e.key === "ArrowLeft" ? Math.max(0, currentCaret - 1) : Math.min(currentVal.length, currentCaret + 1);
802
901
  setSelection((prev) => ({
@@ -859,7 +958,17 @@ function VirtualInput({
859
958
  updateValue(finalString, finalCaretIndex + text.length);
860
959
  }
861
960
  isCompositionRef.current = composing;
862
- }, [hangulMode, setHangulMode, isCompositionRef, shift, updateValue, hasSelection, deleteSelectedText, clearSelection]);
961
+ }, [
962
+ hangulMode,
963
+ setHangulMode,
964
+ isCompositionRef,
965
+ shift,
966
+ updateValue,
967
+ hasSelection,
968
+ deleteSelectedText,
969
+ clearSelection,
970
+ selection
971
+ ]);
863
972
  useImperativeHandle(inputRef, () => {
864
973
  if (isFocused) {
865
974
  return {
@@ -896,7 +1005,13 @@ function VirtualInput({
896
1005
  onFocus: () => onFocus(id, containerRef.current),
897
1006
  onBlur: (e) => onBlur(e),
898
1007
  onKeyDown: handleKeyDown,
1008
+ onContextMenu: (e) => e.preventDefault(),
899
1009
  onClick: handleCanvasClick,
1010
+ onPointerDown: handlePointerDown,
1011
+ onPointerMove: handlePointerMove,
1012
+ onPointerUp: handlePointerUp,
1013
+ onPointerLeave: handlePointerUp,
1014
+ onPointerCancel: handlePointerUp,
900
1015
  "data-virtual-input": "true",
901
1016
  css: `
902
1017
  :host {
@@ -981,11 +1096,16 @@ function useKeypadLayout({
981
1096
  row.forEach((cell, colIndex) => {
982
1097
  const flex = cell.width || 1;
983
1098
  const cellW = flex * unitW;
1099
+ const halfGap = gap / 2;
984
1100
  bounds.push({
985
1101
  x: currentX,
986
1102
  y: currentY,
987
1103
  w: cellW,
988
1104
  h: rowHeight,
1105
+ hitX: currentX - halfGap,
1106
+ hitY: currentY - halfGap,
1107
+ hitW: cellW + gap,
1108
+ hitH: rowHeight + gap,
989
1109
  value: cell.value,
990
1110
  type: cell.type,
991
1111
  label: getTransformedValue(cell),
@@ -1073,7 +1193,7 @@ function useKeypadInteraction({
1073
1193
  const rect = canvas.getBoundingClientRect();
1074
1194
  const x = e.clientX - rect.left;
1075
1195
  const y = e.clientY - rect.top;
1076
- const hitKey = keyBoundsRef.current.find((k) => x >= k.x && x <= k.x + k.w && y >= k.y && y <= k.y + k.h);
1196
+ const hitKey = keyBoundsRef.current.find((k) => x >= k.hitX && x <= k.hitX + k.hitW && y >= k.hitY && y <= k.hitY + k.hitH);
1077
1197
  if (hitKey) {
1078
1198
  const keyIndex = hitKey.rowIndex * 100 + hitKey.colIndex;
1079
1199
  dispatchKeyEvent(hitKey.value, hitKey.type);
@@ -1104,7 +1224,7 @@ function useKeypadInteraction({
1104
1224
  const rect = canvas.getBoundingClientRect();
1105
1225
  const x = e.clientX - rect.left;
1106
1226
  const y = e.clientY - rect.top;
1107
- const hitKey = keyBoundsRef.current.find((k) => x >= k.x && x <= k.x + k.w && y >= k.y && y <= k.y + k.h);
1227
+ const hitKey = keyBoundsRef.current.find((k) => x >= k.hitX && x <= k.hitX + k.hitW && y >= k.hitY && y <= k.hitY + k.hitH);
1108
1228
  if (hitKey) {
1109
1229
  const newKeyIndex = hitKey.rowIndex * 100 + hitKey.colIndex;
1110
1230
  if (newKeyIndex !== press.keyIndex) {
@@ -17,6 +17,10 @@ export type KeyBounds = {
17
17
  y: number;
18
18
  w: number;
19
19
  h: number;
20
+ hitX: number;
21
+ hitY: number;
22
+ hitW: number;
23
+ hitH: number;
20
24
  value: string;
21
25
  type?: string;
22
26
  label: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@uiwwsw/virtual-keyboard",
3
- "version": "0.5.7",
3
+ "version": "0.5.8",
4
4
  "type": "module",
5
5
  "types": "dist/types/index.d.ts",
6
6
  "main": "dist/index.js",