From 2e5120dc724988cd489b8df72c128980dce70b26 Mon Sep 17 00:00:00 2001 From: gaoziman <2942894660@qq.com> Date: Sun, 21 Dec 2025 02:47:27 +0800 Subject: [PATCH] =?UTF-8?q?fix(HTML=E9=A2=84=E8=A7=88):=20=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D=E9=93=BE=E6=8E=A5=E7=82=B9=E5=87=BB=E5=BD=B1=E5=93=8D?= =?UTF-8?q?=E7=88=B6=E7=AA=97=E5=8F=A3=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 注入 标签 - 智能检测现有 head/html 标签位置注入 - 确保预览中的链接在新标签页打开 - 避免链接影响主应用页面 --- src/components/ui/HtmlPreviewModal.tsx | 27 ++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/src/components/ui/HtmlPreviewModal.tsx b/src/components/ui/HtmlPreviewModal.tsx index a699f97..7b189bd 100644 --- a/src/components/ui/HtmlPreviewModal.tsx +++ b/src/components/ui/HtmlPreviewModal.tsx @@ -1,6 +1,6 @@ 'use client'; -import { useState, useCallback, useEffect, useRef } from 'react'; +import { useState, useCallback, useEffect, useRef, useMemo } from 'react'; import { X, Monitor, @@ -83,6 +83,29 @@ export function HtmlPreviewModal({ // 获取当前设备配置 const currentDevice = deviceConfigs.find((d) => d.type === device) || deviceConfigs[0]; + // 处理 HTML 代码,注入 防止链接影响父窗口 + const processedHtmlCode = useMemo(() => { + const baseTag = ''; + + // 检查是否已经有 标签 + if (/ 标签后注入 + if (/]*>/i.test(htmlCode)) { + return htmlCode.replace(/]*>/i, `$&\n ${baseTag}`); + } + + // 尝试在 标签后注入 + if (/]*>/i.test(htmlCode)) { + return htmlCode.replace(/]*>/i, `$&\n \n ${baseTag}\n `); + } + + // 如果都没有,在最前面添加 + return `${baseTag}\n${htmlCode}`; + }, [htmlCode]); + // ESC 关闭 / F11 全屏切换 useEffect(() => { if (!isOpen) return; @@ -348,7 +371,7 @@ export function HtmlPreviewModal({ {/* iframe 预览 */}