1
|
1 |
|
import type { App, Plugin } from 'vue'; |
2
|
1 |
|
import type { PageTitleOptions } from './types'; |
3
|
1 |
|
import { ref } from 'vue'; |
4
|
1 |
|
import { setPageTitle } from './page-title'; |
5
|
1 |
|
import { setupRouter } from './router'; |
6
|
1 |
|
import { PAGE_TITLE, SET_PAGE_TITLE } from './injection-keys'; |
7
|
1 |
|
import { pageTitleMixin } from './mixin'; |
8
|
1 |
|
|
9
|
1 |
|
export * from './composable'; |
10
|
1 |
|
export * from './types'; |
11
|
1 |
|
export { pageTitleMixin } from './mixin'; |
12
|
1 |
|
|
13
|
1 |
|
const pageTitle = (options: PageTitleOptions = {}): Plugin => { |
14
|
8 |
|
// title state |
15
|
8 |
|
const $title = ref<string>(''); |
16
|
8 |
|
|
17
|
8 |
|
const setTitle = (val: string): void => { |
18
|
11 |
|
setPageTitle(val, options); |
19
|
11 |
|
$title.value = val; |
20
|
11 |
|
}; |
21
|
8 |
|
|
22
|
8 |
|
const installedApps = new WeakSet<App>(); |
23
|
8 |
|
|
24
|
8 |
|
if (options.router != null) { |
25
|
1 |
|
setupRouter(options.router, setTitle); |
26
|
1 |
|
} |
27
|
1 |
|
|
28
|
8 |
|
const install = (app: App): void => { |
29
|
8 |
|
// prevent double install |
30
|
8 |
|
if (installedApps.has(app)) { |
31
|
|
|
return; |
32
|
|
|
} |
33
|
|
|
|
34
|
8 |
|
installedApps.add(app); |
35
|
8 |
|
|
36
|
8 |
|
if (options.mixin ?? false) { |
37
|
3 |
|
app.mixin(pageTitleMixin); |
38
|
3 |
|
} |
39
|
3 |
|
|
40
|
8 |
|
app.provide(PAGE_TITLE, $title); |
41
|
8 |
|
app.provide(SET_PAGE_TITLE, setTitle); |
42
|
8 |
|
|
43
|
8 |
|
// add title to component context |
44
|
8 |
|
Object.defineProperty(app.config.globalProperties, '$title', { |
45
|
8 |
|
get: () => $title.value, |
46
|
8 |
|
}); |
47
|
8 |
|
|
48
|
8 |
|
// add setPageTitle to component context |
49
|
8 |
|
Object.defineProperty(app.config.globalProperties, '$setPageTitle', { |
50
|
8 |
|
get: () => setTitle, |
51
|
8 |
|
}); |
52
|
8 |
|
}; |
53
|
8 |
|
|
54
|
8 |
|
return { install }; |
55
|
8 |
|
}; |
56
|
1 |
|
|
57
|
|
|
export { pageTitle }; |
58
|
|
|
|