@uiwwsw/virtual-keyboard 0.4.1 → 0.5.1

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,18 @@ 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);
579
+ const internalValueRef = useRef(internalValue);
580
+ const caretIndexRef = useRef(caretIndex);
590
581
  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]);
582
+ const [showCursor, setShowCursor] = useState2(true);
583
+ const charPositionsRef = useRef([]);
605
584
  useEffect2(() => {
606
- if (controlledValue !== undefined && controlledValue !== internalValue) {
607
- setInternalValue(controlledValue);
608
- }
609
- }, [controlledValue, internalValue]);
585
+ charPositionsRef.current = [];
586
+ }, [internalValue]);
610
587
  const {
611
588
  focusId,
612
589
  onFocus,
@@ -618,305 +595,293 @@ function VirtualInput({
618
595
  shift
619
596
  } = useVirtualInputContext();
620
597
  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]);
598
+ useEffect2(() => {
599
+ if (controlledValue !== undefined && controlledValue !== internalValue) {
600
+ setInternalValue(controlledValue);
601
+ internalValueRef.current = controlledValue;
602
+ }
603
+ }, [controlledValue, internalValue]);
604
+ useEffect2(() => {
605
+ if (!isFocused)
606
+ return;
607
+ const interval = setInterval(() => {
608
+ setShowCursor((prev) => !prev);
609
+ }, 530);
610
+ return () => clearInterval(interval);
611
+ }, [isFocused]);
612
+ useEffect2(() => {
613
+ setShowCursor(true);
614
+ }, [caretIndex, internalValue]);
615
+ const hasSelection = useMemo(() => selection.start !== null && selection.end !== null && selection.start !== selection.end, [selection]);
624
616
  const clearSelection = useCallback(() => {
625
617
  setSelection({ start: null, end: null });
626
618
  }, []);
