huweili-cesium 1.1.2 → 1.1.3

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 (3) hide show
  1. package/README.md +62 -1
  2. package/index.vue +52 -7
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -84,7 +84,68 @@ export default defineConfig({
84
84
  | `mapId` | `string` | `'default'` | 多地图实例 ID,与 `mapStore` 对应 |
85
85
  | `options` | `object` | — | 覆盖/合并 `MapConfig` 的配置 |
86
86
 
87
- 事件:`@onload`,参数 `{ map, center }`(`map` 为 Cesium `Viewer` 实例)。
87
+ 事件:`@onload`,参数 `{ map, center, mapId, toolbar }`(`map` 为 Cesium `Viewer` 实例;`toolbar` 可在加载后动态增删按钮)。
88
+
89
+ ### 各项目自定义工具栏
90
+
91
+ **方式 1:组件 props(推荐)**
92
+
93
+ ```vue
94
+ <CesiumMap
95
+ map-id="default"
96
+ :show-default-toolbar="true"
97
+ :extra-toolbar-buttons="myExtraButtons"
98
+ />
99
+ ```
100
+
101
+ ```js
102
+ import { useEventBus } from 'huweili-cesium/utils/useEventBus'
103
+
104
+ const { emit } = useEventBus()
105
+
106
+ const myExtraButtons = [
107
+ {
108
+ title: '导出',
109
+ text: '导',
110
+ onClick: () => emit('exportMap', {}),
111
+ },
112
+ ]
113
+ ```
114
+
115
+ 完全不用内置默认按钮、自己定义一整组:
116
+
117
+ ```vue
118
+ <CesiumMap :toolbar-buttons="myToolbarButtons" />
119
+ ```
120
+
121
+ **方式 2:`@onload` 里动态添加**
122
+
123
+ ```js
124
+ function onMapLoad({ map, toolbar }) {
125
+ const btn = toolbar.addToolbarButton({
126
+ title: '测距',
127
+ iconSrc: '/images/ruler.svg',
128
+ onClick: (viewer) => { /* ... */ },
129
+ })
130
+ }
131
+ ```
132
+
133
+ **方式 3:不用 `CesiumMap`,自行拿 npm JS API**
134
+
135
+ ```js
136
+ import { createCustomToolbarButtons } from 'huweili-cesium/customToolbarButtons'
137
+ import { useMapStore } from 'huweili-cesium/stores/mapStore'
138
+
139
+ const mapStore = useMapStore()
140
+ const { addToolbarButtons } = createCustomToolbarButtons()
141
+ const viewer = mapStore.getMap('default') // 以你项目 mapStore API 为准
142
+
143
+ addToolbarButtons(viewer, [
144
+ { title: '自定义', text: 'A', onClick: () => {} },
145
+ ])
146
+ ```
147
+
148
+ 内置默认按钮里的图标路径使用宿主项目的 `import.meta.env.BASE_URL`(如 `/images/new/tj.svg`),请在宿主 `public/images/new/` 放置对应资源;业务按钮(如「机型统计」)通过 `useEventBus` 发事件,宿主需自行 `on('toggleBarChartControl', ...)` 监听。
88
149
 
89
150
  ## 可选业务钩子
90
151
 
package/index.vue CHANGED
@@ -22,7 +22,27 @@ import { createCustomToolbarButtons } from './js/customToolbarButtons.js'
22
22
 
23
23
  const { merge } = objectUtils()
24
24
  const { mouseController, applyOrthographicFrustum, restorePerspectiveFrustum } = basicConfig()
25
- const { addToolbarButtons, removeToolbarButtons, createDefaultToolbarButtons } = createCustomToolbarButtons()
25
+ const {
26
+ addToolbarButton,
27
+ addToolbarButtons,
28
+ removeToolbarButtons,
29
+ createDefaultToolbarButtons,
30
+ } = createCustomToolbarButtons()
31
+
32
+ /** 根据 props 组装最终要渲染的工具栏按钮配置 */
33
+ function resolveToolbarButtonConfigs() {
34
+ if (props.toolbarButtons?.length) {
35
+ return props.toolbarButtons
36
+ }
37
+ const configs = []
38
+ if (props.showDefaultToolbar) {
39
+ configs.push(...createDefaultToolbarButtons({ mapId: props.mapId }))
40
+ }
41
+ if (props.extraToolbarButtons?.length) {
42
+ configs.push(...props.extraToolbarButtons)
43
+ }
44
+ return configs
45
+ }
26
46
 
27
47
  // 获取store实例,保持响应性
28
48
  const mapStore = useMapStore()
@@ -39,6 +59,24 @@ const props = defineProps({
39
59
  mapId: {
40
60
  type: String,
41
61
  default: 'default'
62
+ },
63
+ /** 是否挂载 npm 包内置的默认工具栏按钮 */
64
+ showDefaultToolbar: {
65
+ type: Boolean,
66
+ default: true
67
+ },
68
+ /**
69
+ * 完全自定义工具栏按钮配置(传入后不再使用内置默认按钮)
70
+ * 项格式同 createCustomToolbarButtons 的 addToolbarButton options
71
+ */
72
+ toolbarButtons: {
73
+ type: Array,
74
+ default: undefined
75
+ },
76
+ /** 在默认按钮之后追加的额外按钮(各项目可在此扩展) */
77
+ extraToolbarButtons: {
78
+ type: Array,
79
+ default: () => []
42
80
  }
43
81
  })
44
82
 
@@ -199,18 +237,25 @@ const initCesium = async () => {
199
237
  // 初始化半球区域
200
238
  const center = mapOptions.scene.center || {}
201
239
 
202
- // 在主页按钮附近添加自定义按钮
203
- customButtons = addToolbarButtons(map, createDefaultToolbarButtons({
204
- mapId: props.mapId
205
- }))
240
+ // 在主页按钮附近添加工具栏按钮(默认 + 项目扩展,或完全自定义)
241
+ const toolbarConfigs = resolveToolbarButtonConfigs()
242
+ if (toolbarConfigs.length) {
243
+ customButtons = addToolbarButtons(map, toolbarConfigs)
244
+ }
206
245
 
207
246
  // 设置地图加载状态为已加载
208
247
  mapStore.setMapLoadSta(MapLoadStatus.LOADED, props.mapId)
209
248
 
210
249
  console.log(`Cesium 地图加载成功,mapId: ${props.mapId}`)
211
250
  emit("onload", {
212
- map: map,
213
- center: center
251
+ map,
252
+ center,
253
+ mapId: props.mapId,
254
+ toolbar: {
255
+ addToolbarButton: (opt) => addToolbarButton(map, opt),
256
+ addToolbarButtons: (opts) => addToolbarButtons(map, opts),
257
+ removeToolbarButtons,
258
+ },
214
259
  })
215
260
 
216
261
  // 添加持续渲染循环,避免浏览器进入节能模式
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "huweili-cesium",
3
- "version": "1.1.2",
3
+ "version": "1.1.3",
4
4
  "description": "基于 Cesium 的地图工具库(无人机态势、轨迹、围栏、工具栏等)",
5
5
  "type": "module",
6
6
  "main": "./index.js",