@uiwwsw/virtual-keyboard 0.5.9 → 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 -132
- 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,19 +678,16 @@ function VirtualInput({
|
|
|
686
678
|
return;
|
|
687
679
|
}
|
|
688
680
|
ctx.fillStyle = color;
|
|
689
|
-
|
|
690
|
-
const cIndex = caretIndexRef.current;
|
|
691
|
-
const isCursorVisible = showCursorRef.current;
|
|
692
|
-
if (charPositionsRef.current.length !== text.length + 1) {
|
|
681
|
+
if (charPositionsRef.current.length !== internalValue.length + 1) {
|
|
693
682
|
const positions = [x];
|
|
694
|
-
for (let i = 0;i <
|
|
695
|
-
const w = ctx.measureText(
|
|
683
|
+
for (let i = 0;i < internalValue.length; i++) {
|
|
684
|
+
const w = ctx.measureText(internalValue.slice(0, i + 1)).width;
|
|
696
685
|
positions.push(x + w);
|
|
697
686
|
}
|
|
698
687
|
charPositionsRef.current = positions;
|
|
699
688
|
}
|
|
700
689
|
const charX = charPositionsRef.current;
|
|
701
|
-
const caretPos = charX[
|
|
690
|
+
const caretPos = charX[caretIndex] ?? x;
|
|
702
691
|
const viewLeft = paddingLeft;
|
|
703
692
|
const viewRight = width - paddingRight - borderLeft - borderRight;
|
|
704
693
|
const safety = 2;
|
|
@@ -729,9 +718,9 @@ function VirtualInput({
|
|
|
729
718
|
ctx.fillRect(startX, 0, endX - startX, height);
|
|
730
719
|
ctx.fillStyle = color;
|
|
731
720
|
}
|
|
732
|
-
ctx.fillText(
|
|
733
|
-
if (isFocused &&
|
|
734
|
-
const caretX = charX[
|
|
721
|
+
ctx.fillText(internalValue, x, y);
|
|
722
|
+
if (isFocused && showCursor && !hasSelection) {
|
|
723
|
+
const caretX = charX[caretIndex] ?? x;
|
|
735
724
|
ctx.beginPath();
|
|
736
725
|
ctx.moveTo(caretX, y - fontSize / 2);
|
|
737
726
|
ctx.lineTo(caretX, y + fontSize / 2);
|
|
@@ -739,36 +728,8 @@ function VirtualInput({
|
|
|
739
728
|
ctx.lineWidth = 1.5;
|
|
740
729
|
ctx.stroke();
|
|
741
730
|
}
|
|
742
|
-
if (showCopyFeedback) {
|
|
743
|
-
ctx.save();
|
|
744
|
-
ctx.fillStyle = "rgba(0, 0, 0, 0.7)";
|
|
745
|
-
const msg = "Copied!";
|
|
746
|
-
const msgWidth = ctx.measureText(msg).width + 20;
|
|
747
|
-
const msgHeight = fontSize + 10;
|
|
748
|
-
ctx.translate(scrollXRef.current, 0);
|
|
749
|
-
ctx.restore();
|
|
750
|
-
ctx.save();
|
|
751
|
-
const cx = width / 2;
|
|
752
|
-
const cy = height / 2;
|
|
753
|
-
ctx.fillStyle = "rgba(30, 41, 59, 0.9)";
|
|
754
|
-
const r = 6;
|
|
755
|
-
const rw = msgWidth;
|
|
756
|
-
const rh = msgHeight;
|
|
757
|
-
const rx = cx - rw / 2;
|
|
758
|
-
const ry = cy - rh / 2;
|
|
759
|
-
ctx.beginPath();
|
|
760
|
-
ctx.roundRect(rx, ry, rw, rh, r);
|
|
761
|
-
ctx.fill();
|
|
762
|
-
ctx.fillStyle = "#ffffff";
|
|
763
|
-
ctx.font = `bold ${fontSize * 0.8}px system-ui`;
|
|
764
|
-
ctx.textAlign = "center";
|
|
765
|
-
ctx.textBaseline = "middle";
|
|
766
|
-
ctx.fillText(msg, cx, cy);
|
|
767
|
-
ctx.restore();
|
|
768
|
-
return;
|
|
769
|
-
}
|
|
770
731
|
ctx.restore();
|
|
771
|
-
}, [isFocused, placeholder, hasSelection, selection
|
|
732
|
+
}, [internalValue, caretIndex, isFocused, showCursor, placeholder, hasSelection, selection]);
|
|
772
733
|
useEffect2(() => {
|
|
773
734
|
let animationFrameId;
|
|
774
735
|
const loop = () => {
|
|
@@ -823,57 +784,12 @@ function VirtualInput({
|
|
|
823
784
|
return internalValue.length;
|
|
824
785
|
}, [internalValue]);
|
|
825
786
|
const handleCanvasClick = useCallback((e) => {
|
|
826
|
-
if (showCopyFeedback)
|
|
827
|
-
return;
|
|
828
787
|
const index = getCharIndexFromX(e.clientX);
|
|
829
788
|
caretIndexRef.current = index;
|
|
830
789
|
setCaretIndex(index);
|
|
831
790
|
clearSelection();
|
|
832
791
|
onFocus(id);
|
|
833
|
-
}, [getCharIndexFromX, clearSelection, onFocus, id
|
|
834
|
-
const cancelLongPress = useCallback(() => {
|
|
835
|
-
if (longPressTimerRef.current) {
|
|
836
|
-
clearTimeout(longPressTimerRef.current);
|
|
837
|
-
longPressTimerRef.current = null;
|
|
838
|
-
}
|
|
839
|
-
pointerStartedRef.current = false;
|
|
840
|
-
pointerStartPositionRef.current = null;
|
|
841
|
-
}, []);
|
|
842
|
-
const handlePointerDown = useCallback((e) => {
|
|
843
|
-
if (e.button !== 0)
|
|
844
|
-
return;
|
|
845
|
-
cancelLongPress();
|
|
846
|
-
pointerStartedRef.current = true;
|
|
847
|
-
pointerStartPositionRef.current = { x: e.clientX, y: e.clientY };
|
|
848
|
-
longPressTimerRef.current = window.setTimeout(async () => {
|
|
849
|
-
if (!pointerStartedRef.current)
|
|
850
|
-
return;
|
|
851
|
-
pointerStartedRef.current = false;
|
|
852
|
-
try {
|
|
853
|
-
if (internalValueRef.current) {
|
|
854
|
-
await navigator.clipboard.writeText(internalValueRef.current);
|
|
855
|
-
if (navigator.vibrate)
|
|
856
|
-
navigator.vibrate(50);
|
|
857
|
-
setShowCopyFeedback(true);
|
|
858
|
-
setTimeout(() => setShowCopyFeedback(false), 1500);
|
|
859
|
-
}
|
|
860
|
-
} catch (err) {
|
|
861
|
-
console.error("Failed to copy", err);
|
|
862
|
-
}
|
|
863
|
-
}, 600);
|
|
864
|
-
}, [cancelLongPress]);
|
|
865
|
-
const handlePointerMove = useCallback((e) => {
|
|
866
|
-
if (!pointerStartedRef.current || !pointerStartPositionRef.current)
|
|
867
|
-
return;
|
|
868
|
-
const dx = Math.abs(e.clientX - pointerStartPositionRef.current.x);
|
|
869
|
-
const dy = Math.abs(e.clientY - pointerStartPositionRef.current.y);
|
|
870
|
-
if (dx > 10 || dy > 10) {
|
|
871
|
-
cancelLongPress();
|
|
872
|
-
}
|
|
873
|
-
}, [cancelLongPress]);
|
|
874
|
-
const handlePointerUp = useCallback(() => {
|
|
875
|
-
cancelLongPress();
|
|
876
|
-
}, [cancelLongPress]);
|
|
792
|
+
}, [getCharIndexFromX, clearSelection, onFocus, id]);
|
|
877
793
|
const handleKeyDown = useCallback((e) => {
|
|
878
794
|
const currentVal = internalValueRef.current;
|
|
879
795
|
const currentCaret = caretIndexRef.current;
|
|
@@ -881,20 +797,6 @@ function VirtualInput({
|
|
|
881
797
|
setHangulMode(!hangulMode);
|
|
882
798
|
return;
|
|
883
799
|
}
|
|
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);
|
|
894
|
-
e.preventDefault();
|
|
895
|
-
return;
|
|
896
|
-
}
|
|
897
|
-
}
|
|
898
800
|
if (e.shiftKey && (e.key === "ArrowLeft" || e.key === "ArrowRight")) {
|
|
899
801
|
const newIndex = e.key === "ArrowLeft" ? Math.max(0, currentCaret - 1) : Math.min(currentVal.length, currentCaret + 1);
|
|
900
802
|
setSelection((prev) => ({
|
|
@@ -957,17 +859,7 @@ function VirtualInput({
|
|
|
957
859
|
updateValue(finalString, finalCaretIndex + text.length);
|
|
958
860
|
}
|
|
959
861
|
isCompositionRef.current = composing;
|
|
960
|
-
}, [
|
|
961
|
-
hangulMode,
|
|
962
|
-
setHangulMode,
|
|
963
|
-
isCompositionRef,
|
|
964
|
-
shift,
|
|
965
|
-
updateValue,
|
|
966
|
-
hasSelection,
|
|
967
|
-
deleteSelectedText,
|
|
968
|
-
clearSelection,
|
|
969
|
-
selection
|
|
970
|
-
]);
|
|
862
|
+
}, [hangulMode, setHangulMode, isCompositionRef, shift, updateValue, hasSelection, deleteSelectedText, clearSelection]);
|
|
971
863
|
useImperativeHandle(inputRef, () => {
|
|
972
864
|
if (isFocused) {
|
|
973
865
|
return {
|
|
@@ -1004,13 +896,7 @@ function VirtualInput({
|
|
|
1004
896
|
onFocus: () => onFocus(id, containerRef.current),
|
|
1005
897
|
onBlur: (e) => onBlur(e),
|
|
1006
898
|
onKeyDown: handleKeyDown,
|
|
1007
|
-
onContextMenu: (e) => e.preventDefault(),
|
|
1008
899
|
onClick: handleCanvasClick,
|
|
1009
|
-
onPointerDown: handlePointerDown,
|
|
1010
|
-
onPointerMove: handlePointerMove,
|
|
1011
|
-
onPointerUp: handlePointerUp,
|
|
1012
|
-
onPointerLeave: handlePointerUp,
|
|
1013
|
-
onPointerCancel: handlePointerUp,
|
|
1014
900
|
"data-virtual-input": "true",
|
|
1015
901
|
css: `
|
|
1016
902
|
:host {
|
|
@@ -1095,16 +981,11 @@ function useKeypadLayout({
|
|
|
1095
981
|
row.forEach((cell, colIndex) => {
|
|
1096
982
|
const flex = cell.width || 1;
|
|
1097
983
|
const cellW = flex * unitW;
|
|
1098
|
-
const halfGap = gap / 2;
|
|
1099
984
|
bounds.push({
|
|
1100
985
|
x: currentX,
|
|
1101
986
|
y: currentY,
|
|
1102
987
|
w: cellW,
|
|
1103
988
|
h: rowHeight,
|
|
1104
|
-
hitX: currentX - halfGap,
|
|
1105
|
-
hitY: currentY - halfGap,
|
|
1106
|
-
hitW: cellW + gap,
|
|
1107
|
-
hitH: rowHeight + gap,
|
|
1108
989
|
value: cell.value,
|
|
1109
990
|
type: cell.type,
|
|
1110
991
|
label: getTransformedValue(cell),
|
|
@@ -1170,7 +1051,7 @@ function useKeypadInteraction({
|
|
|
1170
1051
|
const canRepeat = press.type && repeatableKeys.has(press.type) || repeatableKeys.has(press.value);
|
|
1171
1052
|
if (!canRepeat)
|
|
1172
1053
|
return;
|
|
1173
|
-
const firstDelay =
|
|
1054
|
+
const firstDelay = 500;
|
|
1174
1055
|
const repeatInterval = 60;
|
|
1175
1056
|
const tick = () => {
|
|
1176
1057
|
const storedPress = activePresses.current.get(press.pointerId);
|
|
@@ -1189,10 +1070,13 @@ function useKeypadInteraction({
|
|
|
1189
1070
|
if (!canvas)
|
|
1190
1071
|
return;
|
|
1191
1072
|
canvas.setPointerCapture(e.pointerId);
|
|
1073
|
+
activePresses.current.forEach((press) => {
|
|
1074
|
+
clearRepeat(press);
|
|
1075
|
+
});
|
|
1192
1076
|
const rect = canvas.getBoundingClientRect();
|
|
1193
1077
|
const x = e.clientX - rect.left;
|
|
1194
1078
|
const y = e.clientY - rect.top;
|
|
1195
|
-
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);
|
|
1196
1080
|
if (hitKey) {
|
|
1197
1081
|
const keyIndex = hitKey.rowIndex * 100 + hitKey.colIndex;
|
|
1198
1082
|
dispatchKeyEvent(hitKey.value, hitKey.type);
|
|
@@ -1223,7 +1107,7 @@ function useKeypadInteraction({
|
|
|
1223
1107
|
const rect = canvas.getBoundingClientRect();
|
|
1224
1108
|
const x = e.clientX - rect.left;
|
|
1225
1109
|
const y = e.clientY - rect.top;
|
|
1226
|
-
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);
|
|
1227
1111
|
if (hitKey) {
|
|
1228
1112
|
const newKeyIndex = hitKey.rowIndex * 100 + hitKey.colIndex;
|
|
1229
1113
|
if (newKeyIndex !== press.keyIndex) {
|