Skip to content

Commit

Permalink
feat(vue-shape): vue-shape's teleport is only registered once
Browse files Browse the repository at this point in the history
  • Loading branch information
wangcch committed Nov 8, 2024
1 parent e725973 commit 9bde3ed
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions packages/x6-vue-shape/src/teleport.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
import { defineComponent, h, reactive, isVue3, Teleport, markRaw, Fragment } from 'vue-demi'
import {
defineComponent,
h,
reactive,
isVue3,
Teleport,
markRaw,
Fragment,
onBeforeUnmount,
} from 'vue-demi'
import { Graph } from '@antv/x6'
import { VueShape } from './node'

Expand All @@ -15,7 +24,10 @@ export function connect(
if (active) {
items[id] = markRaw(
defineComponent({
render: () => h(Teleport, { to: container } as any, [h(component, { node, graph })]),
render: () =>
h(Teleport, { to: container } as any, [
h(component, { node, graph }),
]),
provide: () => ({
getNode: () => node,
getGraph: () => graph,
Expand All @@ -35,14 +47,25 @@ export function isActive() {
return active
}

let itemComponets: any = null

export function getTeleport(): any {
if (!isVue3) {
throw new Error('teleport is only available in Vue3')
}

if (itemComponets && active) {
return null
}

active = true

return defineComponent({
itemComponets = defineComponent({
setup() {
onBeforeUnmount(() => {
itemComponets = null
})

return () =>
h(
Fragment,
Expand All @@ -51,4 +74,5 @@ export function getTeleport(): any {
)
},
})
return itemComponets
}

0 comments on commit 9bde3ed

Please sign in to comment.