/
app.ts
executable file
·138 lines (121 loc) · 3.85 KB
/
app.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import { createHeadManager, Page, PageProps, router } from '@inertiajs/core'
import {
computed,
DefineComponent,
defineComponent,
h,
markRaw,
Plugin,
PropType,
reactive,
ref,
shallowRef,
} from 'vue'
import remember from './remember'
import { VuePageHandlerArgs } from './types'
import useForm from './useForm'
export interface InertiaAppProps {
initialPage: Page
initialComponent?: object
resolveComponent?: (name: string) => DefineComponent | Promise<DefineComponent>
titleCallback?: (title: string) => string
onHeadUpdate?: (elements: string[]) => void
}
export type InertiaApp = DefineComponent<InertiaAppProps>
const component = ref(null)
const page = ref<Page<any>>(null)
const layout = shallowRef(null)
const key = ref(null)
let headManager = null
const App: InertiaApp = defineComponent({
name: 'Inertia',
props: {
initialPage: {
type: Object as PropType<Page>,
required: true,
},
initialComponent: {
type: Object,
required: false,
},
resolveComponent: {
type: Function as PropType<(name: string) => DefineComponent | Promise<DefineComponent>>,
required: false,
},
titleCallback: {
type: Function as PropType<(title: string) => string>,
required: false,
default: (title) => title,
},
onHeadUpdate: {
type: Function as PropType<(elements: string[]) => void>,
required: false,
default: () => () => {},
},
},
setup({ initialPage, initialComponent, resolveComponent, titleCallback, onHeadUpdate }) {
component.value = initialComponent ? markRaw(initialComponent) : null
page.value = initialPage
key.value = null
const isServer = typeof window === 'undefined'
headManager = createHeadManager(isServer, titleCallback, onHeadUpdate)
if (!isServer) {
router.init({
initialPage,
resolveComponent,
swapComponent: async (args: VuePageHandlerArgs) => {
component.value = markRaw(args.component)
page.value = args.page
key.value = args.preserveState ? key.value : Date.now()
},
})
router.on('navigate', () => headManager.forceUpdate())
}
return () => {
if (component.value) {
component.value.inheritAttrs = !!component.value.inheritAttrs
const child = h(component.value, {
...page.value.props,
key: key.value,
})
if (layout.value) {
component.value.layout = layout.value
layout.value = null
}
if (component.value.layout) {
if (typeof component.value.layout === 'function') {
return component.value.layout(h, child)
}
return (Array.isArray(component.value.layout) ? component.value.layout : [component.value.layout])
.concat(child)
.reverse()
.reduce((child, layout) => {
layout.inheritAttrs = !!layout.inheritAttrs
return h(layout, { ...page.value.props }, () => child)
})
}
return child
}
}
},
})
export default App
export const plugin: Plugin = {
install(app) {
router.form = useForm
Object.defineProperty(app.config.globalProperties, '$inertia', { get: () => router })
Object.defineProperty(app.config.globalProperties, '$page', { get: () => page.value })
Object.defineProperty(app.config.globalProperties, '$headManager', { get: () => headManager })
app.mixin(remember)
},
}
export function usePage<SharedProps extends PageProps>(): Page<SharedProps> {
return reactive({
props: computed(() => page.value?.props),
url: computed(() => page.value?.url),
component: computed(() => page.value?.component),
version: computed(() => page.value?.version),
scrollRegions: computed(() => page.value?.scrollRegions),
rememberedState: computed(() => page.value?.rememberedState),
})
}