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
|
|
|
|
|
|
|
export function ssrRenderPortal(
|
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-28 03:45:50 +00:00
|
|
|
parentPush('<!--portal start-->')
|
|
|
|
|
|
|
|
let portalContent: SSRBufferItem
|
|
|
|
|
|
|
|
if (disabled) {
|
|
|
|
contentRenderFn(parentPush)
|
|
|
|
portalContent = `<!---->`
|
|
|
|
} else {
|
|
|
|
const { getBuffer, push } = createBuffer()
|
|
|
|
contentRenderFn(push)
|
|
|
|
push(`<!---->`) // portal end anchor
|
|
|
|
portalContent = getBuffer()
|
|
|
|
}
|
2020-02-26 19:59:53 +00:00
|
|
|
|
|
|
|
const context = parentComponent.appContext.provides[
|
|
|
|
ssrContextKey as any
|
|
|
|
] as SSRContext
|
|
|
|
const portalBuffers =
|
|
|
|
context.__portalBuffers || (context.__portalBuffers = {})
|
2020-03-28 00:49:01 +00:00
|
|
|
if (portalBuffers[target]) {
|
2020-03-28 03:45:50 +00:00
|
|
|
portalBuffers[target].push(portalContent)
|
2020-03-28 00:49:01 +00:00
|
|
|
} else {
|
2020-03-28 03:45:50 +00:00
|
|
|
portalBuffers[target] = [portalContent]
|
2020-03-28 00:49:01 +00:00
|
|
|
}
|
2020-03-28 03:45:50 +00:00
|
|
|
|
|
|
|
parentPush('<!--portal end-->')
|
2020-02-26 19:59:53 +00:00
|
|
|
}
|