627
619
  const deleteSelectedText = useCallback(() => {
628
- if (!hasSelection || selectionStart === null || selectionEnd === null) {
629
- return { newString: internalValue, finalCaretIndex: caretIndex };
620
+ const currentVal = internalValueRef.current;
621
+ if (!hasSelection || selection.start === null || selection.end === null) {
622
+ return { newString: currentVal, finalCaretIndex: caretIndexRef.current };
630
623
  }
631
- const [start, end] = [selectionStart, selectionEnd].sort((a, b) => a - b);
632
- const newString = internalValue.slice(0, start) + internalValue.slice(end);
624
+ const [start, end] = [selection.start, selection.end].sort((a, b) => a - b);
625
+ const newString = currentVal.slice(0, start) + currentVal.slice(end);
633
626
  clearSelection();
634
627
  return { newString, finalCaretIndex: start };
635
628
  }, [
636
- internalValue,
637
- caretIndex,
638
629
  hasSelection,
639
- selectionStart,
640
- selectionEnd,
630
+ selection,
641
631
  clearSelection
642
632
  ]);
643
- const handlePaste = useCallback((e) => {
644
- e.preventDefault();
645
- const pastedText = e.clipboardData.getData("text/plain");
646
- if (!pastedText)
633
+ const draw = useCallback(() => {
634
+ const canvas = canvasRef.current;
635
+ const container = containerRef.current;
636
+ if (!canvas || !container)
647
637
  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 });
638
+ const ctx = canvas.getContext("2d");
639
+ if (!ctx)
665
640
  return;
641
+ const dpr = window.devicePixelRatio || 1;
642
+ const rect = container.getBoundingClientRect();
643
+ if (canvas.width !== rect.width * dpr || canvas.height !== rect.height * dpr) {
644
+ canvas.width = rect.width * dpr;
645
+ canvas.height = rect.height * dpr;
646
+ ctx.scale(dpr, dpr);
647
+ charPositionsRef.current = [];
666
648
  }
667
- if (e.key === "HangulMode") {
668
- setHangulMode(!hangulMode);
649
+ const width = rect.width;
650
+ const height = rect.height;
651
+ ctx.clearRect(0, 0, width, height);
652
+ const computedStyle = window.getComputedStyle(container);
653
+ const font = `${computedStyle.fontSize} ${computedStyle.fontFamily}`;
654
+ ctx.font = font;
655
+ ctx.textBaseline = "middle";
656
+ const fontSize = parseFloat(computedStyle.fontSize);
657
+ const y = height / 2;
658
+ const x = 0;
659
+ if (!internalValue && placeholder) {
660
+ ctx.fillStyle = "#aaa";
661
+ ctx.fillText(placeholder, x, y);
662
+ if (isFocused && showCursor) {
663
+ ctx.beginPath();
664
+ ctx.moveTo(x, y - fontSize / 2);
665
+ ctx.lineTo(x, y + fontSize / 2);
666
+ ctx.strokeStyle = "black";
667
+ ctx.lineWidth = 1.5;
668
+ ctx.stroke();
669
+ }
669
670
  return;
670
671
  }
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
- });
672
+ ctx.fillStyle = "black";
673
+ if (charPositionsRef.current.length !== internalValue.length + 1) {
674
+ let currentX = x;
675
+ const positions = [x];
676
+ for (let i = 0;i < internalValue.length; i++) {
677
+ const w = ctx.measureText(internalValue[i]).width;
678
+ currentX += w;
679
+ positions.push(currentX);
680
+ }
681
+ charPositionsRef.current = positions;
682
+ }
683
+ const charX = charPositionsRef.current;
684
+ if (hasSelection && selection.start !== null && selection.end !== null) {
685
+ const [start, end] = [selection.start, selection.end].sort((a, b) => a - b);
686
+ const startX = charX[start] ?? x;
687
+ const endX = charX[end] ?? charX[charX.length - 1];
688
+ ctx.fillStyle = "#b4d5fe";
689
+ ctx.fillRect(startX, 0, endX - startX, height);
690
+ ctx.fillStyle = "black";
691
+ }
692
+ ctx.fillText(internalValue, x, y);
693
+ if (isFocused && showCursor && !hasSelection) {
694
+ const caretX = charX[caretIndex] ?? x;
695
+ ctx.beginPath();
696
+ ctx.moveTo(caretX, y - fontSize / 2);
697
+ ctx.lineTo(caretX, y + fontSize / 2);
698
+ ctx.strokeStyle = "black";
699
+ ctx.lineWidth = 1.5;
700
+ ctx.stroke();
701
+ }
702
+ }, [internalValue, caretIndex, isFocused, showCursor, placeholder, hasSelection, selection]);
703
+ useEffect2(() => {
704
+ let animationFrameId;
705
+ const loop = () => {
706
+ draw();
707
+ animationFrameId = requestAnimationFrame(loop);
708
+ };
709
+ loop();
710
+ return () => cancelAnimationFrame(animationFrameId);
711
+ }, [draw]);
712
+ const updateValue = useCallback((newValue, newCaretIndex) => {
713
+ internalValueRef.current = newValue;
714
+ caretIndexRef.current = newCaretIndex;
715
+ if (controlledValue === undefined) {
716
+ setInternalValue(newValue);
717
+ }
718
+ const fakeInput = { value: newValue };
719
+ const changeEvent = {
720
+ target: fakeInput,
721
+ currentTarget: fakeInput
722
+ };
723
+ onChange?.(changeEvent);
724
+ setCaretIndex(newCaretIndex);
725
+ }, [controlledValue, onChange]);
726
+ const getCharIndexFromX = useCallback((clientX) => {
727
+ const canvas = canvasRef.current;
728
+ if (!canvas)
729
+ return 0;
730
+ const rect = canvas.getBoundingClientRect();
731
+ const x = clientX - rect.left;
732
+ if (charPositionsRef.current.length === internalValue.length + 1) {
733
+ const charX = charPositionsRef.current;
734
+ for (let i = 0;i < charX.length - 1; i++) {
735
+ const center = (charX[i] + charX[i + 1]) / 2;
736
+ if (x < center)
737
+ return i;
738
+ }
739
+ return internalValue.length;
740
+ }
741
+ const ctx = canvas.getContext("2d");
742
+ if (!ctx)
743
+ return 0;
744
+ const computedStyle = window.getComputedStyle(containerRef.current);
745
+ ctx.font = `${computedStyle.fontSize} ${computedStyle.fontFamily}`;
746
+ let currentX = 0;
747
+ for (let i = 0;i < internalValue.length; i++) {
748
+ const w = ctx.measureText(internalValue[i]).width;
749
+ if (x < currentX + w / 2) {
750
+ return i;
679
751
  }
752
+ currentX += w;
753
+ }
754
+ return internalValue.length;
755
+ }, [internalValue]);
756
+ const handleCanvasClick = useCallback((e) => {
757
+ const index = getCharIndexFromX(e.clientX);
758
+ caretIndexRef.current = index;
759
+ setCaretIndex(index);
760
+ clearSelection();
761
+ onFocus(id);
762
+ }, [getCharIndexFromX, clearSelection, onFocus, id]);
763
+ const handleKeyDown = useCallback((e) => {
764
+ const currentVal = internalValueRef.current;
765
+ const currentCaret = caretIndexRef.current;
766
+ if (e.key === "HangulMode") {
767
+ setHangulMode(!hangulMode);
680
768
  return;
681
769
  }
682
- if ((e.ctrlKey || e.metaKey) && (e.key.toLowerCase() === "c" || e.key.toLowerCase() === "v"))
770
+ if (e.shiftKey && (e.key === "ArrowLeft" || e.key === "ArrowRight")) {
771
+ const newIndex = e.key === "ArrowLeft" ? Math.max(0, currentCaret - 1) : Math.min(currentVal.length, currentCaret + 1);
772
+ setSelection((prev) => ({
773
+ start: prev.start ?? currentCaret,
774
+ end: newIndex
775
+ }));
776
+ caretIndexRef.current = newIndex;
777
+ setCaretIndex(newIndex);
683
778
  return;
779
+ }
684
780
  switch (e.key) {
685
- case "Backspace": {
686
- isCompositionRef.current = false;
687
- if (hasSelection) {
688
- const { newString, finalCaretIndex } = deleteSelectedText();
689
- updateValue(newString, finalCaretIndex);
690
- } 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
781
  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();
717
- }
718
- setCaretIndex(newCaretIndex);
719
- break;
782
+ const newIndex = Math.max(0, currentCaret - 1);
783
+ caretIndexRef.current = newIndex;
784
+ setCaretIndex(newIndex);
785
+ clearSelection();
786
+ return;
720
787
  }
721
788
  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;
789
+ const newIndex = Math.min(currentVal.length, currentCaret + 1);
790
+ caretIndexRef.current = newIndex;
791
+ setCaretIndex(newIndex);
792
+ clearSelection();
793
+ return;
757
794
  }
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);
795
+ case "Backspace": {
796
+ isCompositionRef.current = false;
797
+ if (hasSelection) {
798
+ const { newString: newString2, finalCaretIndex: finalCaretIndex2 } = deleteSelectedText();
799
+ updateValue(newString2, finalCaretIndex2);
800
+ } else if (currentCaret > 0) {
801
+ const newString2 = currentVal.slice(0, currentCaret - 1) + currentVal.slice(currentCaret);
802
+ updateValue(newString2, currentCaret - 1);
780
803
  }
781
- isCompositionRef.current = composing;
782
- break;
804
+ return;
783
805
  }
