2020-02-26 19:59:53 +00:00
|
|
|
import { ComponentInternalInstance, ssrContextKey } from 'vue'
|
2020-03-28 03:45:50 +00:00
|
|
|
import {
|
|
|
|
SSRContext,
|
|
|
|
createBuffer,
|
|
|
|
PushFn,
|
|
|
|
SSRBufferItem
|
|
|
|
} from '../renderToString'
|
2020-02-26 19:59:53 +00:00
|
|
|
|
2020-03-31 14:52:42 +00:00
|
|
|
export function ssrRenderTeleport(
|
2020-03-28 00:49:01 +00:00
|
|
|
parentPush: PushFn,
|
2020-02-26 19:59:53 +00:00
|
|
|
contentRenderFn: (push: PushFn) => void,
|
|
|
|
target: string,
|
2020-03-28 03:45:50 +00:00
|
|
|
disabled: boolean,
|
2020-02-26 19:59:53 +00:00
|
|
|
parentComponent: ComponentInternalInstance
|
|
|
|
) {
|
2020-03-31 14:52:42 +00:00
|
|
|
parentPush('<!--teleport start-->')
|
2020-03-28 03:45:50 +00:00
|
|
|
|
2020-03-31 14:52:42 +00:00
|
|
|
let teleportContent: SSRBufferItem
|
2020-03-28 03:45:50 +00:00
|
|
|
|
|
|
|
if (disabled) {
|
|
|
|
contentRenderFn(parentPush)
|
2020-03-31 14:52:42 +00:00
|
|
|
teleportContent = `<!---->`
|
2020-03-28 03:45:50 +00:00
|
|
|
} else {
|
|
|
|
const { getBuffer, push } = createBuffer()
|
|
|
|
contentRenderFn(push)
|
2020-03-31 14:52:42 +00:00
|
|
|
push(`<!---->`) // teleport end anchor
|
|
|
|
teleportContent = getBuffer()
|
2020-03-28 03:45:50 +00:00
|
|
|
}
|
2020-02-26 19:59:53 +00:00
|
|
|
|
|
|
|
const context = parentComponent.appContext.provides[
|
|
|
|
ssrContextKey as any
|
|
|
|
] as SSRContext
|
2020-03-31 14:52:42 +00:00
|
|
|
const teleportBuffers =
|
|
|
|
context.__teleportBuffers || (context.__teleportBuffers = {})
|
|
|
|
if (teleportBuffers[target]) {
|
|
|
|
teleportBuffers[target].push(teleportContent)
|
2020-03-28 00:49:01 +00:00
|
|
|
} else {
|
2020-03-31 14:52:42 +00:00
|
|
|
teleportBuffers[target] = [teleportContent]
|
2020-03-28 00:49:01 +00:00
|
|
|
}
|
2020-03-28 03:45:50 +00:00
|
|
|
|
2020-03-31 14:52:42 +00:00
|
|
|
parentPush('<!--teleport end-->')
|
2020-02-26 19:59:53 +00:00
|
|
|
}
|