web-component-gallery 2.0.33 → 2.0.35

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.
@@ -41,14 +41,6 @@ const AmapDraw = {
41
41
  drawLayers: null,
42
42
  drawMouseTool: null,
43
43
  buttonProps: [],
44
- drawButtonProps: [
45
- { type: 'primary', text: '绘制', event: { click: this.AmapDraw } },
46
- { text: '清除', event: { click: this.AmapDrawClear } }
47
- ],
48
- saveButtonProps: [
49
- { type: 'primary', text: '保存', event: { click: this.AmapDrawSave } },
50
- { text: '取消', event: { click: this.AmapDrawReset } }
51
- ],
52
44
  /** 地图类型 */
53
45
  amapType: 1,
54
46
  /** 卫星图 */
@@ -63,12 +55,24 @@ const AmapDraw = {
63
55
  set(newValue) {
64
56
  this.$emit('getAMapLayersInfo', newValue)
65
57
  }
58
+ },
59
+ // 优化后的按钮配置
60
+ optimizedButtonProps() {
61
+ return {
62
+ draw: [
63
+ { type: 'primary', text: '绘制', event: { click: this.handleDrawStart } },
64
+ { text: '清除', event: { click: this.handleDrawClear } }
65
+ ],
66
+ save: [
67
+ { type: 'primary', text: '保存', event: { click: this.handleDrawSave } },
68
+ { text: '取消', event: { click: this.handleDrawReset } }
69
+ ]
70
+ }
66
71
  }
67
72
  },
