@hzab/form-render 1.6.18 → 1.6.19

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 (59) hide show
  1. package/CHANGELOG.md +4 -0
  2. package/README.md +239 -239
  3. package/package.json +57 -57
  4. package/src/common/formily-utils.ts +125 -125
  5. package/src/common/schema-handler.ts +253 -253
  6. package/src/common/schema-merge.ts +68 -68
  7. package/src/components/ArrayBase/index.tsx +349 -349
  8. package/src/components/ArrayBase/style.less +90 -90
  9. package/src/components/ArrayBase/style.ts +2 -2
  10. package/src/components/ArrayCards/index.tsx +149 -149
  11. package/src/components/ArrayCards/style.less +14 -14
  12. package/src/components/ArrayCards/style.ts +4 -4
  13. package/src/components/ArrayTable/index.tsx +411 -411
  14. package/src/components/ArrayTable/style.less +52 -52
  15. package/src/components/ArrayTable/style.ts +7 -7
  16. package/src/components/DatePicker/index.tsx +108 -108
  17. package/src/components/LocationListPicker/assets/icon.js +1 -1
  18. package/src/components/LocationListPicker/components/AddrList/index.less +55 -55
  19. package/src/components/LocationListPicker/components/AddrList/index.tsx +75 -75
  20. package/src/components/LocationListPicker/components/Popup/address.schema.json +21 -21
  21. package/src/components/LocationListPicker/components/Popup/index.less +22 -22
  22. package/src/components/LocationListPicker/components/Popup/index.tsx +92 -92
  23. package/src/components/LocationListPicker/index.less +34 -34
  24. package/src/components/LocationListPicker/index.tsx +521 -520
  25. package/src/components/LocationPicker/Map/AMap/common/loader.ts +58 -58
  26. package/src/components/LocationPicker/Map/AMap/common/utils.ts +431 -431
  27. package/src/components/LocationPicker/Map/AMap/index.jsx +51 -51
  28. package/src/components/LocationPicker/README.md +44 -44
  29. package/src/components/LocationPicker/common/utils.ts +30 -30
  30. package/src/components/LocationPicker/components/ModalContent/index.tsx +387 -387
  31. package/src/components/LocationPicker/components/PickerInfo/index.tsx +109 -109
  32. package/src/components/LocationPicker/components/ResInfo/index.less +38 -38
  33. package/src/components/LocationPicker/components/ResInfo/index.tsx +65 -65
  34. package/src/components/LocationPicker/index.tsx +197 -197
  35. package/src/components/PersonnelSelect/index.less +21 -21
  36. package/src/components/PersonnelSelect/index.module.less +33 -33
  37. package/src/components/PersonnelSelect/type.ts +92 -92
  38. package/src/components/RichEditor/index.less +38 -38
  39. package/src/components/RichEditor/index.tsx +238 -238
  40. package/src/components/TreeCheckbox/components/CheckboxTable/index.tsx +55 -55
  41. package/src/components/TreeCheckbox/components/TabsRender/index.tsx +58 -58
  42. package/src/components/TreeCheckbox/index.less +12 -12
  43. package/src/components/TreeCheckbox/index.tsx +63 -63
  44. package/src/components/Upload/README.md +64 -64
  45. package/src/components/Upload/common/OfflineUpload.ts +339 -339
  46. package/src/components/Upload/common/customRequest.ts +81 -81
  47. package/src/components/Upload/common/fileName.ts +142 -142
  48. package/src/components/Upload/common/handleIOFileList.ts +393 -393
  49. package/src/components/Upload/common/nanoid.ts +7 -7
  50. package/src/components/Upload/common/ossUpload.js +159 -159
  51. package/src/components/Upload/common/utils.js +194 -194
  52. package/src/components/Upload/components/ItemList/index.tsx +52 -52
  53. package/src/components/Upload/components/PreviewModal/previewRender.tsx +80 -80
  54. package/src/components/Upload/index.tsx +17 -17
  55. package/src/components/Upload/uploader-input.jsx +187 -187
  56. package/src/components/Upload/uploader.jsx +316 -316
  57. package/src/components/UserSelect/index.tsx +123 -123
  58. package/src/components/index.tsx +17 -17
  59. package/src/index.tsx +198 -198
