wangzhibo
2026-07-16 b57020623cf0c946706573bf102e548c3a544423
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
<template>
    <iframe
        :src="`${helper.index}${path}?lang=${ctx.serviceLang}`"
        class="iframe"
        :class="{ 'is-hide': hide }"
        ref="iframeRef"
    />
</template>
 
<script setup lang="ts">
defineOptions({ name: 'ai-code-dev' });
 
import { onMounted, onUnmounted, ref } from 'vue';
import { module } from '/@/cool';
import { isString } from 'lodash-es';
import { ctx } from 'virtual:ctx';
 
defineProps({
    path: String,
    hide: Boolean
});
 
const emit = defineEmits(['message']);
 
const helper = module.config('helper');
const iframeRef = ref<HTMLIFrameElement | null>(null);
const event = new Map<string, (data: any) => void>();
 
function send(name: string, data: any, cb?: (data: any) => void) {
    if (cb) event.set(name, cb);
    iframeRef.value?.contentWindow?.postMessage({ name, data }, '*');
}
 
function onMessage(e: MessageEvent) {
    try {
        const { name, data } = isString(e.data) ? JSON.parse(e.data) : e.data;
 
        const msg = event.get(name);
        if (msg) msg(data);
 
        emit('message', { name, data });
    } catch (error) {
        console.error('Invalid message data', error, e.data);
    }
}
 
onMounted(() => {
    window.addEventListener('message', onMessage);
});
 
onUnmounted(() => {
    window.removeEventListener('message', onMessage);
});
 
defineExpose({ send });
</script>
 
<style lang="scss" scoped>
.cl-comm__icon {
    width: auto;
    padding: 0 10px;
    border-color: var(--el-color-primary);
    color: var(--el-color-primary);
 
    .cl-svg {
        color: var(--el-color-primary);
    }
}
 
.iframe.is-hide {
    height: 0;
    width: 0;
    opacity: 0;
    overflow: hidden;
}
</style>