@haklex/rich-diff 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,28 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Innei
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
23
+
24
+ Additional Terms and Conditions
25
+
26
+ ----------------
27
+
28
+ Use of this software is governed by the terms of MIT and, in addition, by the terms and conditions described in the additional file (ADDITIONAL_TERMS.md). By using this software, you agree to abide by these additional terms and conditions.
package/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # @haklex/rich-diff
2
+
3
+ 编辑器状态差异对比组件。
4
+
5
+ ## 安装
6
+
7
+ ```bash
8
+ pnpm add @haklex/rich-diff @haklex/rich-editor
9
+ ```
10
+
11
+ ## 导出
12
+
13
+ ```ts
14
+ export { RichDiff } from './RichDiff'
15
+ export type { RichDiffProps } from './RichDiff'
16
+ export { computeDiff } from './compute-diff'
17
+ export type { DiffHunk, DiffOpType } from './compute-diff'
18
+ ```
19
+
20
+ ## 使用
21
+
22
+ ```tsx
23
+ import { RichDiff } from '@haklex/rich-diff'
24
+ import '@haklex/rich-diff/style.css'
25
+
26
+ <RichDiff oldValue={oldState} newValue={newState} />
27
+ ```
28
+
29
+ ## License
30
+
31
+ MIT
@@ -0,0 +1,13 @@
1
+ import { RichRendererProps } from '@haklex/rich-editor';
2
+ import { SerializedEditorState } from 'lexical';
3
+ export interface RichDiffProps {
4
+ oldValue: SerializedEditorState;
5
+ newValue: SerializedEditorState;
6
+ variant?: RichRendererProps['variant'];
7
+ theme?: RichRendererProps['theme'];
8
+ rendererConfig?: RichRendererProps['rendererConfig'];
9
+ extraNodes?: RichRendererProps['extraNodes'];
10
+ className?: string;
11
+ }
12
+ export declare function RichDiff({ oldValue, newValue, variant, theme, rendererConfig, extraNodes, className, }: RichDiffProps): import("react/jsx-runtime").JSX.Element;
13
+ //# sourceMappingURL=RichDiff.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"RichDiff.d.ts","sourceRoot":"","sources":["../src/RichDiff.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAA;AAI5D,OAAO,KAAK,EAAE,qBAAqB,EAAyB,MAAM,SAAS,CAAA;AAM3E,MAAM,WAAW,aAAa;IAC5B,QAAQ,EAAE,qBAAqB,CAAA;IAC/B,QAAQ,EAAE,qBAAqB,CAAA;IAC/B,OAAO,CAAC,EAAE,iBAAiB,CAAC,SAAS,CAAC,CAAA;IACtC,KAAK,CAAC,EAAE,iBAAiB,CAAC,OAAO,CAAC,CAAA;IAClC,cAAc,CAAC,EAAE,iBAAiB,CAAC,gBAAgB,CAAC,CAAA;IACpD,UAAU,CAAC,EAAE,iBAAiB,CAAC,YAAY,CAAC,CAAA;IAC5C,SAAS,CAAC,EAAE,MAAM,CAAA;CACnB;AA2ED,wBAAgB,QAAQ,CAAC,EACvB,QAAQ,EACR,QAAQ,EACR,OAAmB,EACnB,KAAe,EACf,cAAc,EACd,UAAU,EACV,SAAS,GACV,EAAE,aAAa,2CA+Df"}
@@ -0,0 +1,8 @@
1
+ import { SerializedEditorState, SerializedLexicalNode } from 'lexical';
2
+ export type DiffOpType = 'equal' | 'insert' | 'delete';
3
+ export interface DiffHunk {
4
+ type: DiffOpType;
5
+ nodes: SerializedLexicalNode[];
6
+ }
7
+ export declare function computeDiff(oldState: SerializedEditorState, newState: SerializedEditorState): DiffHunk[];
8
+ //# sourceMappingURL=compute-diff.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"compute-diff.d.ts","sourceRoot":"","sources":["../src/compute-diff.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,qBAAqB,EAAE,qBAAqB,EAAE,MAAM,SAAS,CAAA;AAE3E,MAAM,MAAM,UAAU,GAAG,OAAO,GAAG,QAAQ,GAAG,QAAQ,CAAA;AAEtD,MAAM,WAAW,QAAQ;IACvB,IAAI,EAAE,UAAU,CAAA;IAChB,KAAK,EAAE,qBAAqB,EAAE,CAAA;CAC/B;AA0fD,wBAAgB,WAAW,CACzB,QAAQ,EAAE,qBAAqB,EAC/B,QAAQ,EAAE,qBAAqB,GAC9B,QAAQ,EAAE,CA0CZ"}
@@ -0,0 +1,5 @@
1
+ export type { DiffHunk, DiffOpType } from './compute-diff';
2
+ export { computeDiff } from './compute-diff';
3
+ export type { RichDiffProps } from './RichDiff';
4
+ export { RichDiff } from './RichDiff';
5
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,aAAa,CAAA;AAEpB,YAAY,EAAE,QAAQ,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAA;AAC1D,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAA;AAC5C,YAAY,EAAE,aAAa,EAAE,MAAM,YAAY,CAAA;AAC/C,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAA"}
package/dist/index.mjs ADDED
@@ -0,0 +1,491 @@
1
+ import { jsx, jsxs } from "react/jsx-runtime";
2
+ import { getVariantClass, ColorSchemeProvider } from "@haklex/rich-editor";
3
+ import { RichRenderer } from "@haklex/rich-editor/renderer";
4
+ import { PortalThemeProvider } from "@haklex/rich-style-token";
5
+ import { useMemo } from "react";
6
+ const CHAR_DIFF_MAX_MATRIX_CELLS = 5e4;
7
+ const DELETE_MARK_STYLE = "background-color: color-mix(in srgb, var(--rc-alert-caution) 22%, transparent); text-decoration: line-through;";
8
+ const INSERT_MARK_STYLE = "background-color: color-mix(in srgb, var(--rc-alert-tip) 22%, transparent);";
9
+ function getNodeTypeKey(node) {
10
+ const n = node;
11
+ if (n.type === "heading") return `heading:${n.tag}`;
12
+ if (n.type === "list") return `list:${n.listType}`;
13
+ return n.type || "unknown";
14
+ }
15
+ function nodesEqual(a, b) {
16
+ return JSON.stringify(a) === JSON.stringify(b);
17
+ }
18
+ function isTextNode(node) {
19
+ const n = node;
20
+ return n.type === "text" && typeof n.text === "string";
21
+ }
22
+ function getChildren(node) {
23
+ const n = node;
24
+ return Array.isArray(n.children) ? n.children : null;
25
+ }
26
+ function appendStyle(baseStyle, extraStyle) {
27
+ const normalizedBase = typeof baseStyle === "string" ? baseStyle.trim() : "";
28
+ if (!normalizedBase) return extraStyle;
29
+ return `${normalizedBase}${normalizedBase.endsWith(";") ? "" : ";"} ${extraStyle}`;
30
+ }
31
+ function cloneTextNode(node, text, markKind) {
32
+ let { style } = node;
33
+ if (markKind === "delete") {
34
+ style = appendStyle(style, DELETE_MARK_STYLE);
35
+ } else if (markKind === "insert") {
36
+ style = appendStyle(style, INSERT_MARK_STYLE);
37
+ }
38
+ const cloned = {
39
+ ...node,
40
+ text,
41
+ style
42
+ };
43
+ return cloned;
44
+ }
45
+ function cloneNodeWithChildren(node, children) {
46
+ return {
47
+ ...node,
48
+ children
49
+ };
50
+ }
51
+ function decorateSubtree(node, kind) {
52
+ if (isTextNode(node)) {
53
+ return cloneTextNode(node, node.text, kind);
54
+ }
55
+ const children = getChildren(node);
56
+ if (!children) return node;
57
+ return cloneNodeWithChildren(
58
+ node,
59
+ children.map((child) => decorateSubtree(child, kind))
60
+ );
61
+ }
62
+ function reverseText(value) {
63
+ return Array.from(value).reverse().join("");
64
+ }
65
+ function mergeTextOps(ops) {
66
+ const merged = [];
67
+ for (const op of ops) {
68
+ if (!op.text) continue;
69
+ const last = merged.at(-1);
70
+ if (last && last.kind === op.kind) {
71
+ last.text += op.text;
72
+ } else {
73
+ merged.push({ ...op });
74
+ }
75
+ }
76
+ return merged;
77
+ }
78
+ function diffMiddleChars(oldChars, newChars) {
79
+ const m = oldChars.length;
80
+ const n = newChars.length;
81
+ if (m === 0 && n === 0) return [];
82
+ if (m === 0) return [{ kind: "insert", text: newChars.join("") }];
83
+ if (n === 0) return [{ kind: "delete", text: oldChars.join("") }];
84
+ if (m * n > CHAR_DIFF_MAX_MATRIX_CELLS) {
85
+ return [
86
+ { kind: "delete", text: oldChars.join("") },
87
+ { kind: "insert", text: newChars.join("") }
88
+ ];
89
+ }
90
+ const dp = Array.from(
91
+ { length: m + 1 },
92
+ () => new Array(n + 1).fill(0)
93
+ );
94
+ for (let i2 = 1; i2 <= m; i2++) {
95
+ for (let j2 = 1; j2 <= n; j2++) {
96
+ if (oldChars[i2 - 1] === newChars[j2 - 1]) {
97
+ dp[i2][j2] = dp[i2 - 1][j2 - 1] + 1;
98
+ } else {
99
+ dp[i2][j2] = Math.max(dp[i2 - 1][j2], dp[i2][j2 - 1]);
100
+ }
101
+ }
102
+ }
103
+ const reversedOps = [];
104
+ let i = m;
105
+ let j = n;
106
+ while (i > 0 && j > 0) {
107
+ if (oldChars[i - 1] === newChars[j - 1]) {
108
+ const last = reversedOps.at(-1);
109
+ if (last && last.kind === "equal") {
110
+ last.text += oldChars[i - 1];
111
+ } else {
112
+ reversedOps.push({ kind: "equal", text: oldChars[i - 1] });
113
+ }
114
+ i--;
115
+ j--;
116
+ continue;
117
+ }
118
+ if (dp[i - 1][j] >= dp[i][j - 1]) {
119
+ const last = reversedOps.at(-1);
120
+ if (last && last.kind === "delete") {
121
+ last.text += oldChars[i - 1];
122
+ } else {
123
+ reversedOps.push({ kind: "delete", text: oldChars[i - 1] });
124
+ }
125
+ i--;
126
+ } else {
127
+ const last = reversedOps.at(-1);
128
+ if (last && last.kind === "insert") {
129
+ last.text += newChars[j - 1];
130
+ } else {
131
+ reversedOps.push({ kind: "insert", text: newChars[j - 1] });
132
+ }
133
+ j--;
134
+ }
135
+ }
136
+ while (i > 0) {
137
+ const last = reversedOps.at(-1);
138
+ if (last && last.kind === "delete") {
139
+ last.text += oldChars[i - 1];
140
+ } else {
141
+ reversedOps.push({ kind: "delete", text: oldChars[i - 1] });
142
+ }
143
+ i--;
144
+ }
145
+ while (j > 0) {
146
+ const last = reversedOps.at(-1);
147
+ if (last && last.kind === "insert") {
148
+ last.text += newChars[j - 1];
149
+ } else {
150
+ reversedOps.push({ kind: "insert", text: newChars[j - 1] });
151
+ }
152
+ j--;
153
+ }
154
+ const forwardOps = reversedOps.reverse().map((op) => ({ kind: op.kind, text: reverseText(op.text) }));
155
+ return mergeTextOps(forwardOps);
156
+ }
157
+ function diffTextByChar(oldText, newText) {
158
+ const oldChars = Array.from(oldText);
159
+ const newChars = Array.from(newText);
160
+ let prefix = 0;
161
+ while (prefix < oldChars.length && prefix < newChars.length && oldChars[prefix] === newChars[prefix]) {
162
+ prefix++;
163
+ }
164
+ let oldSuffix = oldChars.length - 1;
165
+ let newSuffix = newChars.length - 1;
166
+ while (oldSuffix >= prefix && newSuffix >= prefix && oldChars[oldSuffix] === newChars[newSuffix]) {
167
+ oldSuffix--;
168
+ newSuffix--;
169
+ }
170
+ const ops = [];
171
+ if (prefix > 0) {
172
+ ops.push({
173
+ kind: "equal",
174
+ text: oldChars.slice(0, prefix).join("")
175
+ });
176
+ }
177
+ ops.push(
178
+ ...diffMiddleChars(
179
+ oldChars.slice(prefix, oldSuffix + 1),
180
+ newChars.slice(prefix, newSuffix + 1)
181
+ )
182
+ );
183
+ if (oldSuffix < oldChars.length - 1) {
184
+ ops.push({
185
+ kind: "equal",
186
+ text: oldChars.slice(oldSuffix + 1).join("")
187
+ });
188
+ }
189
+ return mergeTextOps(ops);
190
+ }
191
+ function splitTextNodeByCharDiff(oldNode, newNode) {
192
+ const ops = diffTextByChar(oldNode.text, newNode.text);
193
+ const oldNodes = [];
194
+ const newNodes = [];
195
+ let changed = false;
196
+ for (const op of ops) {
197
+ if (!op.text) continue;
198
+ if (op.kind === "equal") {
199
+ oldNodes.push(cloneTextNode(oldNode, op.text));
200
+ newNodes.push(cloneTextNode(newNode, op.text));
201
+ continue;
202
+ }
203
+ changed = true;
204
+ if (op.kind === "delete") {
205
+ oldNodes.push(cloneTextNode(oldNode, op.text, "delete"));
206
+ continue;
207
+ }
208
+ newNodes.push(cloneTextNode(newNode, op.text, "insert"));
209
+ }
210
+ return {
211
+ oldNodes: oldNodes.length > 0 ? oldNodes : [oldNode],
212
+ newNodes: newNodes.length > 0 ? newNodes : [newNode],
213
+ changed
214
+ };
215
+ }
216
+ function alignNodes(oldNodes, newNodes) {
217
+ const m = oldNodes.length;
218
+ const n = newNodes.length;
219
+ const dp = Array.from(
220
+ { length: m + 1 },
221
+ () => new Array(n + 1).fill(0)
222
+ );
223
+ for (let i2 = 1; i2 <= m; i2++) {
224
+ for (let j2 = 1; j2 <= n; j2++) {
225
+ const skip = Math.max(dp[i2 - 1][j2], dp[i2][j2 - 1]);
226
+ if (getNodeTypeKey(oldNodes[i2 - 1]) === getNodeTypeKey(newNodes[j2 - 1])) {
227
+ const score = nodesEqual(oldNodes[i2 - 1], newNodes[j2 - 1]) ? 2 : 1;
228
+ dp[i2][j2] = Math.max(skip, dp[i2 - 1][j2 - 1] + score);
229
+ } else {
230
+ dp[i2][j2] = skip;
231
+ }
232
+ }
233
+ }
234
+ const ops = [];
235
+ let i = m;
236
+ let j = n;
237
+ while (i > 0 && j > 0) {
238
+ const typeMatch = getNodeTypeKey(oldNodes[i - 1]) === getNodeTypeKey(newNodes[j - 1]);
239
+ if (typeMatch) {
240
+ const exact = nodesEqual(oldNodes[i - 1], newNodes[j - 1]);
241
+ const score = exact ? 2 : 1;
242
+ if (dp[i][j] === dp[i - 1][j - 1] + score) {
243
+ ops.push(
244
+ exact ? { kind: "equal", node: oldNodes[i - 1] } : {
245
+ kind: "modify",
246
+ oldNode: oldNodes[i - 1],
247
+ newNode: newNodes[j - 1]
248
+ }
249
+ );
250
+ i--;
251
+ j--;
252
+ continue;
253
+ }
254
+ }
255
+ if (dp[i - 1][j] >= dp[i][j - 1]) {
256
+ ops.push({ kind: "delete", node: oldNodes[i - 1] });
257
+ i--;
258
+ } else {
259
+ ops.push({ kind: "insert", node: newNodes[j - 1] });
260
+ j--;
261
+ }
262
+ }
263
+ while (i > 0) {
264
+ ops.push({ kind: "delete", node: oldNodes[--i] });
265
+ }
266
+ while (j > 0) {
267
+ ops.push({ kind: "insert", node: newNodes[--j] });
268
+ }
269
+ ops.reverse();
270
+ return ops;
271
+ }
272
+ function diffChildrenInline(oldChildren, newChildren) {
273
+ const ops = alignNodes(oldChildren, newChildren);
274
+ const nextOldChildren = [];
275
+ const nextNewChildren = [];
276
+ let changed = false;
277
+ for (const op of ops) {
278
+ switch (op.kind) {
279
+ case "equal":
280
+ nextOldChildren.push(op.node);
281
+ nextNewChildren.push(op.node);
282
+ break;
283
+ case "delete":
284
+ changed = true;
285
+ nextOldChildren.push(decorateSubtree(op.node, "delete"));
286
+ break;
287
+ case "insert":
288
+ changed = true;
289
+ nextNewChildren.push(decorateSubtree(op.node, "insert"));
290
+ break;
291
+ case "modify": {
292
+ changed = true;
293
+ if (isTextNode(op.oldNode) && isTextNode(op.newNode)) {
294
+ const textDiff = splitTextNodeByCharDiff(op.oldNode, op.newNode);
295
+ nextOldChildren.push(...textDiff.oldNodes);
296
+ nextNewChildren.push(...textDiff.newNodes);
297
+ break;
298
+ }
299
+ const nested = diffNodeInline(op.oldNode, op.newNode);
300
+ if (nested) {
301
+ nextOldChildren.push(nested.oldNode);
302
+ nextNewChildren.push(nested.newNode);
303
+ } else {
304
+ nextOldChildren.push(decorateSubtree(op.oldNode, "delete"));
305
+ nextNewChildren.push(decorateSubtree(op.newNode, "insert"));
306
+ }
307
+ break;
308
+ }
309
+ }
310
+ }
311
+ return {
312
+ oldChildren: nextOldChildren,
313
+ newChildren: nextNewChildren,
314
+ changed
315
+ };
316
+ }
317
+ function diffNodeInline(oldNode, newNode) {
318
+ if (getNodeTypeKey(oldNode) !== getNodeTypeKey(newNode)) return null;
319
+ if (nodesEqual(oldNode, newNode)) {
320
+ return { oldNode, newNode, changed: false };
321
+ }
322
+ const oldChildren = getChildren(oldNode);
323
+ const newChildren = getChildren(newNode);
324
+ if (!oldChildren || !newChildren) return null;
325
+ const childDiff = diffChildrenInline(oldChildren, newChildren);
326
+ if (!childDiff.changed) {
327
+ return { oldNode, newNode, changed: false };
328
+ }
329
+ return {
330
+ oldNode: cloneNodeWithChildren(oldNode, childDiff.oldChildren),
331
+ newNode: cloneNodeWithChildren(newNode, childDiff.newChildren),
332
+ changed: true
333
+ };
334
+ }
335
+ function diffModifiedNode(oldNode, newNode) {
336
+ const nested = diffNodeInline(oldNode, newNode);
337
+ if (nested) {
338
+ return { oldNode: nested.oldNode, newNode: nested.newNode };
339
+ }
340
+ return {
341
+ oldNode: decorateSubtree(oldNode, "delete"),
342
+ newNode: decorateSubtree(newNode, "insert")
343
+ };
344
+ }
345
+ function computeDiff(oldState, newState) {
346
+ const ops = alignNodes(oldState.root.children, newState.root.children);
347
+ const hunks = [];
348
+ for (const op of ops) {
349
+ switch (op.kind) {
350
+ case "equal": {
351
+ const last = hunks.at(-1);
352
+ if (last && last.type === "equal") {
353
+ last.nodes.push(op.node);
354
+ } else {
355
+ hunks.push({ type: "equal", nodes: [op.node] });
356
+ }
357
+ break;
358
+ }
359
+ case "modify":
360
+ {
361
+ const inline = diffModifiedNode(op.oldNode, op.newNode);
362
+ hunks.push(
363
+ { type: "delete", nodes: [inline.oldNode] },
364
+ { type: "insert", nodes: [inline.newNode] }
365
+ );
366
+ }
367
+ break;
368
+ case "delete":
369
+ hunks.push({
370
+ type: "delete",
371
+ nodes: [decorateSubtree(op.node, "delete")]
372
+ });
373
+ break;
374
+ case "insert":
375
+ hunks.push({
376
+ type: "insert",
377
+ nodes: [decorateSubtree(op.node, "insert")]
378
+ });
379
+ break;
380
+ }
381
+ }
382
+ return hunks;
383
+ }
384
+ function buildRows(hunks) {
385
+ const rows = [];
386
+ let i = 0;
387
+ while (i < hunks.length) {
388
+ const hunk = hunks[i];
389
+ if (hunk.type === "equal") {
390
+ rows.push({ left: hunk, right: hunk });
391
+ i++;
392
+ } else {
393
+ const deletes = [];
394
+ const inserts = [];
395
+ while (i < hunks.length && hunks[i].type !== "equal") {
396
+ if (hunks[i].type === "delete") deletes.push(hunks[i]);
397
+ else inserts.push(hunks[i]);
398
+ i++;
399
+ }
400
+ const maxLen = Math.max(deletes.length, inserts.length);
401
+ for (let k = 0; k < maxLen; k++) {
402
+ rows.push({
403
+ left: k < deletes.length ? deletes[k] : null,
404
+ right: k < inserts.length ? inserts[k] : null
405
+ });
406
+ }
407
+ }
408
+ }
409
+ return rows;
410
+ }
411
+ function wrapDoc(nodes) {
412
+ return {
413
+ root: {
414
+ children: nodes,
415
+ direction: "ltr",
416
+ format: "",
417
+ indent: 0,
418
+ type: "root",
419
+ version: 1
420
+ }
421
+ };
422
+ }
423
+ function HunkRenderer({
424
+ hunk,
425
+ variant,
426
+ theme,
427
+ rendererConfig,
428
+ extraNodes
429
+ }) {
430
+ const doc = useMemo(() => wrapDoc(hunk.nodes), [hunk.nodes]);
431
+ return /* @__PURE__ */ jsx(
432
+ RichRenderer,
433
+ {
434
+ value: doc,
435
+ variant,
436
+ theme,
437
+ rendererConfig,
438
+ extraNodes
439
+ }
440
+ );
441
+ }
442
+ function RichDiff({
443
+ oldValue,
444
+ newValue,
445
+ variant = "article",
446
+ theme = "light",
447
+ rendererConfig,
448
+ extraNodes,
449
+ className
450
+ }) {
451
+ const hunks = useMemo(
452
+ () => computeDiff(oldValue, newValue),
453
+ [oldValue, newValue]
454
+ );
455
+ const rows = useMemo(() => buildRows(hunks), [hunks]);
456
+ const rendererProps = { variant, theme, rendererConfig, extraNodes };
457
+ const variantClass = getVariantClass(variant, theme);
458
+ return /* @__PURE__ */ jsx(PortalThemeProvider, { className: variantClass, children: /* @__PURE__ */ jsx(ColorSchemeProvider, { colorScheme: theme, children: /* @__PURE__ */ jsx(
459
+ "div",
460
+ {
461
+ className: variantClass,
462
+ style: { width: "100%", maxWidth: "100%" },
463
+ children: /* @__PURE__ */ jsxs("div", { className: `rich-diff ${className || ""}`, children: [
464
+ /* @__PURE__ */ jsxs("div", { className: "rich-diff-header", children: [
465
+ /* @__PURE__ */ jsx("div", { className: "rich-diff-header-cell rich-diff-header-old", children: "Old" }),
466
+ /* @__PURE__ */ jsx("div", { className: "rich-diff-header-cell rich-diff-header-new", children: "New" })
467
+ ] }),
468
+ /* @__PURE__ */ jsx("div", { className: "rich-diff-body", children: rows.map((row, i) => /* @__PURE__ */ jsxs("div", { className: "rich-diff-row", children: [
469
+ /* @__PURE__ */ jsx(
470
+ "div",
471
+ {
472
+ className: `rich-diff-cell rich-diff-cell-old ${row.left ? row.left.type === "delete" ? "rich-diff-delete" : "" : "rich-diff-empty"}`,
473
+ children: row.left && /* @__PURE__ */ jsx(HunkRenderer, { hunk: row.left, ...rendererProps })
474
+ }
475
+ ),
476
+ /* @__PURE__ */ jsx(
477
+ "div",
478
+ {
479
+ className: `rich-diff-cell rich-diff-cell-new ${row.right ? row.right.type === "insert" ? "rich-diff-insert" : "" : "rich-diff-empty"}`,
480
+ children: row.right && /* @__PURE__ */ jsx(HunkRenderer, { hunk: row.right, ...rendererProps })
481
+ }
482
+ )
483
+ ] }, i)) })
484
+ ] })
485
+ }
486
+ ) }) });
487
+ }
488
+ export {
489
+ RichDiff,
490
+ computeDiff
491
+ };
@@ -0,0 +1,67 @@
1
+ .rich-diff {
2
+ border: 1px solid var(--rc-border);
3
+ border-radius: 6px;
4
+ overflow: hidden;
5
+ font-size: 14px;
6
+ }
7
+
8
+ .rich-diff-header {
9
+ display: grid;
10
+ grid-template-columns: 1fr 1fr;
11
+ border-bottom: 1px solid var(--rc-border);
12
+ background: var(--rc-bg-secondary);
13
+ }
14
+
15
+ .rich-diff-header-cell {
16
+ padding: 8px 16px;
17
+ font-weight: 600;
18
+ font-size: 12px;
19
+ text-transform: uppercase;
20
+ letter-spacing: 0.05em;
21
+ color: var(--rc-text-secondary);
22
+ }
23
+
24
+ .rich-diff-header-old {
25
+ border-right: 1px solid var(--rc-border);
26
+ }
27
+
28
+ .rich-diff-body {
29
+ display: flex;
30
+ flex-direction: column;
31
+ }
32
+
33
+ .rich-diff-row {
34
+ display: grid;
35
+ grid-template-columns: 1fr 1fr;
36
+ border-bottom: 1px solid var(--rc-border);
37
+ }
38
+
39
+ .rich-diff-row:last-child {
40
+ border-bottom: none;
41
+ }
42
+
43
+ .rich-diff-cell {
44
+ padding: 4px 12px;
45
+ min-height: 32px;
46
+ overflow: hidden;
47
+ }
48
+
49
+ .rich-diff-cell-old {
50
+ border-right: 1px solid var(--rc-border);
51
+ }
52
+
53
+ .rich-diff-delete {
54
+ background-color: color-mix(in srgb, var(--rc-alert-caution) 15%, transparent);
55
+ }
56
+
57
+ .rich-diff-delete .rich-content__body {
58
+ opacity: 0.85;
59
+ }
60
+
61
+ .rich-diff-insert {
62
+ background-color: color-mix(in srgb, var(--rc-alert-tip) 15%, transparent);
63
+ }
64
+
65
+ .rich-diff-empty {
66
+ background-color: var(--rc-bg-secondary);
67
+ }
package/package.json ADDED
@@ -0,0 +1,44 @@
1
+ {
2
+ "name": "@haklex/rich-diff",
3
+ "version": "0.0.1",
4
+ "description": "Rich diff editor",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "exports": {
8
+ ".": {
9
+ "import": "./dist/index.mjs",
10
+ "types": "./dist/index.d.ts"
11
+ },
12
+ "./style.css": "./dist/rich-diff.css"
13
+ },
14
+ "main": "./dist/index.mjs",
15
+ "files": [
16
+ "dist"
17
+ ],
18
+ "devDependencies": {
19
+ "@types/react": "^19.0.0",
20
+ "@types/react-dom": "^19.0.0",
21
+ "lexical": "^0.39.0",
22
+ "react": ">=19",
23
+ "react-dom": ">=19",
24
+ "typescript": "^5.9.0",
25
+ "vite": "^7.3.1",
26
+ "vite-plugin-dts": "^4.5.0",
27
+ "@haklex/rich-editor": "0.0.1",
28
+ "@haklex/rich-style-token": "0.0.1"
29
+ },
30
+ "peerDependencies": {
31
+ "react": ">=19",
32
+ "react-dom": ">=19",
33
+ "@haklex/rich-editor": "0.0.1",
34
+ "@haklex/rich-style-token": "0.0.1"
35
+ },
36
+ "publishConfig": {
37
+ "access": "public"
38
+ },
39
+ "scripts": {
40
+ "build": "vite build",
41
+ "dev:build": "vite build --watch"
42
+ },
43
+ "types": "./dist/index.d.ts"
44
+ }