From 4d96d4386e28fd9e6037f8f3afac29033e09239d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A7=8B=E4=BA=8E=E5=88=9D=E8=A7=81?= <752204717@qq.com> Date: Thu, 23 Jan 2025 13:33:19 +0800 Subject: [PATCH] tijiao --- src/components/page/components/table/word.vue | 106 +++++++++--------- 1 file changed, 55 insertions(+), 51 deletions(-) diff --git a/src/components/page/components/table/word.vue b/src/components/page/components/table/word.vue index d223a40..a939247 100644 --- a/src/components/page/components/table/word.vue +++ b/src/components/page/components/table/word.vue @@ -1059,61 +1059,65 @@ export default { return emptyContentIndexes; }, + escapeRegExp(string) { + return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); +}, highlightText(text, annotations) { - if (!this.isPreview) { - let tempText = text; + if (!this.isPreview) { + let tempText = text; - // 1. 使用占位符替代批注内容 - const placeholders = []; - annotations.forEach((annotation, index) => { - if (annotation.content == '') { - return tempText; - } - const placeholder = `{{remark_placeholder_${index}}}`; // 占位符 - const regExp = new RegExp(`(${annotation.content})`, 'gi'); // 全局匹配批注内容 - - // 如果批注内容存在,替换成占位符 - if (tempText.includes(annotation.content)) { - tempText = tempText.replace(regExp, placeholder); - placeholders.push({ placeholder, annotation, index }); - } - }); - - // 2. 为占位符添加高亮 - placeholders.forEach(({ placeholder, annotation, index }) => { - const regExp = new RegExp(placeholder, 'gi'); - - tempText = tempText.replace(regExp, (match) => { - const randomColor = this.getRandomLightColor(); - return ` - - ${annotation.content} - - ${index + 1} - - - `; -}); - - }); - - // 3. 返回处理后的文本 + // 1. 使用占位符替代批注内容 + const placeholders = []; + annotations.forEach((annotation, index) => { + if (annotation.content == '') { return tempText; - } else { - return text; } - }, + const escapedContent = this.escapeRegExp(annotation.content); // 转义正则表达式中的特殊字符 + const placeholder = `{{remark_placeholder_${index}}}`; // 占位符 + const regExp = new RegExp(`(${escapedContent})`, 'gi'); // 全局匹配批注内容 + + // 如果批注内容存在,替换成占位符 + if (tempText.includes(annotation.content)) { + tempText = tempText.replace(regExp, placeholder); + placeholders.push({ placeholder, annotation, index }); + } + }); + + // 2. 为占位符添加高亮 + placeholders.forEach(({ placeholder, annotation, index }) => { + const regExp = new RegExp(placeholder, 'gi'); + + tempText = tempText.replace(regExp, (match) => { + const randomColor = this.getRandomLightColor(); + return ` + + ${annotation.content} + + ${index + 1} + + + `; + }); + }); + + // 3. 返回处理后的文本 + return tempText; + } else { + return text; + } +}, + getCommentsData() { if (!this.isPreview) {