huweili-cesium 1.2.43 → 1.2.45

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 (2) hide show
  1. package/js/basis.js +179 -15
  2. package/package.json +1 -1
package/js/basis.js CHANGED
@@ -15,6 +15,13 @@ import {
15
15
  teardownOrthographicWheelZoom,
16
16
  restorePerspectiveControllerState,
17
17
  } from './utils/cameraInteraction.js'
18
+ import { getMapCenterPosition } from './utils/getMapCenterPosition.js'
19
+ import { resetCompassDirection } from './toolbar/compass.js'
20
+ import { createBasemapSwitcher } from './utils/basemapSwitcher.js'
21
+ import { createZoomController } from './toolbar/zoomController.js'
22
+ import { useEventBus } from './utils/useEventBus.js'
23
+ import { applyOrthographicZoomFactor } from './utils/orthographicCameraZoom.js'
24
+ import { measureDistance } from './measureDistance.js'
18
25
 
19
26
  export function basicConfig() {
20
27
 
@@ -721,16 +728,180 @@ export function basicConfig() {
721
728
  }
722
729
  }
723
730
 
731
+ /**
732
+ * 创建默认首页地图工具栏按钮
733
+ * @param {Object} options
734
+ * @param {string} options.mapId 地图实例 ID
735
+ * @param {string} [options.baseUrl='/'] 静态资源基础路径
736
+ * @param {number} [options.distance2D=8000] 2D 视角距离
737
+ * @param {number} [options.distance3D=15000] 3D 视角距离
738
+ * @returns {Object[]} 默认工具栏按钮配置
739
+ */
740
+ const createDefaultHomeToolbarButtons = ({
741
+ mapId,
742
+ baseUrl = '/',
743
+ distance2D = 8000,
744
+ distance3D = 15000,
745
+ } = {}) => {
746
+ const { zoomIn: zoomInDefault, zoomOut: zoomOutDefault } = createZoomController()
747
+ const { toggleBasemapPickerPanel } = createBasemapSwitcher()
748
+ const { startMeasure } = measureDistance()
749
+ const { emit } = useEventBus()
750
+
751
+ const zoomIn = (factor = 0.8) => {
752
+ const map = mapStore.getMap(mapId)
753
+ if (map && applyOrthographicZoomFactor(map, mapId, factor)) return
754
+ zoomInDefault(mapId, factor)
755
+ }
756
+
757
+ const zoomOut = (factor = 1.25) => {
758
+ const map = mapStore.getMap(mapId)
759
+ if (map && applyOrthographicZoomFactor(map, mapId, factor)) return
760
+ zoomOutDefault(mapId, factor)
761
+ }
762
+
763
+ return [
764
+ {
765
+ index: 0,
766
+ title: '指北针',
767
+ text: 'N',
768
+ isCompass: true,
769
+ onClick: () => resetCompassDirection(),
770
+ },
771
+ {
772
+ index: 1,
773
+ title: '测距',
774
+ iconSrc: `${baseUrl}/images/new/ceju.svg`,
775
+ onClick: () => {
776
+ const measureId = `home_measure_${Date.now()}`
777
+ startMeasure({
778
+ id: measureId,
779
+ mapId,
780
+ color: '#00ffff',
781
+ width: 3,
782
+ })
783
+ },
784
+ },
785
+ {
786
+ index: 2,
787
+ title: '返回初始点位',
788
+ iconSrc: `${baseUrl}/images/new/home.svg`,
789
+ onClick: (v) => {
790
+ const { lng, lat } = getMapCenterPosition({ mapInstanceId: mapId })
791
+ setCesiumCenter({ lng, lat, map: v, mapId })
792
+ },
793
+ },
794
+ {
795
+ index: 3,
796
+ title: '2D/3D视角切换',
797
+ text: '2D',
798
+ onClick: (_v, btn) => {
799
+ const result = toggleViewAtViewportCenter(mapId, {
800
+ distance2D,
801
+ distance3D,
802
+ })
803
+ if (result.success && btn) btn.innerText = result.currentMode
804
+ mapStore.setSceneMode(result.currentMode, mapId)
805
+ emit('map-scene-mode-changed', { mapId, sceneMode: result.currentMode })
806
+ },
807
+ },
808
+ {
809
+ index: 4,
810
+ title: '降低视角',
811
+ iconSrc: `${baseUrl}/images/new/jia.svg`,
812
+ onClick: () => zoomIn(),
813
+ },
814
+ {
815
+ index: 5,
816
+ title: '抬升视角',
817
+ iconSrc: `${baseUrl}/images/new/jian.svg`,
818
+ onClick: () => zoomOut(),
819
+ },
820
+ {
821
+ index: 6,
822
+ title: '切换底图',
823
+ iconSrc: `${baseUrl}/images/new/map.svg`,
824
+ onClick: (v, btn) => toggleBasemapPickerPanel(v, btn || null),
825
+ },
826
+ ]
827
+ }
828
+
724
829
  /**
725
830
  * 创建工具栏展开/折叠和拖拽控制器
726
831
  * - 单击展开按钮:初始化拖拽能力,不触发展开/折叠
727
832
  * - 双击展开按钮:展开/折叠同级工具栏按钮
728
833
  * - 拖拽展开按钮:移动整个工具栏按钮组
729
834
  */
