huweili-cesium 1.2.44 → 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 +73 -15
  2. package/package.json +1 -1
package/js/basis.js CHANGED
@@ -832,10 +832,76 @@ export function basicConfig() {
832
832
  * - 双击展开按钮:展开/折叠同级工具栏按钮
833
833
  * - 拖拽展开按钮:移动整个工具栏按钮组
834
834
  */
835
- const createToolbarExpandController = () => {
835
+ const createToolbarExpandController = ({
836
+ storageKey = 'huweili-cesium-toolbar-position',
837
+ } = {}) => {
836
838
  let isToolbarExpanded = true
837
839
  let isToolbarDragging = false
838
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
+
839
905
  const applyToggleExpand = (btn) => {
840
906
  const toolbar = btn?.parentElement
841
907
  if (!toolbar) return
@@ -857,6 +923,7 @@ export function basicConfig() {
857
923
 
858
924
  btn.dataset.toolbarDragReady = 'true'
859
925
  btn.style.cursor = 'move'
926
+ restoreToolbarPosition(btn)
860
927
 
861
928
  btn.addEventListener('dblclick', (event) => {
862
929
  event.preventDefault()
@@ -876,13 +943,7 @@ export function basicConfig() {
876
943
  const startTop = rect.top
877
944
  let hasMoved = false
878
945
 
879
- toolbar.style.position = 'fixed'
880
- toolbar.style.left = `${startLeft}px`
881
- toolbar.style.top = `${startTop}px`
882
- toolbar.style.right = 'auto'
883
- toolbar.style.bottom = 'auto'
884
- toolbar.style.zIndex = toolbar.style.zIndex || '9999'
885
- toolbar.style.margin = '0'
946
+ applyToolbarPosition(toolbar, startLeft, startTop)
886
947
 
887
948
  const moveToolbar = (moveEvent) => {
888
949
  const deltaX = moveEvent.clientX - startX
@@ -893,13 +954,7 @@ export function basicConfig() {
893
954
  isToolbarDragging = true
894
955
  }
895
956
 
896
- const maxLeft = window.innerWidth - toolbar.offsetWidth
897
- const maxTop = window.innerHeight - toolbar.offsetHeight
898
- const nextLeft = Math.min(Math.max(startLeft + deltaX, 0), Math.max(maxLeft, 0))
899
- const nextTop = Math.min(Math.max(startTop + deltaY, 0), Math.max(maxTop, 0))
900
-
901
- toolbar.style.left = `${nextLeft}px`
902
- toolbar.style.top = `${nextTop}px`
957
+ applyToolbarPosition(toolbar, startLeft + deltaX, startTop + deltaY)
903
958
  }
904
959
 
905
960
  const stopDrag = () => {
@@ -908,6 +963,9 @@ export function basicConfig() {
908
963
  window.removeEventListener('pointercancel', stopDrag)
909
964
 
910
965
  if (hasMoved) {
966
+ const currentRect = toolbar.getBoundingClientRect()
967
+ saveToolbarPosition(currentRect.left, currentRect.top)
968
+
911
969
  window.setTimeout(() => {
912
970
  isToolbarDragging = false
913
971
  }, 0)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "huweili-cesium",
3
- "version": "1.2.44",
3
+ "version": "1.2.45",
4
4
  "description": "基于 Cesium 的地图工具库(无人机态势、轨迹、围栏、工具栏等)",
5
5
  "type": "module",
6
6
  "main": "./index.js",