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,9 +12,18 @@ import type {
12
12
  SphereRectangle,
13
13
  } from "../types";
14
14
 
15
+ export interface RectangleRef {
16
+ getRectangle(): SphereRectangle | null;
17
+ togglePopup(show?: boolean, location?: Location): void;
18
+ getBound(): Bound | null;
19
+ getArea(language?: string): number | string | null;
20
+ updateStyle(options: Partial<GeometryOptions>): void;
21
+ }
22
+
15
23
  export interface RectangleProps {
16
24
  position: Location;
17
25
  size: Size | Location;
26
+ ref?: Ref<RectangleRef>;
18
27
  title?: string;
19
28
  detail?: string;
20
29
  popup?: PopupOptions;
@@ -32,163 +41,149 @@ export interface RectangleProps {
32
41
  onDrop?: (rectangle: SphereRectangle) => void;
33
42
  }
34
43
 
35
- export interface RectangleRef {
36
- getRectangle(): SphereRectangle | null;
37
- togglePopup(show?: boolean, location?: Location): void;
38
- getBound(): Bound | null;
39
- getArea(language?: string): number | string | null;
40
- updateStyle(options: Partial<GeometryOptions>): void;
41
- }
42
-
43
- export const Rectangle = forwardRef<RectangleRef, RectangleProps>(
44
- function Rectangle(
45
- {
46
- position,
47
- size,
48
- title,
49
- detail,
50
- popup,
51
- visibleRange,
52
- lineWidth,
53
- lineColor,
54
- fillColor,
55
- lineStyle,
56
- clickable,
57
- draggable,
58
- editable,
59
- zIndex,
60
- onClick,
61
- onDrag,
62
- onDrop,
63
- },
64
- ref
65
- ) {
66
- const { map, isReady } = useMapContext();
67
- const { sphere } = useSphereContext();
68
- const rectangleRef = useRef<SphereRectangle | null>(null);
69
- const callbacksRef = useRef({ onClick, onDrag, onDrop });
70
-
71
- useEffect(() => {
72
- callbacksRef.current = { onClick, onDrag, onDrop };
73
- }, [onClick, onDrag, onDrop]);
74
-
75
- useEffect(() => {
76
- if (!(isReady && map && sphere)) {
77
- return;
44
+ export function Rectangle({
45
+ position,
46
+ size,
47
+ ref,
48
+ title,
49
+ detail,
50
+ popup,
51
+ visibleRange,
52
+ lineWidth,
53
+ lineColor,
54
+ fillColor,
55
+ lineStyle,
56
+ clickable,
57
+ draggable,
58
+ editable,
59
+ zIndex,
60
+ onClick,
61
+ onDrag,
62
+ onDrop,
63
+ }: RectangleProps) {
64
+ const { map, isReady } = useMapContext();
65
+ const { sphere } = useSphereContext();
66
+ const rectangleRef = useRef<SphereRectangle | null>(null);
67
+ const callbacksRef = useRef({ onClick, onDrag, onDrop });
68
+
69
+ useEffect(() => {
70
+ callbacksRef.current = { onClick, onDrag, onDrop };
71
+ }, [onClick, onDrag, onDrop]);
72
+
73
+ useEffect(() => {
74
+ if (!(isReady && map && sphere)) {
75
+ return;
76
+ }
77
+
78
+ const options: GeometryOptions = {};
79
+
80
+ if (title) {
81
+ options.title = title;
82
+ }
83
+ if (detail) {
84
+ options.detail = detail;
85
+ }
86
+ if (popup) {
87
+ options.popup = popup;
88
+ }
89
+ if (visibleRange) {
90
+ options.visibleRange = visibleRange;
91
+ }
92
+ if (typeof lineWidth === "number") {
93
+ options.lineWidth = lineWidth;
94
+ }
95
+ if (lineColor) {
96
+ options.lineColor = lineColor;
97
+ }
98
+ if (fillColor) {
99
+ options.fillColor = fillColor;
100
+ }
101
+ if (lineStyle) {
102
+ options.lineStyle = lineStyle;
103
+ }
104
+ if (typeof clickable === "boolean") {
105
+ options.clickable = clickable;
106
+ }
107
+ if (typeof draggable === "boolean") {
108
+ options.draggable = draggable;
109
+ }
110
+ if (typeof editable === "boolean") {
111
+ options.editable = editable;
112
+ }
113
+ if (typeof zIndex === "number") {
114
+ options.zIndex = zIndex;
115
+ }
116
+
117
+ const rectangle = new sphere.Rectangle(position, size, options);
118
+ rectangleRef.current = rectangle;
119
+
120
+ map.Overlays.add(rectangle);
121
+
122
+ const handleOverlayClick = (data: { overlay: SphereRectangle }) => {
123
+ if (data.overlay === rectangle) {
124
+ callbacksRef.current.onClick?.(rectangle);
78
125
  }
126
+ };
79
127
 
80
- const options: GeometryOptions = {};
81
-
82
- if (title) {
83
- options.title = title;
84
- }
85
- if (detail) {
86
- options.detail = detail;
87
- }
88
- if (popup) {
89
- options.popup = popup;
90
- }
91
- if (visibleRange) {
92
- options.visibleRange = visibleRange;
93
- }
94
- if (typeof lineWidth === "number") {
95
- options.lineWidth = lineWidth;
96
- }
97
- if (lineColor) {
98
- options.lineColor = lineColor;
99
- }
100
- if (fillColor) {
101
- options.fillColor = fillColor;
102
- }
103
- if (lineStyle) {
104
- options.lineStyle = lineStyle;
105
- }
106
- if (typeof clickable === "boolean") {
107
- options.clickable = clickable;
108
- }
109
- if (typeof draggable === "boolean") {
110
- options.draggable = draggable;
111
- }
112
- if (typeof editable === "boolean") {
113
- options.editable = editable;
114
- }
115
- if (typeof zIndex === "number") {
116
- options.zIndex = zIndex;
128
+ const handleOverlayDrag = (overlay: SphereRectangle) => {
129
+ if (overlay === rectangle) {
130
+ callbacksRef.current.onDrag?.(rectangle);
117
131
  }
132
+ };
118
133
 
119
- const rectangle = new sphere.Rectangle(position, size, options);
120
- rectangleRef.current = rectangle;
121
-
122
- map.Overlays.add(rectangle);
123
-
124
- const handleOverlayClick = (data: { overlay: SphereRectangle }) => {
125
- if (data.overlay === rectangle) {
126
- callbacksRef.current.onClick?.(rectangle);
127
- }
128
- };
129
-
130
- const handleOverlayDrag = (overlay: SphereRectangle) => {
131
- if (overlay === rectangle) {
132
- callbacksRef.current.onDrag?.(rectangle);
133
- }
134
- };
135
-
136
- const handleOverlayDrop = (overlay: SphereRectangle) => {
137
- if (overlay === rectangle) {
138
- callbacksRef.current.onDrop?.(rectangle);
139
- }
140
- };
141
-
142
- map.Event.bind("overlayClick", handleOverlayClick);
143
- map.Event.bind("overlayDrag", handleOverlayDrag);
144
- map.Event.bind("overlayDrop", handleOverlayDrop);
145
-
146
- return () => {
147
- map.Event.unbind("overlayClick", handleOverlayClick);
148
- map.Event.unbind("overlayDrag", handleOverlayDrag);
149
- map.Event.unbind("overlayDrop", handleOverlayDrop);
150
- map.Overlays.remove(rectangle);
151
- rectangleRef.current = null;
152
- };
153
- }, [
154
- isReady,
155
- map,
156
- sphere,
157
- position,
158
- size,
159
- title,
160
- detail,
161
- popup,
162
- visibleRange,
163
- lineWidth,
164
- lineColor,
165
- fillColor,
166
- lineStyle,
167
- clickable,
168
- draggable,
169
- editable,
170
- zIndex,
171
- ]);
172
-
173
- useImperativeHandle(
174
- ref,
175
- () => ({
176
- getRectangle: () => rectangleRef.current,
177
- togglePopup: (show?: boolean, location?: Location) => {
178
- rectangleRef.current?.pop(show, location);
179
- },
180
- getBound: () => rectangleRef.current?.bound() ?? null,
181
- getArea: (language?: string) =>
182
- rectangleRef.current?.size(language) ?? null,
183
- updateStyle: (options: Partial<GeometryOptions>) => {
184
- rectangleRef.current?.update(options);
185
- },
186
- }),
187
- []
188
- );
189
-
190
- return null;
191
- }
192
- );
193
-
194
- export default Rectangle;
134
+ const handleOverlayDrop = (overlay: SphereRectangle) => {
135
+ if (overlay === rectangle) {
136
+ callbacksRef.current.onDrop?.(rectangle);
137
+ }
138
+ };
139
+
140
+ map.Event.bind("overlayClick", handleOverlayClick);
141
+ map.Event.bind("overlayDrag", handleOverlayDrag);
142
+ map.Event.bind("overlayDrop", handleOverlayDrop);
143
+
144
+ return () => {
145
+ map.Event.unbind("overlayClick", handleOverlayClick);
146
+ map.Event.unbind("overlayDrag", handleOverlayDrag);
147
+ map.Event.unbind("overlayDrop", handleOverlayDrop);
148
+ map.Overlays.remove(rectangle);
149
+ rectangleRef.current = null;
150
+ };
151
+ }, [
152
+ isReady,
153
+ map,
154
+ sphere,
155
+ position,
156
+ size,
157
+ title,
158
+ detail,
159
+ popup,
160
+ visibleRange,
161
+ lineWidth,
162
+ lineColor,
163
+ fillColor,
164
+ lineStyle,
165
+ clickable,
166
+ draggable,
167
+ editable,
168
+ zIndex,
169
+ ]);
170
+
171
+ useImperativeHandle(
172
+ ref,
173
+ () => ({
174
+ getRectangle: () => rectangleRef.current,
175
+ togglePopup: (show?: boolean, location?: Location) => {
176
+ rectangleRef.current?.pop(show, location);
177
+ },
178
+ getBound: () => rectangleRef.current?.bound() ?? null,
179
+ getArea: (language?: string) =>
180
+ rectangleRef.current?.size(language) ?? null,
181
+ updateStyle: (options: Partial<GeometryOptions>) => {
182
+ rectangleRef.current?.update(options);
183
+ },
184
+ }),
185
+ []
186
+ );
187
+
188
+ return null;
189
+ }