gistda-sphere-react 1.0.0 → 1.0.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.
@@ -1,4 +1,4 @@
1
- import { forwardRef, useEffect, useImperativeHandle, useRef } from "react";
1
+ import { type Ref, useEffect, useImperativeHandle, useRef } from "react";
2
2
  import { useMapContext } from "../context/MapContext";
3
3
  import { useSphereContext } from "../context/SphereContext";
4
4
  import type {
@@ -12,8 +12,20 @@ import type {
12
12
  SpherePolyline,
13
13
  } from "../types";
14
14
 
15
+ export interface PolylineRef {
16
+ getPolyline(): SpherePolyline | null;
17
+ togglePopup(show?: boolean, location?: Location): void;
18
+ getPivot(): Location | null;
19
+ getCentroid(): Location | null;
20
+ getBound(): Bound | null;
21
+ getLength(language?: string): number | string | null;
22
+ rotate(angle: number): void;
23
+ updateStyle(options: Partial<GeometryOptions>): void;
24
+ }
25
+
15
26
  export interface PolylineProps {
16
27
  positions: Location[];
28
+ ref?: Ref<PolylineRef>;
17
29
  title?: string;
18
30
  detail?: string;
19
31
  label?: string;
@@ -33,179 +45,162 @@ export interface PolylineProps {
33
45
  onDrop?: (polyline: SpherePolyline) => void;
34
46
  }
35
47
 
36
- export interface PolylineRef {
37
- getPolyline(): SpherePolyline | null;
38
- togglePopup(show?: boolean, location?: Location): void;
39
- getPivot(): Location | null;
40
- getCentroid(): Location | null;
41
- getBound(): Bound | null;
42
- getLength(language?: string): number | string | null;
43
- rotate(angle: number): void;
44
- updateStyle(options: Partial<GeometryOptions>): void;
45
- }
46
-
47
- export const Polyline = forwardRef<PolylineRef, PolylineProps>(
48
- function Polyline(
49
- {
50
- positions,
51
- title,
52
- detail,
53
- label,
54
- labelOptions,
55
- popup,
56
- visibleRange,
57
- lineWidth,
58
- lineColor,
59
- lineStyle,
60
- pivot,
61
- clickable,
62
- draggable,
63
- pointer,
64
- zIndex,
65
- onClick,
66
- onDrag,
67
- onDrop,
68
- },
69
- ref
70
- ) {
71
- const { map, isReady } = useMapContext();
72
- const { sphere } = useSphereContext();
73
- const polylineRef = useRef<SpherePolyline | null>(null);
74
- const callbacksRef = useRef({ onClick, onDrag, onDrop });
75
-
76
- useEffect(() => {
77
- callbacksRef.current = { onClick, onDrag, onDrop };
78
- }, [onClick, onDrag, onDrop]);
79
-
80
- useEffect(() => {
81
- if (!(isReady && map && sphere) || positions.length < 2) {
82
- return;
48
+ export function Polyline({
49
+ positions,
50
+ ref,
51
+ title,
52
+ detail,
53
+ label,
54
+ labelOptions,
55
+ popup,
56
+ visibleRange,
57
+ lineWidth,
58
+ lineColor,
59
+ lineStyle,
60
+ pivot,
61
+ clickable,
62
+ draggable,
63
+ pointer,
64
+ zIndex,
65
+ onClick,
66
+ onDrag,
67
+ onDrop,
68
+ }: PolylineProps) {
69
+ const { map, isReady } = useMapContext();
70
+ const { sphere } = useSphereContext();
71
+ const polylineRef = useRef<SpherePolyline | null>(null);
72
+ const callbacksRef = useRef({ onClick, onDrag, onDrop });
73
+
74
+ useEffect(() => {
75
+ callbacksRef.current = { onClick, onDrag, onDrop };
76
+ }, [onClick, onDrag, onDrop]);
77
+
78
+ useEffect(() => {
79
+ if (!(isReady && map && sphere) || positions.length < 2) {
80
+ return;
81
+ }
82
+
83
+ const options: GeometryOptions = {};
84
+
85
+ if (title) {
86
+ options.title = title;
87
+ }
88
+ if (detail) {
89
+ options.detail = detail;
90
+ }
91
+ if (label) {
92
+ options.label = label;
93
+ }
94
+ if (labelOptions) {
95
+ options.labelOptions = labelOptions;
96
+ }
97
+ if (popup) {
98
+ options.popup = popup;
99
+ }
100
+ if (visibleRange) {
101
+ options.visibleRange = visibleRange;
102
+ }
103
+ if (typeof lineWidth === "number") {
104
+ options.lineWidth = lineWidth;
105
+ }
106
+ if (lineColor) {
107
+ options.lineColor = lineColor;
108
+ }
109
+ if (lineStyle) {
110
+ options.lineStyle = lineStyle;
111
+ }
112
+ if (pivot) {
113
+ options.pivot = pivot;
114
+ }
115
+ if (typeof clickable === "boolean") {
116
+ options.clickable = clickable;
117
+ }
118
+ if (typeof draggable === "boolean") {
119
+ options.draggable = draggable;
120
+ }
121
+ if (typeof pointer === "boolean") {
122
+ options.pointer = pointer;
123
+ }
124
+ if (typeof zIndex === "number") {
125
+ options.zIndex = zIndex;
126
+ }
127
+
128
+ const polyline = new sphere.Polyline(positions, options);
129
+ polylineRef.current = polyline;
130
+
131
+ map.Overlays.add(polyline);
132
+
133
+ const handleOverlayClick = (data: { overlay: SpherePolyline }) => {
134
+ if (data.overlay === polyline) {
135
+ callbacksRef.current.onClick?.(polyline);
83
136
  }
137
+ };
84
138
 
85
- const options: GeometryOptions = {};
86
-
87
- if (title) {
88
- options.title = title;
89
- }
90
- if (detail) {
91
- options.detail = detail;
92
- }
93
- if (label) {
94
- options.label = label;
95
- }
96
- if (labelOptions) {
97
- options.labelOptions = labelOptions;
98
- }
99
- if (popup) {
100
- options.popup = popup;
101
- }
102
- if (visibleRange) {
103
- options.visibleRange = visibleRange;
104
- }
105
- if (typeof lineWidth === "number") {
106
- options.lineWidth = lineWidth;
107
- }
108
- if (lineColor) {
109
- options.lineColor = lineColor;
110
- }
111
- if (lineStyle) {
112
- options.lineStyle = lineStyle;
113
- }
114
- if (pivot) {
115
- options.pivot = pivot;
116
- }
117
- if (typeof clickable === "boolean") {
118
- options.clickable = clickable;
119
- }
120
- if (typeof draggable === "boolean") {
121
- options.draggable = draggable;
122
- }
123
- if (typeof pointer === "boolean") {
124
- options.pointer = pointer;
125
- }
126
- if (typeof zIndex === "number") {
127
- options.zIndex = zIndex;
139
+ const handleOverlayDrag = (overlay: SpherePolyline) => {
140
+ if (overlay === polyline) {
141
+ callbacksRef.current.onDrag?.(polyline);
128
142
  }
143
+ };
129
144
 
130
- const polyline = new sphere.Polyline(positions, options);
131
- polylineRef.current = polyline;
132
-
133
- map.Overlays.add(polyline);
134
-
135
- const handleOverlayClick = (data: { overlay: SpherePolyline }) => {
136
- if (data.overlay === polyline) {
137
- callbacksRef.current.onClick?.(polyline);
138
- }
139
- };
140
-
141
- const handleOverlayDrag = (overlay: SpherePolyline) => {
142
- if (overlay === polyline) {
143
- callbacksRef.current.onDrag?.(polyline);
144
- }
145
- };
146
-
147
- const handleOverlayDrop = (overlay: SpherePolyline) => {
148
- if (overlay === polyline) {
149
- callbacksRef.current.onDrop?.(polyline);
150
- }
151
- };
152
-
153
- map.Event.bind("overlayClick", handleOverlayClick);
154
- map.Event.bind("overlayDrag", handleOverlayDrag);
155
- map.Event.bind("overlayDrop", handleOverlayDrop);
156
-
157
- return () => {
158
- map.Event.unbind("overlayClick", handleOverlayClick);
159
- map.Event.unbind("overlayDrag", handleOverlayDrag);
160
- map.Event.unbind("overlayDrop", handleOverlayDrop);
161
- map.Overlays.remove(polyline);
162
- polylineRef.current = null;
163
- };
164
- }, [
165
- isReady,
166
- map,
167
- sphere,
168
- positions,
169
- title,
170
- detail,
171
- label,
172
- labelOptions,
173
- popup,
174
- visibleRange,
175
- lineWidth,
176
- lineColor,
177
- lineStyle,
178
- pivot,
179
- clickable,
180
- draggable,
181
- pointer,
182
- zIndex,
183
- ]);
184
-
185
- useImperativeHandle(
186
- ref,
187
- () => ({
188
- getPolyline: () => polylineRef.current,
189
- togglePopup: (show?: boolean, location?: Location) => {
190
- polylineRef.current?.pop(show, location);
191
- },
192
- getPivot: () => polylineRef.current?.pivot() ?? null,
193
- getCentroid: () => polylineRef.current?.centroid() ?? null,
194
- getBound: () => polylineRef.current?.bound() ?? null,
195
- getLength: (language?: string) =>
196
- polylineRef.current?.size(language) ?? null,
197
- rotate: (angle: number) => {
198
- polylineRef.current?.rotate(angle);
199
- },
200
- updateStyle: (options: Partial<GeometryOptions>) => {
201
- polylineRef.current?.update(options);
202
- },
203
- }),
204
- []
205
- );
206
-
207
- return null;
208
- }
209
- );
210
-
211
- export default Polyline;
145
+ const handleOverlayDrop = (overlay: SpherePolyline) => {
146
+ if (overlay === polyline) {
147
+ callbacksRef.current.onDrop?.(polyline);
148
+ }
149
+ };
150
+
151
+ map.Event.bind("overlayClick", handleOverlayClick);
152
+ map.Event.bind("overlayDrag", handleOverlayDrag);
153
+ map.Event.bind("overlayDrop", handleOverlayDrop);
154
+
155
+ return () => {
156
+ map.Event.unbind("overlayClick", handleOverlayClick);
157
+ map.Event.unbind("overlayDrag", handleOverlayDrag);
158
+ map.Event.unbind("overlayDrop", handleOverlayDrop);
159
+ map.Overlays.remove(polyline);
160
+ polylineRef.current = null;
161
+ };
162
+ }, [
163
+ isReady,
164
+ map,
165
+ sphere,
166
+ positions,
167
+ title,
168
+ detail,
169
+ label,
170
+ labelOptions,
171
+ popup,
172
+ visibleRange,
173
+ lineWidth,
174
+ lineColor,
175
+ lineStyle,
176
+ pivot,
177
+ clickable,
178
+ draggable,
179
+ pointer,
180
+ zIndex,
181
+ ]);
182
+
183
+ useImperativeHandle(
184
+ ref,
185
+ () => ({
186
+ getPolyline: () => polylineRef.current,
187
+ togglePopup: (show?: boolean, location?: Location) => {
188
+ polylineRef.current?.pop(show, location);
189
+ },
190
+ getPivot: () => polylineRef.current?.pivot() ?? null,
191
+ getCentroid: () => polylineRef.current?.centroid() ?? null,
192
+ getBound: () => polylineRef.current?.bound() ?? null,
193
+ getLength: (language?: string) =>
194
+ polylineRef.current?.size(language) ?? null,
195
+ rotate: (angle: number) => {
196
+ polylineRef.current?.rotate(angle);
197
+ },
198
+ updateStyle: (options: Partial<GeometryOptions>) => {
199
+ polylineRef.current?.update(options);
200
+ },
201
+ }),
202
+ []
203
+ );
204
+
205
+ return null;
206
+ }
@@ -1,10 +1,19 @@
1
- import { forwardRef, useEffect, useImperativeHandle, useRef } from "react";
1
+ import { type Ref, useEffect, useImperativeHandle, useRef } from "react";
2
2
  import { useMapContext } from "../context/MapContext";
3
3
  import { useSphereContext } from "../context/SphereContext";
4
4
  import type { Location, PopupOptions, Size, SpherePopup } from "../types";
5
5
 
6
+ export interface PopupRef {
7
+ getPopup(): SpherePopup | null;
8
+ setPosition(location: Location): void;
9
+ setTitle(title: string): void;
10
+ setDetail(detail: string): void;
11
+ getElement(): HTMLElement | null;
12
+ }
13
+
6
14
  export interface PopupProps {
7
15
  position: Location;
16
+ ref?: Ref<PopupRef>;
8
17
  title?: string;
9
18
  detail?: string;
10
19
  loadDetail?: (element: HTMLElement) => void;
@@ -15,28 +24,18 @@ export interface PopupProps {
15
24
  onClose?: (popup: SpherePopup) => void;
16
25
  }
17
26
 
18
- export interface PopupRef {
19
- getPopup(): SpherePopup | null;
20
- setPosition(location: Location): void;
21
- setTitle(title: string): void;
22
- setDetail(detail: string): void;
23
- getElement(): HTMLElement | null;
24
- }
25
-
26
- export const Popup = forwardRef<PopupRef, PopupProps>(function Popup(
27
- {
28
- position,
29
- title,
30
- detail,
31
- loadDetail,
32
- html,
33
- loadHtml,
34
- size,
35
- closable = true,
36
- onClose,
37
- },
38
- ref
39
- ) {
27
+ export function Popup({
28
+ position,
29
+ ref,
30
+ title,
31
+ detail,
32
+ loadDetail,
33
+ html,
34
+ loadHtml,
35
+ size,
36
+ closable = true,
37
+ onClose,
38
+ }: PopupProps) {
40
39
  const { map, isReady } = useMapContext();
41
40
  const { sphere } = useSphereContext();
42
41
  const popupRef = useRef<SpherePopup | null>(null);
@@ -125,6 +124,4 @@ export const Popup = forwardRef<PopupRef, PopupProps>(function Popup(
125
124
  );
126
125
 
127
126
  return null;
128
- });
129
-
130
- export default Popup;
127
+ }