@nocobase/plugin-map 0.11.1-alpha.5 → 0.12.0-alpha.2

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.
Files changed (197) hide show
  1. package/client.d.ts +2 -3
  2. package/client.js +1 -1
  3. package/dist/client/index.js +3092 -0
  4. package/{lib → dist}/index.d.ts +1 -0
  5. package/dist/index.js +18 -0
  6. package/dist/locale/en-US.js +6 -0
  7. package/dist/locale/fr-FR.js +6 -0
  8. package/dist/locale/pt-BR.js +51 -0
  9. package/dist/locale/zh-CN.js +54 -0
  10. package/dist/server/actions/index.js +43 -0
  11. package/dist/server/collections/mapConfiguration.js +32 -0
  12. package/dist/server/constants.js +5 -0
  13. package/dist/server/fields/circle.js +47 -0
  14. package/dist/server/fields/index.js +33 -0
  15. package/dist/server/fields/lineString.js +54 -0
  16. package/dist/server/fields/point.js +57 -0
  17. package/dist/server/fields/polygon.js +53 -0
  18. package/dist/server/helpers/index.js +31 -0
  19. package/dist/server/index.js +11 -0
  20. package/dist/server/plugin.js +59 -0
  21. package/dist/server/value-parsers/index.js +53 -0
  22. package/docs/en-US/changelog.md +1 -0
  23. package/docs/en-US/index.md +1 -0
  24. package/docs/en-US/installation.md +1 -0
  25. package/docs/en-US/tabs.json +18 -0
  26. package/docs/en-US/usage.md +1 -0
  27. package/docs/zh-CN/changelog.md +1 -0
  28. package/docs/zh-CN/index.md +1 -0
  29. package/docs/zh-CN/installation.md +1 -0
  30. package/docs/zh-CN/tabs.json +18 -0
  31. package/docs/zh-CN/usage.md +1 -0
  32. package/package.json +19 -30
  33. package/server.d.ts +2 -3
  34. package/server.js +1 -1
  35. package/lib/client/block/MapActionInitializers.js +0 -93
  36. package/lib/client/block/MapBlock.js +0 -38
  37. package/lib/client/block/MapBlockDesigner.js +0 -196
  38. package/lib/client/block/MapBlockInitializer.js +0 -124
  39. package/lib/client/block/MapBlockProvider.js +0 -91
  40. package/lib/client/block/index.js +0 -58
  41. package/lib/client/block/utils.js +0 -102
  42. package/lib/client/components/AMap/Block.js +0 -350
  43. package/lib/client/components/AMap/Map.js +0 -458
  44. package/lib/client/components/AMap/Search.js +0 -121
  45. package/lib/client/components/AMap/index.js +0 -27
  46. package/lib/client/components/Configuration.js +0 -160
  47. package/lib/client/components/Designer.js +0 -167
  48. package/lib/client/components/GoogleMaps/Block.js +0 -370
  49. package/lib/client/components/GoogleMaps/Map.js +0 -466
  50. package/lib/client/components/GoogleMaps/Search.js +0 -128
  51. package/lib/client/components/GoogleMaps/index.js +0 -27
  52. package/lib/client/components/GoogleMaps/utils.js +0 -44
  53. package/lib/client/components/Map.js +0 -50
  54. package/lib/client/components/MapBlock.js +0 -40
  55. package/lib/client/components/MapComponent.js +0 -42
  56. package/lib/client/components/ReadPretty.js +0 -56
  57. package/lib/client/components/index.js +0 -71
  58. package/lib/client/constants.js +0 -19
  59. package/lib/client/fields/circle.js +0 -32
  60. package/lib/client/fields/index.js +0 -17
  61. package/lib/client/fields/lineString.js +0 -32
  62. package/lib/client/fields/point.js +0 -32
  63. package/lib/client/fields/polygon.js +0 -32
  64. package/lib/client/fields/schema.js +0 -64
  65. package/lib/client/hooks/index.js +0 -16
  66. package/lib/client/hooks/useMapConfiguration.js +0 -54
  67. package/lib/client/index.js +0 -73
  68. package/lib/client/initialize.js +0 -50
  69. package/lib/client/locale/index.js +0 -38
  70. package/lib/client/types.js +0 -5
  71. package/lib/client/utils.js +0 -1
  72. package/lib/index.js +0 -13
  73. package/lib/locale/en-US.js +0 -9
  74. package/lib/locale/fr-FR.js +0 -9
  75. package/lib/locale/pt-BR.js +0 -54
  76. package/lib/locale/zh-CN.js +0 -57
  77. package/lib/server/actions/index.js +0 -55
  78. package/lib/server/collections/mapConfiguration.js +0 -30
  79. package/lib/server/constants.js +0 -8
  80. package/lib/server/fields/circle.js +0 -58
  81. package/lib/server/fields/index.js +0 -49
  82. package/lib/server/fields/lineString.js +0 -65
  83. package/lib/server/fields/point.js +0 -68
  84. package/lib/server/fields/polygon.js +0 -64
  85. package/lib/server/helpers/index.js +0 -32
  86. package/lib/server/index.js +0 -13
  87. package/lib/server/plugin.js +0 -79
  88. package/lib/server/value-parsers/index.js +0 -75
  89. package/src/client/block/MapActionInitializers.tsx +0 -97
  90. package/src/client/block/MapBlock.tsx +0 -11
  91. package/src/client/block/MapBlockDesigner.tsx +0 -161
  92. package/src/client/block/MapBlockInitializer.tsx +0 -91
  93. package/src/client/block/MapBlockProvider.tsx +0 -55
  94. package/src/client/block/index.tsx +0 -35
  95. package/src/client/block/utils.ts +0 -81
  96. package/src/client/components/AMap/Block.tsx +0 -314
  97. package/src/client/components/AMap/Map.tsx +0 -461
  98. package/src/client/components/AMap/Search.tsx +0 -92
  99. package/src/client/components/AMap/index.ts +0 -2
  100. package/src/client/components/Configuration.tsx +0 -117
  101. package/src/client/components/Designer.tsx +0 -147
  102. package/src/client/components/GoogleMaps/Block.tsx +0 -341
  103. package/src/client/components/GoogleMaps/Map.tsx +0 -470
  104. package/src/client/components/GoogleMaps/Search.tsx +0 -107
  105. package/src/client/components/GoogleMaps/index.ts +0 -2
  106. package/src/client/components/GoogleMaps/utils.ts +0 -33
  107. package/src/client/components/Map.tsx +0 -33
  108. package/src/client/components/MapBlock.tsx +0 -24
  109. package/src/client/components/MapComponent.tsx +0 -23
  110. package/src/client/components/ReadPretty.tsx +0 -28
  111. package/src/client/components/index.ts +0 -6
  112. package/src/client/constants.ts +0 -11
  113. package/src/client/fields/circle.ts +0 -23
  114. package/src/client/fields/index.ts +0 -11
  115. package/src/client/fields/lineString.ts +0 -23
  116. package/src/client/fields/point.ts +0 -23
  117. package/src/client/fields/polygon.ts +0 -23
  118. package/src/client/fields/schema.ts +0 -54
  119. package/src/client/hooks/index.ts +0 -1
  120. package/src/client/hooks/useMapConfiguration.ts +0 -42
  121. package/src/client/index.tsx +0 -55
  122. package/src/client/initialize.tsx +0 -32
  123. package/src/client/locale/index.ts +0 -19
  124. package/src/client/types.ts +0 -1
  125. package/src/client/utils.ts +0 -0
  126. package/src/index.ts +0 -1
  127. package/src/locale/en-US.ts +0 -3
  128. package/src/locale/fr-FR.ts +0 -3
  129. package/src/locale/pt-BR.ts +0 -51
  130. package/src/locale/zh-CN.ts +0 -52
  131. package/src/server/__tests__/fields.test.ts +0 -157
  132. package/src/server/actions/index.ts +0 -44
  133. package/src/server/collections/mapConfiguration.ts +0 -29
  134. package/src/server/constants.ts +0 -1
  135. package/src/server/fields/.gitkeep +0 -0
  136. package/src/server/fields/circle.ts +0 -48
  137. package/src/server/fields/index.ts +0 -4
  138. package/src/server/fields/lineString.ts +0 -55
  139. package/src/server/fields/point.ts +0 -58
  140. package/src/server/fields/polygon.ts +0 -54
  141. package/src/server/helpers/index.ts +0 -25
  142. package/src/server/index.ts +0 -1
  143. package/src/server/plugin.ts +0 -57
  144. package/src/server/value-parsers/index.ts +0 -55
  145. /package/{lib → dist}/client/block/MapActionInitializers.d.ts +0 -0
  146. /package/{lib → dist}/client/block/MapBlock.d.ts +0 -0
  147. /package/{lib → dist}/client/block/MapBlockDesigner.d.ts +0 -0
  148. /package/{lib → dist}/client/block/MapBlockInitializer.d.ts +0 -0
  149. /package/{lib → dist}/client/block/MapBlockProvider.d.ts +0 -0
  150. /package/{lib → dist}/client/block/index.d.ts +0 -0
  151. /package/{lib → dist}/client/block/utils.d.ts +0 -0
  152. /package/{lib → dist}/client/components/AMap/Block.d.ts +0 -0
  153. /package/{lib → dist}/client/components/AMap/Map.d.ts +0 -0
  154. /package/{lib → dist}/client/components/AMap/Search.d.ts +0 -0
  155. /package/{lib → dist}/client/components/AMap/index.d.ts +0 -0
  156. /package/{lib → dist}/client/components/Configuration.d.ts +0 -0
  157. /package/{lib → dist}/client/components/Designer.d.ts +0 -0
  158. /package/{lib → dist}/client/components/GoogleMaps/Block.d.ts +0 -0
  159. /package/{lib → dist}/client/components/GoogleMaps/Map.d.ts +0 -0
  160. /package/{lib → dist}/client/components/GoogleMaps/Search.d.ts +0 -0
  161. /package/{lib → dist}/client/components/GoogleMaps/index.d.ts +0 -0
  162. /package/{lib → dist}/client/components/GoogleMaps/utils.d.ts +0 -0
  163. /package/{lib → dist}/client/components/Map.d.ts +0 -0
  164. /package/{lib → dist}/client/components/MapBlock.d.ts +0 -0
  165. /package/{lib → dist}/client/components/MapComponent.d.ts +0 -0
  166. /package/{lib → dist}/client/components/ReadPretty.d.ts +0 -0
  167. /package/{lib → dist}/client/components/index.d.ts +0 -0
  168. /package/{lib → dist}/client/constants.d.ts +0 -0
  169. /package/{lib → dist}/client/fields/circle.d.ts +0 -0
  170. /package/{lib → dist}/client/fields/index.d.ts +0 -0
  171. /package/{lib → dist}/client/fields/lineString.d.ts +0 -0
  172. /package/{lib → dist}/client/fields/point.d.ts +0 -0
  173. /package/{lib → dist}/client/fields/polygon.d.ts +0 -0
  174. /package/{lib → dist}/client/fields/schema.d.ts +0 -0
  175. /package/{lib → dist}/client/hooks/index.d.ts +0 -0
  176. /package/{lib → dist}/client/hooks/useMapConfiguration.d.ts +0 -0
  177. /package/{lib → dist}/client/index.d.ts +0 -0
  178. /package/{lib → dist}/client/initialize.d.ts +0 -0
  179. /package/{lib → dist}/client/locale/index.d.ts +0 -0
  180. /package/{lib → dist}/client/types.d.ts +0 -0
  181. /package/{lib → dist}/client/utils.d.ts +0 -0
  182. /package/{lib → dist}/locale/en-US.d.ts +0 -0
  183. /package/{lib → dist}/locale/fr-FR.d.ts +0 -0
  184. /package/{lib → dist}/locale/pt-BR.d.ts +0 -0
  185. /package/{lib → dist}/locale/zh-CN.d.ts +0 -0
  186. /package/{lib → dist}/server/actions/index.d.ts +0 -0
  187. /package/{lib → dist}/server/collections/mapConfiguration.d.ts +0 -0
  188. /package/{lib → dist}/server/constants.d.ts +0 -0
  189. /package/{lib → dist}/server/fields/circle.d.ts +0 -0
  190. /package/{lib → dist}/server/fields/index.d.ts +0 -0
  191. /package/{lib → dist}/server/fields/lineString.d.ts +0 -0
  192. /package/{lib → dist}/server/fields/point.d.ts +0 -0
  193. /package/{lib → dist}/server/fields/polygon.d.ts +0 -0
  194. /package/{lib → dist}/server/helpers/index.d.ts +0 -0
  195. /package/{lib → dist}/server/index.d.ts +0 -0
  196. /package/{lib → dist}/server/plugin.d.ts +0 -0
  197. /package/{lib → dist}/server/value-parsers/index.d.ts +0 -0
