Skip to content

Commit

Permalink
refactor: improve useVueCesium function
Browse files Browse the repository at this point in the history
  • Loading branch information
zouyaoji committed Aug 12, 2022
1 parent 9906aa8 commit 6eeec48
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
9 changes: 6 additions & 3 deletions packages/components/viewer/src/useViewer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -881,12 +881,16 @@ export default function (props: VcViewerProps, ctx, vcInstance: VcComponentInter
eventsState.registerEvents(false)
}

const { removeCesiumScript } = props

viewer._vcPickScreenSpaceEventHandler && viewer._vcPickScreenSpaceEventHandler.destroy()
viewer._vcViewerScreenSpaceEventHandler && viewer._vcViewerScreenSpaceEventHandler.destroy()
viewer._vcPickScreenSpaceEventHandler = undefined!
viewer._vcViewerScreenSpaceEventHandler = undefined!

revokeExtensions(viewer)
removeCesiumScript && revokeExtensions(viewer)

delete vcInstance.appContext.config.globalProperties.$VueCesium[viewer.container.id]

if (globalThis.XE) {
earth && earth.destroy()
Expand All @@ -898,9 +902,8 @@ export default function (props: VcViewerProps, ctx, vcInstance: VcComponentInter
viewer && viewer.destroy()
}

vcInstance.viewer = undefined!
vcInstance.viewer = undefined
vcInstance.mounted = false
const { removeCesiumScript } = props
if (removeCesiumScript && globalThis.Cesium) {
const scripts = document.getElementsByTagName('script')
const removeScripts: Array<HTMLScriptElement | HTMLLinkElement> = []
Expand Down
20 changes: 16 additions & 4 deletions packages/composables/use-vue-cesium/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/*
* @Author: zouyaoji@https://github.com/zouyaoji
* @Date: 2021-04-06 09:21:02
* @LastEditTime: 2022-08-12 11:50:40
* @LastEditTime: 2022-08-12 16:24:13
* @LastEditors: zouyaoji
* @Description:
* @FilePath: \10_vue-cesium\packages\composables\use-vue-cesium\index.ts
* @FilePath: \vue-cesium@next\packages\composables\use-vue-cesium\index.ts
*/
import { getCurrentInstance, inject } from 'vue'
import { VcViewerProvider } from '@vue-cesium/utils/types'
Expand All @@ -13,9 +13,21 @@ import { vcKey } from '@vue-cesium/utils/config'

export default function useVueCesium(containerId?: string): VcViewerProvider {
const instance = getCurrentInstance()
const logger = useLog(undefined)
const provides = instance.parent == null ? instance.vnode.appContext && instance.vnode.appContext.provides : (instance.parent as any).provides
if ((!provides || !(vcKey in provides)) && !containerId) {
containerId = 'cesiumContainer'
}
const logger = useLog()
if (instance) {
return containerId ? (instance.appContext.config.globalProperties.$VueCesium[containerId] as VcViewerProvider) : inject<VcViewerProvider>(vcKey)
if (containerId) {
const $vc = instance.appContext.config.globalProperties.$VueCesium
if (!$vc) {
logger.warn('Failed to get $vc, reason: containerId does not exist.')
}
return $vc
} else {
return inject<VcViewerProvider>(vcKey)
}
} else {
logger.warn('VueCesium useVueCesium() can only be used inside setup().')
}
Expand Down

0 comments on commit 6eeec48

Please sign in to comment.