HtmlBlock.vue 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <template>
  2. <div class="ai-html">
  3. <div class="ai-html__head">
  4. <span class="ai-html__label">HTML 预览</span>
  5. <span v-if="pending" class="ai-html__pending">接收中…</span>
  6. <span class="ai-html__spacer"></span>
  7. <button class="ai-html__btn" type="button" :class="{ 'is-on': mode === 'preview' }" @click="mode = 'preview'">
  8. 预览
  9. </button>
  10. <button class="ai-html__btn" type="button" :class="{ 'is-on': mode === 'source' }" @click="mode = 'source'">
  11. 源码
  12. </button>
  13. <button class="ai-html__btn" type="button" @click="openNewTab">新窗口</button>
  14. </div>
  15. <div v-if="pending" class="ai-html__skeleton ai-skeleton" :style="{ height: '160px' }"></div>
  16. <div v-else-if="mode === 'source'" class="ai-html__source">
  17. <CodeBlock :text="html" lang="html" />
  18. </div>
  19. <iframe
  20. v-else
  21. ref="frameRef"
  22. class="ai-html__frame"
  23. :style="{ height: `${frameHeight}px` }"
  24. :srcdoc="srcdoc"
  25. sandbox="allow-scripts"
  26. referrerpolicy="no-referrer"
  27. aria-label="AI HTML 预览"
  28. ></iframe>
  29. </div>
  30. </template>
  31. <script lang="ts" setup>
  32. /**
  33. * HTML 渲染块
  34. *
  35. * 安全策略:用 sandbox="allow-scripts"(**不给** allow-same-origin)隔离渲染,
  36. * 使 AI 产出的 HTML/JS 运行在不透明源下,无法访问宿主页面的 DOM / Cookie / storage。
  37. * 高度自适应通过注入脚本 postMessage 回传,父级校验 event.source 后采用。
  38. */
  39. import { computed, onBeforeUnmount, onMounted, ref } from 'vue';
  40. import CodeBlock from './CodeBlock.vue';
  41. const props = withDefaults(
  42. defineProps<{
  43. html: string;
  44. /** 是否仍在流式输出(未闭合时不渲染,避免半截页面) */
  45. pending?: boolean;
  46. }>(),
  47. { pending: false },
  48. );
  49. const mode = ref<'preview' | 'source'>('preview');
  50. const frameRef = ref<HTMLIFrameElement>();
  51. const frameHeight = ref(240);
  52. const isSvg = computed(() => /^\s*<svg[\s>]/i.test(props.html || ''));
  53. /** 注入高度上报脚本:内容变化时把 scrollHeight 回传父级 */
  54. const srcdoc = computed(() => {
  55. const body = props.html || '';
  56. const report = `<script>
  57. (function () {
  58. function send() {
  59. try {
  60. parent.postMessage({ __aiHtmlHeight: Math.max(document.documentElement.scrollHeight, document.body ? document.body.scrollHeight : 0) }, '*');
  61. } catch (e) {}
  62. }
  63. window.addEventListener('load', send);
  64. document.addEventListener('DOMContentLoaded', function () {
  65. send();
  66. if (window.ResizeObserver && document.body) {
  67. new ResizeObserver(send).observe(document.body);
  68. }
  69. });
  70. setTimeout(send, 60);
  71. setTimeout(send, 400);
  72. })();
  73. <\/script>`;
  74. if (isSvg.value) {
  75. 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>`;
  76. }
  77. return `<!DOCTYPE html><html><head><meta charset="utf-8"><style>html,body{margin:0;background:transparent}</style></head><body>${body}${report}</body></html>`;
  78. });
  79. function onMessage(e: MessageEvent) {
  80. if (!frameRef.value || e.source !== frameRef.value.contentWindow) return;
  81. const h = (e.data || {}).__aiHtmlHeight;
  82. if (typeof h === 'number' && h > 0) {
  83. frameHeight.value = Math.min(Math.max(h + 8, 120), 900);
  84. }
  85. }
  86. function openNewTab() {
  87. const win = window.open('', '_blank');
  88. if (!win) return;
  89. win.document.write(props.html || '');
  90. win.document.close();
  91. }
  92. onMounted(() => window.addEventListener('message', onMessage));
  93. onBeforeUnmount(() => window.removeEventListener('message', onMessage));
  94. </script>
  95. <style lang="less" scoped>
  96. .ai-html {
  97. margin: var(--ai-sp-3) 0;
  98. border: 1px solid var(--ai-border);
  99. border-radius: var(--ai-radius);
  100. background: var(--ai-bg);
  101. overflow: hidden;
  102. &__head {
  103. display: flex;
  104. align-items: center;
  105. gap: var(--ai-sp-2);
  106. padding: var(--ai-sp-1) var(--ai-sp-2) var(--ai-sp-1) var(--ai-sp-3);
  107. border-bottom: 1px solid var(--ai-border);
  108. }
  109. &__label {
  110. font-size: 11px;
  111. letter-spacing: 0.04em;
  112. text-transform: uppercase;
  113. color: var(--ai-text-2);
  114. }
  115. &__pending {
  116. font-size: 11px;
  117. color: var(--ai-primary);
  118. }
  119. &__spacer {
  120. flex: 1 1 auto;
  121. }
  122. &__btn {
  123. padding: 2px 8px;
  124. font-size: 11px;
  125. color: var(--ai-text-2);
  126. background: transparent;
  127. border: 1px solid transparent;
  128. border-radius: var(--ai-radius-sm);
  129. cursor: pointer;
  130. transition: all 0.15s ease-out;
  131. &:hover,
  132. &.is-on {
  133. color: var(--ai-primary);
  134. border-color: fade(@primary-color, 35%);
  135. background: var(--ai-primary-soft);
  136. }
  137. }
  138. &__frame {
  139. display: block;
  140. width: 100%;
  141. border: 0;
  142. background: @white;
  143. }
  144. &__source {
  145. padding: 0;
  146. :deep(.ai-code) {
  147. margin: 0;
  148. }
  149. /* 外层 .ai-html 已有边框,源码视图不再重复一圈 */
  150. :deep([data-streamdown='code-block']) {
  151. border: 0;
  152. border-radius: 0;
  153. }
  154. }
  155. &__skeleton {
  156. margin: var(--ai-sp-3);
  157. }
  158. }
  159. </style>