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 行」
|
||||
}
|
||||
|
||||
@@ -92,7 +92,8 @@
|
||||
"highlight(props):关键词高亮,命中片段包 mark(大小写不敏感全部命中),text / keyword 支持信号响应式刷新",
|
||||
"inputTag(props):标签输入,输入后回车 / 逗号成标签、chip 可单个删除、空框退格删末个,value 信号双向绑定,max 满额自动禁输",
|
||||
"layout(props, ...children):布局骨架,页头 / 侧栏 / 内容 / 页脚四区一次成型,侧栏可折叠(collapsed 信号双向绑定)",
|
||||
"jsonViewer(props):JSON 树查看,按层折叠、分支带计数摘要、叶子按类型着色,只读不编辑"
|
||||
"jsonViewer(props):JSON 树查看,按层折叠、分支带计数摘要、叶子按类型着色,只读不编辑",
|
||||
"diffView(props):文本差异对比,行级 LCS 对齐 + 增删着色 + 双行号列,顶部带 +N / −N 统计"
|
||||
],
|
||||
"keyTypes": [
|
||||
"ButtonProps",
|
||||
@@ -4075,6 +4076,41 @@
|
||||
"数组下标与对象键都走同一套行渲染,收起时右侧显示「3 项 / 2 个键」,先看规模再决定要不要展开",
|
||||
"要展示编辑结果时不要用它(只读):输入侧请用 textarea,展示侧再交给 jsonViewer"
|
||||
]
|
||||
},
|
||||
{
|
||||
"id": "diffView",
|
||||
"title": "文本差异对比 DiffView",
|
||||
"demo": "sec-diff",
|
||||
"props": [
|
||||
{
|
||||
"n": "before",
|
||||
"t": "string",
|
||||
"d": "旧文本(按行比对,CRLF 与 LF 等价)"
|
||||
},
|
||||
{
|
||||
"n": "after",
|
||||
"t": "string",
|
||||
"d": "新文本"
|
||||
},
|
||||
{
|
||||
"n": "showStats",
|
||||
"t": "boolean",
|
||||
"d": "是否在顶部显示 +N / −N 统计条;缺省 true"
|
||||
},
|
||||
{
|
||||
"n": "ariaLabel",
|
||||
"t": "string",
|
||||
"d": "可访问名;缺省自动播报「新增 N 行,删除 M 行」"
|
||||
}
|
||||
],
|
||||
"example": "diffView({ before: oldText, after: newText })",
|
||||
"steps": [
|
||||
"把改前 / 改后两份文本整串交给 before 与 after:不用先切行,CRLF 与 LF 视为同一行",
|
||||
"行级对齐先剪公共前后缀、再对中间段做 LCS:改两行不会把整篇标成差异",
|
||||
"增删行同时有底色与行首 + / − 符号两条通道,色盲用户也能分辨;行号列不参与复制",
|
||||
"顶部统计条给 +N / −N,读屏在容器上直接播报同一组数字,不必逐行听",
|
||||
"超大文本(中间段行数乘积超过 40 万)会退化成「整段替换」——语义仍对,只是不再逐行对齐;要逐行对齐就先分段比对"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
+75
-1
@@ -4994,7 +4994,8 @@ h5.ua-type-title { font-size: var(--ua-font-size-md); }
|
||||
.ua-cmdk-list,
|
||||
.ua-timepicker-col,
|
||||
.ua-layout-content,
|
||||
.ua-json {
|
||||
.ua-json,
|
||||
.ua-diff {
|
||||
scrollbar-width: thin; /* 标准属性:Firefox / Chromium 121+ 生效,细条不挤内容 */
|
||||
scrollbar-color: var(--ua-color-scrollbar) transparent; /* 拇指走令牌、轨道透明,避免画出方块底 */
|
||||
}
|
||||
@@ -5555,3 +5556,76 @@ h5.ua-type-title { font-size: var(--ua-font-size-md); }
|
||||
color: GrayText;
|
||||
}
|
||||
}
|
||||
|
||||
/* ---- 文本差异对比:增删只靠底色 + 符号双通道,色盲用户也能分辨(符号列 aria-hidden,读屏靠底色语义类) ---- */
|
||||
.ua-diff {
|
||||
overflow: auto;
|
||||
border: 1px solid var(--ua-border-subtle);
|
||||
border-radius: var(--ua-radius-md);
|
||||
background: var(--ua-color-surface);
|
||||
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; /* 等宽:差异对齐才看得出来 */
|
||||
font-size: var(--ua-font-size-sm);
|
||||
line-height: var(--ua-leading-normal);
|
||||
}
|
||||
|
||||
.ua-diff-stats {
|
||||
display: flex;
|
||||
gap: var(--ua-space-3);
|
||||
padding: var(--ua-space-2) var(--ua-space-3);
|
||||
border-bottom: 1px solid var(--ua-border-subtle);
|
||||
background: var(--ua-color-surface-inset);
|
||||
font-size: var(--ua-font-size-xs);
|
||||
}
|
||||
|
||||
/* 统计条两个数字与行底色同源:+ 走成功色、− 走危险色,不另造一套色 */
|
||||
.ua-diff-add {
|
||||
color: var(--ua-color-success);
|
||||
}
|
||||
|
||||
.ua-diff-del {
|
||||
color: var(--ua-color-danger);
|
||||
}
|
||||
|
||||
.ua-diff-row {
|
||||
display: flex;
|
||||
align-items: baseline;
|
||||
gap: var(--ua-space-2);
|
||||
padding: 0 var(--ua-space-3);
|
||||
white-space: pre; /* 缩进与空格是差异的一部分,不能被折行压平 */
|
||||
}
|
||||
|
||||
.ua-diff-no {
|
||||
flex: 0 0 auto;
|
||||
min-width: var(--ua-space-6);
|
||||
color: var(--ua-color-text-faint);
|
||||
font-size: var(--ua-font-size-xs);
|
||||
text-align: right;
|
||||
user-select: none; /* 行号不进复制内容:复制差异时只带走代码 */
|
||||
}
|
||||
|
||||
.ua-diff-sign {
|
||||
flex: 0 0 auto;
|
||||
width: var(--ua-space-3);
|
||||
color: var(--ua-color-text-faint);
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.ua-diff-text {
|
||||
flex: 1 1 auto;
|
||||
}
|
||||
|
||||
.ua-diff-row.is-add {
|
||||
background: color-mix(in srgb, var(--ua-color-success) var(--ua-alpha-tint), transparent);
|
||||
}
|
||||
|
||||
.ua-diff-row.is-add .ua-diff-sign {
|
||||
color: var(--ua-color-success);
|
||||
}
|
||||
|
||||
.ua-diff-row.is-del {
|
||||
background: color-mix(in srgb, var(--ua-color-danger) var(--ua-alpha-tint), transparent);
|
||||
}
|
||||
|
||||
.ua-diff-row.is-del .ua-diff-sign {
|
||||
color: var(--ua-color-danger);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user