Logo

diff.js+diff2html-ui.js 使用实例

photo

2025年05月04日

<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>文件差异对比</title>
    <link rel="stylesheet" href="https://unpkg.com/diff2html/bundles/css/diff2html.min.css">
    <style>
        body {
            font-family: Arial, sans-serif;
        }
        #result {
            border: 1px solid #ddd;
            padding: 10px;
            margin-top: 20px;
            overflow: auto;
        }
    </style>
</head>
<body>
<h1>对比远程文件与本地文本差异</h1>
<div>
    <textarea id="localText" rows="10" cols="50" placeholder="输入本地文本..."></textarea>
</div>
<button id="compareBtn">比较</button>
<div id="result"></div>

<script src="https://cdn.jsdelivr.net/npm/diff@5.0.0/dist/diff.min.js"></script>
<script src="
https://cdn.jsdelivr.net/npm/diff2html@3.4.48/bundles/js/diff2html-ui.min.js
"></script>
<script>
    document.getElementById('compareBtn').addEventListener('click', function() {
        const remoteFileUrl = 'http://idipportal-dev.ied.com/data/8ball_IDIP_20240821_238.xml'; // 替换为目标远程文件的 URL
        const localText = document.getElementById('localText').value;

        // 使用 Fetch API 获取远程文件内容
        fetch(remoteFileUrl)
            .then(response => {
                if (!response.ok) {
                    throw new Error('网络错误');
                }
                return response.text();
            })
            .then(remoteText => {
                // 使用 Diff.js 计算差异
                const diff = Diff.diffLines(remoteText,localText);

                // 将 diff 转换为 Diff2Html 所需的格式
                let addedCount = 0;
                let removedCount = 0;
                const ttdiffString = diff.map(part => {
                    const prefix = part.added ? '+' : part.removed ? '-' : ' ';
                    // 更新计数器
                    if (part.added) {
                        addedCount += part.count || part.value.split('\n').length; // 计算添加的行数
                    }
                    if (part.removed) {
                        removedCount += part.count || part.value.split('\n').length; // 计算删除的行数
                    }
                    return part.value.split('\n').map(line => `${prefix}${line}`).join('\n');
                }).join('\n');

                const diffString = `diff --git a/8ball_IDIP_20240821_238.xml b/8ball_IDIP_20240821_238.xml
index 0000001..0ddf2ba
--- a/8ball_IDIP_20240821_238.xml
+++ b/8ball_IDIP_20240821_238.xml
@@ -${removedCount} +${addedCount} @@
${ttdiffString}
`;

                var targetElement = document.getElementById('result');
                var configuration = {
                    drawFileList: false,
                    fileListToggle: false,
                    fileListStartVisible: false,
                    fileContentToggle: false,
                    matching: 'lines',
                    outputFormat: 'side-by-side',
                    synchronisedScroll: true,
                    highlight: true,
                    renderNothingWhenEmpty: false,
                };
                var diff2htmlUi = new Diff2HtmlUI(targetElement, diffString, configuration);
                diff2htmlUi.draw();
                diff2htmlUi.highlightCode();
            })
            .catch(error => {
                console.error('获取远程文件失败:', error);
                document.getElementById('result').innerText = '错误: ' + error.message;
            });
    });
</script>
</body>
</html>

参考文档:

https://github.com/rtfpessoa/diff2html-cli

https://github.com/kpdecker/jsdiff#change-objects

https://www.cnblogs.com/kaikaiya/p/18374182

橙子主题打折出售

其实我不卖,主要是这里是放广告的,所以就放了一个
毕竟主题都没做完,卖了也是坑.

购买它
所有附件
该文章没有附件.
本文为原创文章,请注意保留出处!

留言板

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

在现代的Web应用程序中,提供自动语言检测功能可以显著提升用户体验。本文将介绍如何实现一个具有自动语...实现一个自动语言检测的翻译工具

热门文章

WordPress 后台编辑器样式实现直接预览 在WordPress3.0以后,有一个新的实用功能:你可以更改默认后台编辑器(TinyMCE)的样...WordPress后台编辑器样式实现直接预览 作者:Pastore Antonio
1571 浏览量
C#图片处理 通常对一幅图片的处理包括:格式变换,缩放(Scale),翻转(Rotate),截取(Clip),滤镜...C#图片处理 作者:Pastore Antonio
1530 浏览量
【干货】Chrome插件(扩展)开发全攻略 写在前面我花了将近一个多月的时间断断续续写下这篇博文,并精心写下完整demo,写博客的辛苦大家懂的...【干货】Chrome插件(扩展)开发全攻略 作者:Pastore Antonio
1525 浏览量
memcached 处理 多端口:https://blog.csdn.net/Erica_1230/article/deta...memcached处理 作者:Pastore Antonio
1523 浏览量
又到泡桐花开时 暮春四月,百花争艳,千姿百态,香飘千里,万木争荣。有一种泡桐树,迟于阳春三月里盛开的桃树、杏树、李树,欣欣然,翩翩然,花开芬芳,来到五彩缤纷姹紫嫣红的大自然界,向人们一展芳姿,怒放于中原大地,成为晚春时节一道靓丽的风景线。 作者:Pastore Antonio
1490 浏览量