当前位置: 首页 > news >正文

vue展示修改前后对比,并显示修改标注diff

在这里插入图片描述

动态父组件

 <template><el-buttontype="primary"size="small"plain@click="showDiffDialog(subItem)">查看修改内容</el-button><TextDiffDialogv-model:visible="diffDialogVisible":before="currentDiffItem?.before?.[currentDiffItem?.fieldName] || ''":after="currentDiffItem?.after?.[currentDiffItem?.fieldName] || ''"/></el-scrollbar>
</template><script>import TextDiffDialog from './TextDiffDialog.vue';export default {name: 'logIndex',components: { TextDiffDialog },data() {return {diffDialogVisible: false,currentDiffItem: null,};},props: {formId: String,},methods: {showDiffDialog(item) {this.currentDiffItem = item;this.diffDialogVisible = true;},},created() {},mounted() {},
};
</script><style scoped lang="scss">.el-button--small {padding: 0px 6px;height: 26px;line-height: 26px;border: 0;
}
</style>
npm install diff

//子组件

<template><el-dialogv-model="dialogVisible"title="变更对比"width="60%":before-close="handleClose"class="text-diff-dialog"><template #header><div class="diff-header"><div class="diff-title"><i class="iconfont icon-shejibiangeng"></i>变更对比</div><div class="diff-stats"><el-switch v-model="showDiff" active-text="显示修改标注" /><el-tooltipeffect="light"content="<p><span style='background: #52bd94;color: #172890;padding: 2px;border-radius: 2px;'>绿色背景</span> 表示新增</p><p><span style='background:#f4b6b6;color: #172890;padding: 2px;border-radius: 2px;'>红色背景</span> 表示删除</p>"raw-content><i style="color: #a5adba" class="el-icon-question"></i></el-tooltip></div></div></template><div class="diff-container"><div class="diff-content" ref="diffContent"><div class="diff-panel before"><div class="panel-header">修改前</div><div class="panel-content"><divv-for="(line, index) in beforeLines":key="'before-' + index"class="diff-line"><span class="line-number">{{ line.lineNumber }}</span><span class="line-content" v-html="line.content"></span></div></div></div><div class="diff-panel after"><divclass="panel-header"style="display: flex; justify-content: space-between">修改后 <span class="total">{{ totalChanges }} 处修改</span></div><div class="panel-content"><divv-for="(line, index) in afterLines":key="'after-' + index"class="diff-line"><span class="line-number">{{ line.lineNumber }}</span><span class="line-content" v-html="line.content"></span></div></div></div></div></div></el-dialog>
</template><script>
import { diffChars } from 'diff';export default {name: 'TextDiffDialog',props: {visible: {type: Boolean,default: false,},before: {type: String,default: '',},after: {type: String,default: '',},},data() {return {dialogVisible: false,beforeLines: [],afterLines: [],showDiff: false,totalChanges: 0,};},watch: {visible(val) {this.dialogVisible = val;if (val) {this.$nextTick(() => {this.generateDiff();});}},dialogVisible(val) {if (!val) {this.showDiff = false;this.$emit('update:visible', false);}},showDiff() {this.generateDiff();},},methods: {handleClose() {this.dialogVisible = false;},generateDiff() {const beforeText = this.before || '';const afterText = this.after || '';// 将文本分割成行const beforeLines = beforeText.split('\n');const afterLines = afterText.split('\n');this.beforeLines = beforeLines.map((line, index) => ({lineNumber: index + 1,content: this.escapeHtml(line),}));// 使用diffChars进行字符级别的差异比较const changes = diffChars(beforeText, afterText);let currentLine = '';let lineNumber = 1;this.afterLines = [];this.totalChanges = 0;changes.forEach((change) => {if (change.added) {if (this.showDiff) {currentLine += `<span class="diff-added">${this.escapeHtml(change.value)}</span>`;} else {currentLine += this.escapeHtml(change.value);}this.totalChanges++;} else if (change.removed) {if (this.showDiff) {currentLine += `<span class="diff-deleted">${this.escapeHtml(change.value)}</span>`;}this.totalChanges++;} else {currentLine += this.escapeHtml(change.value);}// 处理换行if (change.value.includes('\n')) {const lines = currentLine.split('\n');lines.forEach((line, index) => {if (index < lines.length - 1) {this.afterLines.push({lineNumber: lineNumber++,content: line,});}});currentLine = lines[lines.length - 1];}});// 添加最后一行if (currentLine) {this.afterLines.push({lineNumber: lineNumber,content: currentLine,});}},escapeHtml(text) {const div = document.createElement('div');div.textContent = text;return div.innerHTML;},},
};
</script><style lang="scss" scoped>
.text-diff-dialog {:deep(.el-dialog__body) {padding: 0;}
}.diff-container {height: 600px;display: flex;flex-direction: column;width: 100%;
}.diff-header {padding: 0 6px 6px 6px;border-bottom: 1px solid #ebeef5;display: flex;justify-content: space-between;align-items: center;
}.diff-stats {display: flex;align-items: center;gap: 6px;margin-right: 20px;
}.total {color: #6b778c !important;font-size: 13px;
}
.diff-content {flex: 1;display: flex;overflow: hidden;
}.diff-panel {flex: 1;display: flex;flex-direction: column;border-right: 1px solid #ebeef5;&:last-child {border-right: none;}.panel-header {padding: 12px 16px;background-color: #f5f7fa;border-bottom: 1px solid #ebeef5;font-weight: 500;color: #172b4d;font-size: 16px;}.panel-content {flex: 1;overflow-y: auto;padding: 16px;font-family: monospace;white-space: pre-wrap;word-break: break-all;}
}.diff-line {display: flex;line-height: 1.5;.line-number {color: #909399;text-align: right;padding-right: 8px;user-select: none;}.line-content {color: #172b4d;font-size: 14px;flex: 1;:deep(.diff-added) {background-color: #52bd94;border-radius: 2px;padding: 2px;margin-left: 2px;}:deep(.diff-deleted) {background-color: #f4b6b6;text-decoration: line-through;border-radius: 2px;padding: 2px;margin-left: 2px;}}
}
.diff-title {color: #172b4d;font-size: 16px;font-weight: 500;line-height: 24px; /* 150% */
}
</style>

相关文章:

  • YOLOv2 深度解析:目标检测领域的进阶之路
  • 借教室--二分+查分
  • 柠檬(lemon)是什么东西?
  • leetcode:1688. 比赛中的配对次数(python3解法,数学相关算法题)
  • 深耕数字化赛道,联众优车以创新风控体系构筑汽车金融护城河
  • 【脚本】一键部署脚本
  • DH加密详解
  • SD08_解决由于anaconda版本过低无法安装高版本python的问题
  • camera_venc_thread线程获取高分辨率编码码流
  • PH热榜 | 2025-05-27
  • MySQL connection close 后, mysql server上的行为是什么
  • Python 实现简易版的文件管理(结合网络编程)
  • 一键重装Windows/Linux系统,支持虚拟服务器
  • redis高并发问题
  • (自用)Java学习-5.15(模糊搜索,收藏,购物车)
  • 公共场所人脸识别设备备案合规要点
  • 实战分享:DolphinScheduler 中 Shell 任务环境变量最佳配置方式
  • Python 实现桶排序详解
  • SNTP 协议详解:网络时间同步的轻量级解决方案
  • TLE9893-2QKW62S新建Keil MDK工程
  • 沈阳网站建设费用/叶涛网站推广优化
  • 大型网站怎么做/百度在线提问
  • 国外专门做童装的网站/图片外链在线生成网址
  • 微网站模板前后台/网络营销专业怎么样
  • 福州微网站开发/优化网站seo
  • 有哪些做电子商务的网站/上海seo网站推广