@uiwwsw/virtual-keyboard 0.7.0 → 0.7.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
@@ -1185,7 +1185,8 @@ function useKeypadInteraction({
1185
1185
  toggleKorean,
1186
1186
  shift,
1187
1187
  getTransformedValue,
1188
- keyBoundsRef
1188
+ keyBoundsRef,
1189
+ calculateLayout
1189
1190
  }) {
1190
1191
  const activePresses = useRef3(new Map);
1191
1192
  const repeatableKeys = useMemo2(() => new Set(["char", "Backspace", "Delete", "Space", "ArrowLeft", "ArrowRight"]), []);
@@ -1211,7 +1212,6 @@ function useKeypadInteraction({
1211
1212
  shiftKey: shift
1212
1213
  });
1213
1214
  inputRef.current?.handleKeyDown(event);
1214
- inputRef.current?.scrollIntoView();
1215
1215
  }, [getTransformedValue, inputRef, shift, toggleKorean, toggleShift]);
1216
1216
  const clearRepeat = useCallback3((press) => {
1217
1217
  if (!press?.repeatTimeout)
@@ -1240,7 +1240,9 @@ function useKeypadInteraction({
1240
1240
  const canvas = e.currentTarget;
1241
1241
  if (!canvas)
1242
1242
  return;
1243
- canvas.setPointerCapture(e.pointerId);
1243
+ if (!keyBoundsRef.current.length) {
1244
+ keyBoundsRef.current = calculateLayout();
1245
+ }
1244
1246
  activePresses.current.forEach((press) => {
1245
1247
  clearRepeat(press);
1246
1248
  });
@@ -1266,7 +1268,7 @@ function useKeypadInteraction({
1266
1268
  keyIndex: -1
1267
1269
  });
1268
1270
  }
1269
- }, [dispatchKeyEvent, startRepeat, keyBoundsRef]);
1271
+ }, [calculateLayout, clearRepeat, dispatchKeyEvent, startRepeat, keyBoundsRef]);
1270
1272
  const handlePointerMove = useCallback3((e) => {
1271
1273
  e.preventDefault();
1272
1274
  const press = activePresses.current.get(e.pointerId);
@@ -1375,9 +1377,23 @@ function VirtualKeypad({
1375
1377
  toggleKorean,
1376
1378
  shift,
1377
1379
  getTransformedValue,
1378
- keyBoundsRef
1380
+ keyBoundsRef,
1381
+ calculateLayout
1379
1382
  });
1380
- const drawKey = (ctx, key, isPressed, isActiveModifier, scale, currentTheme) => {
1383
+ const roundRect = useCallback4((ctx, x, y, w, h, r) => {
1384
+ if (w < 2 * r)
1385
+ r = w / 2;
1386
+ if (h < 2 * r)
1387
+ r = h / 2;
1388
+ ctx.beginPath();
1389
+ ctx.moveTo(x + r, y);
1390
+ ctx.arcTo(x + w, y, x + w, y + h, r);
1391
+ ctx.arcTo(x + w, y + h, x, y + h, r);
1392
+ ctx.arcTo(x, y + h, x, y, r);
1393
+ ctx.arcTo(x, y, x + w, y, r);
1394
+ ctx.closePath();
1395
+ }, []);
1396
+ const drawKey = useCallback4((ctx, key, isPressed, isActiveModifier, scale, currentTheme) => {
1381
1397
  const r = 10 / scale;
1382
1398
  const { x, y, w, h } = key;
1383
1399
  const colors = currentTheme === "dark" ? {
@@ -1421,20 +1437,7 @@ function VirtualKeypad({
1421
1437
  ctx.font = `500 ${fontSize}px "${fontFamily}"`;
1422
1438
  ctx.fillStyle = textColor;
1423
1439
  ctx.fillText(key.label, x + w / 2, y + h / 2 + pressOffset);
1424
- };
1425
- const roundRect = (ctx, x, y, w, h, r) => {
1426
- if (w < 2 * r)
1427
- r = w / 2;
1428
- if (h < 2 * r)
1429
- r = h / 2;
1430
- ctx.beginPath();
1431
- ctx.moveTo(x + r, y);
1432
- ctx.arcTo(x + w, y, x + w, y + h, r);
1433
- ctx.arcTo(x + w, y + h, x, y + h, r);
1434
- ctx.arcTo(x, y + h, x, y, r);
1435
- ctx.arcTo(x, y, x + w, y, r);
1436
- ctx.closePath();
1437
- };
1440
+ }, [roundRect]);
1438
1441
  const draw = useCallback4(() => {
1439
1442
  const canvas = canvasRef.current;
1440
1443
  if (!canvas)
@@ -1474,28 +1477,51 @@ function VirtualKeypad({
1474
1477
  const isActiveModifier = key.value === "Shift" && shift || key.value === "HangulMode" && hangulMode;
1475
1478
  drawKey(ctx, key, isPressed, isActiveModifier, scale, theme);
1476
1479
  });
1477
- }, [calculateLayout, hangulMode, shift, viewport.scale, activePresses, keyBoundsRef, theme]);
1480
+ }, [calculateLayout, drawKey, hangulMode, shift, viewport.scale, activePresses, keyBoundsRef, theme]);
1478
1481
  useEffect4(() => {
1479
1482
  keyBoundsRef.current = [];
1480
1483
  }, [hangulMode, shift, keyBoundsRef]);
1484
+ const outsideTapRef = useRef4(null);
1481
1485
  useEffect4(() => {
1482
- const handleGlobalPointerDown = (e) => {
1486
+ const isInsideVirtual = (path, keypadEl) => {
1487
+ const inKeypad = path.includes(keypadEl);
1488
+ const inInput = path.some((node) => node instanceof Element && node.getAttribute("data-virtual-input") === "true");
1489
+ return inKeypad || inInput;
1490
+ };
1491
+ const handlePointerDown2 = (e) => {
1483
1492
  if (!containerRef.current || !focusId)
1484
1493
  return;
1485
1494
  const path = e.composedPath();
1486
- if (path.includes(containerRef.current)) {
1495
+ if (isInsideVirtual(path, containerRef.current)) {
1496
+ outsideTapRef.current = null;
1487
1497
  return;
1488
1498
  }
1489
- const isInput = path.some((node) => {
1490
- return node instanceof Element && node.getAttribute("data-virtual-input") === "true";
1491
- });
1492
- if (isInput)
1499
+ outsideTapRef.current = { pointerId: e.pointerId, x: e.clientX, y: e.clientY };
1500
+ };
1501
+ const handlePointerUp2 = (e) => {
1502
+ if (!containerRef.current || !focusId)
1503
+ return;
1504
+ const tap = outsideTapRef.current;
1505
+ outsideTapRef.current = null;
1506
+ if (!tap || tap.pointerId !== e.pointerId)
1507
+ return;
1508
+ const dx = Math.abs(e.clientX - tap.x);
1509
+ const dy = Math.abs(e.clientY - tap.y);
1510
+ const moved = Math.hypot(dx, dy) > 10;
1511
+ if (moved)
1512
+ return;
1513
+ const path = e.composedPath();
1514
+ if (isInsideVirtual(path, containerRef.current))
1493
1515
  return;
1494
1516
  onBlur(true);
1495
1517
  };
1496
- window.addEventListener("pointerdown", handleGlobalPointerDown, { capture: true });
1518
+ window.addEventListener("pointerdown", handlePointerDown2, { capture: true });
1519
+ window.addEventListener("pointerup", handlePointerUp2, { capture: true });
1520
+ window.addEventListener("pointercancel", handlePointerUp2, { capture: true });
1497
1521
  return () => {
1498
- window.removeEventListener("pointerdown", handleGlobalPointerDown, { capture: true });
1522
+ window.removeEventListener("pointerdown", handlePointerDown2, { capture: true });
1523
+ window.removeEventListener("pointerup", handlePointerUp2, { capture: true });
1524
+ window.removeEventListener("pointercancel", handlePointerUp2, { capture: true });
1499
1525
  };
1500
1526
  }, [focusId, onBlur]);
1501
1527
  useEffect4(() => {
@@ -13,13 +13,14 @@ type VirtualInputContextType = {
13
13
  toggleKorean: () => void;
14
14
  shift: boolean;
15
15
  };
16
- export declare function useKeypadInteraction({ inputRef, toggleShift, toggleKorean, shift, getTransformedValue, keyBoundsRef }: VirtualInputContextType & {
16
+ export declare function useKeypadInteraction({ inputRef, toggleShift, toggleKorean, shift, getTransformedValue, keyBoundsRef, calculateLayout, }: VirtualInputContextType & {
17
17
  getTransformedValue: (cell: {
18
18
  label?: string;
19
19
  value: string;
20
20
  type?: string;
21
21
  }) => string;
22
22
  keyBoundsRef: React.MutableRefObject<KeyBounds[]>;
23
+ calculateLayout: () => KeyBounds[];
23
24
  }): {
24
25
  activePresses: import("react").RefObject<Map<number, ActivePress>>;
25
26
  handlePointerDown: (e: React.PointerEvent<HTMLCanvasElement>) => void;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@uiwwsw/virtual-keyboard",
3
- "version": "0.7.0",
3
+ "version": "0.7.1",
4
4
  "type": "module",
5
5
  "types": "dist/types/index.d.ts",
6
6
  "main": "dist/index.js",