@uiwwsw/virtual-keyboard 0.5.8 → 0.6.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 +16 -133
- package/dist/types/hooks/useKeypadLayout.d.ts +0 -4
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -581,14 +581,6 @@ 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);
|
|
592
584
|
const charPositionsRef = useRef([]);
|
|
593
585
|
const scrollXRef = useRef(0);
|
|
594
586
|
useEffect2(() => {
|
|
@@ -686,20 +678,16 @@ function VirtualInput({
|
|
|
686
678
|
return;
|
|
687
679
|
}
|
|
688
680
|
ctx.fillStyle = color;
|
|
689
|
-
|
|
690
|
-
const cIndex = caretIndexRef.current;
|
|
691
|
-
const currentScrollX = scrollXRef.current;
|
|
692
|
-
const isCursorVisible = showCursorRef.current;
|
|
693
|
-
if (charPositionsRef.current.length !== text.length + 1) {
|
|
681
|
+
if (charPositionsRef.current.length !== internalValue.length + 1) {
|
|
694
682
|
const positions = [x];
|
|
695
|
-
for (let i = 0;i <
|
|
696
|
-
const w = ctx.measureText(
|
|
683
|
+
for (let i = 0;i < internalValue.length; i++) {
|
|
684
|
+
const w = ctx.measureText(internalValue.slice(0, i + 1)).width;
|
|
697
685
|
positions.push(x + w);
|
|
698
686
|
}
|
|
699
687
|
charPositionsRef.current = positions;
|
|
700
688
|
}
|
|
701
689
|
const charX = charPositionsRef.current;
|
|
702
|
-
const caretPos = charX[
|
|
690
|
+
const caretPos = charX[caretIndex] ?? x;
|
|
703
691
|
const viewLeft = paddingLeft;
|
|
704
692
|
const viewRight = width - paddingRight - borderLeft - borderRight;
|
|
705
693
|
const safety = 2;
|
|
@@ -730,9 +718,9 @@ function VirtualInput({
|
|
|
730
718
|
ctx.fillRect(startX, 0, endX - startX, height);
|
|
731
719
|
ctx.fillStyle = color;
|
|
732
720
|
}
|
|
733
|
-
ctx.fillText(
|
|
734
|
-
if (isFocused &&
|
|
735
|
-
const caretX = charX[
|
|
721
|
+
ctx.fillText(internalValue, x, y);
|
|
722
|
+
if (isFocused && showCursor && !hasSelection) {
|
|
723
|
+
const caretX = charX[caretIndex] ?? x;
|
|
736
724
|
ctx.beginPath();
|
|
737
725
|
ctx.moveTo(caretX, y - fontSize / 2);
|
|
738
726
|
ctx.lineTo(caretX, y + fontSize / 2);
|
|
@@ -740,36 +728,8 @@ function VirtualInput({
|
|
|
740
728
|
ctx.lineWidth = 1.5;
|
|
741
729
|
ctx.stroke();
|
|
742
730
|
}
|
|
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
|
-
}
|
|
771
731
|
ctx.restore();
|
|
772
|
-
}, [isFocused, placeholder, hasSelection, selection
|
|
732
|
+
}, [internalValue, caretIndex, isFocused, showCursor, placeholder, hasSelection, selection]);
|
|
773
733
|
useEffect2(() => {
|
|
774
734
|
let animationFrameId;
|
|
775
735
|
const loop = () => {
|
|
@@ -824,57 +784,12 @@ function VirtualInput({
|
|
|
824
784
|
return internalValue.length;
|
|
825
785
|
}, [internalValue]);
|
|
826
786
|
const handleCanvasClick = useCallback((e) => {
|
|
827
|
-
if (showCopyFeedback)
|
|
828
|
-
return;
|
|
829
787
|
const index = getCharIndexFromX(e.clientX);
|
|
830
788
|
caretIndexRef.current = index;
|
|
831
789
|
setCaretIndex(index);
|
|
832
790
|
clearSelection();
|
|
833
791
|
onFocus(id);
|
|
834
|
-
}, [getCharIndexFromX, clearSelection, onFocus, id
|
|
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]);
|
|
792
|
+
}, [getCharIndexFromX, clearSelection, onFocus, id]);
|
|
878
793
|
const handleKeyDown = useCallback((e) => {
|
|
879
794
|
const currentVal = internalValueRef.current;
|
|
880
795
|
const currentCaret = caretIndexRef.current;
|
|
@@ -882,20 +797,6 @@ function VirtualInput({
|
|
|
882
797
|
setHangulMode(!hangulMode);
|
|
883
798
|
return;
|
|
884
799
|
}
|
|
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
|
-
}
|
|
899
800
|
if (e.shiftKey && (e.key === "ArrowLeft" || e.key === "ArrowRight")) {
|
|
900
801
|
const newIndex = e.key === "ArrowLeft" ? Math.max(0, currentCaret - 1) : Math.min(currentVal.length, currentCaret + 1);
|
|
901
802
|
setSelection((prev) => ({
|
|
@@ -958,17 +859,7 @@ function VirtualInput({
|
|
|
958
859
|
updateValue(finalString, finalCaretIndex + text.length);
|
|
959
860
|
}
|
|
960
861
|
isCompositionRef.current = composing;
|
|
961
|
-
}, [
|
|
962
|
-
hangulMode,
|
|
963
|
-
setHangulMode,
|
|
964
|
-
isCompositionRef,
|
|
965
|
-
shift,
|
|
966
|
-
updateValue,
|
|
967
|
-
hasSelection,
|
|
968
|
-
deleteSelectedText,
|
|
969
|
-
clearSelection,
|
|
970
|
-
selection
|
|
971
|
-
]);
|
|
862
|
+
}, [hangulMode, setHangulMode, isCompositionRef, shift, updateValue, hasSelection, deleteSelectedText, clearSelection]);
|
|
972
863
|
useImperativeHandle(inputRef, () => {
|
|
973
864
|
if (isFocused) {
|
|
974
865
|
return {
|
|
@@ -1005,13 +896,7 @@ function VirtualInput({
|
|
|
1005
896
|
onFocus: () => onFocus(id, containerRef.current),
|
|
1006
897
|
onBlur: (e) => onBlur(e),
|
|
1007
898
|
onKeyDown: handleKeyDown,
|
|
1008
|
-
onContextMenu: (e) => e.preventDefault(),
|
|
1009
899
|
onClick: handleCanvasClick,
|
|
1010
|
-
onPointerDown: handlePointerDown,
|
|
1011
|
-
onPointerMove: handlePointerMove,
|
|
1012
|
-
onPointerUp: handlePointerUp,
|
|
1013
|
-
onPointerLeave: handlePointerUp,
|
|
1014
|
-
onPointerCancel: handlePointerUp,
|
|
1015
900
|
"data-virtual-input": "true",
|
|
1016
901
|
css: `
|
|
1017
902
|
:host {
|
|
@@ -1096,16 +981,11 @@ function useKeypadLayout({
|
|
|
1096
981
|
row.forEach((cell, colIndex) => {
|
|
1097
982
|
const flex = cell.width || 1;
|
|
1098
983
|
const cellW = flex * unitW;
|
|
1099
|
-
const halfGap = gap / 2;
|
|
1100
984
|
bounds.push({
|
|
1101
985
|
x: currentX,
|
|
1102
986
|
y: currentY,
|
|
1103
987
|
w: cellW,
|
|
1104
988
|
h: rowHeight,
|
|
1105
|
-
hitX: currentX - halfGap,
|
|
1106
|
-
hitY: currentY - halfGap,
|
|
1107
|
-
hitW: cellW + gap,
|
|
1108
|
-
hitH: rowHeight + gap,
|
|
1109
989
|
value: cell.value,
|
|
1110
990
|
type: cell.type,
|
|
1111
991
|
label: getTransformedValue(cell),
|
|
@@ -1171,7 +1051,7 @@ function useKeypadInteraction({
|
|
|
1171
1051
|
const canRepeat = press.type && repeatableKeys.has(press.type) || repeatableKeys.has(press.value);
|
|
1172
1052
|
if (!canRepeat)
|
|
1173
1053
|
return;
|
|
1174
|
-
const firstDelay =
|
|
1054
|
+
const firstDelay = 500;
|
|
1175
1055
|
const repeatInterval = 60;
|
|
1176
1056
|
const tick = () => {
|
|
1177
1057
|
const storedPress = activePresses.current.get(press.pointerId);
|
|
@@ -1190,10 +1070,13 @@ function useKeypadInteraction({
|
|
|
1190
1070
|
if (!canvas)
|
|
1191
1071
|
return;
|
|
1192
1072
|
canvas.setPointerCapture(e.pointerId);
|
|
1073
|
+
activePresses.current.forEach((press) => {
|
|
1074
|
+
clearRepeat(press);
|
|
1075
|
+
});
|
|
1193
1076
|
const rect = canvas.getBoundingClientRect();
|
|
1194
1077
|
const x = e.clientX - rect.left;
|
|
1195
1078
|
const y = e.clientY - rect.top;
|
|
1196
|
-
const hitKey = keyBoundsRef.current.find((k) => x >= k.
|
|
1079
|
+
const hitKey = keyBoundsRef.current.find((k) => x >= k.x && x <= k.x + k.w && y >= k.y && y <= k.y + k.h);
|
|
1197
1080
|
if (hitKey) {
|
|
1198
1081
|
const keyIndex = hitKey.rowIndex * 100 + hitKey.colIndex;
|
|
1199
1082
|
dispatchKeyEvent(hitKey.value, hitKey.type);
|
|
@@ -1224,7 +1107,7 @@ function useKeypadInteraction({
|
|
|
1224
1107
|
const rect = canvas.getBoundingClientRect();
|
|
1225
1108
|
const x = e.clientX - rect.left;
|
|
1226
1109
|
const y = e.clientY - rect.top;
|
|
1227
|
-
const hitKey = keyBoundsRef.current.find((k) => x >= k.
|
|
1110
|
+
const hitKey = keyBoundsRef.current.find((k) => x >= k.x && x <= k.x + k.w && y >= k.y && y <= k.y + k.h);
|
|
1228
1111
|
if (hitKey) {
|
|
1229
1112
|
const newKeyIndex = hitKey.rowIndex * 100 + hitKey.colIndex;
|
|
1230
1113
|
if (newKeyIndex !== press.keyIndex) {
|