mirror of
https://github.com/XiaoDaiGua-Ray/ray-template.git
synced 2026-11-27 15:05:09 +08:00
Protect route drafts and refine keyboard, responsive, and primary button behavior. Add CURD workspace examples with tests and bilingual documentation.
90 lines
2.4 KiB
Vue
90 lines
2.4 KiB
Vue
<template>
|
|
<component
|
|
:is="deferred ? 'details' : 'div'"
|
|
:class="deferred ? 'details custom-block' : undefined"
|
|
@toggle="openExample"
|
|
>
|
|
<summary v-if="deferred">
|
|
{{ lang.startsWith('en') ? 'Run example' : '运行示例' }}
|
|
</summary>
|
|
<div v-if="!deferred || started" class="ray-admin-example-frame">
|
|
<iframe
|
|
ref="exampleFrame"
|
|
class="ray-admin-example"
|
|
:src="frameSrc"
|
|
:title="title"
|
|
:height="height ?? 490"
|
|
loading="lazy"
|
|
referrerpolicy="strict-origin"
|
|
@load="syncTheme"
|
|
/>
|
|
</div>
|
|
<p>
|
|
<a :href="src" target="_blank" rel="noopener">{{ newTabText }}</a>
|
|
</p>
|
|
</component>
|
|
</template>
|
|
|
|
<script setup lang="ts">
|
|
import { useData, withBase } from 'vitepress'
|
|
import { computed, onMounted, ref, useTemplateRef, watch } from 'vue'
|
|
|
|
const props = defineProps<{
|
|
path: string
|
|
title: string
|
|
layout?: boolean
|
|
height?: number
|
|
focus?: string
|
|
deferred?: boolean
|
|
}>()
|
|
const { isDark, lang } = useData()
|
|
const frame = useTemplateRef<HTMLIFrameElement>('exampleFrame')
|
|
const started = ref(false)
|
|
const frameDark = ref(isDark.value)
|
|
const newTabText = computed(() =>
|
|
lang.value.startsWith('en')
|
|
? 'Run this example in a new tab'
|
|
: '在新标签页运行此示例',
|
|
)
|
|
const openExample = (event: Event) => {
|
|
if ((event.currentTarget as HTMLDetailsElement).open && !started.value) {
|
|
frameDark.value = isDark.value
|
|
started.value = true
|
|
}
|
|
}
|
|
const adminBase = withBase('/').replace(/[^/]+\/$/, '')
|
|
const exampleUrl = (dark: boolean) => {
|
|
const query = new URLSearchParams({
|
|
theme: dark ? 'dark' : 'light',
|
|
lang: lang.value.startsWith('en') ? 'en' : 'zh-CN',
|
|
})
|
|
|
|
if (props.layout) query.set('layout', '1')
|
|
|
|
if (props.focus) query.set('focus', props.focus)
|
|
|
|
const server = import.meta.env.DEV
|
|
? import.meta.env.VITE_RAY_EXAMPLE_ORIGIN || 'http://127.0.0.1:9527'
|
|
: ''
|
|
|
|
return `${server}${adminBase}example.html?${query}#${props.path}`
|
|
}
|
|
const frameSrc = computed(() => exampleUrl(frameDark.value))
|
|
const src = computed(() => exampleUrl(isDark.value))
|
|
|
|
const syncTheme = () => {
|
|
const iframe = frame.value
|
|
|
|
iframe?.contentWindow?.postMessage(
|
|
{
|
|
type: 'ray-template:example-theme',
|
|
theme: isDark.value ? 'dark' : 'light',
|
|
},
|
|
new URL(iframe.src).origin,
|
|
)
|
|
}
|
|
|
|
onMounted(syncTheme)
|
|
watch(isDark, syncTheme)
|
|
</script>
|