@uiwwsw/virtual-keyboard 0.4.1 → 0.5.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
@@ -1,14 +1,60 @@
1
1
  // src/components/Input.tsx
2
2
  import {
3
- useState as useState2,
4
- useMemo,
5
3
  useCallback,
6
- useId,
7
- useImperativeHandle,
8
4
  useEffect as useEffect2,
9
- useRef
5
+ useMemo,
6
+ useRef,
7
+ useState as useState2,
8
+ useImperativeHandle,
9
+ useId
10
10
  } from "react";
11
11
 
12
+ // src/components/ShadowWrapper.tsx
13
+ import { useEffect, useState, memo } from "react";
14
+ import ReactDOM from "react-dom";
15
+ import { jsxDEV, Fragment } from "react/jsx-dev-runtime";
16
+ var ShadowWrapper = memo(({
17
+ children,
18
+ css,
19
+ tagName
20
+ }) => {
21
+ const [host, setHost] = useState(null);
22
+ const [shadowRoot, setShadowRoot] = useState(null);
23
+ const Component = tagName;
24
+ useEffect(() => {
25
+ if (host && !shadowRoot) {
26
+ const shadow = host.attachShadow({ mode: "closed" });
27
+ setShadowRoot(shadow);
28
+ }
29
+ }, [host, shadowRoot]);
30
+ return /* @__PURE__ */ jsxDEV(Component, {
31
+ ref: setHost,
32
+ children: shadowRoot && ReactDOM.createPortal(/* @__PURE__ */ jsxDEV(Fragment, {
33
+ children: [
34
+ css && /* @__PURE__ */ jsxDEV("style", {
35
+ children: css
36
+ }, undefined, false, undefined, this),
37
+ children
38
+ ]
39
+ }, undefined, true, undefined, this), shadowRoot)
40
+ }, undefined, false, undefined, this);
41
+ });
42
+ ShadowWrapper.displayName = "ShadowWrapper";
43
+
44
+ // src/components/Context.tsx
45
+ import {
46
+ createContext,
47
+ useContext
48
+ } from "react";
49
+ var VirtualInputContext = createContext(null);
50
+ function useVirtualInputContext() {
51
+ const context = useContext(VirtualInputContext);
52
+ if (!context) {
53
+ throw new Error("Input must be used within an <VirtualInputProvider>. Wrap your app with <VirtualInputProvider>.");
54
+ }
55
+ return context;
56
+ }
57
+
12
58
  // node_modules/es-hangul/dist/index.mjs