68
73
  watch: {
69
74
  drawInfo: {
70
- handler(newVal, oldVal) {
71
- const newValue = this.drawLayersInfo
75
+ handler(newVal) {
72
76
  newVal && this.echoDrawLayers(this.drawType)
73
77
  },
74
78
  deep: true
@@ -87,10 +91,8 @@ const AmapDraw = {
87
91
  async mounted() {
88
92
  this.amapStore = await getAMapInstance()
89
93
  this.amap = this.amapStore.initMap(this.drawEl)
90
- this.amapSatellite = new this.amapStore.AMap.TileLayer.Satellite()
91
- this.amap.add(this.amapSatellite)
92
- // this.amapSatellite.hide()
93
- this.buttonProps = [].concat(this.drawButtonProps)
94
+ this.initMap()
95
+ this.buttonProps = [...this.optimizedButtonProps.draw]
94
96
  this.echoDrawLayers(this.drawType)
95
97
  /** 如果有限制区域,则描绘在地图上 */
96
98
  this.drawLimitArea && this.setDrawLimitArea()
@@ -99,59 +101,115 @@ const AmapDraw = {
99
101
  this.AmapDrawClear()
100
102
  },
101
103
  methods: {
102
- /** 绘制 */
103
- AmapDraw() {
104
- this.buttonProps = [].concat(this.saveButtonProps)
105
- this.drawMouseTool = new this.amapStore.AMap.MouseTool(this.amap)
106
-
107
- this.AmapDrawClear()
108
- this.setDrawLayers(this.drawType)
109
-
104
+ /**
105
+ * 初始化地图实例
106
+ */
107
+ initMap() {
108
+ this.amap = this.amapStore.initMap(this.drawEl)
109
+ this.amapSatellite = new this.amapStore.AMap.TileLayer.Satellite()
110
+ this.amap.add(this.amapSatellite)
111
+ },
112
+ /**
113
+ * 开始绘制
114
+ */
115
+ handleDrawStart() {
116
+ this.buttonProps = [...this.optimizedButtonProps.save]
117
+ this.initDrawTool()
118
+ this.prepareDrawEnvironment()
119
+
110
120
  this.drawMouseTool.on('draw', ({ obj }) => {
111
- if (this.drawArea && !this[`is${this.drawType}InRing`](obj))
112
- return this.amap.remove(obj), this.$message.error('未在范围内描绘!')
113
- /** 暂未支持一次性绘制多个 */
114
- this.drawCount > 1
115
- ? ((this.drawLayers = this.drawLayers || []), this.drawLayers.push(obj))
116
- : (this.drawLayers = obj)
117
- if (this.drawLayers && (this.drawLayers.length == this.drawCount || this.drawCount == 1)) {
118
- return this.drawMouseTool.close()
119
- }
121
+ this.handleDrawComplete(obj)
120
122
  })
121
123
  },
122
- /** 清空绘制 / 清空搜索 */
123
- AmapDrawClear() {
124
- this.amap.getAllOverlays().forEach(layerItem => layerItem.getExtData() != 'Area' && this.amap.remove(layerItem))
125
- // this.amap.clearMap()
124
+
125
+ /**
126
+ * 处理绘制完成事件
127
+ */
128
+ handleDrawComplete(obj) {
129
+ if (this.drawArea && !this.checkInBoundary(obj)) {
130
+ this.amap.remove(obj)
131
+ this.$message.error('未在范围内描绘!')
132
+ return
133
+ }
134
+
135
+ this.updateDrawLayers(obj)
136
+
137
+ if (this.isDrawComplete()) {
138
+ this.drawMouseTool.close()
139
+ }
140
+ },
141
+
142
+ /**
143
+ * 检查绘制是否完成
144
+ */
145
+ isDrawComplete() {
146
+ return this.drawLayers &&
147
+ (this.drawLayers.length === this.drawCount || this.drawCount === 1)
148
+ },
149
+
150
+ /**
151
+ * 更新绘制图层
152
+ */
153
+ updateDrawLayers(obj) {
154
+ if (this.drawCount > 1) {
155
+ this.drawLayers = this.drawLayers || []
156
+ this.drawLayers.push(obj)
157
+ } else {
158
+ this.drawLayers = obj
159
+ }
160
+ },
161
+
162
+ /**
163
+ * 清除绘制
164
+ */
165
+ handleDrawClear() {
166
+ this.cleanOverlays()
126
167
  this.drawLayers = null
127
168
  this.drawLayersInfo = {}
128
169
  },
129
- /** 保存当前绘制 */
130
- AmapDrawSave() {
170
+
171
+ /**
172
+ * 保存绘制
173
+ */
174
+ handleDrawSave() {
131
175
  this.assignDrawLayers(this.drawType)
132
- this.buttonProps = [].concat(this.drawButtonProps)
176
+ this.buttonProps = [...this.optimizedButtonProps.draw]
133
177
  },
134
- /** 取消当前绘制 */
135
- AmapDrawReset() {
136
- this.buttonProps = [].concat(this.drawButtonProps)
137
- this.AmapDrawClear()
178
+
179
+ /**
180
+ * 取消绘制
181
+ */
182
+ handleDrawReset() {
183
+ this.buttonProps = [...this.optimizedButtonProps.draw]
184
+ this.handleDrawClear()
138
185
  },
139
- /** 搜索地址 */
186
+
187
+ /**
188
+ * 搜索地址选择
189
+ */
140
190
  searchChoose(searchMarker, searchInfo) {
141
- this.AmapDrawClear()
191
+ this.handleDrawClear()
142
192
  searchMarker.setMap(this.amap)
143
193
  this.amap.setZoomAndCenter(15, searchMarker.getPosition())
144
- this.drawType == 'point' && (this.drawLayersInfo = searchInfo)
145
- // this.$emit('getAMapLayersInfo', this.drawLayersInfo)
194
+ this.drawType === 'point' && (this.drawLayersInfo = searchInfo)
146
195
  },
147
- /** 检测覆盖物配置信息并同步 */
196
+
197
+ /**
198
+ * 设置覆盖物选项
199
+ */
148
200
  setOverlayOptions(options) {
149
201
  if (!this.drawLayers) return
150
- this.drawCount > 1
151
- ? this.drawLayers.forEach(drawItem => drawItem.setOptions(options))
152
- : this.drawLayers.setOptions(options)
202
+
203
+ if (this.drawCount > 1) {
204
+ this.drawLayers.forEach(drawItem => drawItem.setOptions(options))
205
+ } else {
206
+ this.drawLayers.setOptions(options)
207
+ }
153
208
  },
154
- /** 绘制限定区域 */
209
+
210
+ /**
211
+ * 设置绘制限制区域
212
+ */
155
213
  setDrawLimitArea() {
156
214
  let areaRange = []
157
215
  try {
@@ -159,12 +217,15 @@ const AmapDraw = {
159
217
  } catch {
160
218
  areaRange = this.drawLimitArea
161
219
  }
220
+
162
221
  this.drawArea && this.amap.remove(this.drawArea)
163
- /** 判断目前地图上存在的所绘制覆盖物是否在限制范围内 */
164
222
  this.setMapArea(areaRange)
165
- this.judgeOverlays()
223
+ this.validateOverlays()
166
224
  },
167
- /** 绘制地图区域范围 */
225
+
226
+ /**
227
+ * 设置地图区域范围
228
+ */
168
229
  setMapArea(jurisdictionRange) {
169
230
  this.drawArea = new this.amapStore.AMap.Polygon({
170
231
  map: this.amap,
@@ -173,16 +234,42 @@ const AmapDraw = {
173
234
  strokeWeight: 6,
174
235
  ...this.drawLimitAreaOptions
175
236
  })
237
+
176
238
  const { lng, lat } = this.drawArea.getBounds().getCenter()
177
239
  this.amap.setZoomAndCenter(15, [lng, lat])
178
240
  },
179
- /** 判断一个线段是否在面内 */
180
- islineInRing(polyline) {
241
+
242
+ /**
243
+ * 验证覆盖物是否在边界内
244
+ */
245
+ validateOverlays() {
246
+ this.amap.getAllOverlays().forEach(layerItem => {
247
+ if (layerItem.getExtData() !== 'Area' && !this.checkInBoundary(layerItem)) {
248
+ this.removeOverlay(layerItem)
249
+ }
250
+ })
251
+ },
252
+
253
+ /**
254
+ * 检查是否在边界内
255
+ */
256
+ checkInBoundary(obj) {
257
+ const type = this.drawType
258
+ return type === 'line' ? this.isLineInBoundary(obj) : this.isPointInBoundary(obj)
259
+ },
260
+
261
+ /**
262
+ * 检查线段是否在边界内
263
+ */
264
+ isLineInBoundary(polyline) {
181
265
  const paths = polyline.getPath()
182
- return paths.every(pointItem => this.ispointInRing(pointItem))
266
+ return paths.every(pointItem => this.isPointInBoundary(pointItem))
183
267
  },
184
- /** 判断一个点是否在面内 */
185
- ispointInRing(point) {
268
+
269
+ /**
270
+ * 检查点是否在边界内
271
+ */
272
+ isPointInBoundary(point) {
186
273
  let position
187
274
  try {
188
275
  position = point.getPosition()
@@ -191,144 +278,232 @@ const AmapDraw = {
191
278
  }
192
279
  return this.amapStore.AMap.GeometryUtil.isPointInRing(position, this.drawArea.getPath())
193
280
  },
194
- /** 判断地图上所覆盖物是否在限制范围内 */
195
- judgeOverlays() {
196
- this.amap
197
- .getAllOverlays()
198
- .forEach(
199
- layerItem =>
200
- layerItem.getExtData() != 'Area' &&
201
- !this[`is${this.drawType}InRing`](layerItem) &&
202
- this.removeOverlay(layerItem)
203
- )
204
- },
205
- /** 清除单个覆盖物(或 不符合区域范围的覆盖物 */
281
+
282
+ /**
283
+ * 移除覆盖物
284
+ */
206
285
  removeOverlay(layerItem) {
207
286
  this.amap.remove(layerItem)
208
- this.$message.error('未在区域范围内,请重新描绘!')
287
+ this.$message.error('未在区域范围内,请重新描绘!')
288
+
209
289
  try {
210
- const i = this.drawLayers.findIndex(layer => layer.getExtData() == layerItem.getExtData())
211
- this.drawLayers.splice(i, 1)
212
- } catch {
213
- this.drawLayers = null
214
- this.drawLayersInfo = {}
290
+ if (Array.isArray(this.drawLayers)) {
291
+ const index = this.drawLayers.findIndex(layer => layer.getExtData() === layerItem.getExtData())
292
+ if (index !== -1) {
293
+ this.drawLayers.splice(index, 1)
294
+ }
295
+ } else {
296
+ this.drawLayers = null
297
+ this.drawLayersInfo = {}
298
+ }
299
+ } catch (error) {
300
+ console.error('移除覆盖物出错:', error)
215
301
  }
216
302
  },
217
- /** 根据类型处理不同数据 */
218
- async setDrawLayers(drawType) {
219
- const type = {
303
+
304
+ /**
305
+ * 初始化绘制工具
306
+ */
307
+ initDrawTool() {
308
+ this.drawMouseTool = new this.amapStore.AMap.MouseTool(this.amap)
309
+ this.handleDrawClear()
310
+ this.prepareDrawLayers(this.drawType)
311
+ },
312
+
313
+ /**
314
+ * 准备绘制环境
315
+ */
316
+ prepareDrawEnvironment() {
317
+ this.drawMouseTool.on('draw', ({ obj }) => {
318
+ this.handleDrawComplete(obj)
319
+ })
320
+ },
321
+
322
+ /**
323
+ * 准备绘制图层
324
+ */
325
+ async prepareDrawLayers(drawType) {
326
+ const typeMap = {
220
327
  point: 'marker',
221
328
  line: 'polyline',
222
329
  polygon: 'polygon'
223
330
  }
224
-
225
- /** 图层基础配置 */
226
- const drawOptions =
227
- drawType == 'point'
228
- ? {
229
- anchor: 'bottom-center',
230
- icon: await this.amapStore.setIcon(this.drawIcon)
231
- }
232
- : { strokeWeight: 8 }
233
-
234
- this.drawMouseTool[type[drawType]]({
235
- ...drawOptions,
331
+
332
+ const baseOptions = drawType === 'point'
333
+ ? {
334
+ anchor: 'bottom-center',
335
+ icon: await this.amapStore.setIcon(this.drawIcon)
336
+ }
337
+ : { strokeWeight: 8 }
338
+
339
+ this.drawMouseTool[typeMap[drawType]]({
340
+ ...baseOptions,
236
341
  ...this.drawOptions
237
342
  })
238
343
  },
239
- /** 赋值不同图层数据 */
344
+
345
+ /**
346
+ * 分配图层数据
347
+ */
240
348
  assignDrawLayers(drawType) {
241
- if (drawType == 'point') {
242
- const { lng, lat } = this.drawLayers.getPosition()
243
- const position = { longitude: lng, latitude: lat }
244
- this.amapStore.getMapAddress(position, address => {
245
- this.drawLayersInfo = {
246
- ...position,
247
- ...address
248
- }
249
- })
349
+ if (drawType === 'point') {
350
+ this.handlePointDraw()
250
351
  return
251
352
  }
353
+ this.handleShapeDraw()
354
+ },
355
+
356
+ /**
357
+ * 处理点绘制
358
+ */
359
+ handlePointDraw() {
360
+ const { lng, lat } = this.drawLayers.getPosition()
361
+ const position = { longitude: lng, latitude: lat }
362
+
363
+ this.amapStore.getMapAddress(position, address => {
364
+ this.drawLayersInfo = {
365
+ ...position,
366
+ ...address
367
+ }
368
+ })
369
+ },
370
+
371
+ /**
372
+ * 处理形状绘制
373
+ */
374
+ handleShapeDraw() {
252
375
  const range = this.drawLayers.getPath().map(({ lng, lat }) => [lng, lat])
253
376
  this.drawLayersInfo = { range }
254
377
  },
255
- /** 回显不同类型图层 */
378
+
379
+ /**
380
+ * 回显绘制图层
381
+ */
256
382
  echoDrawLayers(drawType) {
257
- /** 如若没挂载layers属性则退出 */
258
383
  if (!this.amapStore || !this.amap) return
384
+
385
+ this.cleanOverlays()
386
+
387
+ if (this.drawInfoAll) {
388
+ this.renderAllDrawLayers()
389
+ return
390
+ }
391
+
392
+ this.renderSingleDrawLayer(drawType)
393
+ },
259
394
 
260
- const drawLayersItem = async (drawItem, type, i = 0) => {
261
- let layersItem = null
262
- const { longitude, latitude, range } = drawItem
263
- if ((longitude && latitude) || range) {
264
- const setOptions = {
265
- extData: `draw${type}${i}`,
266
- ...this.drawOptions
267
- }
268
-
269
- if (type == 'point') {
270
- layersItem = await this.amapStore.setMarker(this.drawIcon, {
271
- position: [longitude, latitude],
272
- ...setOptions
273
- })
274
- } else {
275
- const options = {
276
- path: range,
277
- strokeWeight: 8,
278
- ...setOptions
279
- }
280
- layersItem =
281
- type == 'line' ? new this.amapStore.AMap.Polyline(options) : new this.amapStore.AMap.Polygon(options)
282
- }
395
+ /**
396
+ * 清理覆盖物
397
+ */
398
+ cleanOverlays() {
399
+ this.amap.getAllOverlays().forEach(layerItem => {
400
+ if (layerItem.getExtData() !== 'Area') {
401
+ this.amap.remove(layerItem)
402
+ }
403
+ })
404
+ },
283
405
 
284
- layersItem.setMap(this.amap)
285
- this.amap.setFitView()
406
+ /**
407
+ * 渲染所有绘制图层
408
+ */
409
+ renderAllDrawLayers() {
410
+ for (const key in this.drawInfoAll) {
411
+ const isArray = Array.isArray(this.drawInfoAll[key])
412
+
413
+ if (isArray) {
414
+ this.drawInfoAll[key].forEach((drawItem, i) => {
415
+ this.renderDrawLayer(drawItem, key, i)
416
+ })
417
+ } else {
418
+ this.renderDrawLayer(this.drawInfoAll[key], key)
286
419
  }
287
420
  }
421
+ },
288
422
 
289
- if (!this.isExhibition) {
290
- drawLayersItem(this.drawLayersInfo, drawType)
291
- return
292
- }
423
+ /**
424
+ * 渲染单个绘制图层
425
+ */
426
+ renderSingleDrawLayer(drawType) {
427
+ this.renderDrawLayer(this.drawLayersInfo, drawType)
428
+ },
293
429
 
294
- if (this.drawInfoAll) {
295
- for (let key in this.drawInfoAll) {
296
- const isArray = Array.isArray(this.drawInfoAll[key])
297
- isArray
298
- ? this.drawInfoAll[key].forEach((drawItem, i) => {
299
- drawLayersItem(drawItem, key, i)
300
- })
301
- : drawLayersItem(this.drawInfoAll[key], key)
430
+ /**
431
+ * 渲染绘制图层
432
+ */
433
+ async renderDrawLayer(drawItem, type, index = 0) {
434
+ if (!drawItem) return
435
+
436
+ const { longitude, latitude, range } = drawItem
437
+ if (!((longitude && latitude) || range)) return
438
+
439
+ const setOptions = {
440
+ extData: `draw${type}${index}`,
441
+ ...this.drawOptions
442
+ }
443
+
444
+ let layersItem = null
445
+
446
+ if (type === 'point') {
447
+ layersItem = await this.amapStore.setMarker(this.drawIcon, {
448
+ position: [longitude, latitude],
449
+ ...setOptions
450
+ })
451
+ } else {
452
+ const options = {
453
+ path: range,
454
+ strokeWeight: 8,
455
+ ...setOptions
302
456
  }
303
- return
457
+
458
+ layersItem = type === 'line'
459
+ ? new this.amapStore.AMap.Polyline(options)
460
+ : new this.amapStore.AMap.Polygon(options)
304
461
  }
305
-
306
- drawLayersItem(this.drawLayersInfo, drawType)
462
+
463
+ layersItem.setMap(this.amap)
464
+ this.amap.setFitView()
307
465
  },
308
- /** 切换地图类型 */
466
+
467
+ /**
468
+ * 切换地图类型
469
+ */
309
470
  onLayersSwitch(key) {
310
471
  this.amapType = key
311
- this.amapType ?
312
- this.amapSatellite.show() :
313
- this.amapSatellite.hide()
472
+ this.amapType ? this.amapSatellite.show() : this.amapSatellite.hide()
473
+ },
474
+
475
+ /**
476
+ * 清理资源
477
+ */
478
+ cleanupResources() {
479
+ this.handleDrawClear()
480
+ this.drawMouseTool && this.drawMouseTool.close()
314
481
  }
315
482
  },
316
483
  render(h) {
317
484
  const amapLayers = ['电子', '卫星']
318
- const { drawEl, drawIcon, amapType, buttonProps, isExhibition, AmapDrawClear, searchChoose, onLayersSwitch } = this
485
+ const { drawEl, drawIcon, amapType, buttonProps, isExhibition } = this
319
486
 
320
487
  return (
321
488
  <div class="AmapDraw">
322
489
  {!isExhibition && (
323
- <AmapSearch {...{ props: { drawIcon, buttonProps }, on: { searchReset: AmapDrawClear, searchChoose } }} />
490
+ <AmapSearch
491
+ {...{
492
+ props: { drawIcon, buttonProps },
493
+ on: {
494
+ searchReset: this.handleDrawClear,
495
+ searchChoose: this.searchChoose
496
+ }
497
+ }}
498
+ />
324
499
  )}
325
500
  <div id={drawEl} />
326
501
  <div class="AmapDraw__Layers">
327
502
  {amapLayers.map((layer, i) => (
328
503
  <span
329
- key={i}
330
- class={i === amapType && 'LayersActive'}
331
- onClick={() => onLayersSwitch(i)}
504
+ key={i}
505
+ class={i === amapType && 'LayersActive'}
506
+ onClick={() => this.onLayersSwitch(i)}
332
507
  >
333
508
  {layer}
334
509
  </span>
@@ -7,7 +7,7 @@ const BrowseProps = {
7
7
  /** 支持http地址可配置 */
8
8
  httpsUrl: PropTypes.object.def({}),
9
9
  /** 限制高度 */
10
- astrictH: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
10
+ astrictH: PropTypes.oneOfType([PropTypes.string, PropTypes.number])
11
11
  }
12
12
 
13
13
  // 文件类型常量
@@ -60,9 +60,7 @@ function renderContent(h, url, images) {
60
60
 
61
61
  const attrs = {
62
62
  src: url,
63
- style: astrictH
64
- ? `width: auto; height: ${astrictH}px; objectFit: contain;`
65
- : 'width: 100%; height: 100%;'
63
+ ...(astrictH && { style: `width: auto; height: ${astrictH}px; objectFit: contain;` })
66
64
  }
67
65
 
68
66
  if (CustomTag === 'iframe') attrs['controls'] = true
@@ -99,7 +97,7 @@ const Browse = {
99
97
  renderContent.call(this, h, dataItem.url, innerData)
100
98
  )
101
99
  }
102
- </div>
100
+ </div>
103
101
  )
104
102
  }
105
103
  }
@@ -1,4 +1,4 @@
1
- .Browse, iframe {
1
+ iframe {
2
2
  width: 100%;
3
3
  height: 100%;
4
4
  }