784
806
  }
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
- };
807
+ const result = parseKeyInput(e, hangulMode);
808
+ if (result.toggleHangulMode) {
809
+ setHangulMode(!hangulMode);
810
+ return;
811
+ }
812
+ if (!result.handled || !result.text)
813
+ return;
814
+ if (e.ctrlKey || e.metaKey || e.altKey)
815
+ return;
816
+ const { composing } = result;
817
+ let { text } = result;
818
+ if (shift && text.length === 1 && text.match(/[a-z]/i)) {
819
+ text = text.toUpperCase();
820
+ }
821
+ const { newString, finalCaretIndex } = hasSelection ? deleteSelectedText() : { newString: currentVal, finalCaretIndex: currentCaret };
822
+ const prevChar = newString[finalCaretIndex - 1];
823
+ if (composing && isHangul(prevChar) && isCompositionRef.current) {
824
+ const combined = assemble([prevChar, text]);
825
+ const finalString = newString.slice(0, finalCaretIndex - 1) + combined + newString.slice(finalCaretIndex);
826
+ updateValue(finalString, finalCaretIndex - 1 + combined.length);
827
+ } else {
828
+ const finalString = newString.slice(0, finalCaretIndex) + text + newString.slice(finalCaretIndex);
829
+ updateValue(finalString, finalCaretIndex + text.length);
830
+ }
831
+ isCompositionRef.current = composing;
832
+ }, [hangulMode, setHangulMode, isCompositionRef, shift, updateValue, hasSelection, deleteSelectedText, clearSelection]);
824
833
  useImperativeHandle(inputRef, () => {
825
834
  if (isFocused) {
826
- return {
827
- handleKeyDown
828
- };
835
+ return { handleKeyDown };
829
836
  }
830
837
  return inputRef.current;
831
838
  }, [isFocused, handleKeyDown, inputRef]);