13
59
  function excludeLastElement(array) {
14
60
  const lastElement = array[array.length - 1];
@@ -462,65 +508,6 @@ function isHangul(char) {
462
508
  return /^[\u1100-\u11FF\u3130-\u318F\uAC00-\uD7AF]$/.test(char);
463
509
  }
464
510
 
465
- // src/components/ShadowWrapper.tsx
466
- import { useEffect, useState, memo } from "react";
467
- import ReactDOM from "react-dom";
468
- import { jsxDEV, Fragment } from "react/jsx-dev-runtime";
469
- var ShadowWrapper = memo(({
470
- children,
471
- css,
472
- tagName
473
- }) => {
474
- const [host, setHost] = useState(null);
475
- const [shadowRoot, setShadowRoot] = useState(null);
476
- const Component = tagName;
477
- useEffect(() => {
478
- if (host && !shadowRoot) {
479
- const shadow = host.attachShadow({ mode: "closed" });
480
- setShadowRoot(shadow);
481
- }
482
- }, [host, shadowRoot]);
483
- return /* @__PURE__ */ jsxDEV(Component, {
484
- ref: setHost,
485
- children: shadowRoot && ReactDOM.createPortal(/* @__PURE__ */ jsxDEV(Fragment, {
486
- children: [
487
- css && /* @__PURE__ */ jsxDEV("style", {
488
- children: css
489
- }, undefined, false, undefined, this),
490
- children
491
- ]
492
- }, undefined, true, undefined, this), shadowRoot)
493
- }, undefined, false, undefined, this);
494
- });
495
- ShadowWrapper.displayName = "ShadowWrapper";
496
-
497
- // src/components/BlinkingCaret.tsx
498
- import { jsxDEV as jsxDEV2 } from "react/jsx-dev-runtime";
499
- function BlinkingCaret() {
500
- return /* @__PURE__ */ jsxDEV2("span", {
501
- className: "blink",
502
- style: {
503
- margin: "0 -0.12em",
504
- position: "absolute"
505
- },
506
- children: "|"
507
- }, undefined, false, undefined, this);
508
- }
509
-
510
- // src/components/Context.tsx
511
- import {
512
- createContext,
513
- useContext
514
- } from "react";
515
- var VirtualInputContext = createContext(null);
516
- function useVirtualInputContext() {
517
- const context = useContext(VirtualInputContext);
518
- if (!context) {
519
- throw new Error("Input must be used within an <VirtualInputProvider>. Wrap your app with <VirtualInputProvider>.");
520
- }
521
- return context;
522
- }
523
-
524
511
  // src/utils/parseKeyInput.ts
525
512
  function parseKeyInput(e, hangulMode) {
526
513
  const isMac = navigator.userAgent.includes("Mac");
@@ -576,7 +563,7 @@ function parseKeyInput(e, hangulMode) {
576
563
  }
577
564
 
578
565
  // src/components/Input.tsx
579
- import { jsxDEV as jsxDEV3 } from "react/jsx-dev-runtime";
566
+ import { jsxDEV as jsxDEV2 } from "react/jsx-dev-runtime";
580
567
  function VirtualInput({
581
568
  value: controlledValue,
582
569
  defaultValue,
@@ -585,28 +572,16 @@ function VirtualInput({
585
572
  ...props
586
573
  }) {
587
574
  const id = useId();
575
+ const canvasRef = useRef(null);
576
+ const containerRef = useRef(null);
588
577
  const [internalValue, setInternalValue] = useState2(controlledValue ?? defaultValue ?? "");
589
578
  const [caretIndex, setCaretIndex] = useState2(() => (controlledValue ?? defaultValue ?? "").length);
590
579
  const [selection, setSelection] = useState2({ start: null, end: null });
591
- const divRef = useRef(null);
592
- const letters = useMemo(() => internalValue.split(""), [internalValue]);
593
- const updateValue = useCallback((newValue, newCaretIndex) => {
594
- if (controlledValue === undefined) {
595
- setInternalValue(newValue);
596
- }
597
- const fakeInput = { value: newValue };
598
- const changeEvent = {
599
- target: fakeInput,
600
- currentTarget: fakeInput
601
- };
602
- onChange?.(changeEvent);
603
- setCaretIndex(newCaretIndex);
604
- }, [controlledValue, onChange]);
580
+ const [showCursor, setShowCursor] = useState2(true);
581
+ const charPositionsRef = useRef([]);
605
582
  useEffect2(() => {
606
- if (controlledValue !== undefined && controlledValue !== internalValue) {
607
- setInternalValue(controlledValue);
608
- }
609
- }, [controlledValue, internalValue]);
583
+ charPositionsRef.current = [];
584
+ }, [internalValue]);
610
585
  const {
611
586
  focusId,
612
587
  onFocus,
@@ -618,17 +593,31 @@ function VirtualInput({
618
593
  shift
619
594
  } = useVirtualInputContext();
620
595
  const isFocused = focusId === id;
621
- const selectionStart = selection.start;
622
- const selectionEnd = selection.end;
623
- const hasSelection = useMemo(() => selectionStart !== null && selectionEnd !== null && selectionStart !== selectionEnd, [selectionStart, selectionEnd]);
596
+ useEffect2(() => {
597
+ if (controlledValue !== undefined && controlledValue !== internalValue) {
598
+ setInternalValue(controlledValue);
599
+ }
600
+ }, [controlledValue, internalValue]);
601
+ useEffect2(() => {
602
+ if (!isFocused)
603
+ return;
604
+ const interval = setInterval(() => {
605
+ setShowCursor((prev) => !prev);
606
+ }, 530);
607
+ return () => clearInterval(interval);
608
+ }, [isFocused]);
609
+ useEffect2(() => {
610
+ setShowCursor(true);
611
+ }, [caretIndex, internalValue]);
612
+ const hasSelection = useMemo(() => selection.start !== null && selection.end !== null && selection.start !== selection.end, [selection]);
624
613
  const clearSelection = useCallback(() => {
625
614
  setSelection({ start: null, end: null });
626
615
  }, []);
627
616
  const deleteSelectedText = useCallback(() => {
628
- if (!hasSelection || selectionStart === null || selectionEnd === null) {
617
+ if (!hasSelection || selection.start === null || selection.end === null) {
629
618
  return { newString: internalValue, finalCaretIndex: caretIndex };
630
619
  }
631
- const [start, end] = [selectionStart, selectionEnd].sort((a, b) => a - b);
620
+ const [start, end] = [selection.start, selection.end].sort((a, b) => a - b);
632
621
  const newString = internalValue.slice(0, start) + internalValue.slice(end);
633
622
  clearSelection();
634
623
  return { newString, finalCaretIndex: start };
@@ -636,287 +625,249 @@ function VirtualInput({
636
625
  internalValue,
637
626
  caretIndex,
638
627
  hasSelection,
639
- selectionStart,
640
- selectionEnd,
628
+ selection,
641
629
  clearSelection
642
630
  ]);
643
- const handlePaste = useCallback((e) => {
644
- e.preventDefault();
645
- const pastedText = e.clipboardData.getData("text/plain");
646
- if (!pastedText)
631
+ const draw = useCallback(() => {
632
+ const canvas = canvasRef.current;
633
+ const container = containerRef.current;
634
+ if (!canvas || !container)
647
635
  return;
648
- const { newString, finalCaretIndex } = hasSelection ? deleteSelectedText() : { newString: internalValue, finalCaretIndex: caretIndex };
649
- const finalString = newString.slice(0, finalCaretIndex) + pastedText + newString.slice(finalCaretIndex);
650
- const newCaretPosition = finalCaretIndex + pastedText.length;
651
- updateValue(finalString, newCaretPosition);
652
- clearSelection();
653
- }, [
654
- internalValue,
655
- caretIndex,
656
- hasSelection,
657
- clearSelection,
658
- deleteSelectedText,
659
- updateValue
660
- ]);
661
- const handleKeyDown = useCallback((e) => {
662
- if ((e.ctrlKey || e.metaKey) && e.key.toLowerCase() === "a") {
663
- e.preventDefault();
664
- setSelection({ start: 0, end: letters.length });
636
+ const ctx = canvas.getContext("2d");
637
+ if (!ctx)
665
638
  return;
639
+ const dpr = window.devicePixelRatio || 1;
640
+ const rect = container.getBoundingClientRect();
641
+ if (canvas.width !== rect.width * dpr || canvas.height !== rect.height * dpr) {
642
+ canvas.width = rect.width * dpr;
643
+ canvas.height = rect.height * dpr;
644
+ ctx.scale(dpr, dpr);
645
+ charPositionsRef.current = [];
666
646
  }
667
- if (e.key === "HangulMode") {
668
- setHangulMode(!hangulMode);
647
+ const width = rect.width;
648
+ const height = rect.height;
649
+ ctx.clearRect(0, 0, width, height);
650
+ const computedStyle = window.getComputedStyle(container);
651
+ const font = `${computedStyle.fontSize} ${computedStyle.fontFamily}`;
652
+ ctx.font = font;
653
+ ctx.textBaseline = "middle";
654
+ const fontSize = parseFloat(computedStyle.fontSize);
655
+ const y = height / 2;
656
+ const x = 0;
657
+ if (!internalValue && placeholder) {
658
+ ctx.fillStyle = "#aaa";
659
+ ctx.fillText(placeholder, x, y);
660
+ if (isFocused && showCursor) {
661
+ ctx.beginPath();
662
+ ctx.moveTo(x, y - fontSize / 2);
663
+ ctx.lineTo(x, y + fontSize / 2);
664
+ ctx.strokeStyle = "black";
665
+ ctx.lineWidth = 1.5;
666
+ ctx.stroke();
667
+ }
669
668
  return;
670
669
  }
671
- if ((e.ctrlKey || e.metaKey) && e.key.toLowerCase() === "c") {
672
- if (hasSelection && selectionStart !== null && selectionEnd !== null) {
673
- e.preventDefault();
674
- const [start, end] = [selectionStart, selectionEnd].sort((a, b) => a - b);
675
- const textToCopy = letters.slice(start, end).join("");
676
- navigator.clipboard.writeText(textToCopy).catch((err) => {
677
- console.error("Failed to copy text: ", err);
678
- });
670
+ ctx.fillStyle = "black";
671
+ if (charPositionsRef.current.length !== internalValue.length + 1) {
672
+ let currentX = x;
673
+ const positions = [x];
674
+ for (let i = 0;i < internalValue.length; i++) {
675
+ const w = ctx.measureText(internalValue[i]).width;
676
+ currentX += w;
677
+ positions.push(currentX);
678
+ }
679
+ charPositionsRef.current = positions;
680
+ }
681
+ const charX = charPositionsRef.current;
682
+ if (hasSelection && selection.start !== null && selection.end !== null) {
683
+ const [start, end] = [selection.start, selection.end].sort((a, b) => a - b);
684
+ const startX = charX[start] ?? x;
685
+ const endX = charX[end] ?? charX[charX.length - 1];
686
+ ctx.fillStyle = "#b4d5fe";
687
+ ctx.fillRect(startX, 0, endX - startX, height);
688
+ ctx.fillStyle = "black";
689
+ }
690
+ ctx.fillText(internalValue, x, y);
691
+ if (isFocused && showCursor && !hasSelection) {
692
+ const caretX = charX[caretIndex] ?? x;
693
+ ctx.beginPath();
694
+ ctx.moveTo(caretX, y - fontSize / 2);
695
+ ctx.lineTo(caretX, y + fontSize / 2);
696
+ ctx.strokeStyle = "black";
697
+ ctx.lineWidth = 1.5;
698
+ ctx.stroke();
699
+ }
700
+ }, [internalValue, caretIndex, isFocused, showCursor, placeholder, hasSelection, selection]);
701
+ useEffect2(() => {
702
+ let animationFrameId;
703
+ const loop = () => {
704
+ draw();
705
+ animationFrameId = requestAnimationFrame(loop);
706
+ };
707
+ loop();
708
+ return () => cancelAnimationFrame(animationFrameId);
709
+ }, [draw]);
710
+ const updateValue = useCallback((newValue, newCaretIndex) => {
711
+ if (controlledValue === undefined) {
712
+ setInternalValue(newValue);
713
+ }
714
+ const fakeInput = { value: newValue };
715
+ const changeEvent = {
716
+ target: fakeInput,
717
+ currentTarget: fakeInput
718
+ };
719
+ onChange?.(changeEvent);
720
+ setCaretIndex(newCaretIndex);
721
+ }, [controlledValue, onChange]);
722
+ const getCharIndexFromX = useCallback((clientX) => {
723
+ const canvas = canvasRef.current;
724
+ if (!canvas)
725
+ return 0;
726
+ const rect = canvas.getBoundingClientRect();
727
+ const x = clientX - rect.left;
728
+ if (charPositionsRef.current.length === internalValue.length + 1) {
729
+ const charX = charPositionsRef.current;
730
+ for (let i = 0;i < charX.length - 1; i++) {
731
+ const center = (charX[i] + charX[i + 1]) / 2;
732
+ if (x < center)
733
+ return i;
679
734
  }
735
+ return internalValue.length;
736
+ }
737
+ const ctx = canvas.getContext("2d");
738
+ if (!ctx)
739
+ return 0;
740
+ const computedStyle = window.getComputedStyle(containerRef.current);
741
+ ctx.font = `${computedStyle.fontSize} ${computedStyle.fontFamily}`;
742
+ let currentX = 0;
743
+ for (let i = 0;i < internalValue.length; i++) {
744
+ const w = ctx.measureText(internalValue[i]).width;
745
+ if (x < currentX + w / 2) {
746
+ return i;
747
+ }
748
+ currentX += w;
749
+ }
750
+ return internalValue.length;
751
+ }, [internalValue]);
752
+ const handleCanvasClick = useCallback((e) => {
753
+ const index = getCharIndexFromX(e.clientX);
754
+ setCaretIndex(index);
755
+ clearSelection();
756
+ onFocus(id);
757
+ }, [getCharIndexFromX, clearSelection, onFocus, id]);
758
+ const handleKeyDown = useCallback((e) => {
759
+ if (e.key === "HangulMode") {
760
+ setHangulMode(!hangulMode);
680
761
  return;
681
762
  }
682
- if ((e.ctrlKey || e.metaKey) && (e.key.toLowerCase() === "c" || e.key.toLowerCase() === "v"))
763
+ if (e.shiftKey && (e.key === "ArrowLeft" || e.key === "ArrowRight")) {
764
+ const newIndex = e.key === "ArrowLeft" ? Math.max(0, caretIndex - 1) : Math.min(internalValue.length, caretIndex + 1);
765
+ setSelection((prev) => ({
766
+ start: prev.start ?? caretIndex,
767
+ end: newIndex
768
+ }));
769
+ setCaretIndex(newIndex);
683
770
  return;
771
+ }
684
772
  switch (e.key) {
773
+ case "ArrowLeft":
774
+ setCaretIndex((prev) => Math.max(0, prev - 1));
775
+ clearSelection();
776
+ return;
777
+ case "ArrowRight":
778
+ setCaretIndex((prev) => Math.min(internalValue.length, prev + 1));
779
+ clearSelection();
780
+ return;
685
781
  case "Backspace": {
686
782
  isCompositionRef.current = false;
687
783
  if (hasSelection) {
688
- const { newString, finalCaretIndex } = deleteSelectedText();
689
- updateValue(newString, finalCaretIndex);
784
+ const { newString: newString2, finalCaretIndex: finalCaretIndex2 } = deleteSelectedText();
785
+ updateValue(newString2, finalCaretIndex2);
690
786
  } else if (caretIndex > 0) {
691
- const newString = internalValue.slice(0, caretIndex - 1) + internalValue.slice(caretIndex);
692
- updateValue(newString, caretIndex - 1);
693
- }
694
- break;
695
- }
696
- case "Delete": {
697
- isCompositionRef.current = false;
698
- if (hasSelection) {
699
- const { newString, finalCaretIndex } = deleteSelectedText();
700
- updateValue(newString, finalCaretIndex);
701
- } else if (caretIndex < letters.length) {
702
- const newString = internalValue.slice(0, caretIndex) + internalValue.slice(caretIndex + 1);
703
- updateValue(newString, caretIndex);
704
- }
705
- break;
706
- }
707
- case "ArrowLeft": {
708
- isCompositionRef.current = false;
709
- const newCaretIndex = Math.max(0, caretIndex - 1);
710
- if (e.shiftKey) {
711
- setSelection((prev) => ({
712
- start: prev.start ?? caretIndex,
713
- end: newCaretIndex
714
- }));
715
- } else {
716
- clearSelection();
787
+ const newString2 = internalValue.slice(0, caretIndex - 1) + internalValue.slice(caretIndex);
788
+ updateValue(newString2, caretIndex - 1);
717
789
  }
718
- setCaretIndex(newCaretIndex);
719
- break;
720
- }
721
- case "ArrowRight": {
722
- isCompositionRef.current = false;
723
- const newCaretIndex = Math.min(letters.length, caretIndex + 1);
724
- if (e.shiftKey) {
725
- setSelection((prev) => ({
726
- start: prev.start ?? caretIndex,
727
- end: newCaretIndex
728
- }));
729
- } else {
730
- clearSelection();
731
- }
732
- setCaretIndex(newCaretIndex);
733
- break;
734
- }
735
- case "Unidentified":
736
- case "Shift":
737
- case "Control":
738
- case "Alt":
739
- case "Meta":
740
- case "CapsLock":
741
- case "Enter":
742
- case "Tab":
743
- case "ArrowUp":
744
- case "ArrowDown":
745
- e.stopPropagation();
746
- break;
747
- case "End": {
748
- const newCaretIndex = letters.length;
749
- if (e.shiftKey) {
750
- setSelection((prev) => ({
751
- ...prev,
752
- end: newCaretIndex
753
- }));
754
- }
755
- setCaretIndex(newCaretIndex);
756
- break;
757
- }
758
- default: {
759
- const result = parseKeyInput(e, hangulMode);
760
- if (result.toggleHangulMode) {
761
- setHangulMode(!hangulMode);
762
- return;
763
- }
764
- if (!result.handled || !result.text)
765
- return;
766
- const { composing } = result;
767
- let { text } = result;
768
- if (shift && text.length === 1 && text.match(/[a-z]/i)) {
769
- text = text.toUpperCase();
770
- }
771
- const { newString, finalCaretIndex } = hasSelection ? deleteSelectedText() : { newString: internalValue, finalCaretIndex: caretIndex };
772
- const prevChar = newString[finalCaretIndex - 1];
773
- if (composing && isHangul(prevChar) && isCompositionRef.current) {
774
- const combined = assemble([prevChar, text]);
775
- const finalString = newString.slice(0, finalCaretIndex - 1) + combined + newString.slice(finalCaretIndex);
776
- updateValue(finalString, finalCaretIndex - 1 + combined.length);
777
- } else {
778
- const finalString = newString.slice(0, finalCaretIndex) + text + newString.slice(finalCaretIndex);
779
- updateValue(finalString, finalCaretIndex + text.length);
780
- }
781
- isCompositionRef.current = composing;
782
- break;
790
+ return;
783
791
  }
784
792
  }
785
- }, [
786
- letters,
787
- caretIndex,
788
- hasSelection,
789
- selectionStart,
790
- selectionEnd,
791
- clearSelection,
792
- deleteSelectedText,
793
- hangulMode,
794
- setHangulMode,
795
- isCompositionRef,
796
- shift,
797
- updateValue,
798
- internalValue
799
- ]);
800
- const handleFocus = useCallback(() => {
801
- onFocus(id);
802
- }, [onFocus, id]);
803
- const handleBlur = useCallback(() => {
804
- onBlur();
805
- }, [onBlur]);
806
- const handleClickWrap = useCallback(() => {
807
- clearSelection();
808
- setCaretIndex(letters.length);
809
- }, [clearSelection, letters.length]);
810
- const handleClickLetter = useCallback((index) => {
811
- clearSelection();
812
- setCaretIndex(index);
813
- }, [clearSelection]);
814
- const isSelected = useCallback((index) => {
815
- if (!hasSelection || selectionStart === null || selectionEnd === null)
816
- return false;
817
- const [start, end] = [selectionStart, selectionEnd].sort((a, b) => a - b);
818
- return index >= start && index < end;
819
- }, [hasSelection, selectionStart, selectionEnd]);
820
- const handleClickLetterEvent = (e) => {
821
- e.stopPropagation();
822
- handleClickLetter(Number(e.currentTarget.dataset.value));
823
- };
793
+ const result = parseKeyInput(e, hangulMode);
794
+ if (result.toggleHangulMode) {
795
+ setHangulMode(!hangulMode);
796
+ return;
797
+ }
798
+ if (!result.handled || !result.text)
799
+ return;
800
+ if (e.ctrlKey || e.metaKey || e.altKey)
801
+ return;
802
+ const { composing } = result;
803
+ let { text } = result;
804
+ if (shift && text.length === 1 && text.match(/[a-z]/i)) {
805
+ text = text.toUpperCase();
806
+ }
807
+ const { newString, finalCaretIndex } = hasSelection ? deleteSelectedText() : { newString: internalValue, finalCaretIndex: caretIndex };
808
+ const prevChar = newString[finalCaretIndex - 1];
809
+ if (composing && isHangul(prevChar) && isCompositionRef.current) {
810
+ const combined = assemble([prevChar, text]);
811
+ const finalString = newString.slice(0, finalCaretIndex - 1) + combined + newString.slice(finalCaretIndex);
812
+ updateValue(finalString, finalCaretIndex - 1 + combined.length);
813
+ } else {
814
+ const finalString = newString.slice(0, finalCaretIndex) + text + newString.slice(finalCaretIndex);
815
+ updateValue(finalString, finalCaretIndex + text.length);
816
+ }
817
+ isCompositionRef.current = composing;
818
+ }, [internalValue, caretIndex, hangulMode, setHangulMode, isCompositionRef, shift, updateValue, hasSelection, deleteSelectedText, clearSelection]);
824
819
  useImperativeHandle(inputRef, () => {
825
820
  if (isFocused) {
826
- return {
827
- handleKeyDown
828
- };
821
+ return { handleKeyDown };
829
822
  }
830
823
  return inputRef.current;
831
824
  }, [isFocused, handleKeyDown, inputRef]);
832
- return /* @__PURE__ */ jsxDEV3(ShadowWrapper, {
833
- tagName: "virtual-input",
825
+ return /* @__PURE__ */ jsxDEV2(ShadowWrapper, {
826
+ tagName: "virtual-input-canvas",
834
827
  css: `
835
- .wrap {
836
- white-space: pre;
837
- position: relative;
838
- cursor: text;
839
- border-radius: 4px;
840
- user-select: none;
841
- }
842
- .wrap::after {
843
- content: " ";
844
- }
845
- .placeholder {
846
- position: absolute;
847
- left: 0;
848
- top: 0;
849
- color: #aaa;
850
- pointer-events: none;
851
- }
852
- .letter {
853
- position: relative;
854
- outline: none;
855
- }
856
- .letter.selected {
857
- background-color: #b4d5fe;
858
- }
859
- @keyframes blink {
860
- 0%,
861
- 49% {
862
- opacity: 1;
863
- }
864
- 50%,
865
- 100% {
866
- opacity: 0;
867
- }
868
- }
869
- .blink {
870
- animation: blink 1s step-start infinite;
871
- }
872
- `,
873
- children: /* @__PURE__ */ jsxDEV3("div", {
828
+ canvas {
829
+ width: 100%;
830
+ height: 100%;
831
+ cursor: text;
832
+ display: block;
833
+ }
834
+ `,
835
+ children: /* @__PURE__ */ jsxDEV2("div", {
836
+ ref: containerRef,
874
837
  ...props,
875
- ref: divRef,
876
- className: "wrap",
877
838
  role: "textbox",
878
- "aria-placeholder": placeholder,
879
839
  tabIndex: 0,
880
840
  style: {
881
841
  minHeight: "1.5em",
882
- lineHeight: "1.5em",
842
+ height: "1.5em",
883
843
  outline: "none",
884
844
  font: "inherit",
885
- width: "100%"
845
+ width: "100%",
846
+ position: "relative"
886
847
  },
887
- onFocus: handleFocus,
888
- onBlur: handleBlur,
848
+ onFocus: () => onFocus(id),
849
+ onBlur,
889
850
  onKeyDown: handleKeyDown,
890
- onClick: handleClickWrap,
891
- onPaste: handlePaste,
892
- children: [
893
- !internalValue && placeholder && /* @__PURE__ */ jsxDEV3("span", {
894
- className: "placeholder",
895
- children: placeholder
896
- }, undefined, false, undefined, this),
897
- letters.map((char, i) => /* @__PURE__ */ jsxDEV3("span", {
898
- children: [
899
- caretIndex === i && isFocused && !hasSelection && /* @__PURE__ */ jsxDEV3(BlinkingCaret, {}, undefined, false, undefined, this),
900
- /* @__PURE__ */ jsxDEV3("span", {
901
- role: "button",
902
- "data-value": i,
903
- onClick: handleClickLetterEvent,
904
- className: `letter ${isSelected(i) ? "selected" : ""}`,
905
- children: char
906
- }, undefined, false, undefined, this)
907
- ]
908
- }, `char-${char}-${i}`, true, undefined, this)),
909
- caretIndex === letters.length && isFocused && !hasSelection && /* @__PURE__ */ jsxDEV3(BlinkingCaret, {}, undefined, false, undefined, this)
910
- ]
911
- }, undefined, true, undefined, this)
851
+ onClick: handleCanvasClick,
852
+ children: /* @__PURE__ */ jsxDEV2("canvas", {
853
+ ref: canvasRef
854
+ }, undefined, false, undefined, this)
855
+ }, undefined, false, undefined, this)
912
856
  }, undefined, false, undefined, this);
913
857
  }
914
858
  // src/components/Provider.tsx
915
859
  import {
916
860
  useState as useState5,
917
- useRef as useRef2,
861
+ useRef as useRef5,
862
+ useCallback as useCallback6,
863
+ useEffect as useEffect6
864
+ } from "react";
865
+
866
+ // src/components/Keypad.tsx
867
+ import {
918
868
  useCallback as useCallback4,
919
- useEffect as useEffect5
869
+ useEffect as useEffect3,
870
+ useRef as useRef4
920
871
  } from "react";
921
872
 
922
873
  // src/utils/isMobileAgent.ts
@@ -925,31 +876,19 @@ function isMobileAgent(userAgent = navigator.userAgent) {
925
876
  return mobileRegex.test(userAgent);
926
877
  }
927
878
 
928
- // src/components/Keypad.tsx
929
- import { useCallback as useCallback2 } from "react";
930
- import { jsxDEV as jsxDEV4 } from "react/jsx-dev-runtime";
931
- function VirtualKeypad({
879
+ // src/hooks/useKeypadLayout.ts
880
+ import { useCallback as useCallback2, useRef as useRef2 } from "react";
881
+ function useKeypadLayout({
932
882
  layout,
933
- viewport
883
+ viewport,
884
+ hangulMode,
885
+ shift
934
886
  }) {
935
- const {
936
- focusId,
937
- onBlur,
938
- onFocus,
939
- inputRef,
940
- shift,
941
- hangulMode,
942
- toggleShift,
943
- toggleKorean
944
- } = useVirtualInputContext();
945
- const handleFocus = useCallback2(() => {
946
- if (!focusId)
947
- return;
948
- onFocus(focusId);
949
- }, [onFocus, focusId]);
887
+ const keyBoundsRef = useRef2([]);
950
888
  const getTransformedValue = useCallback2((cell) => {
951
889
  if (cell.type === "char") {
952
- if (hangulMode) {
890
+ const isConvertibleHangulSource = hangulMode && /[a-zA-Z]/.test(cell.value) && cell.value.length === 1;
891
+ if (isConvertibleHangulSource) {
953
892
  return convertQwertyToHangul(cell.value);
954
893
  }
955
894
  if (shift) {
@@ -959,12 +898,70 @@ function VirtualKeypad({
959
898
  }
960
899
  return cell.label ?? cell.value;
961
900
  }, [hangulMode, shift]);
962
- const insertCharacter = useCallback2((e) => {
963
- const target = e.target;
964
- if (!(target instanceof HTMLButtonElement))
965
- return;
966
- const { value, dataset } = target;
967
- const type = dataset.type;
901
+ const calculateLayout = useCallback2(() => {
902
+ if (!layout)
903
+ return [];
904
+ const scale = viewport.scale;
905
+ const compactRatio = Math.max(0, Math.min(1, (viewport.width - 260) / 140));
906
+ const padding = (2 + (10 - 2) * compactRatio) / scale;
907
+ const gap = (2 + (8 - 2) * compactRatio) / scale;
908
+ const totalHeight = 200 / scale;
909
+ const width = viewport.width;
910
+ const height = totalHeight;
911
+ const contentW = width - padding * 2;
912
+ const contentH = height - padding * 2;
913
+ const bounds = [];
914
+ const rowCount = layout.length;
915
+ const rowHeight = (contentH - (rowCount - 1) * gap) / rowCount;
916
+ let currentY = padding;
917
+ layout.forEach((row, rowIndex) => {
918
+ const rowWidth = contentW;
919
+ const totalFlexGrow = row.reduce((acc, cell) => acc + (cell.width || 1), 0);
920
+ const totalGapW = (row.length - 1) * gap;
921
+ const availableW = rowWidth - totalGapW;
922
+ const unitW = availableW / totalFlexGrow;
923
+ let currentX = padding;
924
+ row.forEach((cell, colIndex) => {
925
+ const flex = cell.width || 1;
926
+ const cellW = flex * unitW;
927
+ bounds.push({
928
+ x: currentX,
929
+ y: currentY,
930
+ w: cellW,
931
+ h: rowHeight,
932
+ value: cell.value,
933
+ type: cell.type,
934
+ label: getTransformedValue(cell),
935
+ rowIndex,
936
+ colIndex,
937
+ isAction: cell.type === "action"
938
+ });
939
+ currentX += cellW + gap;
940
+ });
941
+ currentY += rowHeight + gap;
942
+ });
943
+ return bounds;
944
+ }, [layout, viewport, getTransformedValue]);
945
+ return {
946
+ keyBoundsRef,
947
+ calculateLayout,
948
+ getTransformedValue
949
+ };
950
+ }
951
+
952
+ // src/hooks/useKeypadInteraction.ts
953
+ import { useCallback as useCallback3, useRef as useRef3, useMemo as useMemo2 } from "react";
954
+ function useKeypadInteraction({
955
+ inputRef,
956
+ toggleShift,
957
+ toggleKorean,
958
+ shift,
959
+ getTransformedValue,
960
+ keyBoundsRef
961
+ }) {
962
+ const activePresses = useRef3(new Map);
963
+ const repeatableKeys = useMemo2(() => new Set(["char", "Backspace", "Delete", "Space", "ArrowLeft", "ArrowRight"]), []);
964
+ const dispatchKeyEvent = useCallback3((value, type) => {
968
965
  if (typeof navigator !== "undefined" && "vibrate" in navigator) {
969
966
  navigator.vibrate?.(10);
970
967
  }
@@ -979,123 +976,286 @@ function VirtualKeypad({
979
976
  }
980
977
  }
981
978
  const event = new KeyboardEvent("keydown", {
982
- key: getTransformedValue({
983
- value,
984
- type
985
- }),
979
+ key: getTransformedValue({ value, type }),
986
980
  code: `Key${value.toUpperCase()}`,
987
981
  bubbles: true,
988
982
  cancelable: true,
989
983
  shiftKey: shift
990
984
  });
991
985
  inputRef.current?.handleKeyDown(event);
992
- }, [inputRef, shift, toggleShift, toggleKorean, getTransformedValue]);
986
+ }, [getTransformedValue, inputRef, shift, toggleKorean, toggleShift]);
987
+ const clearRepeat = useCallback3((press) => {
988
+ if (!press?.repeatTimeout)
989
+ return;
990
+ window.clearTimeout(press.repeatTimeout);
991
+ }, []);
992
+ const startRepeat = useCallback3((press) => {
993
+ const canRepeat = press.type && repeatableKeys.has(press.type) || repeatableKeys.has(press.value);
994
+ if (!canRepeat)
995
+ return;
996
+ const firstDelay = 300;
997
+ const repeatInterval = 60;
998
+ const tick = () => {
999
+ const storedPress = activePresses.current.get(press.pointerId);
1000
+ if (!storedPress)
1001
+ return;
1002
+ dispatchKeyEvent(storedPress.value, storedPress.type);
1003
+ storedPress.repeatTimeout = window.setTimeout(tick, repeatInterval);
1004
+ activePresses.current.set(press.pointerId, storedPress);
1005
+ };
1006
+ press.repeatTimeout = window.setTimeout(tick, firstDelay);
1007
+ activePresses.current.set(press.pointerId, press);
1008
+ }, [dispatchKeyEvent, repeatableKeys]);
1009
+ const handlePointerDown = useCallback3((e) => {
1010
+ e.preventDefault();
1011
+ const canvas = e.currentTarget;
1012
+ if (!canvas)
1013
+ return;
1014
+ const rect = canvas.getBoundingClientRect();
1015
+ const x = e.clientX - rect.left;
1016
+ const y = e.clientY - rect.top;
1017
+ const hitKey = keyBoundsRef.current.find((k) => x >= k.x && x <= k.x + k.w && y >= k.y && y <= k.y + k.h);
1018
+ if (hitKey) {
1019
+ const keyIndex = hitKey.rowIndex * 100 + hitKey.colIndex;
1020
+ dispatchKeyEvent(hitKey.value, hitKey.type);
1021
+ const press = {
1022
+ pointerId: e.pointerId,
1023
+ value: hitKey.value,
1024
+ type: hitKey.type,
1025
+ keyIndex
1026
+ };
1027
+ activePresses.current.set(e.pointerId, press);
1028
+ startRepeat(press);
1029
+ }
1030
+ }, [dispatchKeyEvent, startRepeat, keyBoundsRef]);
1031
+ const handlePointerMove = useCallback3((e) => {
1032
+ const press = activePresses.current.get(e.pointerId);
1033
+ if (!press)
1034
+ return;
1035
+ const canvas = e.currentTarget;
1036
+ if (!canvas)
1037
+ return;
1038
+ const rect = canvas.getBoundingClientRect();
1039
+ const x = e.clientX - rect.left;
1040
+ const y = e.clientY - rect.top;
1041
+ const hitKey = keyBoundsRef.current.find((k) => x >= k.x && x <= k.x + k.w && y >= k.y && y <= k.y + k.h);
1042
+ if (hitKey) {
1043
+ const newKeyIndex = hitKey.rowIndex * 100 + hitKey.colIndex;
1044
+ if (newKeyIndex !== press.keyIndex) {
1045
+ clearRepeat(press);
1046
+ dispatchKeyEvent(hitKey.value, hitKey.type);
1047
+ const newPress = {
1048
+ pointerId: e.pointerId,
1049
+ value: hitKey.value,
1050
+ type: hitKey.type,
1051
+ keyIndex: newKeyIndex
1052
+ };
1053
+ activePresses.current.set(e.pointerId, newPress);
1054
+ startRepeat(newPress);
1055
+ }
1056
+ } else {
1057
+ clearRepeat(press);
1058
+ activePresses.current.delete(e.pointerId);
1059
+ }
1060
+ }, [clearRepeat, dispatchKeyEvent, startRepeat, keyBoundsRef]);
1061
+ const handlePointerUp = useCallback3((e) => {
1062
+ const press = activePresses.current.get(e.pointerId);
1063
+ if (press) {
1064
+ clearRepeat(press);
1065
+ activePresses.current.delete(e.pointerId);
1066
+ }
1067
+ }, [clearRepeat]);
1068
+ return {
1069
+ activePresses,
1070
+ handlePointerDown,
1071
+ handlePointerMove,
1072
+ handlePointerUp
1073
+ };
1074
+ }
1075
+
1076
+ // src/components/Keypad.tsx
1077
+ import { jsxDEV as jsxDEV3 } from "react/jsx-dev-runtime";
1078
+ function VirtualKeypad({
1079
+ layout,
1080
+ viewport
1081
+ }) {
1082
+ const {
1083
+ focusId,
1084
+ onBlur,
1085
+ onFocus,
1086
+ inputRef,
1087
+ shift,
1088
+ hangulMode,
1089
+ toggleShift,
1090
+ toggleKorean
1091
+ } = useVirtualInputContext();
1092
+ const canvasRef = useRef4(null);
1093
+ const containerRef = useRef4(null);
1094
+ const {
1095
+ keyBoundsRef,
1096
+ calculateLayout,
1097
+ getTransformedValue
1098
+ } = useKeypadLayout({
1099
+ layout,
1100
+ viewport,
1101
+ hangulMode,
1102
+ shift
1103
+ });
1104
+ const {
1105
+ activePresses,
1106
+ handlePointerDown,
1107
+ handlePointerMove,
1108
+ handlePointerUp
1109
+ } = useKeypadInteraction({
1110
+ inputRef,
1111
+ toggleShift,
1112
+ toggleKorean,
1113
+ shift,
1114
+ getTransformedValue,
1115
+ keyBoundsRef
1116
+ });
1117
+ const drawRoundedRect = (ctx, x, y, w, h, r) => {
1118
+ ctx.beginPath();
1119
+ ctx.moveTo(x + r, y);
1120
+ ctx.lineTo(x + w - r, y);
1121
+ ctx.quadraticCurveTo(x + w, y, x + w, y + r);
1122
+ ctx.lineTo(x + w, y + h - r);
1123
+ ctx.quadraticCurveTo(x + w, y + h, x + w - r, y + h);
1124
+ ctx.lineTo(x + r, y + h);
1125
+ ctx.quadraticCurveTo(x, y + h, x, y + h - r);
1126
+ ctx.lineTo(x, y + r);
1127
+ ctx.quadraticCurveTo(x, y, x + r, y);
1128
+ ctx.closePath();
1129
+ };
1130
+ const draw = useCallback4(() => {
1131
+ const canvas = canvasRef.current;
1132
+ if (!canvas)
1133
+ return;
1134
+ const ctx = canvas.getContext("2d", { alpha: false });
1135
+ if (!ctx)
1136
+ return;
1137
+ const dpr = window.devicePixelRatio || 1;
1138
+ const rect = canvas.getBoundingClientRect();
1139
+ if (Math.abs(canvas.width - rect.width * dpr) > 1 || Math.abs(canvas.height - rect.height * dpr) > 1) {
1140
+ canvas.width = rect.width * dpr;
1141
+ canvas.height = rect.height * dpr;
1142
+ ctx.scale(dpr, dpr);
1143
+ keyBoundsRef.current = calculateLayout();
1144
+ }
1145
+ if (keyBoundsRef.current.length === 0) {
1146
+ keyBoundsRef.current = calculateLayout();
1147
+ }
1148
+ const width = rect.width;
1149
+ const height = rect.height;
1150
+ ctx.fillStyle = "#e8eaee";
1151
+ ctx.fillRect(0, 0, width, height);
1152
+ const scale = viewport.scale;
1153
+ keyBoundsRef.current.forEach((key) => {
1154
+ let isPressed = false;
1155
+ for (const press of activePresses.current.values()) {
1156
+ const keyIndex = key.rowIndex * 100 + key.colIndex;
1157
+ if (press.keyIndex === keyIndex) {
1158
+ isPressed = true;
1159
+ break;
1160
+ }
1161
+ }
1162
+ const isActiveModifier = key.value === "Shift" && shift || key.value === "HangulMode" && hangulMode;
1163
+ const radius = 12 / scale;
1164
+ let drawX = key.x;
1165
+ let drawY = key.y;
1166
+ let drawW = key.w;
1167
+ let drawH = key.h;
1168
+ if (isPressed) {
1169
+ const shrink = 2;
1170
+ drawX += shrink;
1171
+ drawY += shrink;
1172
+ drawW -= shrink * 2;
1173
+ drawH -= shrink * 2;
1174
+ }
1175
+ drawRoundedRect(ctx, drawX, drawY, drawW, drawH, radius);
1176
+ if (isPressed) {
1177
+ const gradient = ctx.createLinearGradient(drawX, drawY, drawX, drawY + drawH);
1178
+ gradient.addColorStop(0, "rgba(248, 249, 251, 0.95)");
1179
+ gradient.addColorStop(1, "#c9d2df");
1180
+ ctx.fillStyle = gradient;
1181
+ } else if (isActiveModifier) {
1182
+ const gradient = ctx.createLinearGradient(drawX, drawY, drawX, drawY + drawH);
1183
+ gradient.addColorStop(0, "#e0ecff");
1184
+ gradient.addColorStop(1, "#99b7ff");
1185
+ ctx.fillStyle = gradient;
1186
+ } else if (key.isAction) {
1187
+ const gradient = ctx.createLinearGradient(drawX, drawY, drawX, drawY + drawH);
1188
+ gradient.addColorStop(0, "#f8fafc");
1189
+ gradient.addColorStop(1, "#cbd5f5");
1190
+ ctx.fillStyle = gradient;
1191
+ } else {
1192
+ const gradient = ctx.createLinearGradient(drawX, drawY, drawX, drawY + drawH);
1193
+ gradient.addColorStop(0, "rgba(255, 255, 255, 0.95)");
1194
+ gradient.addColorStop(1, "#dfe3eb");
1195
+ ctx.fillStyle = gradient;
1196
+ }
1197
+ ctx.fill();
1198
+ ctx.lineWidth = 1 / scale;
1199
+ if (isActiveModifier) {
1200
+ ctx.strokeStyle = "rgba(37, 99, 235, 0.25)";
1201
+ } else if (key.isAction) {
1202
+ ctx.strokeStyle = "rgba(59, 130, 246, 0.55)";
1203
+ } else {
1204
+ ctx.strokeStyle = "rgba(148, 163, 184, 0.7)";
1205
+ }
1206
+ ctx.stroke();
1207
+ ctx.textAlign = "center";
1208
+ ctx.textBaseline = "middle";
1209
+ const fontSize = 18 / scale;
1210
+ ctx.font = `600 ${fontSize}px sans-serif`;
1211
+ if (isActiveModifier || key.isAction) {
1212
+ ctx.fillStyle = "#1e3a8a";
1213
+ } else {
1214
+ ctx.fillStyle = "#1f2933";
1215
+ }
1216
+ ctx.fillText(key.label, drawX + drawW / 2, drawY + drawH / 2);
1217
+ });
1218
+ }, [calculateLayout, hangulMode, shift, viewport.scale, activePresses, keyBoundsRef]);
1219
+ useEffect3(() => {
1220
+ let handle;
1221
+ const loop = () => {
1222
+ draw();
1223
+ handle = requestAnimationFrame(loop);
1224
+ };
1225
+ loop();
1226
+ return () => cancelAnimationFrame(handle);
1227
+ }, [draw]);
993
1228
  if (!focusId || !isMobileAgent())
994
1229
  return null;
995
- return /* @__PURE__ */ jsxDEV4(ShadowWrapper, {
996
- tagName: "virtual-keypad",
1230
+ return /* @__PURE__ */ jsxDEV3(ShadowWrapper, {
1231
+ tagName: "virtual-keypad-canvas",
997
1232
  css: `
998
- .keypad-container {
999
- display: flex;
1000
- flex-direction: column;
1001
- position: fixed;
1002
- background-color: #e8eaee;
1003
- padding: calc(12px / var(--scale-factor));
1004
- box-sizing: border-box;
1005
- gap: calc(10px / var(--scale-factor));
1006
- border-radius: calc(18px / var(--scale-factor));
1007
- box-shadow: 0 calc(-6px / var(--scale-factor)) calc(30px / var(--scale-factor)) rgba(15, 23, 42, 0.2);
1008
- user-select: none;
1009
- touch-action: manipulation;
1010
- }
1011
- .keypad-row {
1012
- display: flex;
1013
- flex: 1;
1014
- gap: calc(10px / var(--scale-factor));
1015
- }
1016
- @keyframes key-pop {
1017
- from {
1018
- opacity: 0.9;
1019
- transform: translate(-50%, 5px) scale(1);
1020
- }
1021
- to {
1022
- opacity: 1;
1023
- transform: translate(-50%, calc(-15px / var(--scale-factor))) scale(1.4);
1024
- }
1025
- }
1026
- @keyframes key-press {
1027
- from { transform: scale(1); }
1028
- to { transform: scale(0.96); }
1029
- }
1030
- .keypad-button {
1031
- position: relative;
1032
- flex: 1;
1033
- display: flex;
1034
- justify-content: center;
1035
- align-items: center;
1036
- background: linear-gradient(180deg, rgba(255, 255, 255, 0.95) 0%, #dfe3eb 100%);
1037
- border: calc(1px / var(--scale-factor)) solid rgba(148, 163, 184, 0.7);
1038
- border-radius: calc(12px / var(--scale-factor));
1039
- font-size: calc(18px / var(--scale-factor));
1040
- font-weight: 600;
1041
- letter-spacing: 0.01em;
1042
- color: #1f2933;
1043
- cursor: pointer;
1044
- box-shadow:
1045
- 0 calc(2px / var(--scale-factor)) calc(4px / var(--scale-factor)) rgba(15, 23, 42, 0.12),
1046
- inset 0 calc(1px / var(--scale-factor)) calc(1px / var(--scale-factor)) rgba(255, 255, 255, 0.65);
1047
- transition: transform 80ms ease, box-shadow 80ms ease, background 120ms ease;
1048
- }
1049
- .keypad-button:active {
1050
- animation: key-press 0.05s forwards;
1051
- box-shadow: 0 calc(1px / var(--scale-factor)) calc(2px / var(--scale-factor)) rgba(15, 23, 42, 0.18);
1052
- background: linear-gradient(180deg, rgba(248, 249, 251, 0.95) 0%, #c9d2df 100%);
1053
- }
1054
- .key-popup {
1055
- display: none;
1056
- position: absolute;
1057
- left: 50%;
1058
- bottom: 80%;
1059
- min-width: 100%;
1060
- padding: calc(8px / var(--scale-factor)) calc(12px / var(--scale-factor));
1061
- background-color: rgba(248, 250, 252, 0.98);
1062
- color: #0f172a;
1063
- border-radius: calc(10px / var(--scale-factor));
1064
- box-shadow: 0 calc(-4px / var(--scale-factor)) calc(18px / var(--scale-factor)) rgba(15, 23, 42, 0.18);
1065
- pointer-events: none;
1066
- font-size: calc(26px / var(--scale-factor));
1067
- font-weight: 500;
1068
- text-align: center;
1069
- z-index: 10;
1070
- line-height: 1.2;
1071
- }
1072
- .keypad-button:active .key-popup {
1073
- display: block;
1074
- animation: key-pop 0.1s ease-out forwards;
1075
- }
1076
- .keypad-button.action {
1077
- background: linear-gradient(180deg, #f8fafc 0%, #cbd5f5 100%);
1078
- color: #1e3a8a;
1079
- border-color: rgba(59, 130, 246, 0.55);
1080
- }
1081
- .keypad-button.active-modifier {
1082
- background: linear-gradient(180deg, #e0ecff 0%, #99b7ff 100%);
1083
- color: #1e3a8a;
1084
- box-shadow:
1085
- 0 calc(3px / var(--scale-factor)) calc(6px / var(--scale-factor)) rgba(37, 99, 235, 0.25),
1086
- inset 0 calc(1px / var(--scale-factor)) calc(1px / var(--scale-factor)) rgba(255, 255, 255, 0.55);
1087
- }
1088
- .keypad-button:focus-visible {
1089
- outline: none;
1090
- box-shadow: 0 0 0 calc(3px / var(--scale-factor)) rgba(59, 130, 246, 0.35);
1091
- }
1092
-
1093
- `,
1094
- children: /* @__PURE__ */ jsxDEV4("div", {
1095
- onFocus: handleFocus,
1233
+ .keypad-wrapper {
1234
+ position: fixed;
1235
+ background-color: #e8eaee;
1236
+ border-radius: calc(18px / var(--scale-factor));
1237
+ box-shadow: 0 calc(-6px / var(--scale-factor)) calc(30px / var(--scale-factor)) rgba(15, 23, 42, 0.2);
1238
+ user-select: none;
1239
+ -webkit-user-select: none;
1240
+ touch-action: none;
1241
+ z-index: 9999;
1242
+ overflow: hidden; /* Canvas containment */
1243
+ }
1244
+ canvas {
1245
+ display: block;
1246
+ width: 100%;
1247
+ height: 100%;
1248
+ }
1249
+ `,
1250
+ children: /* @__PURE__ */ jsxDEV3("div", {
1251
+ ref: containerRef,
1252
+ className: "keypad-wrapper",
1253
+ onFocus: () => {
1254
+ if (focusId)
1255
+ onFocus(focusId);
1256
+ },
1096
1257
  onBlur,
1097
- tabIndex: -1,
1098
- className: "keypad-container",
1258
+ onContextMenu: (e) => e.preventDefault(),
1099
1259
  style: {
1100
1260
  left: viewport.offsetLeft,
1101
1261
  width: viewport.width,
@@ -1103,49 +1263,20 @@ function VirtualKeypad({
1103
1263
  height: Math.round(200 / viewport.scale),
1104
1264
  "--scale-factor": viewport.scale
1105
1265
  },
1106
- children: layout?.map((row, i) => /* @__PURE__ */ jsxDEV4("div", {
1107
- className: "keypad-row",
1108
- children: row.map((cell, j) => {
1109
- const buttonClasses = ["keypad-button"];
1110
- if (cell.type === "action") {
1111
- buttonClasses.push("action");
1112
- }
1113
- if (cell.value === "Shift" && shift) {
1114
- buttonClasses.push("active-modifier");
1115
- }
1116
- if (cell.value === "HangulMode" && hangulMode) {
1117
- buttonClasses.push("active-modifier");
1118
- }
1119
- const style = {};
1120
- if (cell.width) {
1121
- style.flex = `0 0 calc(${cell.width}px / var(--scale-factor))`;
1122
- }
1123
- if (cell.height) {
1124
- style.height = `calc(${cell.height}px / var(--scale-factor))`;
1125
- }
1126
- return /* @__PURE__ */ jsxDEV4("button", {
1127
- type: "button",
1128
- value: cell.value,
1129
- "data-type": cell.type,
1130
- onClick: insertCharacter,
1131
- className: buttonClasses.join(" "),
1132
- style,
1133
- children: [
1134
- getTransformedValue(cell),
1135
- /* @__PURE__ */ jsxDEV4("div", {
1136
- className: "key-popup",
1137
- children: getTransformedValue(cell)
1138
- }, undefined, false, undefined, this)
1139
- ]
1140
- }, `${i}-${j}`, true, undefined, this);
1141
- })
1142
- }, i, false, undefined, this))
1266
+ children: /* @__PURE__ */ jsxDEV3("canvas", {
1267
+ ref: canvasRef,
1268
+ onPointerDown: handlePointerDown,
1269
+ onPointerMove: handlePointerMove,
1270
+ onPointerUp: handlePointerUp,
1271
+ onPointerLeave: handlePointerUp,
1272
+ onPointerCancel: handlePointerUp
1273
+ }, undefined, false, undefined, this)
1143
1274
  }, undefined, false, undefined, this)
1144
1275
  }, undefined, false, undefined, this);
1145
1276
  }
1146
1277
 
1147
1278
  // src/hooks/useStorage.ts
1148
- import { useState as useState3, useCallback as useCallback3, useEffect as useEffect3 } from "react";
1279
+ import { useState as useState3, useCallback as useCallback5, useEffect as useEffect4 } from "react";
1149
1280
  var isBrowser = typeof window !== "undefined";
1150
1281
  function useStorage(key, initialValue) {
1151
1282
  const [storedValue, setStoredValue] = useState3(() => {
@@ -1166,7 +1297,7 @@ function useStorage(key, initialValue) {
1166
1297
  return initialValue instanceof Function ? initialValue() : initialValue;
1167
1298
  }
1168
1299
  });
1169
- const setValue = useCallback3((value) => {
1300
+ const setValue = useCallback5((value) => {
1170
1301
  if (!isBrowser) {
1171
1302
  console.warn(`Tried to set localStorage key "${key}" in a non-browser environment.`);
1172
1303
  return;
@@ -1179,7 +1310,7 @@ function useStorage(key, initialValue) {
1179
1310
  console.error(`Error setting localStorage key "${key}":`, error);
1180
1311
  }
1181
1312
  }, [key, storedValue]);
1182
- useEffect3(() => {
1313
+ useEffect4(() => {
1183
1314
  if (!isBrowser)
1184
1315
  return;
1185
1316
  const handleStorageChange = (event) => {
@@ -1247,7 +1378,7 @@ var qwerty_default = [
1247
1378
  ];
1248
1379
 
1249
1380
  // src/hooks/useVisualViewport.ts
1250
- import { useState as useState4, useEffect as useEffect4 } from "react";
1381
+ import { useEffect as useEffect5, useState as useState4 } from "react";
1251
1382
  var getVisualViewport = () => {
1252
1383
  if (typeof window !== "undefined" && window.visualViewport) {
1253
1384
  return {
@@ -1266,40 +1397,58 @@ var getVisualViewport = () => {
1266
1397
  offsetTop: 0
1267
1398
  };
1268
1399
  };
1400
+ var isSameViewport = (prev, next) => prev.width === next.width && prev.height === next.height && prev.scale === next.scale && prev.offsetLeft === next.offsetLeft && prev.offsetTop === next.offsetTop;
1269
1401
  var useVisualViewport = () => {
1270
1402
  const [viewport, setViewport] = useState4(getVisualViewport);
1271
- useEffect4(() => {
1403
+ useEffect5(() => {
1404
+ if (typeof window === "undefined")
1405
+ return;
1406
+ let rafId = null;
1407
+ const applyViewportUpdate = () => {
1408
+ const nextViewport = getVisualViewport();
1409
+ setViewport((prev) => isSameViewport(prev, nextViewport) ? prev : nextViewport);
1410
+ };
1272
1411
  const handleResize = () => {
1273
- setViewport(getVisualViewport());
1412
+ if (rafId !== null) {
1413
+ cancelAnimationFrame(rafId);
1414
+ }
1415
+ rafId = requestAnimationFrame(applyViewportUpdate);
1274
1416
  };
1275
- if (typeof window !== "undefined" && window.visualViewport) {
1276
- window.visualViewport.addEventListener("resize", handleResize);
1277
- window.visualViewport.addEventListener("scroll", handleResize);
1278
- return () => {
1279
- window.visualViewport?.removeEventListener("resize", handleResize);
1280
- window.visualViewport?.removeEventListener("scroll", handleResize);
1281
- };
1417
+ if (window.visualViewport) {
1418
+ window.visualViewport.addEventListener("resize", handleResize, { passive: true });
1419
+ window.visualViewport.addEventListener("scroll", handleResize, { passive: true });
1420
+ } else {
1421
+ window.addEventListener("resize", handleResize, { passive: true });
1282
1422
  }
1283
- window.addEventListener("resize", handleResize);
1284
- return () => window.removeEventListener("resize", handleResize);
1423
+ return () => {
1424
+ if (rafId !== null) {
1425
+ cancelAnimationFrame(rafId);
1426
+ }
1427
+ if (window.visualViewport) {
1428
+ window.visualViewport.removeEventListener("resize", handleResize);
1429
+ window.visualViewport.removeEventListener("scroll", handleResize);
1430
+ } else {
1431
+ window.removeEventListener("resize", handleResize);
1432
+ }
1433
+ };
1285
1434
  }, []);
1286
1435
  return viewport;
1287
1436
  };
1288
1437
 
1289
1438
  // src/components/Provider.tsx
1290
- import { jsxDEV as jsxDEV5 } from "react/jsx-dev-runtime";
1439
+ import { jsxDEV as jsxDEV4 } from "react/jsx-dev-runtime";
1291
1440
  function VirtualInputProvider({
1292
1441
  children,
1293
1442
  layout = qwerty_default,
1294
1443
  defaultHangulMode = true
1295
1444
  }) {
1296
- const inputRef = useRef2(null);
1297
- const sti = useRef2(setTimeout(() => null, 0));
1445
+ const inputRef = useRef5(null);
1446
+ const sti = useRef5(setTimeout(() => null, 0));
1298
1447
  const [focusId, setFocusId] = useState5();
1299
1448
  const [shift, setShift] = useState5(false);
1300
1449
  const [hangulMode, setHangulMode] = useStorage("virtual-keyboard-hangul-mode", defaultHangulMode);
1301
1450
  const viewport = useVisualViewport();
1302
- useEffect5(() => {
1451
+ useEffect6(() => {
1303
1452
  if (focusId) {
1304
1453
  document.body.style.paddingBottom = `${Math.round(200 / viewport.scale)}px`;
1305
1454
  } else {
@@ -1319,14 +1468,14 @@ function VirtualInputProvider({
1319
1468
  isCompositionRef.current = false;
1320
1469
  }, 0);
1321
1470
  };
1322
- const toggleShift = useCallback4(() => {
1471
+ const toggleShift = useCallback6(() => {
1323
1472
  setShift((prev) => !prev);
1324
1473
  }, []);
1325
- const toggleKorean = useCallback4(() => {
1474
+ const toggleKorean = useCallback6(() => {
1326
1475
  setHangulMode((prev) => !prev);
1327
1476
  }, [setHangulMode]);
1328
- const isCompositionRef = useRef2(undefined);
1329
- return /* @__PURE__ */ jsxDEV5(VirtualInputContext.Provider, {
1477
+ const isCompositionRef = useRef5(undefined);
1478
+ return /* @__PURE__ */ jsxDEV4(VirtualInputContext.Provider, {
1330
1479
  value: {
1331
1480
  inputRef,
1332
1481
  isCompositionRef,
@@ -1341,7 +1490,7 @@ function VirtualInputProvider({
1341
1490
  },
1342
1491
  children: [
1343
1492
  children,
1344
- /* @__PURE__ */ jsxDEV5(VirtualKeypad, {
1493
+ /* @__PURE__ */ jsxDEV4(VirtualKeypad, {
1345
1494
  layout,
1346
1495
  viewport
1347
1496
  }, undefined, false, undefined, this)