| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- <template>
- <div class="ai-html">
- <div class="ai-html__head">
- <span class="ai-html__label">HTML 预览</span>
- <span v-if="pending" class="ai-html__pending">接收中…</span>
- <span class="ai-html__spacer"></span>
- <button class="ai-html__btn" type="button" :class="{ 'is-on': mode === 'preview' }" @click="mode = 'preview'">
- 预览
- </button>
- <button class="ai-html__btn" type="button" :class="{ 'is-on': mode === 'source' }" @click="mode = 'source'">
- 源码
- </button>
- <button class="ai-html__btn" type="button" @click="openNewTab">新窗口</button>
- </div>
- <div v-if="pending" class="ai-html__skeleton ai-skeleton" :style="{ height: '160px' }"></div>
- <div v-else-if="mode === 'source'" class="ai-html__source">
- <CodeBlock :text="html" lang="html" />
- </div>
- <iframe
- v-else
- ref="frameRef"
- class="ai-html__frame"
- :style="{ height: `${frameHeight}px` }"
- :srcdoc="srcdoc"
- sandbox="allow-scripts"
- referrerpolicy="no-referrer"
- aria-label="AI HTML 预览"
- ></iframe>
- </div>
- </template>
- <script lang="ts" setup>
- /**
- * HTML 渲染块
- *
- * 安全策略:用 sandbox="allow-scripts"(**不给** allow-same-origin)隔离渲染,
- * 使 AI 产出的 HTML/JS 运行在不透明源下,无法访问宿主页面的 DOM / Cookie / storage。
- * 高度自适应通过注入脚本 postMessage 回传,父级校验 event.source 后采用。
- */
- import { computed, onBeforeUnmount, onMounted, ref } from 'vue';
- import CodeBlock from './CodeBlock.vue';
- const props = withDefaults(
- defineProps<{
- html: string;
- /** 是否仍在流式输出(未闭合时不渲染,避免半截页面) */
- pending?: boolean;
- }>(),
- { pending: false },
- );
- const mode = ref<'preview' | 'source'>('preview');
- const frameRef = ref<HTMLIFrameElement>();
- const frameHeight = ref(240);
- const isSvg = computed(() => /^\s*<svg[\s>]/i.test(props.html || ''));
- /** 注入高度上报脚本:内容变化时把 scrollHeight 回传父级 */
- const srcdoc = computed(() => {
- const body = props.html || '';
- const report = `<script>
- (function () {
- function send() {
- try {
- parent.postMessage({ __aiHtmlHeight: Math.max(document.documentElement.scrollHeight, document.body ? document.body.scrollHeight : 0) }, '*');
- } catch (e) {}
- }
- window.addEventListener('load', send);
- document.addEventListener('DOMContentLoaded', function () {
- send();
- if (window.ResizeObserver && document.body) {
- new ResizeObserver(send).observe(document.body);
- }
- });
- setTimeout(send, 60);
- setTimeout(send, 400);
- })();
- <\/script>`;
- if (isSvg.value) {
- return `<!DOCTYPE html><html><head><meta charset="utf-8"><style>html,body{margin:0;padding:0;background:transparent}svg{display:block;max-width:100%;height:auto}</style></head><body>${body}${report}</body></html>`;
- }
- return `<!DOCTYPE html><html><head><meta charset="utf-8"><style>html,body{margin:0;background:transparent}</style></head><body>${body}${report}</body></html>`;
- });
- function onMessage(e: MessageEvent) {
- if (!frameRef.value || e.source !== frameRef.value.contentWindow) return;
- const h = (e.data || {}).__aiHtmlHeight;
- if (typeof h === 'number' && h > 0) {
- frameHeight.value = Math.min(Math.max(h + 8, 120), 900);
- }
- }
- function openNewTab() {
- const win = window.open('', '_blank');
- if (!win) return;
- win.document.write(props.html || '');
- win.document.close();
- }
- onMounted(() => window.addEventListener('message', onMessage));
- onBeforeUnmount(() => window.removeEventListener('message', onMessage));
- </script>
- <style lang="less" scoped>
- .ai-html {
- margin: var(--ai-sp-3) 0;
- border: 1px solid var(--ai-border);
- border-radius: var(--ai-radius);
- background: var(--ai-bg);
- overflow: hidden;
- &__head {
- display: flex;
- align-items: center;
- gap: var(--ai-sp-2);
- padding: var(--ai-sp-1) var(--ai-sp-2) var(--ai-sp-1) var(--ai-sp-3);
- border-bottom: 1px solid var(--ai-border);
- }
- &__label {
- font-size: 11px;
- letter-spacing: 0.04em;
- text-transform: uppercase;
- color: var(--ai-text-2);
- }
- &__pending {
- font-size: 11px;
- color: var(--ai-primary);
- }
- &__spacer {
- flex: 1 1 auto;
- }
- &__btn {
- padding: 2px 8px;
- font-size: 11px;
- color: var(--ai-text-2);
- background: transparent;
- border: 1px solid transparent;
- border-radius: var(--ai-radius-sm);
- cursor: pointer;
- transition: all 0.15s ease-out;
- &:hover,
- &.is-on {
- color: var(--ai-primary);
- border-color: fade(@primary-color, 35%);
- background: var(--ai-primary-soft);
- }
- }
- &__frame {
- display: block;
- width: 100%;
- border: 0;
- background: @white;
- }
- &__source {
- padding: 0;
- :deep(.ai-code) {
- margin: 0;
- }
- /* 外层 .ai-html 已有边框,源码视图不再重复一圈 */
- :deep([data-streamdown='code-block']) {
- border: 0;
- border-radius: 0;
- }
- }
- &__skeleton {
- margin: var(--ai-sp-3);
- }
- }
- </style>
|