@@ -1,431 +1,431 @@
1
- import * as turf from "@turf/turf";
2
- import { nanoid } from "nanoid";
3
-
4
- import AMapLoader from "./loader";
5
- import { markerIconBase64 } from "../../../assets/svg-icon";
6
-
7
- if (!window._AMapLoaderTemp) {
8
- window._AMapLoaderTemp = {};
9
- }
10
-
11
- export function setKey(key, securityJsCode) {
12
- window._AMapLoaderTemp.key = key;
13
- window._AMapLoaderTemp.securityJsCode = securityJsCode;
14
- }
15
-
16
- export function setServerKey(key, securityJsCode) {
17
- window._AMapLoaderTemp.serverKey = key;
18
- window._AMapLoaderTemp.serverSecurityJsCode = securityJsCode;
19
- }
20
-
21
- export function setSecurityJsCode(securityJsCode) {
22
- window._AMapLoaderTemp.securityJsCode = securityJsCode;
23
- window._AMapSecurityConfig = {
24
- securityJsCode: securityJsCode,
25
- };
26
- }
27
-
28
- export interface IMarker {
29
- setIcon: Function;
30
- setSize: Function;
31
- setOffset: Function;
32
- getPosition: Function;
33
- }
34
-
35
- export type setMarkerOptT = {
36
- id?: number | string;
37
- marker?: IMarker;
38
- map?: Object;
39
- AMap?: Object;
40
- markerIconConf?: Object;
41
- markerConf?: Object;
42
- onClick?: Function;
43
- data?: Object;
44
- };
45
-
46
- export type markersT = {
47
- [k: number | string]: IMarker;
48
- };
49
-
50
- export class MapUtils {
51
- public map;
52
- public AMap;
53
- public markers: markersT = {};
54
- public circles = {};
55
- public pickerMarker;
56
- public markerIconConf;
57
- public markerConf;
58
- public polyEditor = {
59
- PolygonEditor: undefined,
60
- PolylineEditor: undefined,
61
- };
62
- public polygonEnum = {};
63
- public polygonConf;
64
- public mapDomRef;
65
-
66
- constructor({ map, AMap, mapDomRef, markerIconConf, markerConf, polygonConf }) {
67
- if (!map) {
68
- throw new Error("请传入地图实例");
69
- }
70
- this.map = map;
71
- this.AMap = AMap;
72
- this.markerIconConf = markerIconConf || {};
73
- this.markerConf = markerConf || {};
74
- this.polygonEnum = {};
75
- this.polygonConf = polygonConf || {};
76
- this.mapDomRef = mapDomRef;
77
- }
78
-
79
- /**
80
- * 获取地图中心点
81
- * @param map
82
- * @returns { lng: Number, lat: Number }
83
- */
84
- getCenter(map = this.map) {
85
- if (!map) {
86
- throw new Error("请传入地图实例和 AMap");
87
- }
88
- return map && map.getCenter && map.getCenter();
89
- }
90
-
91
- /**
92
- * 设置地图中心点
93
- * @param lon
94
- * @param lat
95
- * @param opt
96
- */
97
- setCenter(
98
- lon,
99
- lat,
100
- opt = { immediately: undefined, duration: undefined, map: this.map, AMap: this.AMap || window.AMap },
101
- ) {
102
- const { map = this.map, AMap = this.AMap || window.AMap, immediately, duration } = opt || {};
103
- if (!(AMap && map)) {
104
- throw new Error("请传入地图实例和 AMap");
105
- }
106
- const position = new window.AMap.LngLat(lon, lat);
107
- // 设置中心点
108
- map.setCenter(position, immediately, duration);
109
- }
110
-
111
- setFitView() {
112
- // 调整地图以适应所有标记点
113
- this.map.setFitView();
114
- }
115
-
116
- /**
117
- * 创建或修改 Marker
118
- * @param lon
119
- * @param lat
120
- * @param opt
121
- * @returns
122
- */
123
- setMarker(lon, lat, opt?: setMarkerOptT) {
124
- const {
125
- data,
126
- id = nanoid(),
127
- marker,
128
- map = this.map,
129
- AMap = this.AMap,
130
- markerIconConf = {},
131
- markerConf = {},
132
- onClick,
133
- } = opt || {};
134
-
135
- if (!(AMap && map)) {
136
- throw new Error("请传入地图实例和 AMap");
137
- }
138
- if (lon === null || lon === undefined || lat === null || lat === undefined) {
139
- console.warn("setMarker 请传入正确的经纬度");
140
- return;
141
- }
142
-
143
- const position = new AMap.LngLat(lon, lat);
144
- let _marker = marker || this.markers[id];
145
-
146
- // 创建 Marker 或修改位置
147
- if (_marker) {
148
- // @ts-ignore
149
- _marker.setPosition(position);
150
- return _marker;
151
- }
152
-
153
- const size = new AMap.Size(34, 34);
154
- const icon = new AMap.Icon({
155
- size, //图标尺寸
156
- image: markerIconBase64, // Icon 的图像
157
- imageSize: size, // 根据所设置的大小拉伸或压缩图片
158
- ...this.markerIconConf,
159
- ...markerIconConf,
160
- });
161
- const _m = new AMap.Marker({
162
- offset: new AMap.Pixel(-17, -34),
163
- icon,
164
- position,
165
- extData: {
166
- id,
167
- data,
168
- },
169
- ...this.markerConf,
170
- ...markerConf,
171
- });
172
-
173
- this.markers[id] = _m;
174
-
175
- if (onClick) {
176
- _m.on("click", onClick);
177
- }
178
-
179
- map?.add(_m);
180
- return _m;
181
- }
182
-
183
- /**
184
- * 创建绘制实例
185
- */
186
- createPoly(element) {
187
- if (element !== "Polypoint") {
188
- // 同时绘制多种形状问题修复
189
- if (this.polyEditor[`${element}Editor`]) {
190
- return this.polyEditor[`${element}Editor`];
191
- }
192
- this.polyEditor[`${element}Editor`] = new this.AMap[`${element}Editor`](this.map, undefined, {
193
- createOptions: {
194
- strokeColor: "#1791fc",
195
- strokeWeight: 2,
196
- fillOpacity: 0.4,
197
- fillColor: "#1791fc",
198
- },
199
- editOptions: {
200
- strokeColor: "#1791fc",
201
- strokeWeight: 2,
202
- fillOpacity: 0.4,
203
- fillColor: "#1791fc",
204
- },
205
- ...this.polygonConf,
206
- });
207
- }
208
- }
209
-
210
- /**
211
- * 按points绘制元素实例
212
- */
213
- setPolygon(opt) {
214
- const { points, id, onClick, element = "Polygon", map = this.map, AMap = this.AMap } = opt;
215
-
216
- if (!(AMap && map)) {
217
- throw new Error("请传入地图实例和 AMap");
218
- }
219
-
220
- const marker = this.polygonEnum[id];
221
- if (marker) {
222
- return;
223
- }
224
-
225
- const graphical = new this.AMap[element]({
226
- path: points,
227
- strokeColor: "#1791fc",
228
- strokeWeight: 2,
229
- fillOpacity: 0.4,
230
- fillColor: "#1791fc",
231
- });
232
-
233
- graphical.id = id;
234
- // 多边形绘制完毕保存中心点
235
- graphical.path = points;
236
- graphical.center = this.getPolygonCenter(points);
237
- this.polygonEnum[id] = graphical;
238
- this.map.add([graphical]);
239
- graphical.on("click", onClick);
240
- }
241
-
242
- /**
243
- * 创建手动绘制实例
244
- */
245
- startPolygon(opt) {
246
- const { onClick, element = "Polygon" } = opt;
247
- this.polyEditor[`${element}Editor`].on("add", (data) => {
248
- const id = nanoid();
249
- data.target.id = id;
250
- const graphical = data.target;
251
- this.polygonEnum[id] = graphical;
252
- graphical.on("click", onClick);
253
- });
254
-
255
- this.polyEditor[`${element}Editor`].on("end", (data) => {
256
- // 多边形绘制完毕保存中心点
257
- const graphical = data.target;
258
- if (graphical.type === "AMap.Overlay") {
259
- graphical.path = graphical.getPath().map((item) => [item.lng, item.lat]);
260
- graphical.center = this.getPolygonCenter(graphical.path);
261
- }
262
- });
263
- }
264
-
265
- /**
266
- * 手动开始绘制函数
267
- */
268
- setDrawPolygon({ map = this.map, AMap = this.AMap, element = "Polygon" }) {
269
- if (!(AMap && map)) {
270
- throw new Error("请传入地图实例和 AMap");
271
- }
272
- this.polyEditor[`${element}Editor`].close();
273
- this.polyEditor[`${element}Editor`].setTarget();
274
- this.polyEditor[`${element}Editor`].open();
275
- }
276
-
277
- /**
278
- * 编辑态
279
- * @param item
280
- */
281
- setEditPolygon({ id, element = "Polygon" }) {
282
- const target = this.polygonEnum[id];
283
- if (!target) {
284
- console.warn("setEditPolygon 找不到目标对象:", id);
285
- return;
286
- }
287
- this.polyEditor[`${element}Editor`].setTarget(target);
288
- this.polyEditor[`${element}Editor`].open();
289
- }
290
-
291
- setMarkerIcon(
292
- id,
293
- opt = {
294
- size: null,
295
- image: null,
296
- offset: null,
297
- },
298
- ) {
299
- const marker = this.markers[id];
300
- if (!marker) {
301
- return;
302
- }
303
- const { image, offset } = opt || {};
304
- const AMap = this.AMap;
305
-
306
- const size = new AMap.Size(...opt?.size);
307
- const icon = new AMap.Icon({
308
- size, //图标尺寸
309
- image: image, // Icon 的图像
310
- imageSize: size, // 根据所设置的大小拉伸或压缩图片
311
- });
312
-
313
- marker.setIcon(icon);
314
- marker.setSize(size);
315
- marker.setOffset(new AMap.Pixel(...offset));
316
- }
317
-
318
- rmPolygon(id, element = "Polygon") {
319
- this.map.remove(this.polygonEnum[id]);
320
- this.polyEditor[`${element}Editor`].close();
321
- this.polyEditor[`${element}Editor`].setTarget();
322
- delete this.polygonEnum[id];
323
- }
324
-
325
- /**
326
- * 删除指定 marker
327
- * @param id
328
- */
329
- rmMarker(id) {
330
- this.map.remove(this.markers[id]);
331
- delete this.markers[id];
332
- }
333
-
334
- setCircle(lon, lat, radius, opt?) {
335
- const { id = nanoid(), circle, map = this.map, AMap = this.AMap, onClick, circleConf = {} } = opt || {};
336
- if (!(AMap && map)) {
337
- throw new Error("请传入地图实例和 AMap");
338
- }
339
- if (lon === null || lon === undefined || lat === null || lat === undefined) {
340
- console.warn("setMarker 请传入正确的经纬度");
341
- return;
342
- }
343
- // circles
344
-
345
- const center = new AMap.LngLat(lon, lat);
346
- let _circle = circle || this.circles[id];
347
-
348
- // 创建 Marker 或修改位置
349
- if (_circle) {
350
- // @ts-ignore
351
- _circle.setCenter(center);
352
- _circle.setRadius(radius);
353
- return _circle;
354
- }
355
-
356
- //创建圆形 Circle 实例
357
- const _c = new AMap.Circle({
358
- center: center, // 圆心
359
- radius: radius, // 半径
360
- borderWeight: 1, // 描边的宽度
361
- strokeColor: "#0080FF", // 轮廓线颜色
362
- strokeWeight: 1, // 轮廓线宽度
363
- fillOpacity: 0.1, // 圆形填充透明度
364
- fillColor: "#0080FF", // 圆形填充颜色
365
- zIndex: 100, // 圆形的叠加顺序
366
- ...circleConf,
367
- });
368
- _c.on("click", onClick);
369
- this.circles[id] = _c;
370
- map?.add(_c);
371
- return _c;
372
- }
373
-
374
- rmCircle(id) {
375
- this.map.remove(this.circles[id]);
376
- delete this.circles[id];
377
- }
378
-
379
-
380
- // 判断当前点击点位是否在地图现有元素范围内,并回调函数返回值
381
- isContains(e, callBack) {
382
- const element = {
383
- ...this.circles,
384
- ...this.polygonEnum,
385
- };
386
- const { lng, lat } = e.lnglat || {};
387
- const point = new this.AMap.LngLat(lng, lat);
388
- const isRange = Object.keys(element).some((key) => element[key].contains(point));
389
- if (!isRange) {
390
- callBack && callBack(e);
391
- }
392
- return isRange;
393
- }
394
-
395
- /**
396
- * 创建或修改地图选点的 Marker
397
- * 在当前 utils 中唯一
398
- * @param lon
399
- * @param lat
400
- * @param opt
401
- * @returns
402
- */
403
- setPickerMarker(lon, lat, opt?: any) {
404
- if (lon && lat) {
405
- const { marker = this.pickerMarker } = opt || {};
406
- this.pickerMarker = this.setMarker(lon, lat, {
407
- ...opt,
408
- marker,
409
- });
410
- }
411
- }
412
-
413
- getPolygonCenter(coordinate) {
414
- if (coordinate.length > 0) {
415
- let features = turf.featureCollection(coordinate.map((item) => turf.point(item)));
416
- let center = turf.center(features).geometry.coordinates;
417
- return center;
418
- }
419
- return [];
420
- }
421
-
422
- on(...args) {
423
- return this.map?.on(...args);
424
- }
425
-
426
- off(...args) {
427
- return this.map?.off(...args);
428
- }
429
- }
430
-
431
- export { AMapLoader };
1
+ import * as turf from "@turf/turf";
2
+ import { nanoid } from "nanoid";
3
+
4
+ import AMapLoader from "./loader";
5
+ import { markerIconBase64 } from "../../../assets/svg-icon";
6
+
7
+ if (!window._AMapLoaderTemp) {
8
+ window._AMapLoaderTemp = {};
9
+ }
10
+
11
+ export function setKey(key, securityJsCode) {
12
+ window._AMapLoaderTemp.key = key;
13
+ window._AMapLoaderTemp.securityJsCode = securityJsCode;
14
+ }
15
+
16
+ export function setServerKey(key, securityJsCode) {
17
+ window._AMapLoaderTemp.serverKey = key;
18
+ window._AMapLoaderTemp.serverSecurityJsCode = securityJsCode;
19
+ }
20
+
21
+ export function setSecurityJsCode(securityJsCode) {
22
+ window._AMapLoaderTemp.securityJsCode = securityJsCode;
23
+ window._AMapSecurityConfig = {
24
+ securityJsCode: securityJsCode,
25
+ };
26
+ }
27
+
28
+ export interface IMarker {
29
+ setIcon: Function;
30
+ setSize: Function;
31
+ setOffset: Function;
32
+ getPosition: Function;
33
+ }
34
+
35
+ export type setMarkerOptT = {
36
+ id?: number | string;
37
+ marker?: IMarker;
38
+ map?: Object;
39
+ AMap?: Object;
40
+ markerIconConf?: Object;
41
+ markerConf?: Object;
42
+ onClick?: Function;
43
+ data?: Object;
44
+ };
45
+
46
+ export type markersT = {
47
+ [k: number | string]: IMarker;
48
+ };
49
+
50
+ export class MapUtils {
51
+ public map;
52
+ public AMap;
53
+ public markers: markersT = {};
54
+ public circles = {};
55
+ public pickerMarker;
56
+ public markerIconConf;
57
+ public markerConf;
58
+ public polyEditor = {
59
+ PolygonEditor: undefined,
60
+ PolylineEditor: undefined,
61
+ };
62
+ public polygonEnum = {};
63
+ public polygonConf;
64
+ public mapDomRef;
65
+
66
+ constructor({ map, AMap, mapDomRef, markerIconConf, markerConf, polygonConf }) {
67
+ if (!map) {
68
+ throw new Error("请传入地图实例");
69
+ }
70
+ this.map = map;
71
+ this.AMap = AMap;
72
+ this.markerIconConf = markerIconConf || {};
73
+ this.markerConf = markerConf || {};
74
+ this.polygonEnum = {};
75
+ this.polygonConf = polygonConf || {};
76
+ this.mapDomRef = mapDomRef;
77
+ }
78
+
79
+ /**
80
+ * 获取地图中心点
81
+ * @param map
82
+ * @returns { lng: Number, lat: Number }
83
+ */
84
+ getCenter(map = this.map) {
85
+ if (!map) {
86
+ throw new Error("请传入地图实例和 AMap");
87
+ }
88
+ return map && map.getCenter && map.getCenter();
89
+ }
90
+
91
+ /**
92
+ * 设置地图中心点
93
+ * @param lon
94
+ * @param lat
95
+ * @param opt
96
+ */
97
+ setCenter(
98
+ lon,
99
+ lat,
100
+ opt = { immediately: undefined, duration: undefined, map: this.map, AMap: this.AMap || window.AMap },
101
+ ) {
102
+ const { map = this.map, AMap = this.AMap || window.AMap, immediately, duration } = opt || {};
103
+ if (!(AMap && map)) {
104
+ throw new Error("请传入地图实例和 AMap");
105
+ }
106
+ const position = new window.AMap.LngLat(lon, lat);
107
+ // 设置中心点
108
+ map.setCenter(position, immediately, duration);
109
+ }
110
+
111
+ setFitView() {
112
+ // 调整地图以适应所有标记点
113
+ this.map.setFitView();
114
+ }
115
+
116
+ /**
117
+ * 创建或修改 Marker
118
+ * @param lon
119
+ * @param lat
120
+ * @param opt
121
+ * @returns
122
+ */
123
+ setMarker(lon, lat, opt?: setMarkerOptT) {
124
+ const {
125
+ data,
126
+ id = nanoid(),
127
+ marker,
128
+ map = this.map,
129
+ AMap = this.AMap,
130
+ markerIconConf = {},
131
+ markerConf = {},
132
+ onClick,
133
+ } = opt || {};
134
+
135
+ if (!(AMap && map)) {
136
+ throw new Error("请传入地图实例和 AMap");
137
+ }
138
+ if (lon === null || lon === undefined || lat === null || lat === undefined) {
139
+ console.warn("setMarker 请传入正确的经纬度");
140
+ return;
141
+ }
142
+
143
+ const position = new AMap.LngLat(lon, lat);
144
+ let _marker = marker || this.markers[id];
145
+
146
+ // 创建 Marker 或修改位置
147
+ if (_marker) {
148
+ // @ts-ignore
149
+ _marker.setPosition(position);
150
+ return _marker;
151
+ }
152
+
153
+ const size = new AMap.Size(34, 34);
154
+ const icon = new AMap.Icon({
155
+ size, //图标尺寸
156
+ image: markerIconBase64, // Icon 的图像
157
+ imageSize: size, // 根据所设置的大小拉伸或压缩图片
158
+ ...this.markerIconConf,
159
+ ...markerIconConf,
160
+ });
161
+ const _m = new AMap.Marker({
162
+ offset: new AMap.Pixel(-17, -34),
163
+ icon,
164
+ position,
165
+ extData: {
166
+ id,
167
+ data,
168
+ },
169
+ ...this.markerConf,
170
+ ...markerConf,
171
+ });
172
+
173
+ this.markers[id] = _m;
174
+
175
+ if (onClick) {
176
+ _m.on("click", onClick);
177
+ }
178
+
179
+ map?.add(_m);
180
+ return _m;
181
+ }
182
+
183
+ /**
184
+ * 创建绘制实例
185
+ */
186
+ createPoly(element) {
187
+ if (element !== "Polypoint") {
188
+ // 同时绘制多种形状问题修复
189
+ if (this.polyEditor[`${element}Editor`]) {
190
+ return this.polyEditor[`${element}Editor`];
191
+ }
192
+ this.polyEditor[`${element}Editor`] = new this.AMap[`${element}Editor`](this.map, undefined, {
193
+ createOptions: {
194
+ strokeColor: "#1791fc",
195
+ strokeWeight: 2,
196
+ fillOpacity: 0.4,
197
+ fillColor: "#1791fc",
198
+ },
199
+ editOptions: {
200
+ strokeColor: "#1791fc",
201
+ strokeWeight: 2,
202
+ fillOpacity: 0.4,
203
+ fillColor: "#1791fc",
204
+ },
205
+ ...this.polygonConf,
206
+ });
207
+ }
208
+ }
209
+
210
+ /**
211
+ * 按points绘制元素实例
212
+ */
213
+ setPolygon(opt) {
214
+ const { points, id, onClick, element = "Polygon", map = this.map, AMap = this.AMap } = opt;
215
+
216
+ if (!(AMap && map)) {
217
+ throw new Error("请传入地图实例和 AMap");
218
+ }
219
+
220
+ const marker = this.polygonEnum[id];
221
+ if (marker) {
222
+ return;
223
+ }
224
+
225
+ const graphical = new this.AMap[element]({
226
+ path: points,
227
+ strokeColor: "#1791fc",
228
+ strokeWeight: 2,
229
+ fillOpacity: 0.4,
230
+ fillColor: "#1791fc",
231
+ });
232
+
233
+ graphical.id = id;
234
+ // 多边形绘制完毕保存中心点
235
+ graphical.path = points;
236
+ graphical.center = this.getPolygonCenter(points);
237
+ this.polygonEnum[id] = graphical;
238
+ this.map.add([graphical]);
239
+ graphical.on("click", onClick);
240
+ }
241
+
242
+ /**
243
+ * 创建手动绘制实例
244
+ */
245
+ startPolygon(opt) {
246
+ const { onClick, element = "Polygon" } = opt;
247
+ this.polyEditor[`${element}Editor`].on("add", (data) => {
248
+ const id = nanoid();
249
+ data.target.id = id;
250
+ const graphical = data.target;
251
+ this.polygonEnum[id] = graphical;
252
+ graphical.on("click", onClick);
253
+ });
254
+
255
+ this.polyEditor[`${element}Editor`].on("end", (data) => {
256
+ // 多边形绘制完毕保存中心点
257
+ const graphical = data.target;
258
+ if (graphical.type === "AMap.Overlay") {
259
+ graphical.path = graphical.getPath().map((item) => [item.lng, item.lat]);
260
+ graphical.center = this.getPolygonCenter(graphical.path);
261
+ }
262
+ });
263
+ }
264
+
265
+ /**
266
+ * 手动开始绘制函数
267
+ */
268
+ setDrawPolygon({ map = this.map, AMap = this.AMap, element = "Polygon" }) {
269
+ if (!(AMap && map)) {
270
+ throw new Error("请传入地图实例和 AMap");
271
+ }
272
+ this.polyEditor[`${element}Editor`].close();
273
+ this.polyEditor[`${element}Editor`].setTarget();
274
+ this.polyEditor[`${element}Editor`].open();
275
+ }
276
+
277
+ /**
278
+ * 编辑态
279
+ * @param item
280
+ */
281
+ setEditPolygon({ id, element = "Polygon" }) {
282
+ const target = this.polygonEnum[id];
283
+ if (!target) {
284
+ console.warn("setEditPolygon 找不到目标对象:", id);
285
+ return;
286
+ }
287
+ this.polyEditor[`${element}Editor`].setTarget(target);
288
+ this.polyEditor[`${element}Editor`].open();
289
+ }
290
+
291
+ setMarkerIcon(
292
+ id,
293
+ opt = {
294
+ size: null,
295
+ image: null,
296
+ offset: null,
297
+ },
298
+ ) {
299
+ const marker = this.markers[id];
300
+ if (!marker) {
301
+ return;
302
+ }
303
+ const { image, offset } = opt || {};
304
+ const AMap = this.AMap;
305
+
306
+ const size = new AMap.Size(...opt?.size);
307
+ const icon = new AMap.Icon({
308
+ size, //图标尺寸
309
+ image: image, // Icon 的图像
310
+ imageSize: size, // 根据所设置的大小拉伸或压缩图片
311
+ });
312
+
313
+ marker.setIcon(icon);
314
+ marker.setSize(size);
315
+ marker.setOffset(new AMap.Pixel(...offset));
316
+ }
317
+
318
+ rmPolygon(id, element = "Polygon") {
319
+ this.map.remove(this.polygonEnum[id]);
320
+ this.polyEditor[`${element}Editor`].close();
321
+ this.polyEditor[`${element}Editor`].setTarget();
322
+ delete this.polygonEnum[id];
323
+ }
324
+
325
+ /**
326
+ * 删除指定 marker
327
+ * @param id
328
+ */
329
+ rmMarker(id) {
330
+ this.map.remove(this.markers[id]);
331
+ delete this.markers[id];
332
+ }
333
+
334
+ setCircle(lon, lat, radius, opt?) {
335
+ const { id = nanoid(), circle, map = this.map, AMap = this.AMap, onClick, circleConf = {} } = opt || {};
336
+ if (!(AMap && map)) {
337
+ throw new Error("请传入地图实例和 AMap");
338
+ }
339
+ if (lon === null || lon === undefined || lat === null || lat === undefined) {
340
+ console.warn("setMarker 请传入正确的经纬度");
341
+ return;
342
+ }
343
+ // circles
344
+
345
+ const center = new AMap.LngLat(lon, lat);
346
+ let _circle = circle || this.circles[id];
347
+
348
+ // 创建 Marker 或修改位置
349
+ if (_circle) {
350
+ // @ts-ignore
351
+ _circle.setCenter(center);
352
+ _circle.setRadius(radius);
353
+ return _circle;
354
+ }
355
+
356
+ //创建圆形 Circle 实例
357
+ const _c = new AMap.Circle({
358
+ center: center, // 圆心
359
+ radius: radius, // 半径
360
+ borderWeight: 1, // 描边的宽度
361
+ strokeColor: "#0080FF", // 轮廓线颜色
362
+ strokeWeight: 1, // 轮廓线宽度
363
+ fillOpacity: 0.1, // 圆形填充透明度
364
+ fillColor: "#0080FF", // 圆形填充颜色
365
+ zIndex: 100, // 圆形的叠加顺序
366
+ ...circleConf,
367
+ });
368
+ _c.on("click", onClick);
369
+ this.circles[id] = _c;
370
+ map?.add(_c);
371
+ return _c;
372
+ }
373
+
374
+ rmCircle(id) {
375
+ this.map.remove(this.circles[id]);
376
+ delete this.circles[id];
377
+ }
378
+
379
+
380
+ // 判断当前点击点位是否在地图现有元素范围内,并回调函数返回值
381
+ isContains(e, callBack) {
382
+ const element = {
383
+ ...this.circles,
384
+ ...this.polygonEnum,
385
+ };
386
+ const { lng, lat } = e.lnglat || {};
387
+ const point = new this.AMap.LngLat(lng, lat);
388
+ const isRange = Object.keys(element).some((key) => element[key].contains(point));
389
+ if (!isRange) {
390
+ callBack && callBack(e);
391
+ }
392
+ return isRange;
393
+ }
394
+
395
+ /**
396
+ * 创建或修改地图选点的 Marker
397
+ * 在当前 utils 中唯一
398
+ * @param lon
399
+ * @param lat
400
+ * @param opt
401
+ * @returns
402
+ */
403
+ setPickerMarker(lon, lat, opt?: any) {
404
+ if (lon && lat) {
405
+ const { marker = this.pickerMarker } = opt || {};
406
+ this.pickerMarker = this.setMarker(lon, lat, {
407
+ ...opt,
408
+ marker,
409
+ });
410
+ }
411
+ }
412
+
413
+ getPolygonCenter(coordinate) {
414
+ if (coordinate.length > 0) {
415
+ let features = turf.featureCollection(coordinate.map((item) => turf.point(item)));
416
+ let center = turf.center(features).geometry.coordinates;
417
+ return center;
418
+ }
419
+ return [];
420
+ }
421
+
422
+ on(...args) {
423
+ return this.map?.on(...args);
424
+ }
425
+
426
+ off(...args) {
427
+ return this.map?.off(...args);
428
+ }
429
+ }
430
+
431
+ export { AMapLoader };