730
- const createToolbarExpandController = () => {
835
+ const createToolbarExpandController = ({
836
+ storageKey = 'huweili-cesium-toolbar-position',
837
+ } = {}) => {
731
838
  let isToolbarExpanded = true
732
839
  let isToolbarDragging = false
733
840
 
841
+ const getStorage = () => {
842
+ try {
843
+ return window?.localStorage || null
844
+ } catch (_error) {
845
+ return null
846
+ }
847
+ }
848
+
849
+ const readToolbarPosition = () => {
850
+ const storage = getStorage()
851
+ if (!storage || !storageKey) return null
852
+
853
+ try {
854
+ const value = storage.getItem(storageKey)
855
+ if (!value) return null
856
+
857
+ const position = JSON.parse(value)
858
+ if (!Number.isFinite(position?.left) || !Number.isFinite(position?.top)) return null
859
+ return position
860
+ } catch (_error) {
861
+ return null
862
+ }
863
+ }
864
+
865
+ const saveToolbarPosition = (left, top) => {
866
+ const storage = getStorage()
867
+ if (!storage || !storageKey) return
868
+
869
+ try {
870
+ storage.setItem(storageKey, JSON.stringify({ left, top }))
871
+ } catch (_error) {
872
+ // ignore storage write error
873
+ }
874
+ }
875
+
876
+ const applyToolbarPosition = (toolbar, left, top) => {
877
+ if (!toolbar) return
878
+
879
+ const maxLeft = window.innerWidth - toolbar.offsetWidth
880
+ const maxTop = window.innerHeight - toolbar.offsetHeight
881
+ const nextLeft = Math.min(Math.max(left, 0), Math.max(maxLeft, 0))
882
+ const nextTop = Math.min(Math.max(top, 0), Math.max(maxTop, 0))
883
+
884
+ toolbar.style.position = 'fixed'
885
+ toolbar.style.left = `${nextLeft}px`
886
+ toolbar.style.top = `${nextTop}px`
887
+ toolbar.style.right = 'auto'
888
+ toolbar.style.bottom = 'auto'
889
+ toolbar.style.zIndex = toolbar.style.zIndex || '9999'
890
+ toolbar.style.margin = '0'
891
+ }
892
+
893
+ const restoreToolbarPosition = (btn) => {
894
+ const toolbar = btn?.parentElement
895
+ if (!toolbar) return
896
+
897
+ const position = readToolbarPosition()
898
+ if (!position) return
899
+
900
+ window.requestAnimationFrame(() => {
901
+ applyToolbarPosition(toolbar, position.left, position.top)
902
+ })
903
+ }
904
+
734
905
  const applyToggleExpand = (btn) => {
735
906
  const toolbar = btn?.parentElement
736
907
  if (!toolbar) return
@@ -752,6 +923,7 @@ export function basicConfig() {
752
923
 
753
924
  btn.dataset.toolbarDragReady = 'true'
754
925
  btn.style.cursor = 'move'
926
+ restoreToolbarPosition(btn)
755
927
 
756
928
  btn.addEventListener('dblclick', (event) => {
757
929
  event.preventDefault()
@@ -771,13 +943,7 @@ export function basicConfig() {
771
943
  const startTop = rect.top
772
944
  let hasMoved = false
773
945
 
774
- toolbar.style.position = 'fixed'
775
- toolbar.style.left = `${startLeft}px`
776
- toolbar.style.top = `${startTop}px`
777
- toolbar.style.right = 'auto'
778
- toolbar.style.bottom = 'auto'
779
- toolbar.style.zIndex = toolbar.style.zIndex || '9999'
780
- toolbar.style.margin = '0'
946
+ applyToolbarPosition(toolbar, startLeft, startTop)
781
947
 
782
948
  const moveToolbar = (moveEvent) => {
783
949
  const deltaX = moveEvent.clientX - startX
@@ -788,13 +954,7 @@ export function basicConfig() {
788
954
  isToolbarDragging = true
789
955
  }
790
956
 
791
- const maxLeft = window.innerWidth - toolbar.offsetWidth
792
- const maxTop = window.innerHeight - toolbar.offsetHeight
793
- const nextLeft = Math.min(Math.max(startLeft + deltaX, 0), Math.max(maxLeft, 0))
794
- const nextTop = Math.min(Math.max(startTop + deltaY, 0), Math.max(maxTop, 0))
795
-
796
- toolbar.style.left = `${nextLeft}px`
797
- toolbar.style.top = `${nextTop}px`
957
+ applyToolbarPosition(toolbar, startLeft + deltaX, startTop + deltaY)
798
958
  }
799
959
 
800
960
  const stopDrag = () => {
@@ -803,6 +963,9 @@ export function basicConfig() {
803
963
  window.removeEventListener('pointercancel', stopDrag)
804
964
 
805
965
  if (hasMoved) {
966
+ const currentRect = toolbar.getBoundingClientRect()
967
+ saveToolbarPosition(currentRect.left, currentRect.top)
968
+
806
969
  window.setTimeout(() => {
807
970
  isToolbarDragging = false
808
971
  }, 0)
@@ -884,6 +1047,7 @@ export function basicConfig() {
884
1047
  applyOrthographicFrustum,
885
1048
  restorePerspectiveFrustum,
886
1049
  setInitialCameraView,
1050
+ createDefaultHomeToolbarButtons,
887
1051
  createToolbarExpandController,
888
1052
  syncCameraInteractionMode: (map, mapId, sceneMode) =>
889
1053
  syncCameraInteractionMode(map, mapId, sceneMode, orthoViewHelpers()),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "huweili-cesium",
3
- "version": "1.2.43",
3
+ "version": "1.2.45",
4
4
  "description": "基于 Cesium 的地图工具库(无人机态势、轨迹、围栏、工具栏等)",
5
5
  "type": "module",
6
6
  "main": "./index.js",