iter(g269): 新增文本差异对比组件
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
import { h } from '@ui-arch/frame';
|
||||
import type { DiffViewProps } from './types.js';
|
||||
|
||||
// 一条差异:same 原样、add 只在右边、del 只在左边
|
||||
type DiffOp = { type: 'same' | 'add' | 'del'; text: string };
|
||||
// LCS 计算上限:中间段行数乘积超过这个值就退化为「整段替换」,避免大文件把主线程卡住
|
||||
const DP_LIMIT = 400_000;
|
||||
|
||||
/*** 切行:末尾换行不产生一行空内容(否则每个文件都会多出一条假的空行差异) ***/
|
||||
function toLines(text: string): string[] {
|
||||
const lines = text.split(/\r?\n/);
|
||||
if (lines.length > 1 && lines[lines.length - 1] === '') lines.pop(); // 尾部换行忽略
|
||||
return lines;
|
||||
}
|
||||
|
||||
/*** 中间段做 LCS 对齐:先剪掉公共前后缀,剩下的才真算,日常改动只占几行 ***/
|
||||
function alignMiddle(a: string[], b: string[]): DiffOp[] {
|
||||
if (!a.length || !b.length) return [...a.map((text) => ({ type: 'del' as const, text })), ...b.map((text) => ({ type: 'add' as const, text }))];
|
||||
if (a.length * b.length > DP_LIMIT) return [...a.map((text) => ({ type: 'del' as const, text })), ...b.map((text) => ({ type: 'add' as const, text }))]; // 超限:整段替换,语义仍正确
|
||||
const dp: number[][] = Array.from({ length: a.length + 1 }, () => new Array<number>(b.length + 1).fill(0)); // dp[i][j]:a 前 i 行与 b 前 j 行的 LCS 长度
|
||||
for (let i = a.length - 1; i >= 0; i -= 1) {
|
||||
for (let j = b.length - 1; j >= 0; j -= 1) {
|
||||
dp[i][j] = a[i] === b[j] ? dp[i + 1][j + 1] + 1 : Math.max(dp[i + 1][j], dp[i][j + 1]);
|
||||
}
|
||||
}
|
||||
const ops: DiffOp[] = [];
|
||||
let i = 0;
|
||||
let j = 0;
|
||||
while (i < a.length && j < b.length) {
|
||||
if (a[i] === b[j]) { ops.push({ type: 'same', text: a[i] }); i += 1; j += 1; continue; }
|
||||
if (dp[i + 1][j] >= dp[i][j + 1]) { ops.push({ type: 'del', text: a[i] }); i += 1; continue; }
|
||||
ops.push({ type: 'add', text: b[j] }); j += 1;
|
||||
}
|
||||
for (; i < a.length; i += 1) ops.push({ type: 'del', text: a[i] });
|
||||
for (; j < b.length; j += 1) ops.push({ type: 'add', text: b[j] });
|
||||
return ops;
|
||||
}
|
||||
|
||||
/*** 逐行差异:剪公共前后缀后对齐中间段,输出完整 same / add / del 序列 ***/
|
||||
function diffLines(before: string, after: string): DiffOp[] {
|
||||
const a = toLines(before);
|
||||
const b = toLines(after);
|
||||
let head = 0;
|
||||
while (head < a.length && head < b.length && a[head] === b[head]) head += 1; // 公共前缀
|
||||
let tail = 0;
|
||||
while (tail < a.length - head && tail < b.length - head && a[a.length - 1 - tail] === b[b.length - 1 - tail]) tail += 1; // 公共后缀
|
||||
const midA = a.slice(head, a.length - tail);
|
||||
const midB = b.slice(head, b.length - tail);
|
||||
return [
|
||||
...a.slice(0, head).map((text) => ({ type: 'same' as const, text })),
|
||||
...alignMiddle(midA, midB),
|
||||
...a.slice(a.length - tail).map((text) => ({ type: 'same' as const, text })),
|
||||
];
|
||||
}
|
||||
|
||||
/*** 文本差异对比:行级对齐 + 增删着色 + 双行号列,超限时退化为整段替换(只影响精度不影响语义) ***/
|
||||
export function diffView(props: DiffViewProps): HTMLElement {
|
||||
const ops = diffLines(props.before, props.after);
|
||||
const added = ops.filter((o) => o.type === 'add').length; // 新增行数:统计条与读屏共用
|
||||
const removed = ops.filter((o) => o.type === 'del').length; // 删除行数
|
||||
let oldNo = 0;
|
||||
let newNo = 0;
|
||||
const rows = ops.map((op) => {
|
||||
if (op.type !== 'add') oldNo += 1; // same 与 del 占旧行号
|
||||
if (op.type !== 'del') newNo += 1; // same 与 add 占新行号
|
||||
const sign = op.type === 'add' ? '+' : op.type === 'del' ? '−' : ' ';
|
||||
return h(
|
||||
'div',
|
||||
{ class: `ua-diff-row is-${op.type}` },
|
||||
h('span', { class: 'ua-diff-no' }, op.type === 'add' ? '' : String(oldNo)),
|
||||
h('span', { class: 'ua-diff-no' }, op.type === 'del' ? '' : String(newNo)),
|
||||
h('span', { class: 'ua-diff-sign', 'aria-hidden': 'true' }, sign),
|
||||
h('span', { class: 'ua-diff-text' }, op.text || ' '),
|
||||
);
|
||||
});
|
||||
const stats = props.showStats === false
|
||||
? null
|
||||
: h('div', { class: 'ua-diff-stats' }, h('span', { class: 'ua-diff-add' }, `+${added}`), h('span', { class: 'ua-diff-del' }, `−${removed}`));
|
||||
return h(
|
||||
'div',
|
||||
{ class: 'ua-diff', role: 'group', 'aria-label': props.ariaLabel ?? `文本差异:新增 ${added} 行,删除 ${removed} 行` },
|
||||
stats,
|
||||
h('div', { class: 'ua-diff-body' }, ...rows),
|
||||
);
|
||||
}
|
||||
@@ -78,3 +78,4 @@ export { highlight } from './highlight.js';
|
||||
export { inputTag } from './inputTag.js';
|
||||
export { layout } from './layout.js';
|
||||
export { jsonViewer } from './jsonViewer.js';
|
||||
export { diffView } from './diffView.js';
|
||||
|
||||
@@ -1030,3 +1030,11 @@ export interface JsonViewerProps {
|
||||
maxHeight?: number; // 最高高度(px),超出在框内滚动;缺省不限高
|
||||
ariaLabel?: string; // 可访问名;缺省「JSON 数据」
|
||||
}
|
||||
|
||||
/*** 文本差异对比入参 ***/
|
||||
export interface DiffViewProps {
|
||||
before: string; // 旧文本(按行比对,CRLF 与 LF 等价)
|
||||
after: string; // 新文本
|
||||
showStats?: boolean; // 是否在顶部显示 +N / −N 统计条;缺省 true
|
||||
ariaLabel?: string; // 可访问名;缺省自动播报「新增 N 行,删除 M 行」
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user