一 实现过程
angualr 依赖安装
npm i --save-dev @types/codemirror npm i --save-dev @types/diff-match-patch
/* * @Description: 文本对比差异 * @version: 1.0 * @Author: shu * @Date: 2022-05-25 20:02:24 * @LastEditors: 修改者填写 * @LastEditTime: 2022-05-25 21:19:39 */ var diff_match_patch = function() { this.Diff_Timeout = 1; this.Diff_EditCost = 4; this.Match_Threshold = .5; this.Match_Distance = 1E3; this.Patch_DeleteThreshold = .5; this.Patch_Margin = 4; this.Match_MaxBits = 32 } , DIFF_DELETE = -1 , DIFF_INSERT = 1 , DIFF_EQUAL = 0; diff_match_patch.Diff = function(a, b) { this[0] = a; this[1] = b } ; diff_match_patch.Diff.prototype.length = 2; diff_match_patch.Diff.prototype.toString = function() { return this[0] + "," + this[1] } ; diff_match_patch.prototype.diff_main = function(a, b, c, d) { "undefined" == typeof d && (d = 0 >= this.Diff_Timeout ? Number.MAX_VALUE : (new Date).getTime() + 1E3 * this.Diff_Timeout); if (null == a || null == b) throw Error("Null input. (diff_main)"); if (a == b) return a ? [new diff_match_patch.Diff(DIFF_EQUAL,a)] : []; "undefined" == typeof c && (c = !0); var e = c , f = this.diff_commonPrefix(a, b); c = a.substring(0, f); a = a.substring(f); b = b.substring(f); f = this.diff_commonSuffix(a, b); var g = a.substring(a.length - f); a = a.substring(0, a.length - f); b = b.substring(0, b.length - f); a = this.diff_compute_(a, b, e, d); c && a.unshift(new diff_match_patch.Diff(DIFF_EQUAL,c)); g && a.push(new diff_match_patch.Diff(DIFF_EQUAL,g)); this.diff_cleanupMerge(a); return a } ; diff_match_patch.prototype.diff_compute_ = function(a, b, c, d) { if (!a) return [new diff_match_patch.Diff(DIFF_INSERT,b)]; if (!b) return [new diff_match_patch.Diff(DIFF_DELETE,a)]; var e = a.length > b.length ? a : b , f = a.length > b.length ? b : a , g = e.indexOf(f); return -1 != g ? (c = [new diff_match_patch.Diff(DIFF_INSERT,e.substring(0, g)), new diff_match_patch.Diff(DIFF_EQUAL,f), new diff_match_patch.Diff(DIFF_INSERT,e.substring(g + f.length))], a.length > b.length && (c[0][0] = c[2][0] = DIFF_DELETE), c) : 1 == f.length ? [new diff_match_patch.Diff(DIFF_DELETE,a), new diff_match_patch.Diff(DIFF_INSERT,b)] : (e = this.diff_halfMatch_(a, b)) ? (b = e[1], f = e[3], a = e[4], e = this.diff_main(e[0], e[2], c, d), c = this.diff_main(b, f, c, d), e.concat([new diff_match_patch.Diff(DIFF_EQUAL,a)], c)) : c && 100 < a.length && 100 < b.length ? this.diff_lineMode_(a, b, d) : this.diff_bisect_(a, b, d) } ; diff_match_patch.prototype.diff_lineMode_ = function(a, b, c) { var d = this.diff_linesToChars_(a, b); a = d.chars1; b = d.chars2; d = d.lineArray; a = this.diff_main(a, b, !1, c); this.diff_charsToLines_(a, d); this.diff_cleanupSemantic(a); a.push(new diff_match_patch.Diff(DIFF_EQUAL,"")); for (var e = d = b = 0, f = "", g = ""; b < a.length; ) { switch (a[b][0]) { case DIFF_INSERT: e++; g += a[b][1]; break; case DIFF_DELETE: d++; f += a[b][1]; break; case DIFF_EQUAL: if (1 <= d && 1 <= e) { a.splice(b - d - e, d + e); b = b - d - e; d = this.diff_main(f, g, !1, c); for (e = d.length - 1; 0 <= e; e--) a.splice(b, 0, d[e]); b += d.length } d = e = 0; g = f = "" } b++ } a.pop(); return a } ; diff_match_patch.prototype.diff_bisect_ = function(a, b, c) { for (var d = a.length, e = b.length, f = Math.ceil((d + e) / 2), g = 2 * f, h = Array(g), l = Array(g), k = 0; k < g; k++) h[k] = -1, l[k] = -1; h[f + 1] = 0; l[f + 1] = 0; k = d - e; for (var m = 0 != k % 2, p = 0, x = 0, w = 0, q = 0, t = 0; t < f && !((new Date).getTime() > c); t++) { for (var v = -t + p; v <= t - x; v += 2) { var n = f + v; var r = v == -t || v != t && h[n - 1] < h[n + 1] ? h[n + 1] : h[n - 1] + 1; for (var y = r - v; r < d && y < e && a.charAt(r) == b.charAt(y); ) r++, y++; h[n] = r; if (r > d) x += 2; else if (y > e) p += 2; else if (m && (n = f + k - v, 0 <= n && n < g && -1 != l[n])) { var u = d - l[n]; if (r >= u) return this.diff_bisectSplit_(a, b, r, y, c) } } for (v = -t + w; v <= t - q; v += 2) { n = f + v; u = v == -t || v != t && l[n - 1] < l[n + 1] ? l[n + 1] : l[n - 1] + 1; for (r = u - v; u < d && r < e && a.charAt(d - u - 1) == b.charAt(e - r - 1); ) u++, r++; l[n] = u; if (u > d) q += 2; else if (r > e) w += 2; else if (!m && (n = f + k - v, 0 <= n && n < g && -1 != h[n] && (r = h[n], y = f + r - n, u = d - u, r >= u))) return this.diff_bisectSplit_(a, b, r, y, c) } } return [new diff_match_patch.Diff(DIFF_DELETE,a), new diff_match_patch.Diff(DIFF_INSERT,b)] } ; diff_match_patch.prototype.diff_bisectSplit_ = function(a, b, c, d, e) { var f = a.substring(0, c) , g = b.substring(0, d); a = a.substring(c); b = b.substring(d); f = this.diff_main(f, g, !1, e); e = this.diff_main(a, b, !1, e); return f.concat(e) } ; diff_match_patch.prototype.diff_linesToChars_ = function(a, b) { function c(a) { for (var b = "", c = 0, g = -1, h = d.length; g < a.length - 1; ) { g = a.indexOf("\n", c); -1 == g && (g = a.length - 1); var l = a.substring(c, g + 1); (e.hasOwnProperty ? e.hasOwnProperty(l) : void 0 !== e[l]) ? b += String.fromCharCode(e[l]) : (h == f && (l = a.substring(c), g = a.length), b += String.fromCharCode(h), e[l] = h, d[h++] = l); c = g + 1 } return b } var d = [] , e = { }; d[0] = ""; var f = 4E4 , g = c(a); f = 65535; var h = c(b); return { chars1: g, chars2: h, lineArray: d } } ; diff_match_patch.prototype.diff_charsToLines_ = function(a, b) { for (var c = 0; c < a.length; c++) { for (var d = a[c][1], e = [], f = 0; f < d.length; f++) e[f] = b[d.charCodeAt(f)]; a[c][1] = e.join("") } } ; diff_match_patch.prototype.diff_commonPrefix = function(a, b) { if (!a || !b || a.charAt(0) != b.charAt