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