Skip to content

Commit 9ab370e

Browse files
author
xuqingkai
committed
docs: ✏️ 调整演示demo iframe加载完成后再同步当前是否暗黑模式
Closes: #210
1 parent 7edc746 commit 9ab370e

File tree

1 file changed

+23
-18
lines changed

1 file changed

+23
-18
lines changed

docs/.vitepress/theme/components/frame.vue

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
<template>
2-
<iframe v-if="href" id="demo" class="iframe demo-model" scrolling="auto" frameborder="0" :src="href"></iframe>
2+
<iframe v-if="href" ref="iframe" id="demo" class="iframe demo-model" scrolling="auto" frameborder="0" :src="href"></iframe>
33
</template>
44

55
<script setup lang="ts">
6-
import { useRoute, useData } from 'vitepress';
7-
import { computed, onMounted, watch } from 'vue'
8-
const baseUrl = process.env.NODE_ENV === 'production' ? `${location.origin}/demo/?timestamp=${new Date().getTime()}#/` : 'http://localhost:5173/demo/#/'
6+
import { useRoute, useData } from 'vitepress'
7+
import { computed, onMounted, ref, watch } from 'vue'
8+
const baseUrl =
9+
process.env.NODE_ENV === 'production' ? `${location.origin}/demo/?timestamp=${new Date().getTime()}#/` : 'http://localhost:5173/demo/#/'
910
1011
const route = useRoute()
1112
const href = computed(() => {
@@ -20,30 +21,34 @@ const href = computed(() => {
2021
return href
2122
})
2223
24+
const iframe = ref<HTMLIFrameElement | null>(null)
25+
2326
const vitepressData = useData()
2427
2528
onMounted(() => {
26-
setTimeout(() => {
27-
ssendMessage()
28-
}, 500);
29-
})
30-
31-
32-
watch(() => vitepressData.isDark.value, () => {
33-
ssendMessage()
29+
iframe.value &&
30+
iframe.value.addEventListener('load', () => {
31+
// 在iframe加载完成后执行发送消息的操作
32+
ssendMessage()
33+
})
3434
})
3535
36-
function ssendMessage(){
37-
const iFrame: any = document.getElementById('demo')
38-
iFrame && iFrame.contentWindow.postMessage(vitepressData.isDark.value, href.value)
36+
watch(
37+
() => vitepressData.isDark.value,
38+
() => {
39+
ssendMessage()
40+
}
41+
)
3942
43+
function ssendMessage() {
44+
if (iframe.value && iframe.value.contentWindow) {
45+
iframe.value.contentWindow.postMessage(vitepressData.isDark.value, href.value)
46+
}
4047
}
4148
4249
function kebabToCamel(input: string): string {
43-
return input.replace(/-([a-z])/g, (match, group) => group.toUpperCase());
50+
return input.replace(/-([a-z])/g, (match, group) => group.toUpperCase())
4451
}
45-
46-
4752
</script>
4853
<style scoped>
4954
::-webkit-scrollbar {

0 commit comments

Comments
 (0)