-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathcustom-content-type.ts
76 lines (63 loc) · 1.79 KB
/
custom-content-type.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import Vue, { VNode } from 'vue'
import { NormalizedScopedSlot } from 'vue/types/vnode'
import { createPlugin, PluginDef } from '@fullcalendar/core'
/*
wrap it in an object with a `vue` key, which the custom content-type handler system will look for
*/
export function wrapVDomGenerator(vDomGenerator: NormalizedScopedSlot) {
return function(props: any) {
return { vue: vDomGenerator(props) }
}
}
export function createVueContentTypePlugin(parent: Vue): PluginDef {
return createPlugin({
contentTypeHandlers: {
vue: () => buildVDomHandler(parent), // looks for the `vue` key
}
});
}
function buildVDomHandler(parent: Vue) {
let currentEl: HTMLElement
let v: ReturnType<typeof initVue> // the Vue instance
function render(el: HTMLElement, vDomContent: VNode[]) { // the handler
if (currentEl !== el) {
if (currentEl && v) { // if changing elements, recreate the vue
v.$destroy()
}
currentEl = el
}
if (!v) {
v = initVue(vDomContent, parent)
// vue's mount method *replaces* the given element. create an artificial inner el
let innerEl = document.createElement('span')
el.appendChild(innerEl)
v.$mount(innerEl)
} else {
v.content = vDomContent
}
}
function destroy() {
if (v) { // needed?
v.$destroy()
}
}
return { render, destroy }
}
function initVue(initialContent: VNode[], parent: Vue) {
return new Vue({
parent,
data: {
content: initialContent,
},
render(h) {
let { content } = this
// the slot result can be an array, but the returned value of a vue component's
// render method must be a single node.
if (content.length === 1) {
return content[0]
} else {
return h('span', {}, content)
}
}
})
}