huweili-cesium 1.2.44 → 1.2.46

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 +133 -51
  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
@@ -852,11 +918,57 @@ export function basicConfig() {
852
918
  btn.classList.toggle('is-collapsed', !isToolbarExpanded)
853
919
  }
854
920
 
921
+ const startToolbarDrag = (event, btn) => {
922
+ const toolbar = btn?.parentElement
923
+ if (!toolbar) return
924
+
925
+ const startX = event.clientX
926
+ const startY = event.clientY
927
+ const rect = toolbar.getBoundingClientRect()
928
+ const startLeft = rect.left
929
+ const startTop = rect.top
930
+ let hasMoved = false
931
+
932
+ applyToolbarPosition(toolbar, startLeft, startTop)
933
+
934
+ const moveToolbar = (moveEvent) => {
935
+ const deltaX = moveEvent.clientX - startX
936
+ const deltaY = moveEvent.clientY - startY
937
+
938
+ if (Math.abs(deltaX) > 3 || Math.abs(deltaY) > 3) {
939
+ hasMoved = true
940
+ isToolbarDragging = true
941
+ }
942
+
943
+ applyToolbarPosition(toolbar, startLeft + deltaX, startTop + deltaY)
944
+ }
945
+
946
+ const stopDrag = () => {
947
+ window.removeEventListener('pointermove', moveToolbar)
948
+ window.removeEventListener('pointerup', stopDrag)
949
+ window.removeEventListener('pointercancel', stopDrag)
950
+
951
+ if (hasMoved) {
952
+ const currentRect = toolbar.getBoundingClientRect()
953
+ saveToolbarPosition(currentRect.left, currentRect.top)
954
+
955
+ window.setTimeout(() => {
956
+ isToolbarDragging = false
957
+ }, 0)
958
+ }
959
+ }
960
+
961
+ window.addEventListener('pointermove', moveToolbar)
962
+ window.addEventListener('pointerup', stopDrag)
963
+ window.addEventListener('pointercancel', stopDrag)
964
+ }
965
+
855
966
  const setupToolbarDrag = (btn) => {
856
967
  if (!btn || btn.dataset.toolbarDragReady === 'true') return
857
968
 
858
969
  btn.dataset.toolbarDragReady = 'true'
859
970
  btn.style.cursor = 'move'
971
+ restoreToolbarPosition(btn)
860
972
 
861
973
  btn.addEventListener('dblclick', (event) => {
862
974
  event.preventDefault()
@@ -866,63 +978,33 @@ export function basicConfig() {
866
978
  })
867
979
 
868
980
  btn.addEventListener('pointerdown', (event) => {
869
- const toolbar = btn.parentElement
870
- if (!toolbar) return
871
-
872
- const startX = event.clientX
873
- const startY = event.clientY
874
- const rect = toolbar.getBoundingClientRect()
875
- const startLeft = rect.left
876
- const startTop = rect.top
877
- let hasMoved = false
878
-
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'
886
-
887
- const moveToolbar = (moveEvent) => {
888
- const deltaX = moveEvent.clientX - startX
889
- const deltaY = moveEvent.clientY - startY
890
-
891
- if (Math.abs(deltaX) > 3 || Math.abs(deltaY) > 3) {
892
- hasMoved = true
893
- isToolbarDragging = true
894
- }
895
-
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`
903
- }
981
+ startToolbarDrag(event, btn)
982
+ })
983
+ }
904
984
 
905
- const stopDrag = () => {
906
- window.removeEventListener('pointermove', moveToolbar)
907
- window.removeEventListener('pointerup', stopDrag)
908
- window.removeEventListener('pointercancel', stopDrag)
985
+ const findExpandButton = () => {
986
+ return document.querySelector('[data-toolbar-expand-toggle="true"]')
987
+ || document.querySelector('[title="展开/折叠 工具栏"]')
988
+ || document.querySelector('[title="展开"]')
989
+ || document.querySelector('[title="收缩"]')
990
+ }
909
991
 
910
- if (hasMoved) {
911
- window.setTimeout(() => {
912
- isToolbarDragging = false
913
- }, 0)
914
- }
915
- }
992
+ const setupBySelector = () => {
993
+ const btn = findExpandButton()
994
+ if (btn) setupToolbarDrag(btn)
995
+ }
916
996
 
917
- window.addEventListener('pointermove', moveToolbar)
918
- window.addEventListener('pointerup', stopDrag)
919
- window.addEventListener('pointercancel', stopDrag)
920
- })
997
+ if (typeof window !== 'undefined' && typeof document !== 'undefined') {
998
+ window.requestAnimationFrame(setupBySelector)
999
+ window.setTimeout(setupBySelector, 300)
921
1000
  }
922
1001
 
923
1002
  return {
924
1003
  toggleExpand: (_viewer, btn) => {
925
- setupToolbarDrag(btn)
1004
+ if (btn) {
1005
+ btn.dataset.toolbarExpandToggle = 'true'
1006
+ setupToolbarDrag(btn)
1007
+ }
926
1008
  },
927
1009
  setupToolbarDrag,
928
1010
  applyToggleExpand,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "huweili-cesium",
3
- "version": "1.2.44",
3
+ "version": "1.2.46",
4
4
  "description": "基于 Cesium 的地图工具库(无人机态势、轨迹、围栏、工具栏等)",
5
5
  "type": "module",
6
6
  "main": "./index.js",