@tpzdsp/next-toolkit 1.4.5 → 1.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tpzdsp/next-toolkit",
3
- "version": "1.4.5",
3
+ "version": "1.5.0",
4
4
  "description": "A reusable React component library for Next.js applications",
5
5
  "type": "module",
6
6
  "private": false,
@@ -24,7 +24,7 @@
24
24
  /* Component-specific styles */
25
25
  @layer components {
26
26
  .focus-yellow {
27
- @apply z-20 focus:border-[#ffbf47] focus:outline focus:outline-[3px] focus:outline-[#ffbf47];
27
+ @apply focus:border-[#ffbf47] focus:outline focus:outline-[3px] focus:outline-[#ffbf47];
28
28
  }
29
29
 
30
30
  .library-button {
package/src/map/index.ts CHANGED
@@ -5,6 +5,8 @@ export * from './LayerSwitcherControl';
5
5
  export * from './utils';
6
6
  export * from './MapComponent';
7
7
  export * from './MapContext';
8
+ export * from './useKeyboardDrawing';
9
+ export * from './useVirtualCursor';
8
10
  export * from './osOpenNamesSearch';
9
11
  export * from './Popup';
10
12
  export * from './projections';
@@ -0,0 +1,114 @@
1
+ import { useCallback, useEffect, useState } from 'react';
2
+
3
+ import { Map as OlMap } from 'ol';
4
+ import { type Coordinate } from 'ol/coordinate';
5
+
6
+ import { useVirtualCursor } from './useVirtualCursor';
7
+ import { KeyboardKeys } from './utils';
8
+
9
+ type UseKeyboardDrawingProps = {
10
+ map: OlMap | undefined;
11
+ isDrawing: boolean;
12
+ addVertex: (coord: Coordinate) => void;
13
+ finishDrawing: () => void;
14
+ cancelDrawing: () => void;
15
+ };
16
+
17
+ const DELTA = 20; // Base movement in pixels
18
+
19
+ export const useKeyboardDrawing = ({
20
+ map,
21
+ isDrawing,
22
+ addVertex,
23
+ finishDrawing,
24
+ cancelDrawing,
25
+ }: UseKeyboardDrawingProps) => {
26
+ const [cursorPosition, setCursorPosition] = useState<Coordinate | null>(null);
27
+
28
+ // Integrate the virtual cursor overlay
29
+ useVirtualCursor(map, isDrawing, cursorPosition);
30
+
31
+ // Initialize cursor at map center when drawing starts
32
+ useEffect(() => {
33
+ if (isDrawing && map) {
34
+ setCursorPosition(map.getView().getCenter() ?? [0, 0]);
35
+ }
36
+
37
+ if (!isDrawing) {
38
+ setCursorPosition(null);
39
+ }
40
+ }, [isDrawing, map]);
41
+
42
+ // Move the virtual cursor by dx/dy steps
43
+ const moveCursor = useCallback(
44
+ (dx: number, dy: number) => {
45
+ if (!cursorPosition || !map) {
46
+ return;
47
+ }
48
+
49
+ const view = map.getView();
50
+ const resolution = view.getResolution() ?? 1;
51
+ const delta = DELTA * resolution;
52
+
53
+ setCursorPosition([
54
+ cursorPosition[0] + dx * delta,
55
+ cursorPosition[1] - dy * delta, // Invert Y for map coordinates
56
+ ]);
57
+ },
58
+ [cursorPosition, map],
59
+ );
60
+
61
+ // Handle keyboard navigation and update cursor
62
+ useEffect(() => {
63
+ if (!map || !isDrawing) {
64
+ return;
65
+ }
66
+
67
+ const handleKeyDown = (event: KeyboardEvent) => {
68
+ if (!isDrawing) {
69
+ return;
70
+ }
71
+
72
+ switch (event.key) {
73
+ case KeyboardKeys.ArrowUp:
74
+ moveCursor(0, -1);
75
+ event.preventDefault();
76
+ break;
77
+ case KeyboardKeys.ArrowDown:
78
+ moveCursor(0, 1);
79
+ event.preventDefault();
80
+ break;
81
+ case KeyboardKeys.ArrowLeft:
82
+ moveCursor(-1, 0);
83
+ event.preventDefault();
84
+ break;
85
+ case KeyboardKeys.ArrowRight:
86
+ moveCursor(1, 0);
87
+ event.preventDefault();
88
+ break;
89
+ case KeyboardKeys.Enter:
90
+ case KeyboardKeys.Space:
91
+ if (cursorPosition) {
92
+ addVertex(cursorPosition);
93
+ }
94
+
95
+ event.preventDefault();
96
+ break;
97
+ case KeyboardKeys.Escape:
98
+ cancelDrawing();
99
+ event.preventDefault();
100
+ break;
101
+ case KeyboardKeys.F:
102
+ finishDrawing();
103
+ event.preventDefault();
104
+ break;
105
+ default:
106
+ break;
107
+ }
108
+ };
109
+
110
+ window.addEventListener('keydown', handleKeyDown);
111
+
112
+ return () => window.removeEventListener('keydown', handleKeyDown);
113
+ }, [map, isDrawing, cursorPosition, addVertex, finishDrawing, cancelDrawing, moveCursor]);
114
+ };
@@ -0,0 +1,72 @@
1
+ import { useEffect, useRef } from 'react';
2
+
3
+ import { Map as OlMap } from 'ol';
4
+ import { type Coordinate } from 'ol/coordinate';
5
+ import Overlay from 'ol/Overlay';
6
+
7
+ const createCursorElement = (): HTMLDivElement => {
8
+ const virtualCursor = document.createElement('div');
9
+
10
+ virtualCursor.style.width = '18px';
11
+ virtualCursor.style.height = '18px';
12
+ virtualCursor.style.background = 'rgba(255, 193, 7, 0.8)';
13
+ virtualCursor.style.border = '2px solid #ffbf47';
14
+ virtualCursor.style.borderRadius = '50%';
15
+ virtualCursor.style.position = 'absolute';
16
+ virtualCursor.style.transform = 'translate(-50%, -50%)';
17
+ virtualCursor.style.pointerEvents = 'none';
18
+ virtualCursor.setAttribute('aria-hidden', 'true');
19
+
20
+ return virtualCursor;
21
+ };
22
+
23
+ export const useVirtualCursor = (
24
+ map: OlMap | undefined,
25
+ isActive: boolean,
26
+ position: Coordinate | null,
27
+ ) => {
28
+ const overlayRef = useRef<Overlay | null>(null);
29
+
30
+ useEffect(() => {
31
+ if (!map) {
32
+ return;
33
+ }
34
+
35
+ if (!overlayRef.current) {
36
+ const el = createCursorElement();
37
+
38
+ overlayRef.current = new Overlay({
39
+ element: el,
40
+ positioning: 'center-center',
41
+ stopEvent: false,
42
+ });
43
+ map.addOverlay(overlayRef.current);
44
+ }
45
+
46
+ const overlay = overlayRef.current;
47
+ const element = overlay?.getElement();
48
+
49
+ if (element) {
50
+ if (isActive && position) {
51
+ overlay.setPosition(position);
52
+ element.style.display = '';
53
+ } else {
54
+ overlay.setPosition(undefined);
55
+ element.style.display = 'none';
56
+ }
57
+ }
58
+
59
+ return () => {
60
+ if (overlayRef.current) {
61
+ const el = overlayRef.current.getElement();
62
+
63
+ if (el) {
64
+ el.style.display = 'none';
65
+ }
66
+
67
+ map.removeOverlay(overlayRef.current);
68
+ overlayRef.current = null;
69
+ }
70
+ };
71
+ }, [map, isActive, position]);
72
+ };
package/src/map/utils.ts CHANGED
@@ -38,3 +38,14 @@ export const getPopupPositionClass = (coordinate: number[], map: Map): PopupDire
38
38
 
39
39
  return 'bottom-right';
40
40
  };
41
+
42
+ export const KeyboardKeys = {
43
+ ArrowUp: 'ArrowUp',
44
+ ArrowDown: 'ArrowDown',
45
+ ArrowLeft: 'ArrowLeft',
46
+ ArrowRight: 'ArrowRight',
47
+ Enter: 'Enter',
48
+ Space: ' ',
49
+ Escape: 'Escape',
50
+ F: 'f',
51
+ } as const;