-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.ts
More file actions
151 lines (138 loc) · 4.37 KB
/
index.ts
File metadata and controls
151 lines (138 loc) · 4.37 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
import { generateStore, getDefaultFiles } from '@opentiny/tiny-robot-playground/utils'
import DefaultTheme from 'vitepress/theme'
import { enhanceAppWithTabs } from 'vitepress-plugin-tabs/client'
import { setupDarkModeListener } from './color-mode'
import Layout from './Layout.vue'
import HomePage from './home/index.vue'
import CustomTable from './components/CustomTable.vue'
import MermaidBlock from './components/MermaidBlock.vue'
import '@opentiny/tiny-robot-style'
import {nextTick, watch} from 'vue';
import {useRoute} from 'vitepress';
import mediumZoom, { Zoom } from 'medium-zoom';
import { insertFurion } from './insert-furion'
// 引入样式文件
import './medium-zoom.css';
import './style.css'
declare global {
interface Window {
__SW_REGISTERED__?: boolean
__CODE_PLAYGROUND_LISTENED__?: boolean
}
}
export default {
...DefaultTheme,
enhanceApp({ app }) {
// 监听暗黑模式变化
setupDarkModeListener()
// 添加 furion 监控埋点
insertFurion()
app.mixin({
mounted() {
registerServiceWorker()
listenCodePlaygroundEvent()
},
})
app.component('HomePage', HomePage)
app.component('CustomTable', CustomTable)
app.component('MermaidBlock', MermaidBlock)
enhanceAppWithTabs(app)
},
Layout,
setup() {
// 为img元素添加点击放大功能并根据路由隐藏 playground 图标
const route = useRoute();
let zoom: Zoom | null = null;
watch(
() => route.path,
() => nextTick(() => {
if (zoom) {
zoom.detach();
}
if (typeof window !== 'undefined'){
zoom = mediumZoom('.main img', {background: 'var(--vp-c-bg)'})
if (route.path.includes('tiny-robot')) {
document.body.classList.remove('hide-code-playground');
} else {
document.body.classList.add('hide-code-playground');
}
}
}),
{immediate: true}
)
},
}
function registerServiceWorker() {
if (
typeof window === 'undefined' ||
typeof navigator === 'undefined' ||
!('serviceWorker' in navigator) ||
window.__SW_REGISTERED__
) {
return
}
window.__SW_REGISTERED__ = true
navigator.serviceWorker
.register(import.meta.env.BASE_URL + 'sw.js')
.then(() => {
console.log('ServiceWorker registration successful')
})
.catch((err) => {
console.log('ServiceWorker registration failed: ', err)
})
}
function listenCodePlaygroundEvent() {
if (typeof window === 'undefined' || window.__CODE_PLAYGROUND_LISTENED__) {
return
}
const route = useRoute();
window.__CODE_PLAYGROUND_LISTENED__ = true
document.addEventListener('code-playground', (event) => {
const detail = (event as CustomEvent).detail
if (!detail) return
const { props, currentFiles, activeFile } = detail
const files: { filename: string; code: string }[] = []
if (Object.keys(currentFiles).length === 0) {
files.push({
filename: 'src/App.vue',
code: props.vueCode,
})
} else {
files.push({
filename: 'src/App.vue',
code: currentFiles[activeFile].code,
})
Object.entries(currentFiles).forEach(([filename, file]) => {
if (filename === activeFile) return
files.push({ filename: `src/${filename}`, code: (file as { code: string }).code })
})
}
const tinyRobotVersion = 'latest'
const defaultFiles = getDefaultFiles({ tinyRobotVersion })
const cssFile = defaultFiles.find((file) => file.filename === 'src/index.css')
if (cssFile) {
files.push(cssFile)
}
const extraPackages: string[] = JSON.parse(decodeURIComponent(props.playground))?.packages || []
const extraImports = extraPackages
.map((pkgAndVersion) => {
const index = pkgAndVersion.lastIndexOf('@')
const pkg = pkgAndVersion.slice(0, index)
const version = pkgAndVersion.slice(index + 1)
return { [pkg]: version }
})
.reduce((acc, curr) => {
return { ...acc, ...curr }
}, {})
const { store } = generateStore({
tinyRobotVersion,
files,
extraImports,
})
if (route.path.includes('tiny-robot')) {
const playgroundUrl =
import.meta.env.VITE_TINY_ROBOT_PLAYGROUND_URL || `${import.meta.env.BASE_URL}/tiny-robot/playground/`
window.open(playgroundUrl.replace(/(?<!:)\/\/+/g, '/') + store.serialize(), '_blank')
}
})
}