@@ -1,470 +0,0 @@
1
- import { SyncOutlined } from '@ant-design/icons';
2
- import { useFieldSchema } from '@formily/react';
3
- import { Loader } from '@googlemaps/js-api-loader';
4
- import { css, useAPIClient, useCollection } from '@nocobase/client';
5
- import { useMemoizedFn } from 'ahooks';
6
- import { Alert, App, Button, Spin } from 'antd';
7
- import React, { useEffect, useImperativeHandle, useMemo, useRef, useState } from 'react';
8
- import { useNavigate } from 'react-router-dom';
9
- import { defaultImage } from '../../constants';
10
- import { useMapConfiguration } from '../../hooks';
11
- import { useMapTranslation } from '../../locale';
12
- import { MapEditorType } from '../../types';
13
- import { Search } from './Search';
14
- import { getCurrentPosition, getIcon } from './utils';
15
-
16
- export type OverlayOptions = google.maps.PolygonOptions & google.maps.MarkerOptions & google.maps.PolylineOptions;
17
-
18
- export const getDrawingMode = (type: MapEditorType) => {
19
- if (type === 'point') {
20
- return 'marker';
21
- } else if (type === 'lineString') {
22
- return 'polyline';
23
- }
24
- return type;
25
- };
26
-
27
- const methodMapping = {
28
- point: {
29
- propertyKey: 'position',
30
- overlay: 'Marker',
31
- },
32
- polygon: {
33
- propertyKey: 'paths',
34
- overlay: 'Polygon',
35
- },
36
- lineString: {
37
- propertyKey: 'path',
38
- overlay: 'Polyline',
39
- },
40
- circle: {
41
- transformOptions(value) {
42
- return {
43
- center: new google.maps.LatLng(value[1], value[0]),
44
- radius: value[2],
45
- } as google.maps.CircleOptions;
46
- },
47
- overlay: 'Circle',
48
- },
49
- };
50
-
51
- export interface GoogleMapsComponentProps {
52
- value?: any;
53
- onChange?: (value: number[]) => void;
54
- disabled?: boolean;
55
- mapType: string;
56
- /**
57
- * only ReadPretty
58
- */
59
- readonly: string;
60
- zoom: number;
61
- type: MapEditorType;
62
- style?: React.CSSProperties;
63
- overlayCommonOptions?: OverlayOptions;
64
- block?: boolean;
65
- }
66
-
67
- export interface GoogleMapForwardedRefProps {
68
- setOverlay: (t: MapEditorType, v: any, o?: OverlayOptions) => google.maps.MVCObject;
69
- getOverlay: (t: MapEditorType, v: any, o?: OverlayOptions) => google.maps.MVCObject;
70
- setFitView: (overlays: google.maps.MVCObject[]) => void;
71
- createDraw: (onlyCreate?: boolean, additionalOptions?: OverlayOptions) => any;
72
- map: google.maps.Map;
73
- overlay: google.maps.MVCObject;
74
- drawingManager: google.maps.drawing.DrawingManager;
75
- errMessage?: string;
76
- }
77
-
78
- export const GoogleMapsComponent = React.forwardRef<GoogleMapForwardedRefProps, GoogleMapsComponentProps>(
79
- (props, ref) => {
80
- const { value, onChange, block = false, readonly, disabled = block, zoom = 13, overlayCommonOptions } = props;
81
- const { accessKey } = useMapConfiguration(props.mapType) || {};
82
- const { t } = useMapTranslation();
83
- const { getField } = useCollection();
84
- const fieldSchema = useFieldSchema();
85
- const drawingManagerRef = useRef<google.maps.drawing.DrawingManager>();
86
- const map = useRef<google.maps.Map>();
87
- const overlayRef = useRef<google.maps.Marker | google.maps.Polygon | google.maps.Polyline | google.maps.Circle>();
88
- const [needUpdateFlag, forceUpdate] = useState([]);
89
- const [errMessage, setErrMessage] = useState('');
90
- const api = useAPIClient();
91
- const { modal } = App.useApp();
92
-
93
- const type = useMemo<MapEditorType>(() => {
94
- if (props.type) return props.type;
95
- const collectionField = getField(fieldSchema?.name);
96
- return collectionField?.interface;
97
- }, [props?.type, fieldSchema?.name]);
98
-
99
- const drawingMode = useRef(getDrawingMode(type) as google.maps.drawing.OverlayType);
100
-
101
- const [commonOptions] = useState<OverlayOptions>({
102
- strokeWeight: 5,
103
- strokeColor: '#4e9bff',
104
- fillColor: '#4e9bff',
105
- strokeOpacity: 1,
106
- editable: !disabled,
107
- draggable: !disabled,
108
- ...overlayCommonOptions,
109
- });
110
-
111
- const navigate = useNavigate();
112
- const mapContainerRef = useRef<HTMLDivElement>();
113
- const cleanupOverlayListenersRef = useRef<Set<() => void>>(new Set());
114
-
115
- const onAndOffListenOverlay = useMemoizedFn((target: typeof overlayRef.current) => {
116
- cleanupOverlayListenersRef.current.forEach((cb) => {
117
- cleanupOverlayListenersRef.current.delete(cb);
118
- });
119
-
120
- if ('getPath' in target) {
121
- const mvcArray = target.getPath();
122
- ['insert_at', 'remove_at', 'set_at'].forEach((event) => {
123
- cleanupOverlayListenersRef.current.add(
124
- mvcArray.addListener(event, () => {
125
- onMapChange(target, true);
126
- }).remove,
127
- );
128
- });
129
- } else if (target instanceof google.maps.Circle) {
130
- ['center_changed', 'radius_changed'].forEach((event) => {
131
- cleanupOverlayListenersRef.current.add(
132
- target.addListener(event, () => {
133
- onMapChange(target, true);
134
- }).remove,
135
- );
136
- });
137
- }
138
- });
139
-
140
- const toRemoveOverlay = useMemoizedFn(() => {
141
- if (overlayRef.current) {
142
- overlayRef.current.unbindAll();
143
- overlayRef.current.setMap(null);
144
- }
145
- if (type !== 'point') {
146
- drawingManagerRef.current?.setDrawingMode(null);
147
- }
148
- });
149
-
150
- const toCenter = useMemoizedFn((position) => {
151
- if (map.current) {
152
- map.current.setCenter(position);
153
- map.current.setZoom(zoom);
154
- }
155
- });
156
-
157
- const setupOverlay = useMemoizedFn((nextOverlay: typeof overlayRef.current) => {
158
- toRemoveOverlay();
159
- onAndOffListenOverlay(nextOverlay);
160
- overlayRef.current = nextOverlay;
161
- });
162
-
163
- const setFitView = useMemoizedFn((overlays: google.maps.MVCObject[]) => {
164
- const bounds = new google.maps.LatLngBounds();
165
-
166
- overlays.forEach((overlay) => {
167
- if (overlay instanceof google.maps.Marker) {
168
- bounds.extend(overlay.getPosition());
169
- } else if (overlay instanceof google.maps.Polyline || overlay instanceof google.maps.Polygon) {
170
- const path = overlay.getPath();
171
- for (let i = 0; i < path.getLength(); i++) {
172
- bounds.extend(path.getAt(i));
173
- }
174
- } else if (overlay instanceof google.maps.Circle) {
175
- bounds.union(overlay.getBounds());
176
- }
177
- });
178
-
179
- map.current.setCenter(bounds.getCenter());
180
- });
181
-
182
- const onFocusOverlay = () => {
183
- if (overlayRef.current) {
184
- setFitView([overlayRef.current]);
185
- }
186
- };
187
-
188
- const onMapChange = useMemoizedFn((target: typeof overlayRef.current, onlyChange = false) => {
189
- let nextValue = null;
190
-
191
- if (type === 'point') {
192
- const { lat, lng } = (target as google.maps.Marker).getPosition();
193
- nextValue = [lng(), lat()];
194
- } else if (type === 'polygon' || type === 'lineString') {
195
- nextValue = (target as google.maps.Polyline)
196
- .getPath()
197
- .getArray()
198
- .map((item) => [item.lng(), item.lat()]);
199
- if (nextValue.length < 2) {
200
- return;
201
- }
202
- } else if (type === 'circle') {
203
- const center = (target as google.maps.Circle).getCenter();
204
- const radius = (target as google.maps.Circle).getRadius();
205
- nextValue = [center.lng(), center.lat(), radius];
206
- }
207
-
208
- if (!onlyChange) {
209
- setupOverlay(target);
210
- }
211
- onChange?.(nextValue);
212
- });
213
-
214
- const createDraw = useMemoizedFn((onlyCreate = false, additionalOptions?: OverlayOptions) => {
215
- const currentOptions = {
216
- ...commonOptions,
217
- ...additionalOptions,
218
- map: map.current,
219
- };
220
- drawingManagerRef.current = new google.maps.drawing.DrawingManager({
221
- drawingMode: drawingMode.current,
222
- drawingControl: false,
223
- markerOptions: { ...currentOptions, icon: getIcon(defaultImage) },
224
- polygonOptions: currentOptions,
225
- polylineOptions: currentOptions,
226
- circleOptions: currentOptions,
227
- map: map.current,
228
- });
229
-
230
- if (!onlyCreate) {
231
- drawingManagerRef.current.addListener('overlaycomplete', (event: { type: string; overlay: unknown }) => {
232
- const overlay = event.overlay as google.maps.Marker;
233
- onMapChange(overlay);
234
- });
235
- }
236
- return drawingManagerRef.current;
237
- });
238
-
239
- const getOverlay = useMemoizedFn((t = type, v = value, o?: OverlayOptions) => {
240
- const mapping = methodMapping[t];
241
- if (!mapping) {
242
- return;
243
- }
244
- const options = { ...commonOptions, icon: getIcon(defaultImage), ...o };
245
-
246
- if ('transformOptions' in mapping) {
247
- Object.assign(options, mapping.transformOptions(v));
248
- } else if ('propertyKey' in mapping) {
249
- options[mapping.propertyKey] = Array.isArray(v[0])
250
- ? v.map((item) => {
251
- return new google.maps.LatLng(item[1], item[0]);
252
- })
253
- : new google.maps.LatLng(v[1], v[0]);
254
- }
255
-
256
- const overlay = new google.maps[mapping.overlay](options);
257
- return overlay;
258
- });
259
-
260
- const setOverlay = useMemoizedFn((t = type, v = value, o?: OverlayOptions) => {
261
- if (!map.current) return;
262
- const nextOverlay = getOverlay(t, v, {
263
- ...o,
264
- map: map.current,
265
- }) as google.maps.Polyline;
266
- return nextOverlay;
267
- });
268
-
269
- // edit mode
270
- useEffect(() => {
271
- if (!map.current) return;
272
- if (!value || (!readonly && overlayRef.current)) {
273
- return;
274
- }
275
- const nextOverlay = setOverlay();
276
- setupOverlay(nextOverlay);
277
- // Focus on the overlay
278
- setFitView([nextOverlay]);
279
- }, [value, needUpdateFlag, type, disabled, readonly, setOverlay, setFitView, setupOverlay]);
280
-
281
- useEffect(() => {
282
- if (!accessKey || map.current || !mapContainerRef.current) return;
283
- let loader: Loader;
284
- try {
285
- loader = new Loader({
286
- apiKey: accessKey,
287
- version: 'weekly',
288
- language: api.auth.getLocale(),
289
- });
290
- } catch (err) {
291
- setErrMessage(t('Load google maps failed, Please check the Api key and refresh the page'));
292
- return;
293
- }
294
-
295
- // google maps api error
296
- const error = console.error;
297
- console.error = (err, ...args) => {
298
- if (err?.includes('InvalidKeyMapError')) {
299
- setErrMessage(t('Load google maps failed, Please check the Api key and refresh the page'));
300
- }
301
- error(err, ...args);
302
- };
303
-
304
- Promise.all([loader.importLibrary('drawing'), loader.importLibrary('core'), loader.importLibrary('geometry')])
305
- .then(async (res) => {
306
- const center = await getCurrentPosition();
307
- map.current = new google.maps.Map(mapContainerRef.current, {
308
- zoom,
309
- center,
310
- mapTypeId: google.maps.MapTypeId.ROADMAP,
311
- zoomControl: false,
312
- streetViewControl: false,
313
- panControl: false,
314
- mapTypeControl: false,
315
- fullscreenControl: false,
316
- });
317
- setErrMessage('');
318
- forceUpdate([]);
319
- })
320
- .catch((err) => {
321
- if (err instanceof Error) {
322
- setErrMessage(err.message);
323
- return;
324
- }
325
- });
326
-
327
- return () => {
328
- map.current?.unbindAll();
329
- map.current = null;
330
- drawingManagerRef.current?.unbindAll();
331
- };
332
- }, [accessKey, api.auth, type, zoom]);
333
-
334
- useEffect(() => {
335
- if (!map.current || !type || disabled || drawingManagerRef.current) return;
336
- createDraw();
337
- }, [createDraw, disabled, needUpdateFlag, type]);
338
-
339
- useImperativeHandle(ref, () => ({
340
- setOverlay,
341
- getOverlay,
342
- setFitView,
343
- createDraw,
344
- map: map.current,
345
- overlay: overlayRef.current,
346
- drawingManager: drawingManagerRef.current,
347
- errMessage,
348
- }));
349
-
350
- const onReset = useMemoizedFn(() => {
351
- const ok = () => {
352
- toRemoveOverlay();
353
- drawingManagerRef.current.setDrawingMode(drawingMode.current);
354
- onChange?.(null);
355
- };
356
- modal.confirm({
357
- title: t('Clear the canvas'),
358
- content: t('Are you sure to clear the canvas?'),
359
- okText: t('Confirm'),
360
- cancelText: t('Cancel'),
361
- onOk() {
362
- ok();
363
- },
364
- });
365
- });
366
-
367
- if (!accessKey || errMessage) {
368
- return (
369
- <Alert
370
- action={
371
- <Button type="primary" onClick={() => navigate('/admin/settings/map/configuration?tab=google')}>
372
- {t('Go to the configuration page')}
373
- </Button>
374
- }
375
- message={errMessage || t('Please configure the Api key first')}
376
- type="error"
377
- />
378
- );
379
- }
380
-
381
- return (
382
- <div
383
- className={css`
384
- position: relative;
385
- height: 500px;
386
- `}
387
- >
388
- {!map.current && (
389
- <div
390
- className={css`
391
- position: absolute;
392
- inset: 0;
393
- display: flex;
394
- align-items: center;
395
- justify-content: center;
396
- `}
397
- >
398
- <Spin />
399
- </div>
400
- )}
401
- {!disabled ? (
402
- <>
403
- {map.current && <Search toCenter={toCenter} mapRef={map} />}
404
- <div
405
- className={css`
406
- position: absolute;
407
- bottom: 80px;
408
- right: 20px;
409
- z-index: 10;
410
- `}
411
- >
412
- <Button
413
- onClick={onFocusOverlay}
414
- disabled={!overlayRef.current}
415
- type="primary"
416
- shape="round"
417
- size="large"
418
- icon={<SyncOutlined />}
419
- ></Button>
420
- </div>
421
- {type === 'lineString' || type === 'polygon' ? (
422
- <div
423
- className={css`
424
- position: absolute;
425
- bottom: 20px;
426
- left: 10px;
427
- z-index: 2;
428
- pointer-events: none;
429
- `}
430
- >
431
- <Alert
432
- message={t('Click to select the starting point and double-click to end the drawing')}
433
- type="info"
434
- />
435
- </div>
436
- ) : null}
437
- <div
438
- className={css`
439
- position: absolute;
440
- bottom: 20px;
441
- right: 20px;
442
- z-index: 2;
443
- `}
444
- >
445
- <Button
446
- disabled={!value}
447
- style={{
448
- height: '40px',
449
- }}
450
- onClick={onReset}
451
- type="primary"
452
- danger
453
- >
454
- {t('Clear')}
455
- </Button>
456
- </div>
457
- </>
458
- ) : null}
459
- <div
460
- ref={mapContainerRef}
461
- className={css`
462
- width: 100%;
463
- height: 100%;
464
- `}
465
- style={props?.style}
466
- ></div>
467
- </div>
468
- );
469
- },
470
- );
@@ -1,107 +0,0 @@
1
- import { css } from '@nocobase/client';
2
- import { useDebounceFn } from 'ahooks';
3
- import { message, Select } from 'antd';
4
- import React, { useEffect, useRef, useState } from 'react';
5
- import { useMapTranslation } from '../../locale';
6
-
7
- interface SearchProps {
8
- toCenter: (p: any) => void;
9
- mapRef: React.RefObject<google.maps.Map>;
10
- }
11
-
12
- export const Search = (props: SearchProps) => {
13
- const { toCenter, mapRef } = props;
14
- const { t } = useMapTranslation();
15
- const placeSearchRef = useRef<google.maps.places.AutocompleteService>();
16
- const [options, setOptions] = useState([]);
17
-
18
- useEffect(() => {
19
- google.maps
20
- .importLibrary('places')
21
- .then((places: google.maps.PlacesLibrary) => {
22
- placeSearchRef.current = new places.AutocompleteService();
23
- })
24
- .catch(() => {
25
- message.error('Please configure the Google API Key correctly');
26
- });
27
- }, [mapRef]);
28
-
29
- const { run: onSearch } = useDebounceFn(
30
- (keyword) => {
31
- if (!placeSearchRef.current) {
32
- return;
33
- }
34
- placeSearchRef.current.getPlacePredictions(
35
- {
36
- input: keyword || ' ',
37
- },
38
- (result, status) => {
39
- if (status === google.maps.places.PlacesServiceStatus.OK) {
40
- setOptions(
41
- result.map((item: any) => {
42
- const structured = item.structured_formatting;
43
- return {
44
- ...item,
45
- label: `${structured.main_text}${structured.secondary_text ? ' ' + structured.secondary_text : ''}`,
46
- value: item.place_id,
47
- };
48
- }),
49
- );
50
- } else {
51
- setOptions([]);
52
- return;
53
- }
54
- },
55
- );
56
- },
57
- {
58
- wait: 300,
59
- },
60
- );
61
-
62
- const onSelect = (value) => {
63
- const place = options.find((o) => {
64
- return o.value === value;
65
- });
66
- const service = new google.maps.places.PlacesService(mapRef.current);
67
- service.getDetails(
68
- {
69
- placeId: place.place_id,
70
- fields: ['geometry'],
71
- },
72
- (result, status) => {
73
- if (status === google.maps.places.PlacesServiceStatus.OK) {
74
- toCenter(result.geometry.location);
75
- }
76
- },
77
- );
78
- };
79
-
80
- return (
81
- <div
82
- className={css`
83
- position: absolute;
84
- top: 10px;
85
- left: 10px;
86
- z-index: 10;
87
- width: calc(100% - 20px);
88
- `}
89
- >
90
- <Select
91
- id="google-map-search"
92
- showSearch
93
- allowClear
94
- style={{
95
- background: 'rgba(255, 255, 255, 0.8)',
96
- }}
97
- placeholder={t('Enter keywords to search')}
98
- filterOption={false}
99
- onSearch={onSearch}
100
- onSelect={onSelect}
101
- options={options}
102
- ></Select>
103
- </div>
104
- );
105
- };
106
-
107
- export default Search;
@@ -1,2 +0,0 @@
1
- export * from './Block';
2
- export * from './Map';
@@ -1,33 +0,0 @@
1
- export const getIcon = (url) => {
2
- return {
3
- url,
4
- scaledSize: {
5
- width: 19,
6
- height: 32,
7
- },
8
- labelOrigin: new google.maps.Point(19 / 2, 32 + 10),
9
- } as google.maps.Icon;
10
- };
11
-
12
- export const getCurrentPosition: () => Promise<{ lat: number; lng: number }> = () => {
13
- return new Promise((resolve) => {
14
- const defaultLatLng = () => {
15
- // google company lat and lng
16
- resolve({ lat: 37.4224764, lng: -122.0842499 });
17
- };
18
-
19
- if (navigator.geolocation) {
20
- navigator.geolocation.getCurrentPosition(
21
- (position) => {
22
- const { latitude, longitude } = position.coords;
23
- resolve({ lat: latitude, lng: longitude });
24
- },
25
- () => {
26
- defaultLatLng();
27
- },
28
- );
29
- } else {
30
- defaultLatLng();
31
- }
32
- });
33
- };
@@ -1,33 +0,0 @@
1
- import { connect, mapReadPretty } from '@formily/react';
2
- import { css } from '@nocobase/client';
3
- import React from 'react';
4
- import { AMapComponentProps } from './AMap';
5
- import Designer from './Designer';
6
- import { MapComponent } from './MapComponent';
7
- import ReadPretty from './ReadPretty';
8
-
9
- type MapProps = AMapComponentProps;
10
-
11
- const InternalMap = connect((props: MapProps) => {
12
- return (
13
- <div
14
- className={css`
15
- height: 100%;
16
- border: 1px solid transparent;
17
- .ant-formily-item-error & {
18
- border: 1px solid #ff4d4f;
19
- }
20
- `}
21
- >
22
- <MapComponent {...props} />
23
- </div>
24
- );
25
- }, mapReadPretty(ReadPretty));
26
-
27
- const Map = InternalMap as typeof InternalMap & {
28
- Designer: typeof Designer;
29
- };
30
-
31
- Map.Designer = Designer;
32
-
33
- export { Map };
@@ -1,24 +0,0 @@
1
- import React, { useMemo } from 'react';
2
- import { useMapTranslation } from '../locale';
3
- import { AMapBlock } from './AMap';
4
- import { GoogleMapsBlock } from './GoogleMaps';
5
-
6
- const MapBlocks = {
7
- amap: AMapBlock,
8
- google: GoogleMapsBlock,
9
- };
10
-
11
- export const MapBlockComponent: React.FC<any> = (props) => {
12
- const { t } = useMapTranslation();
13
- const { mapType } = props;
14
-
15
- const Component = useMemo(() => {
16
- return MapBlocks[mapType];
17
- }, [mapType]);
18
-
19
- if (!Component) {
20
- return <div>{t(`The ${mapType} cannot found`)}</div>;
21
- }
22
-
23
- return <Component {...props} />;
24
- };
@@ -1,23 +0,0 @@
1
- import React, { useMemo } from 'react';
2
- import { useMapTranslation } from '../locale';
3
- import { AMapComponent } from './AMap';
4
- import { GoogleMapsComponent } from './GoogleMaps';
5
-
6
- const MapComponents = {
7
- amap: AMapComponent,
8
- google: GoogleMapsComponent,
9
- };
10
-
11
- export const MapComponent = React.forwardRef<any, any>((props, ref) => {
12
- const { t } = useMapTranslation();
13
- const { mapType } = props;
14
- const Component = useMemo(() => {
15
- return MapComponents[mapType];
16
- }, [mapType]);
17
-
18
- if (!Component) {
19
- return <div>{t(`The ${mapType} cannot found`)}</div>;
20
- }
21
-
22
- return <Component ref={ref} {...props} />;
23
- });