832
- return /* @__PURE__ */ jsxDEV3(ShadowWrapper, {
833
- tagName: "virtual-input",
839
+ return /* @__PURE__ */ jsxDEV2(ShadowWrapper, {
840
+ tagName: "virtual-input-canvas",
834
841
  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", {
842
+ canvas {
843
+ width: 100%;
844
+ height: 100%;
845
+ cursor: text;
846
+ display: block;
847
+ }
848
+ `,
849
+ children: /* @__PURE__ */ jsxDEV2("div", {
850
+ ref: containerRef,
874
851
  ...props,
875
- ref: divRef,
876
- className: "wrap",
877
852
  role: "textbox",
878
- "aria-placeholder": placeholder,
879
853
  tabIndex: 0,
880
854
  style: {
881
855
  minHeight: "1.5em",
882
- lineHeight: "1.5em",
856
+ height: "1.5em",
883
857
  outline: "none",
884
858
  font: "inherit",
885
- width: "100%"
859
+ width: "100%",
860
+ position: "relative"
886
861
  },
887
- onFocus: handleFocus,
888
- onBlur: handleBlur,
862
+ onFocus: () => onFocus(id),
863
+ onBlur,
889
864
  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)
865
+ onClick: handleCanvasClick,
866
+ children: /* @__PURE__ */ jsxDEV2("canvas", {
867
+ ref: canvasRef
868
+ }, undefined, false, undefined, this)
869
+ }, undefined, false, undefined, this)
912
870
  }, undefined, false, undefined, this);
913
871
  }
914
872
  // src/components/Provider.tsx
915
873
  import {
916
874
  useState as useState5,
917
- useRef as useRef2,
875
+ useRef as useRef5,
876
+ useCallback as useCallback6,
877
+ useEffect as useEffect6
878
+ } from "react";
879
+
880
+ // src/components/Keypad.tsx
881
+ import {
918
882
  useCallback as useCallback4,
919
- useEffect as useEffect5
883
+ useEffect as useEffect3,
884
+ useRef as useRef4
920
885
  } from "react";
921
886
 
922
887
  // src/utils/isMobileAgent.ts
@@ -925,31 +890,19 @@ function isMobileAgent(userAgent = navigator.userAgent) {
925
890
  return mobileRegex.test(userAgent);
926
891
  }
927
892
 
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({
893
+ // src/hooks/useKeypadLayout.ts
894
+ import { useCallback as useCallback2, useRef as useRef2 } from "react";
895
+ function useKeypadLayout({
932
896
  layout,
933
- viewport
897
+ viewport,
898
+ hangulMode,
899
+ shift
934
900
  }) {
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]);
901
+ const keyBoundsRef = useRef2([]);
950
902
  const getTransformedValue = useCallback2((cell) => {
951
903
  if (cell.type === "char") {
952
- if (hangulMode) {
904
+ const isConvertibleHangulSource = hangulMode && /[a-zA-Z]/.test(cell.value) && cell.value.length === 1;
905
+ if (isConvertibleHangulSource) {
953
906
  return convertQwertyToHangul(cell.value);
954
907
  }
955
908
  if (shift) {
@@ -959,12 +912,70 @@ function VirtualKeypad({
959
912
  }
960
913
  return cell.label ?? cell.value;
961
914
  }, [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;
915
+ const calculateLayout = useCallback2(() => {
916
+ if (!layout)
917
+ return [];
918
+ const scale = viewport.scale;
919
+ const compactRatio = Math.max(0, Math.min(1, (viewport.width - 260) / 140));
920
+ const padding = (2 + (10 - 2) * compactRatio) / scale;
921
+ const gap = (2 + (8 - 2) * compactRatio) / scale;
922
+ const totalHeight = 200 / scale;
923
+ const width = viewport.width;
924
+ const height = totalHeight;
925
+ const contentW = width - padding * 2;
926
+ const contentH = height - padding * 2;
927
+ const bounds = [];
928
+ const rowCount = layout.length;
929
+ const rowHeight = (contentH - (rowCount - 1) * gap) / rowCount;
930
+ let currentY = padding;
931
+ layout.forEach((row, rowIndex) => {
932
+ const rowWidth = contentW;
933
+ const totalFlexGrow = row.reduce((acc, cell) => acc + (cell.width || 1), 0);
934
+ const totalGapW = (row.length - 1) * gap;
935
+ const availableW = rowWidth - totalGapW;
936
+ const unitW = availableW / totalFlexGrow;
937
+ let currentX = padding;
938
+ row.forEach((cell, colIndex) => {
939
+ const flex = cell.width || 1;
940
+ const cellW = flex * unitW;
941
+ bounds.push({
942
+ x: currentX,
943
+ y: currentY,
944
+ w: cellW,
945
+ h: rowHeight,
946
+ value: cell.value,
947
+ type: cell.type,
948
+ label: getTransformedValue(cell),
949
+ rowIndex,
950
+ colIndex,
951
+ isAction: cell.type === "action"
952
+ });
953
+ currentX += cellW + gap;
954
+ });
955
+ currentY += rowHeight + gap;
956
+ });
957
+ return bounds;
958
+ }, [layout, viewport, getTransformedValue]);
959
+ return {
960
+ keyBoundsRef,
961
+ calculateLayout,
962
+ getTransformedValue
963
+ };
964
+ }
965
+
966
+ // src/hooks/useKeypadInteraction.ts
967
+ import { useCallback as useCallback3, useRef as useRef3, useMemo as useMemo2 } from "react";
968
+ function useKeypadInteraction({
969
+ inputRef,
970
+ toggleShift,
971
+ toggleKorean,
972
+ shift,
973
+ getTransformedValue,
974
+ keyBoundsRef
975
+ }) {
976
+ const activePresses = useRef3(new Map);
977
+ const repeatableKeys = useMemo2(() => new Set(["char", "Backspace", "Delete", "Space", "ArrowLeft", "ArrowRight"]), []);
978
+ const dispatchKeyEvent = useCallback3((value, type) => {
968
979
  if (typeof navigator !== "undefined" && "vibrate" in navigator) {
969
980
  navigator.vibrate?.(10);
970
981
  }
@@ -979,123 +990,287 @@ function VirtualKeypad({
979
990
  }
980
991
  }
981
992
  const event = new KeyboardEvent("keydown", {
982
- key: getTransformedValue({
983
- value,
984
- type
985
- }),
993
+ key: getTransformedValue({ value, type }),
986
994
  code: `Key${value.toUpperCase()}`,
987
995
  bubbles: true,
988
996
  cancelable: true,
989
997
  shiftKey: shift
990
998
  });
991
999
  inputRef.current?.handleKeyDown(event);
992
- }, [inputRef, shift, toggleShift, toggleKorean, getTransformedValue]);
1000
+ }, [getTransformedValue, inputRef, shift, toggleKorean, toggleShift]);
1001
+ const clearRepeat = useCallback3((press) => {
1002
+ if (!press?.repeatTimeout)
1003
+ return;
1004
+ window.clearTimeout(press.repeatTimeout);
1005
+ }, []);
1006
+ const startRepeat = useCallback3((press) => {
1007
+ const canRepeat = press.type && repeatableKeys.has(press.type) || repeatableKeys.has(press.value);
1008
+ if (!canRepeat)
1009
+ return;
1010
+ const firstDelay = 300;
1011
+ const repeatInterval = 60;
1012
+ const tick = () => {
1013
+ const storedPress = activePresses.current.get(press.pointerId);
1014
+ if (!storedPress)
1015
+ return;
1016
+ dispatchKeyEvent(storedPress.value, storedPress.type);
1017
+ storedPress.repeatTimeout = window.setTimeout(tick, repeatInterval);
1018
+ activePresses.current.set(press.pointerId, storedPress);
1019
+ };
1020
+ press.repeatTimeout = window.setTimeout(tick, firstDelay);
1021
+ activePresses.current.set(press.pointerId, press);
1022
+ }, [dispatchKeyEvent, repeatableKeys]);
1023
+ const handlePointerDown = useCallback3((e) => {
1024
+ e.preventDefault();
1025
+ const canvas = e.currentTarget;
1026
+ if (!canvas)
1027
+ return;
1028
+ const rect = canvas.getBoundingClientRect();
1029
+ const x = e.clientX - rect.left;
1030
+ const y = e.clientY - rect.top;
1031
+ const hitKey = keyBoundsRef.current.find((k) => x >= k.x && x <= k.x + k.w && y >= k.y && y <= k.y + k.h);
1032
+ if (hitKey) {
1033
+ const keyIndex = hitKey.rowIndex * 100 + hitKey.colIndex;
1034
+ dispatchKeyEvent(hitKey.value, hitKey.type);
1035
+ const press = {
1036
+ pointerId: e.pointerId,
1037
+ value: hitKey.value,
1038
+ type: hitKey.type,
1039
+ keyIndex
1040
+ };
1041
+ activePresses.current.set(e.pointerId, press);
1042
+ startRepeat(press);
1043
+ }
1044
+ }, [dispatchKeyEvent, startRepeat, keyBoundsRef]);
1045
+ const handlePointerMove = useCallback3((e) => {
1046
+ const press = activePresses.current.get(e.pointerId);
1047
+ if (!press)
1048
+ return;
1049
+ const canvas = e.currentTarget;
1050
+ if (!canvas)
1051
+ return;
1052
+ const rect = canvas.getBoundingClientRect();
1053
+ const x = e.clientX - rect.left;
1054
+ const y = e.clientY - rect.top;
1055
+ const hitKey = keyBoundsRef.current.find((k) => x >= k.x && x <= k.x + k.w && y >= k.y && y <= k.y + k.h);
1056
+ if (hitKey) {
1057
+ const newKeyIndex = hitKey.rowIndex * 100 + hitKey.colIndex;
1058
+ if (newKeyIndex !== press.keyIndex) {
1059
+ clearRepeat(press);
1060
+ dispatchKeyEvent(hitKey.value, hitKey.type);
1061
+ const newPress = {
1062
+ pointerId: e.pointerId,
1063
+ value: hitKey.value,
1064
+ type: hitKey.type,
1065
+ keyIndex: newKeyIndex
1066
+ };
1067
+ activePresses.current.set(e.pointerId, newPress);
1068
+ startRepeat(newPress);
1069
+ }
1070
+ } else {
1071
+ clearRepeat(press);
1072
+ activePresses.current.delete(e.pointerId);
1073
+ }
1074
+ }, [clearRepeat, dispatchKeyEvent, startRepeat, keyBoundsRef]);
1075
+ const handlePointerUp = useCallback3((e) => {
1076
+ const press = activePresses.current.get(e.pointerId);
1077
+ if (press) {
1078
+ clearRepeat(press);
1079
+ activePresses.current.delete(e.pointerId);
1080
+ }
1081
+ }, [clearRepeat]);
1082
+ return {
1083
+ activePresses,
1084
+ handlePointerDown,
1085
+ handlePointerMove,
1086
+ handlePointerUp
1087
+ };
1088
+ }
1089
+
1090
+ // src/components/Keypad.tsx
1091
+ import { jsxDEV as jsxDEV3 } from "react/jsx-dev-runtime";
1092
+ function VirtualKeypad({
1093
+ layout,
1094
+ viewport
1095
+ }) {
1096
+ const {
1097
+ focusId,
1098
+ onBlur,
1099
+ onFocus,
1100
+ inputRef,
1101
+ shift,
1102
+ hangulMode,
1103
+ toggleShift,
1104
+ toggleKorean
1105
+ } = useVirtualInputContext();
1106
+ const canvasRef = useRef4(null);
1107
+ const containerRef = useRef4(null);
1108
+ const {
1109
+ keyBoundsRef,
1110
+ calculateLayout,
1111
+ getTransformedValue
1112
+ } = useKeypadLayout({
1113
+ layout,
1114
+ viewport,
1115
+ hangulMode,
1116
+ shift
1117
+ });
1118
+ const {
1119
+ activePresses,
1120
+ handlePointerDown,
1121
+ handlePointerMove,
1122
+ handlePointerUp
1123
+ } = useKeypadInteraction({
1124
+ inputRef,
1125
+ toggleShift,
1126
+ toggleKorean,
1127
+ shift,
1128
+ getTransformedValue,
1129
+ keyBoundsRef
1130
+ });
1131
+ const drawRoundedRect = (ctx, x, y, w, h, r) => {
1132
+ ctx.beginPath();
1133
+ ctx.moveTo(x + r, y);
1134
+ ctx.lineTo(x + w - r, y);
1135
+ ctx.quadraticCurveTo(x + w, y, x + w, y + r);
1136
+ ctx.lineTo(x + w, y + h - r);
1137
+ ctx.quadraticCurveTo(x + w, y + h, x + w - r, y + h);
1138
+ ctx.lineTo(x + r, y + h);
1139
+ ctx.quadraticCurveTo(x, y + h, x, y + h - r);
1140
+ ctx.lineTo(x, y + r);
1141
+ ctx.quadraticCurveTo(x, y, x + r, y);
1142
+ ctx.closePath();
1143
+ };
1144
+ const draw = useCallback4(() => {
1145
+ const canvas = canvasRef.current;
1146
+ if (!canvas)
1147
+ return;
1148
+ const ctx = canvas.getContext("2d", { alpha: false });
1149
+ if (!ctx)
1150
+ return;
1151
+ const dpr = window.devicePixelRatio || 1;
1152
+ const rect = canvas.getBoundingClientRect();
1153
+ if (Math.abs(canvas.width - rect.width * dpr) > 1 || Math.abs(canvas.height - rect.height * dpr) > 1) {
1154
+ canvas.width = rect.width * dpr;
1155
+ canvas.height = rect.height * dpr;
1156
+ ctx.scale(dpr, dpr);
1157
+ keyBoundsRef.current = calculateLayout();
1158
+ }
1159
+ if (keyBoundsRef.current.length === 0) {
1160
+ keyBoundsRef.current = calculateLayout();
1161
+ }
1162
+ const width = rect.width;
1163
+ const height = rect.height;
1164
+ ctx.fillStyle = "#e8eaee";
1165
+ ctx.fillRect(0, 0, width, height);
1166
+ const scale = viewport.scale;
1167
+ keyBoundsRef.current.forEach((key) => {
1168
+ let isPressed = false;
1169
+ for (const press of activePresses.current.values()) {
1170
+ const keyIndex = key.rowIndex * 100 + key.colIndex;
1171
+ if (press.keyIndex === keyIndex) {
1172
+ isPressed = true;
1173
+ break;
1174
+ }
1175
+ }
1176
+ const isActiveModifier = key.value === "Shift" && shift || key.value === "HangulMode" && hangulMode;
1177
+ const radius = 12 / scale;
1178
+ let drawX = key.x;
1179
+ let drawY = key.y;
1180
+ let drawW = key.w;
1181
+ let drawH = key.h;
1182
+ if (isPressed) {
1183
+ const shrink = 2;
1184
+ drawX += shrink;
1185
+ drawY += shrink;
1186
+ drawW -= shrink * 2;
1187
+ drawH -= shrink * 2;
1188
+ }
1189
+ drawRoundedRect(ctx, drawX, drawY, drawW, drawH, radius);
1190
+ if (isPressed) {
1191
+ const gradient = ctx.createLinearGradient(drawX, drawY, drawX, drawY + drawH);
1192
+ gradient.addColorStop(0, "rgba(248, 249, 251, 0.95)");
1193
+ gradient.addColorStop(1, "#c9d2df");
1194
+ ctx.fillStyle = gradient;
1195
+ } else if (isActiveModifier) {
1196
+ const gradient = ctx.createLinearGradient(drawX, drawY, drawX, drawY + drawH);
1197
+ gradient.addColorStop(0, "#e0ecff");
1198
+ gradient.addColorStop(1, "#99b7ff");
1199
+ ctx.fillStyle = gradient;
1200
+ } else if (key.isAction) {
1201
+ const gradient = ctx.createLinearGradient(drawX, drawY, drawX, drawY + drawH);
1202
+ gradient.addColorStop(0, "#f8fafc");
1203
+ gradient.addColorStop(1, "#cbd5f5");
1204
+ ctx.fillStyle = gradient;
1205
+ } else {
1206
+ const gradient = ctx.createLinearGradient(drawX, drawY, drawX, drawY + drawH);
1207
+ gradient.addColorStop(0, "rgba(255, 255, 255, 0.95)");
1208
+ gradient.addColorStop(1, "#dfe3eb");
1209
+ ctx.fillStyle = gradient;
1210
+ }
1211
+ ctx.fill();
1212
+ ctx.lineWidth = 1 / scale;
1213
+ if (isActiveModifier) {
1214
+ ctx.strokeStyle = "rgba(37, 99, 235, 0.25)";
1215
+ } else if (key.isAction) {
1216
+ ctx.strokeStyle = "rgba(59, 130, 246, 0.55)";
1217
+ } else {
1218
+ ctx.strokeStyle = "rgba(148, 163, 184, 0.7)";
1219
+ }
1220
+ ctx.stroke();
1221
+ ctx.textAlign = "center";
1222
+ ctx.textBaseline = "middle";
1223
+ const fontSize = 18 / scale;
1224
+ ctx.font = `600 ${fontSize}px sans-serif`;
1225
+ if (isActiveModifier || key.isAction) {
1226
+ ctx.fillStyle = "#1e3a8a";
1227
+ } else {
1228
+ ctx.fillStyle = "#1f2933";
1229
+ }
1230
+ ctx.fillText(key.label, drawX + drawW / 2, drawY + drawH / 2);
1231
+ });
1232
+ }, [calculateLayout, hangulMode, shift, viewport.scale, activePresses, keyBoundsRef]);
1233
+ useEffect3(() => {
1234
+ let handle;
1235
+ const loop = () => {
1236
+ draw();
1237
+ handle = requestAnimationFrame(loop);
1238
+ };
1239
+ loop();
1240
+ return () => cancelAnimationFrame(handle);
1241
+ }, [draw]);
993
1242
  if (!focusId || !isMobileAgent())
994
1243
  return null;
995
- return /* @__PURE__ */ jsxDEV4(ShadowWrapper, {
996
- tagName: "virtual-keypad",
1244
+ return /* @__PURE__ */ jsxDEV3(ShadowWrapper, {
1245
+ tagName: "virtual-keypad-canvas",
997
1246
  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,
1247
+ .keypad-wrapper {
1248
+ position: fixed;
1249
+ background-color: #e8eaee;
1250
+ border-radius: calc(18px / var(--scale-factor));
1251
+ box-shadow: 0 calc(-6px / var(--scale-factor)) calc(30px / var(--scale-factor)) rgba(15, 23, 42, 0.2);
1252
+ user-select: none;
1253
+ -webkit-user-select: none;
1254
+ touch-action: none;
1255
+ z-index: 9999;
1256
+ overflow: hidden; /* Canvas containment */
1257
+ }
1258
+ canvas {
1259
+ display: block;
1260
+ width: 100%;
1261
+ height: 100%;
1262
+ }
1263
+ `,
1264
+ children: /* @__PURE__ */ jsxDEV3("div", {
1265
+ ref: containerRef,
1266
+ className: "keypad-wrapper",
1267
+ onFocus: () => {
1268
+ if (focusId)
1269
+ onFocus(focusId);
1270
+ },
1096
1271
  onBlur,
1097
- tabIndex: -1,
1098
- className: "keypad-container",
1272
+ onContextMenu: (e) => e.preventDefault(),
1273
+ onPointerDown: (e) => e.preventDefault(),
1099
1274
  style: {
1100
1275
  left: viewport.offsetLeft,
1101
1276
  width: viewport.width,
@@ -1103,49 +1278,20 @@ function VirtualKeypad({
1103
1278
  height: Math.round(200 / viewport.scale),
1104
1279
  "--scale-factor": viewport.scale
1105
1280
  },
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))
1281
+ children: /* @__PURE__ */ jsxDEV3("canvas", {
1282
+ ref: canvasRef,
1283
+ onPointerDown: handlePointerDown,
1284
+ onPointerMove: handlePointerMove,
1285
+ onPointerUp: handlePointerUp,
1286
+ onPointerLeave: handlePointerUp,
1287
+ onPointerCancel: handlePointerUp
1288
+ }, undefined, false, undefined, this)
1143
1289
  }, undefined, false, undefined, this)
1144
1290
  }, undefined, false, undefined, this);
1145
1291
  }
1146
1292
 
1147
1293
  // src/hooks/useStorage.ts
1148
- import { useState as useState3, useCallback as useCallback3, useEffect as useEffect3 } from "react";
1294
+ import { useState as useState3, useCallback as useCallback5, useEffect as useEffect4 } from "react";
1149
1295
  var isBrowser = typeof window !== "undefined";
1150
1296
  function useStorage(key, initialValue) {
1151
1297
  const [storedValue, setStoredValue] = useState3(() => {
@@ -1166,7 +1312,7 @@ function useStorage(key, initialValue) {
1166
1312
  return initialValue instanceof Function ? initialValue() : initialValue;
1167
1313
  }
1168
1314
  });
1169
- const setValue = useCallback3((value) => {
1315
+ const setValue = useCallback5((value) => {
1170
1316
  if (!isBrowser) {
1171
1317
  console.warn(`Tried to set localStorage key "${key}" in a non-browser environment.`);
1172
1318
  return;
@@ -1179,7 +1325,7 @@ function useStorage(key, initialValue) {
1179
1325
  console.error(`Error setting localStorage key "${key}":`, error);
1180
1326
  }
1181
1327
  }, [key, storedValue]);
1182
- useEffect3(() => {
1328
+ useEffect4(() => {
1183
1329
  if (!isBrowser)
1184
1330
  return;
1185
1331
  const handleStorageChange = (event) => {
@@ -1247,7 +1393,7 @@ var qwerty_default = [
1247
1393
  ];
1248
1394
 
1249
1395
  // src/hooks/useVisualViewport.ts
1250
- import { useState as useState4, useEffect as useEffect4 } from "react";
1396
+ import { useEffect as useEffect5, useState as useState4 } from "react";
1251
1397
  var getVisualViewport = () => {
1252
1398
  if (typeof window !== "undefined" && window.visualViewport) {
1253
1399
  return {
@@ -1266,40 +1412,58 @@ var getVisualViewport = () => {
1266
1412
  offsetTop: 0
1267
1413
  };
1268
1414
  };
1415
+ 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
1416
  var useVisualViewport = () => {
1270
1417
  const [viewport, setViewport] = useState4(getVisualViewport);
1271
- useEffect4(() => {
1418
+ useEffect5(() => {
1419
+ if (typeof window === "undefined")
1420
+ return;
1421
+ let rafId = null;
1422
+ const applyViewportUpdate = () => {
1423
+ const nextViewport = getVisualViewport();
1424
+ setViewport((prev) => isSameViewport(prev, nextViewport) ? prev : nextViewport);
1425
+ };
1272
1426
  const handleResize = () => {
1273
- setViewport(getVisualViewport());
1427
+ if (rafId !== null) {
1428
+ cancelAnimationFrame(rafId);
1429
+ }
1430
+ rafId = requestAnimationFrame(applyViewportUpdate);
1274
1431
  };
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
- };
1432
+ if (window.visualViewport) {
1433
+ window.visualViewport.addEventListener("resize", handleResize, { passive: true });
1434
+ window.visualViewport.addEventListener("scroll", handleResize, { passive: true });
1435
+ } else {
1436
+ window.addEventListener("resize", handleResize, { passive: true });
1282
1437
  }
1283
- window.addEventListener("resize", handleResize);
1284
- return () => window.removeEventListener("resize", handleResize);
1438
+ return () => {
1439
+ if (rafId !== null) {
1440
+ cancelAnimationFrame(rafId);
1441
+ }
1442
+ if (window.visualViewport) {
1443
+ window.visualViewport.removeEventListener("resize", handleResize);
1444
+ window.visualViewport.removeEventListener("scroll", handleResize);
1445
+ } else {
1446
+ window.removeEventListener("resize", handleResize);
1447
+ }
1448
+ };
1285
1449
  }, []);
1286
1450
  return viewport;
1287
1451
  };
1288
1452
 
1289
1453
  // src/components/Provider.tsx
1290
- import { jsxDEV as jsxDEV5 } from "react/jsx-dev-runtime";
1454
+ import { jsxDEV as jsxDEV4 } from "react/jsx-dev-runtime";
1291
1455
  function VirtualInputProvider({
1292
1456
  children,
1293
1457
  layout = qwerty_default,
1294
1458
  defaultHangulMode = true
1295
1459
  }) {
1296
- const inputRef = useRef2(null);
1297
- const sti = useRef2(setTimeout(() => null, 0));
1460
+ const inputRef = useRef5(null);
1461
+ const sti = useRef5(setTimeout(() => null, 0));
1298
1462
  const [focusId, setFocusId] = useState5();
1299
1463
  const [shift, setShift] = useState5(false);
1300
1464
  const [hangulMode, setHangulMode] = useStorage("virtual-keyboard-hangul-mode", defaultHangulMode);
1301
1465
  const viewport = useVisualViewport();
1302
- useEffect5(() => {
1466
+ useEffect6(() => {
1303
1467
  if (focusId) {
1304
1468
  document.body.style.paddingBottom = `${Math.round(200 / viewport.scale)}px`;
1305
1469
  } else {
@@ -1319,14 +1483,14 @@ function VirtualInputProvider({
1319
1483
  isCompositionRef.current = false;
1320
1484
  }, 0);
1321
1485
  };
1322
- const toggleShift = useCallback4(() => {
1486
+ const toggleShift = useCallback6(() => {
1323
1487
  setShift((prev) => !prev);
1324
1488
  }, []);
1325
- const toggleKorean = useCallback4(() => {
1489
+ const toggleKorean = useCallback6(() => {
1326
1490
  setHangulMode((prev) => !prev);
1327
1491
  }, [setHangulMode]);
1328
- const isCompositionRef = useRef2(undefined);
1329
- return /* @__PURE__ */ jsxDEV5(VirtualInputContext.Provider, {
1492
+ const isCompositionRef = useRef5(undefined);
1493
+ return /* @__PURE__ */ jsxDEV4(VirtualInputContext.Provider, {
1330
1494
  value: {
1331
1495
  inputRef,
1332
1496
  isCompositionRef,
@@ -1341,7 +1505,7 @@ function VirtualInputProvider({
1341
1505
  },
1342
1506
  children: [
1343
1507
  children,
1344
- /* @__PURE__ */ jsxDEV5(VirtualKeypad, {
1508
+ /* @__PURE__ */ jsxDEV4(VirtualKeypad, {
1345
1509
  layout,
1346
1510
  viewport
1347
1511
  }, undefined, false, undefined, this)