@uiwwsw/virtual-keyboard 0.5.9 → 0.7.0

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
@@ -672,10 +672,13 @@ function VirtualInput({
672
672
  const caretColor = (computedStyle.caretColor !== "auto" ? computedStyle.caretColor : color) || "black";
673
673
  const y = height / 2;
674
674
  const x = borderLeft + paddingLeft;
675
- if (!internalValue && placeholder) {
675
+ const text = internalValueRef.current;
676
+ const cIndex = caretIndexRef.current;
677
+ const isCursorVisible = showCursorRef.current;
678
+ if (!text && placeholder) {
676
679
  ctx.fillStyle = "#aaa";
677
680
  ctx.fillText(placeholder, x, y);
678
- if (isFocused && showCursor) {
681
+ if (isFocused && isCursorVisible) {
679
682
  ctx.beginPath();
680
683
  ctx.moveTo(x, y - fontSize / 2);
681
684
  ctx.lineTo(x, y + fontSize / 2);
@@ -686,9 +689,6 @@ function VirtualInput({
686
689
  return;
687
690
  }
688
691
  ctx.fillStyle = color;
689
- const text = internalValueRef.current;
690
- const cIndex = caretIndexRef.current;
691
- const isCursorVisible = showCursorRef.current;
692
692
  if (charPositionsRef.current.length !== text.length + 1) {
693
693
  const positions = [x];
694
694
  for (let i = 0;i < text.length; i++) {
@@ -741,22 +741,20 @@ function VirtualInput({
741
741
  }
742
742
  if (showCopyFeedback) {
743
743
  ctx.save();
744
+ ctx.translate(scrollXRef.current, 0);
744
745
  ctx.fillStyle = "rgba(0, 0, 0, 0.7)";
745
746
  const msg = "Copied!";
746
747
  const msgWidth = ctx.measureText(msg).width + 20;
747
748
  const msgHeight = fontSize + 10;
748
- ctx.translate(scrollXRef.current, 0);
749
- ctx.restore();
750
- ctx.save();
751
749
  const cx = width / 2;
752
750
  const cy = height / 2;
753
- ctx.fillStyle = "rgba(30, 41, 59, 0.9)";
754
751
  const r = 6;
755
752
  const rw = msgWidth;
756
753
  const rh = msgHeight;
757
754
  const rx = cx - rw / 2;
758
755
  const ry = cy - rh / 2;
759
756
  ctx.beginPath();
757
+ ctx.fillStyle = "rgba(30, 41, 59, 0.9)";
760
758
  ctx.roundRect(rx, ry, rw, rh, r);
761
759
  ctx.fill();
762
760
  ctx.fillStyle = "#ffffff";
@@ -765,7 +763,6 @@ function VirtualInput({
765
763
  ctx.textBaseline = "middle";
766
764
  ctx.fillText(msg, cx, cy);
767
765
  ctx.restore();
768
- return;
769
766
  }
770
767
  ctx.restore();
771
768
  }, [isFocused, placeholder, hasSelection, selection, showCopyFeedback]);
@@ -815,9 +812,8 @@ function VirtualInput({
815
812
  let currentX = 0;
816
813
  for (let i = 0;i < internalValue.length; i++) {
817
814
  const w = ctx.measureText(internalValue[i]).width;
818
- if (x < currentX + w / 2) {
815
+ if (x < currentX + w / 2)
819
816
  return i;
820
- }
821
817
  currentX += w;
822
818
  }
823
819
  return internalValue.length;
@@ -831,6 +827,46 @@ function VirtualInput({
831
827
  clearSelection();
832
828
  onFocus(id);
833
829
  }, [getCharIndexFromX, clearSelection, onFocus, id, showCopyFeedback]);
830
+ const handleCopy = useCallback((e) => {
831
+ e.preventDefault();
832
+ const currentVal = internalValueRef.current;
833
+ const sel = selection;
834
+ if (sel.start !== null && sel.end !== null && sel.start !== sel.end) {
835
+ const [start, end] = [sel.start, sel.end].sort((a, b) => a - b);
836
+ const textToCopy = currentVal.slice(start, end);
837
+ e.clipboardData.setData("text/plain", textToCopy);
838
+ if (navigator.vibrate)
839
+ navigator.vibrate(50);
840
+ setShowCopyFeedback(true);
841
+ setTimeout(() => setShowCopyFeedback(false), 1500);
842
+ }
843
+ }, [selection]);
844
+ const handleCut = useCallback((e) => {
845
+ e.preventDefault();
846
+ const currentVal = internalValueRef.current;
847
+ if (hasSelection && selection.start !== null && selection.end !== null) {
848
+ const [start, end] = [selection.start, selection.end].sort((a, b) => a - b);
849
+ const textToCopy = currentVal.slice(start, end);
850
+ e.clipboardData.setData("text/plain", textToCopy);
851
+ if (navigator.vibrate)
852
+ navigator.vibrate(50);
853
+ setShowCopyFeedback(true);
854
+ setTimeout(() => setShowCopyFeedback(false), 1500);
855
+ const { newString, finalCaretIndex } = deleteSelectedText();
856
+ updateValue(newString, finalCaretIndex);
857
+ }
858
+ }, [hasSelection, selection, deleteSelectedText, updateValue]);
859
+ const handlePaste = useCallback((e) => {
860
+ e.preventDefault();
861
+ const text = e.clipboardData.getData("text/plain");
862
+ if (!text)
863
+ return;
864
+ const currentVal = internalValueRef.current;
865
+ const currentCaret = caretIndexRef.current;
866
+ const { newString, finalCaretIndex } = hasSelection ? deleteSelectedText() : { newString: currentVal, finalCaretIndex: currentCaret };
867
+ const finalString = newString.slice(0, finalCaretIndex) + text + newString.slice(finalCaretIndex);
868
+ updateValue(finalString, finalCaretIndex + text.length);
869
+ }, [hasSelection, deleteSelectedText, updateValue]);
834
870
  const cancelLongPress = useCallback(() => {
835
871
  if (longPressTimerRef.current) {
836
872
  clearTimeout(longPressTimerRef.current);
@@ -867,9 +903,8 @@ function VirtualInput({
867
903
  return;
868
904
  const dx = Math.abs(e.clientX - pointerStartPositionRef.current.x);
869
905
  const dy = Math.abs(e.clientY - pointerStartPositionRef.current.y);
870
- if (dx > 10 || dy > 10) {
906
+ if (dx > 10 || dy > 10)
871
907
  cancelLongPress();
872
- }
873
908
  }, [cancelLongPress]);
874
909
  const handlePointerUp = useCallback(() => {
875
910
  cancelLongPress();
@@ -881,17 +916,46 @@ function VirtualInput({
881
916
  setHangulMode(!hangulMode);
882
917
  return;
883
918
  }
884
- if ((e.ctrlKey || e.metaKey) && e.key.toLowerCase() === "c") {
885
- if (hasSelection && selection.start !== null && selection.end !== null) {
886
- const [start, end] = [selection.start, selection.end].sort((a, b) => a - b);
887
- const textToCopy = currentVal.slice(start, end);
888
- navigator.clipboard.writeText(textToCopy).then(() => {
889
- if (navigator.vibrate)
890
- navigator.vibrate(50);
891
- setShowCopyFeedback(true);
892
- setTimeout(() => setShowCopyFeedback(false), 1500);
893
- }).catch(console.error);
919
+ const isCmd = e.ctrlKey || e.metaKey;
920
+ const isHome = e.key === "ArrowUp" || isCmd && e.key === "ArrowLeft" || e.key === "Home";
921
+ const isEnd = e.key === "ArrowDown" || isCmd && e.key === "ArrowRight" || e.key === "End";
922
+ if (isHome) {
923
+ e.preventDefault();
924
+ const newIndex = 0;
925
+ if (e.shiftKey) {
926
+ setSelection((prev) => ({
927
+ start: prev.start ?? currentCaret,
928
+ end: newIndex
929
+ }));
930
+ } else {
931
+ clearSelection();
932
+ }
933
+ caretIndexRef.current = newIndex;
934
+ setCaretIndex(newIndex);
935
+ return;
936
+ }
937
+ if (isEnd) {
938
+ e.preventDefault();
939
+ const newIndex = currentVal.length;
940
+ if (e.shiftKey) {
941
+ setSelection((prev) => ({
942
+ start: prev.start ?? currentCaret,
943
+ end: newIndex
944
+ }));
945
+ } else {
946
+ clearSelection();
947
+ }
948
+ caretIndexRef.current = newIndex;
949
+ setCaretIndex(newIndex);
950
+ return;
951
+ }
952
+ if (e.ctrlKey || e.metaKey) {
953
+ const key = e.key.toLowerCase();
954
+ if (key === "a") {
894
955
  e.preventDefault();
956
+ setSelection({ start: 0, end: currentVal.length });
957
+ caretIndexRef.current = currentVal.length;
958
+ setCaretIndex(currentVal.length);
895
959
  return;
896
960
  }
897
961
  }
@@ -957,17 +1021,7 @@ function VirtualInput({
957
1021
  updateValue(finalString, finalCaretIndex + text.length);
958
1022
  }
959
1023
  isCompositionRef.current = composing;
960
- }, [
961
- hangulMode,
962
- setHangulMode,
963
- isCompositionRef,
964
- shift,
965
- updateValue,
966
- hasSelection,
967
- deleteSelectedText,
968
- clearSelection,
969
- selection
970
- ]);
1024
+ }, [hangulMode, setHangulMode, isCompositionRef, shift, updateValue, hasSelection, deleteSelectedText, clearSelection]);
971
1025
  useImperativeHandle(inputRef, () => {
972
1026
  if (isFocused) {
973
1027
  return {
@@ -1005,6 +1059,9 @@ function VirtualInput({
1005
1059
  onBlur: (e) => onBlur(e),
1006
1060
  onKeyDown: handleKeyDown,
1007
1061
  onContextMenu: (e) => e.preventDefault(),
1062
+ onCopy: handleCopy,
1063
+ onCut: handleCut,
1064
+ onPaste: handlePaste,
1008
1065
  onClick: handleCanvasClick,
1009
1066
  onPointerDown: handlePointerDown,
1010
1067
  onPointerMove: handlePointerMove,
@@ -1095,16 +1152,11 @@ function useKeypadLayout({
1095
1152
  row.forEach((cell, colIndex) => {
1096
1153
  const flex = cell.width || 1;
1097
1154
  const cellW = flex * unitW;
1098
- const halfGap = gap / 2;
1099
1155
  bounds.push({
1100
1156
  x: currentX,
1101
1157
  y: currentY,
1102
1158
  w: cellW,
1103
1159
  h: rowHeight,
1104
- hitX: currentX - halfGap,
1105
- hitY: currentY - halfGap,
1106
- hitW: cellW + gap,
1107
- hitH: rowHeight + gap,
1108
1160
  value: cell.value,
1109
1161
  type: cell.type,
1110
1162
  label: getTransformedValue(cell),
@@ -1170,7 +1222,7 @@ function useKeypadInteraction({
1170
1222
  const canRepeat = press.type && repeatableKeys.has(press.type) || repeatableKeys.has(press.value);
1171
1223
  if (!canRepeat)
1172
1224
  return;
1173
- const firstDelay = 300;
1225
+ const firstDelay = 500;
1174
1226
  const repeatInterval = 60;
1175
1227
  const tick = () => {
1176
1228
  const storedPress = activePresses.current.get(press.pointerId);
@@ -1189,10 +1241,13 @@ function useKeypadInteraction({
1189
1241
  if (!canvas)
1190
1242
  return;
1191
1243
  canvas.setPointerCapture(e.pointerId);
1244
+ activePresses.current.forEach((press) => {
1245
+ clearRepeat(press);
1246
+ });
1192
1247
  const rect = canvas.getBoundingClientRect();
1193
1248
  const x = e.clientX - rect.left;
1194
1249
  const y = e.clientY - rect.top;
1195
- const hitKey = keyBoundsRef.current.find((k) => x >= k.hitX && x <= k.hitX + k.hitW && y >= k.hitY && y <= k.hitY + k.hitH);
1250
+ const hitKey = keyBoundsRef.current.find((k) => x >= k.x && x <= k.x + k.w && y >= k.y && y <= k.y + k.h);
1196
1251
  if (hitKey) {
1197
1252
  const keyIndex = hitKey.rowIndex * 100 + hitKey.colIndex;
1198
1253
  dispatchKeyEvent(hitKey.value, hitKey.type);
@@ -1223,7 +1278,7 @@ function useKeypadInteraction({
1223
1278
  const rect = canvas.getBoundingClientRect();
1224
1279
  const x = e.clientX - rect.left;
1225
1280
  const y = e.clientY - rect.top;
1226
- const hitKey = keyBoundsRef.current.find((k) => x >= k.hitX && x <= k.hitX + k.hitW && y >= k.hitY && y <= k.hitY + k.hitH);
1281
+ const hitKey = keyBoundsRef.current.find((k) => x >= k.x && x <= k.x + k.w && y >= k.y && y <= k.y + k.h);
1227
1282
  if (hitKey) {
1228
1283
  const newKeyIndex = hitKey.rowIndex * 100 + hitKey.colIndex;
1229
1284
  if (newKeyIndex !== press.keyIndex) {
@@ -17,10 +17,6 @@ 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;
24
20
  value: string;
25
21
  type?: string;
26
22
  label: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@uiwwsw/virtual-keyboard",
3
- "version": "0.5.9",
3
+ "version": "0.7.0",
4
4
  "type": "module",
5
5
  "types": "dist/types/index.d.ts",
6
6
  "main": "dist/index.js",