huweili-cesium 1.2.46 → 1.2.48

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 +43 -19
  2. package/package.json +1 -1
package/js/basis.js CHANGED
@@ -834,6 +834,8 @@ export function basicConfig() {
834
834
  */
835
835
  const createToolbarExpandController = ({
836
836
  storageKey = 'huweili-cesium-toolbar-position',
837
+ toggleIconSrc = '',
838
+ autoRestore = true,
837
839
  } = {}) => {
838
840
  let isToolbarExpanded = true
839
841
  let isToolbarDragging = false
@@ -874,10 +876,17 @@ export function basicConfig() {
874
876
  }
875
877
 
876
878
  const applyToolbarPosition = (toolbar, left, top) => {
877
- if (!toolbar) return
879
+ if (!toolbar) return false
880
+
881
+ const toolbarWidth = toolbar.offsetWidth
882
+ const toolbarHeight = toolbar.offsetHeight
883
+ const viewportWidth = window.innerWidth
884
+ const viewportHeight = window.innerHeight
885
+
886
+ if (!toolbarWidth || !toolbarHeight || !viewportWidth || !viewportHeight) return false
878
887
 
879
- const maxLeft = window.innerWidth - toolbar.offsetWidth
880
- const maxTop = window.innerHeight - toolbar.offsetHeight
888
+ const maxLeft = viewportWidth - toolbarWidth
889
+ const maxTop = viewportHeight - toolbarHeight
881
890
  const nextLeft = Math.min(Math.max(left, 0), Math.max(maxLeft, 0))
882
891
  const nextTop = Math.min(Math.max(top, 0), Math.max(maxTop, 0))
883
892
 
@@ -888,6 +897,7 @@ export function basicConfig() {
888
897
  toolbar.style.bottom = 'auto'
889
898
  toolbar.style.zIndex = toolbar.style.zIndex || '9999'
890
899
  toolbar.style.margin = '0'
900
+ return true
891
901
  }
892
902
 
893
903
  const restoreToolbarPosition = (btn) => {
@@ -898,7 +908,12 @@ export function basicConfig() {
898
908
  if (!position) return
899
909
 
900
910
  window.requestAnimationFrame(() => {
901
- applyToolbarPosition(toolbar, position.left, position.top)
911
+ const restored = applyToolbarPosition(toolbar, position.left, position.top)
912
+ if (!restored) {
913
+ window.setTimeout(() => {
914
+ applyToolbarPosition(toolbar, position.left, position.top)
915
+ }, 100)
916
+ }
902
917
  })
903
918
  }
904
919
 
@@ -982,32 +997,41 @@ export function basicConfig() {
982
997
  })
983
998
  }
984
999
 
985
- const findExpandButton = () => {
986
- return document.querySelector('[data-toolbar-expand-toggle="true"]')
987
- || document.querySelector('[title="展开/折叠 工具栏"]')
988
- || document.querySelector('[title="展开"]')
989
- || document.querySelector('[title="收缩"]')
1000
+ const findExpandButtonByIcon = () => {
1001
+ if (!toggleIconSrc || typeof document === 'undefined') return null
1002
+
1003
+ const images = Array.from(document.querySelectorAll('img'))
1004
+ const icon = images.find((img) => img.src?.includes(toggleIconSrc) || img.getAttribute('src')?.includes(toggleIconSrc))
1005
+ if (!icon) return null
1006
+
1007
+ return icon.closest('button') || icon.parentElement
990
1008
  }
991
1009
 
992
- const setupBySelector = () => {
993
- const btn = findExpandButton()
994
- if (btn) setupToolbarDrag(btn)
1010
+ const autoRestoreToolbarPosition = (retryCount = 0) => {
1011
+ if (!autoRestore || typeof window === 'undefined') return
1012
+
1013
+ const btn = findExpandButtonByIcon()
1014
+ if (btn) {
1015
+ setupToolbarDrag(btn)
1016
+ return
1017
+ }
1018
+
1019
+ if (retryCount < 20) {
1020
+ window.setTimeout(() => autoRestoreToolbarPosition(retryCount + 1), 100)
1021
+ }
995
1022
  }
996
1023
 
997
- if (typeof window !== 'undefined' && typeof document !== 'undefined') {
998
- window.requestAnimationFrame(setupBySelector)
999
- window.setTimeout(setupBySelector, 300)
1024
+ if (autoRestore && typeof window !== 'undefined') {
1025
+ window.requestAnimationFrame(() => autoRestoreToolbarPosition())
1000
1026
  }
1001
1027
 
1002
1028
  return {
1003
1029
  toggleExpand: (_viewer, btn) => {
1004
- if (btn) {
1005
- btn.dataset.toolbarExpandToggle = 'true'
1006
- setupToolbarDrag(btn)
1007
- }
1030
+ if (btn) setupToolbarDrag(btn)
1008
1031
  },
1009
1032
  setupToolbarDrag,
1010
1033
  applyToggleExpand,
1034
+ restoreToolbarPosition,
1011
1035
  }
1012
1036
  }
1013
1037
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "huweili-cesium",
3
- "version": "1.2.46",
3
+ "version": "1.2.48",
4
4
  "description": "基于 Cesium 的地图工具库(无人机态势、轨迹、围栏、工具栏等)",
5
5
  "type": "module",
6
6
  "main": "./index.js",