-
Notifications
You must be signed in to change notification settings - Fork 667
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
970eac9
commit 1540159
Showing
3 changed files
with
21 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,46 @@ | ||
// @flow | ||
|
||
import Vue from 'vue' | ||
import { compileToFunctions } from 'vue-template-compiler' | ||
|
||
function startsWithTag (str: SlotValue): boolean { | ||
return typeof str === 'string' && str.trim()[0] === '<' | ||
} | ||
|
||
function createVNodesForSlot ( | ||
h: Function, | ||
slotValue: SlotValue, | ||
name: string, | ||
_Vue: any | ||
vm: Component | ||
): VNode | string { | ||
if (typeof slotValue === 'string' && !startsWithTag(slotValue)) { | ||
return slotValue | ||
} | ||
|
||
const el = | ||
typeof slotValue === 'string' ? compileToFunctions(slotValue) : slotValue | ||
|
||
let vnode = h(el) | ||
let vnode | ||
if (typeof slotValue === 'string') { | ||
const vue = new Vue() | ||
const _vue = new _Vue() | ||
for (const key in _vue._renderProxy) { | ||
if (!(vue._renderProxy[key])) { | ||
vue._renderProxy[key] = _vue._renderProxy[key] | ||
} | ||
} | ||
try { | ||
// $FlowIgnore | ||
vnode = el.render.call(vue._renderProxy, h) | ||
vnode = h(vnode.tag, vnode.data || {}, vnode.children) | ||
} catch (e) { | ||
} | ||
const el = compileToFunctions(`<div>${slotValue}{{ }}</div>`) | ||
const _staticRenderFns = vm._renderProxy.$options.staticRenderFns | ||
vm._renderProxy.$options.staticRenderFns = el.staticRenderFns | ||
vnode = el.render.call(vm._renderProxy, h) | ||
vm._renderProxy.$options.staticRenderFns = _staticRenderFns | ||
vnode = vnode.children[0] | ||
} else { | ||
vnode = h(slotValue) | ||
} | ||
if (vnode.data) { | ||
vnode.data.slot = name | ||
} else { | ||
vnode.data = { slot: name } | ||
} | ||
|
||
vnode.data.slot = name | ||
return vnode | ||
} | ||
|
||
export function createSlotVNodes ( | ||
h: Function, | ||
slots: SlotsObject, | ||
_Vue: any | ||
vm: Component | ||
): Array<VNode | string> { | ||
return Object.keys(slots).reduce((acc, key) => { | ||
const content = slots[key] | ||
if (Array.isArray(content)) { | ||
const nodes = content.map( | ||
slotDef => createVNodesForSlot(h, slotDef, key, _Vue) | ||
slotDef => createVNodesForSlot(h, slotDef, key, vm) | ||
) | ||
return acc.concat(nodes) | ||
} | ||
|
||
return acc.concat(createVNodesForSlot(h, content, key, _Vue)) | ||
return acc.concat(createVNodesForSlot(h, content, key, vm)) | ||
}, []) | ||
} |