@bochenw/react-diff-viewer-continued 4.3.0

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.
Files changed (42) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +521 -0
  3. package/lib/cjs/src/comment-row.d.ts +33 -0
  4. package/lib/cjs/src/comment-row.js +58 -0
  5. package/lib/cjs/src/compute-hidden-blocks.d.ts +13 -0
  6. package/lib/cjs/src/compute-hidden-blocks.js +36 -0
  7. package/lib/cjs/src/compute-lines.d.ts +68 -0
  8. package/lib/cjs/src/compute-lines.js +559 -0
  9. package/lib/cjs/src/computeWorker.d.ts +1 -0
  10. package/lib/cjs/src/computeWorker.js +10 -0
  11. package/lib/cjs/src/diff-row.d.ts +40 -0
  12. package/lib/cjs/src/diff-row.js +136 -0
  13. package/lib/cjs/src/expand.d.ts +1 -0
  14. package/lib/cjs/src/expand.js +4 -0
  15. package/lib/cjs/src/fold.d.ts +1 -0
  16. package/lib/cjs/src/fold.js +4 -0
  17. package/lib/cjs/src/index.d.ts +236 -0
  18. package/lib/cjs/src/index.js +783 -0
  19. package/lib/cjs/src/line-number-prefix.d.ts +4 -0
  20. package/lib/cjs/src/line-number-prefix.js +5 -0
  21. package/lib/cjs/src/render-word-diff.d.ts +22 -0
  22. package/lib/cjs/src/render-word-diff.js +212 -0
  23. package/lib/cjs/src/skipped-line-indicator.d.ts +29 -0
  24. package/lib/cjs/src/skipped-line-indicator.js +29 -0
  25. package/lib/cjs/src/styles.d.ts +102 -0
  26. package/lib/cjs/src/styles.js +430 -0
  27. package/lib/cjs/src/workerBundle.d.ts +5 -0
  28. package/lib/cjs/src/workerBundle.js +7 -0
  29. package/lib/esm/src/comment-row.js +58 -0
  30. package/lib/esm/src/compute-hidden-blocks.js +36 -0
  31. package/lib/esm/src/compute-lines.js +559 -0
  32. package/lib/esm/src/computeWorker.js +10 -0
  33. package/lib/esm/src/diff-row.js +136 -0
  34. package/lib/esm/src/expand.js +4 -0
  35. package/lib/esm/src/fold.js +4 -0
  36. package/lib/esm/src/index.js +780 -0
  37. package/lib/esm/src/line-number-prefix.js +5 -0
  38. package/lib/esm/src/render-word-diff.js +211 -0
  39. package/lib/esm/src/skipped-line-indicator.js +29 -0
  40. package/lib/esm/src/styles.js +431 -0
  41. package/lib/esm/src/workerBundle.js +7 -0
  42. package/package.json +90 -0
@@ -0,0 +1,780 @@
1
+ import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import cn from "classnames";
3
+ import * as React from "react";
4
+ import memoize from "memoize-one";
5
+ import { computeHiddenBlocks } from "./compute-hidden-blocks.js";
6
+ import { DiffMethod, DiffType, computeLineInformationWorker, computeDiff, } from "./compute-lines.js";
7
+ import { Expand } from "./expand.js";
8
+ import computeStyles from "./styles.js";
9
+ import { Fold } from "./fold.js";
10
+ import { LineNumberPrefix } from "./line-number-prefix.js";
11
+ import { DiffRow } from "./diff-row.js";
12
+ import { SkippedLineIndicator } from "./skipped-line-indicator.js";
13
+ import { CommentRow } from "./comment-row.js";
14
+ export { LineNumberPrefix } from "./line-number-prefix.js";
15
+ class DiffViewer extends React.Component {
16
+ styles;
17
+ // Cache for on-demand word diff computation
18
+ wordDiffCache = new Map();
19
+ // Refs for measuring content column width and character width
20
+ contentColumnRef = React.createRef();
21
+ charMeasureRef = React.createRef();
22
+ stickyHeaderRef = React.createRef();
23
+ resizeObserver = null;
24
+ // Comment row height measurement for virtualization
25
+ commentRowObserver = null;
26
+ commentRowHeights = new Map();
27
+ commentRowElements = new Map();
28
+ commentRowRefCache = new Map();
29
+ pendingOffsetRecalc = false;
30
+ static ESTIMATED_COMMENT_ROW_HEIGHT = 100;
31
+ /**
32
+ * Shallow comparison for string arrays — avoids unnecessary work when
33
+ * the consumer creates a new array reference with identical contents.
34
+ */
35
+ static shallowArrayEqual(a, b) {
36
+ if (a === b)
37
+ return true;
38
+ if (!a || !b || a.length !== b.length)
39
+ return false;
40
+ for (let i = 0; i < a.length; i++) {
41
+ if (a[i] !== b[i])
42
+ return false;
43
+ }
44
+ return true;
45
+ }
46
+ static defaultProps = {
47
+ oldValue: "",
48
+ newValue: "",
49
+ splitView: true,
50
+ highlightLines: [],
51
+ disableWordDiff: false,
52
+ compareMethod: DiffMethod.CHARS,
53
+ styles: {},
54
+ hideLineNumbers: false,
55
+ extraLinesSurroundingDiff: 3,
56
+ showDiffOnly: true,
57
+ useDarkTheme: false,
58
+ linesOffset: 0,
59
+ nonce: "",
60
+ };
61
+ constructor(props) {
62
+ super(props);
63
+ this.state = {
64
+ expandedBlocks: [],
65
+ noSelect: undefined,
66
+ scrollableContainerRef: React.createRef(),
67
+ computedDiffResult: {},
68
+ isLoading: false,
69
+ visibleStartRow: 0,
70
+ contentColumnWidth: null,
71
+ charWidth: null,
72
+ cumulativeOffsets: null,
73
+ };
74
+ }
75
+ /**
76
+ * Memoized conversion of commentLineIds array to Set for O(1) lookups.
77
+ */
78
+ getCommentLineIdsSet = memoize((ids) => new Set(ids || []));
79
+ /**
80
+ * Memoized conversion of highlightLines array to Set for O(1) lookups.
81
+ */
82
+ getHighlightLinesSet = memoize((lines) => new Set(lines || []));
83
+ /**
84
+ * Creates a ref callback for a CommentRow's <tr> element.
85
+ * When mounted, observes it for height changes via ResizeObserver.
86
+ * Callbacks are cached per lineId to avoid creating new closures on every render.
87
+ */
88
+ getCommentRowRef = (lineId) => {
89
+ let cached = this.commentRowRefCache.get(lineId);
90
+ if (!cached) {
91
+ cached = (el) => {
92
+ if (el) {
93
+ this.commentRowElements.set(lineId, el);
94
+ this.commentRowObserver?.observe(el);
95
+ }
96
+ else {
97
+ const prev = this.commentRowElements.get(lineId);
98
+ if (prev) {
99
+ this.commentRowObserver?.unobserve(prev);
100
+ }
101
+ this.commentRowElements.delete(lineId);
102
+ }
103
+ };
104
+ this.commentRowRefCache.set(lineId, cached);
105
+ }
106
+ return cached;
107
+ };
108
+ /**
109
+ * Debounced offset recalculation — batches multiple ResizeObserver callbacks
110
+ * into a single requestAnimationFrame.
111
+ */
112
+ scheduleOffsetRecalc = () => {
113
+ if (!this.pendingOffsetRecalc) {
114
+ this.pendingOffsetRecalc = true;
115
+ requestAnimationFrame(() => {
116
+ this.pendingOffsetRecalc = false;
117
+ this.recalculateOffsets();
118
+ });
119
+ }
120
+ };
121
+ /**
122
+ * Initializes the ResizeObserver for measuring comment row heights.
123
+ */
124
+ initCommentRowObserver = () => {
125
+ if (typeof ResizeObserver === "undefined" || this.commentRowObserver)
126
+ return;
127
+ this.commentRowObserver = new ResizeObserver((entries) => {
128
+ let changed = false;
129
+ for (const entry of entries) {
130
+ const el = entry.target;
131
+ const lineId = el.getAttribute("data-comment-line");
132
+ if (!lineId)
133
+ continue;
134
+ const height = entry.borderBoxSize?.[0]?.blockSize ?? el.offsetHeight;
135
+ const prev = this.commentRowHeights.get(lineId);
136
+ if (prev !== height) {
137
+ this.commentRowHeights.set(lineId, height);
138
+ changed = true;
139
+ }
140
+ }
141
+ if (changed && this.props.infiniteLoading) {
142
+ this.scheduleOffsetRecalc();
143
+ }
144
+ });
145
+ };
146
+ /**
147
+ * Computes word diff on-demand for a line, with caching.
148
+ * This is used when word diff was deferred during initial computation.
149
+ */
150
+ getWordDiffValues = (left, right, lineIndex) => {
151
+ // Handle empty left/right
152
+ if (!left || !right) {
153
+ return { leftValue: left?.value, rightValue: right?.value };
154
+ }
155
+ // If no raw values, word diff was already computed or disabled
156
+ // Use explicit undefined check since empty string is a valid raw value
157
+ if (left.rawValue === undefined || right.rawValue === undefined) {
158
+ return { leftValue: left.value, rightValue: right.value };
159
+ }
160
+ // Check cache
161
+ const cacheKey = `${lineIndex}-${left.rawValue}-${right.rawValue}`;
162
+ let cached = this.wordDiffCache.get(cacheKey);
163
+ if (!cached) {
164
+ // Compute word diff on-demand
165
+ // Use CHARS method for on-demand computation since rawValue is always a string
166
+ // (JSON/YAML methods only work with objects, not the string lines we have here)
167
+ const compareMethod = (this.props.compareMethod === DiffMethod.JSON || this.props.compareMethod === DiffMethod.YAML)
168
+ ? DiffMethod.CHARS
169
+ : this.props.compareMethod;
170
+ const computed = computeDiff(left.rawValue, right.rawValue, compareMethod);
171
+ cached = { left: computed.left, right: computed.right };
172
+ this.wordDiffCache.set(cacheKey, cached);
173
+ }
174
+ return { leftValue: cached.left, rightValue: cached.right };
175
+ };
176
+ /**
177
+ * Resets code block expand to the initial stage. Will be exposed to the parent component via
178
+ * refs.
179
+ */
180
+ resetCodeBlocks = () => {
181
+ if (this.state.expandedBlocks.length > 0) {
182
+ this.setState({
183
+ expandedBlocks: [],
184
+ });
185
+ return true;
186
+ }
187
+ return false;
188
+ };
189
+ /**
190
+ * Pushes the target expanded code block to the state. During the re-render,
191
+ * this value is used to expand/fold unmodified code.
192
+ */
193
+ onBlockExpand = (id) => {
194
+ const prevState = this.state.expandedBlocks.slice();
195
+ prevState.push(id);
196
+ this.setState({ expandedBlocks: prevState }, () => this.recalculateOffsets());
197
+ };
198
+ /**
199
+ * Gets the height of the sticky header, if present.
200
+ */
201
+ getStickyHeaderHeight() {
202
+ return this.stickyHeaderRef.current?.offsetHeight || 0;
203
+ }
204
+ /**
205
+ * Measures the width of a single character in the monospace font.
206
+ * Falls back to 7.2px if measurement fails.
207
+ */
208
+ measureCharWidth() {
209
+ const span = this.charMeasureRef.current;
210
+ if (!span)
211
+ return 7.2; // fallback
212
+ return span.getBoundingClientRect().width || 7.2;
213
+ }
214
+ /**
215
+ * Measures the available width for content in a content column.
216
+ * Falls back to estimating from container width if direct measurement fails.
217
+ */
218
+ measureContentColumnWidth() {
219
+ // Try direct measurement first
220
+ const cell = this.contentColumnRef.current;
221
+ if (cell && cell.clientWidth > 0) {
222
+ const style = window.getComputedStyle(cell);
223
+ const padding = parseFloat(style.paddingLeft) + parseFloat(style.paddingRight);
224
+ const width = cell.clientWidth - padding;
225
+ if (width > 0)
226
+ return width;
227
+ }
228
+ // Fallback: estimate from container width
229
+ // In split view: container has 2 content columns + gutters (50px each) + markers (28px each)
230
+ // In unified view: 1 content column + 2 gutters + 1 marker
231
+ const container = this.state.scrollableContainerRef.current;
232
+ if (!container || container.clientWidth <= 0)
233
+ return null;
234
+ const containerWidth = container.clientWidth;
235
+ const gutterWidth = this.props.hideLineNumbers ? 0 : 50;
236
+ const markerWidth = 28;
237
+ const gutterCount = this.props.splitView ? 2 : 2; // left gutter(s)
238
+ const markerCount = this.props.splitView ? 2 : 1;
239
+ const contentColumns = this.props.splitView ? 2 : 1;
240
+ const fixedWidth = gutterCount * gutterWidth + markerCount * markerWidth;
241
+ const availableWidth = containerWidth - fixedWidth;
242
+ return Math.max(100, availableWidth / contentColumns); // minimum 100px
243
+ }
244
+ /**
245
+ * Gets the text length from a value that may be a string or DiffInformation array.
246
+ */
247
+ getTextLength(value) {
248
+ if (!value)
249
+ return 0;
250
+ if (typeof value === 'string')
251
+ return value.length;
252
+ return value.reduce((sum, d) => sum + (typeof d.value === 'string' ? d.value.length : 0), 0);
253
+ }
254
+ /**
255
+ * Builds cumulative vertical offsets for each line based on character count and column width.
256
+ * This allows accurate scroll position calculations with variable row heights.
257
+ */
258
+ buildCumulativeOffsets(lineInformation, lineBlocks, blocks, expandedBlocks, showDiffOnly, charWidth, columnWidth, splitView, commentLineIdsSet) {
259
+ const offsets = [0];
260
+ const seenBlocks = new Set();
261
+ const estimatedCommentHeight = this.props.estimatedCommentRowHeight ?? DiffViewer.ESTIMATED_COMMENT_ROW_HEIGHT;
262
+ for (let i = 0; i < lineInformation.length; i++) {
263
+ const line = lineInformation[i];
264
+ if (showDiffOnly) {
265
+ const blockIndex = lineBlocks[i];
266
+ if (blockIndex !== undefined && !expandedBlocks.includes(blockIndex)) {
267
+ const isLastLine = blocks[blockIndex].endLine === i;
268
+ if (!seenBlocks.has(blockIndex) && isLastLine) {
269
+ seenBlocks.add(blockIndex);
270
+ offsets.push(offsets[offsets.length - 1] + DiffViewer.ESTIMATED_ROW_HEIGHT);
271
+ }
272
+ continue;
273
+ }
274
+ }
275
+ // Calculate visual rows for this line
276
+ const leftLen = line.left?.value ? this.getTextLength(line.left.value) : 0;
277
+ const rightLen = line.right?.value ? this.getTextLength(line.right.value) : 0;
278
+ const maxLen = splitView ? Math.max(leftLen, rightLen) : (leftLen || rightLen);
279
+ const charsPerRow = Math.floor(columnWidth / charWidth);
280
+ const visualRows = charsPerRow > 0 ? Math.max(1, Math.ceil(maxLen / charsPerRow)) : 1;
281
+ let lineHeight = visualRows * DiffViewer.ESTIMATED_ROW_HEIGHT;
282
+ // Add height for comment rows on this line
283
+ if (commentLineIdsSet && commentLineIdsSet.size > 0) {
284
+ const leftId = line.left?.lineNumber ? `L-${line.left.lineNumber}` : null;
285
+ const rightId = line.right?.lineNumber ? `R-${line.right.lineNumber}` : null;
286
+ if (leftId && commentLineIdsSet.has(leftId)) {
287
+ lineHeight += this.commentRowHeights.get(leftId) ?? estimatedCommentHeight;
288
+ }
289
+ if (rightId && rightId !== leftId && commentLineIdsSet.has(rightId)) {
290
+ lineHeight += this.commentRowHeights.get(rightId) ?? estimatedCommentHeight;
291
+ }
292
+ }
293
+ offsets.push(offsets[offsets.length - 1] + lineHeight);
294
+ }
295
+ return offsets;
296
+ }
297
+ /**
298
+ * Binary search to find the line index at a given scroll offset.
299
+ */
300
+ findLineAtOffset(scrollTop, offsets) {
301
+ let low = 0;
302
+ let high = offsets.length - 2;
303
+ while (low < high) {
304
+ const mid = Math.floor((low + high + 1) / 2);
305
+ if (offsets[mid] <= scrollTop) {
306
+ low = mid;
307
+ }
308
+ else {
309
+ high = mid - 1;
310
+ }
311
+ }
312
+ return low;
313
+ }
314
+ /**
315
+ * Recalculates cumulative offsets based on current measurements.
316
+ * Called on resize and when blocks are expanded/collapsed.
317
+ */
318
+ recalculateOffsets = () => {
319
+ if (!this.props.infiniteLoading)
320
+ return;
321
+ const columnWidth = this.measureContentColumnWidth();
322
+ const charWidth = this.measureCharWidth();
323
+ if (!columnWidth)
324
+ return;
325
+ const cacheKey = this.getMemoisedKey();
326
+ const { lineInformation, lineBlocks, blocks } = this.state.computedDiffResult[cacheKey] ?? {};
327
+ if (!lineInformation)
328
+ return;
329
+ const offsets = this.buildCumulativeOffsets(lineInformation, lineBlocks, blocks, this.state.expandedBlocks, this.props.showDiffOnly, charWidth, columnWidth, this.props.splitView, this.getCommentLineIdsSet(this.props.commentLineIds));
330
+ this.setState({ cumulativeOffsets: offsets, contentColumnWidth: columnWidth, charWidth }, () => {
331
+ // Force a scroll position update to recalculate visible rows with new offsets
332
+ this.onScroll();
333
+ });
334
+ };
335
+ /**
336
+ * Computes final styles for the diff viewer. It combines the default styles with the user
337
+ * supplied overrides. The computed styles are cached with performance in mind.
338
+ *
339
+ * @param styles User supplied style overrides.
340
+ */
341
+ computeStyles = memoize(computeStyles);
342
+ /**
343
+ *
344
+ * Generates a unique cache key based on the current props used in diff computation.
345
+ *
346
+ * This key is used to memoize results and avoid recomputation for the same inputs.
347
+ * @returns A stringified JSON key representing the current diff settings and input values.
348
+ *
349
+ */
350
+ getMemoisedKey = () => {
351
+ const { oldValue, newValue, disableWordDiff, compareMethod, linesOffset, alwaysShowLines, extraLinesSurroundingDiff, } = this.props;
352
+ return JSON.stringify({
353
+ oldValue,
354
+ newValue,
355
+ disableWordDiff,
356
+ compareMethod,
357
+ linesOffset,
358
+ alwaysShowLines,
359
+ extraLinesSurroundingDiff,
360
+ });
361
+ };
362
+ /**
363
+ * Computes and memoizes the diff result between `oldValue` and `newValue`.
364
+ *
365
+ * If a memoized result exists for the current input configuration, it uses that.
366
+ * Otherwise, it runs the diff logic in a Web Worker to avoid blocking the UI.
367
+ * It also computes hidden line blocks for collapsing unchanged sections,
368
+ * and stores the result in the local component state.
369
+ */
370
+ memoisedCompute = async () => {
371
+ const { oldValue, newValue, disableWordDiff, compareMethod, linesOffset } = this.props;
372
+ const cacheKey = this.getMemoisedKey();
373
+ if (!!this.state.computedDiffResult[cacheKey]) {
374
+ this.setState((prev) => ({
375
+ ...prev,
376
+ isLoading: false
377
+ }));
378
+ return;
379
+ }
380
+ // Defer word diff computation when using infinite loading with reasonable container height
381
+ // This significantly improves initial render time for large diffs
382
+ const containerHeight = this.props.infiniteLoading?.containerHeight;
383
+ const containerHeightPx = containerHeight
384
+ ? typeof containerHeight === 'number'
385
+ ? containerHeight
386
+ : parseInt(containerHeight, 10) || 0
387
+ : 0;
388
+ const shouldDeferWordDiff = !disableWordDiff &&
389
+ !!this.props.infiniteLoading &&
390
+ containerHeightPx > 0 &&
391
+ containerHeightPx < 2000;
392
+ const { lineInformation, diffLines } = await computeLineInformationWorker(oldValue, newValue, disableWordDiff, compareMethod, linesOffset, this.props.alwaysShowLines, shouldDeferWordDiff);
393
+ const extraLines = this.props.extraLinesSurroundingDiff < 0
394
+ ? 0
395
+ : Math.round(this.props.extraLinesSurroundingDiff);
396
+ const { lineBlocks, blocks } = computeHiddenBlocks(lineInformation, diffLines, extraLines);
397
+ this.state.computedDiffResult[cacheKey] = { lineInformation, lineBlocks, blocks };
398
+ this.setState((prev) => ({
399
+ ...prev,
400
+ computedDiffResult: this.state.computedDiffResult,
401
+ isLoading: false,
402
+ }), () => {
403
+ // Trigger offset recalculation after diff is computed and rendered
404
+ // Use requestAnimationFrame to ensure DOM is ready for measurement
405
+ if (this.props.infiniteLoading) {
406
+ requestAnimationFrame(() => this.recalculateOffsets());
407
+ }
408
+ });
409
+ };
410
+ // Estimated row height based on lineHeight: 1.6em with 12px base font
411
+ static ESTIMATED_ROW_HEIGHT = 19;
412
+ /**
413
+ * Handles scroll events on the scrollable container.
414
+ *
415
+ * Updates the visible start row for virtualization.
416
+ */
417
+ onScroll = () => {
418
+ const container = this.state.scrollableContainerRef.current;
419
+ if (!container || !this.props.infiniteLoading)
420
+ return;
421
+ // Account for sticky header height in scroll calculations
422
+ const headerHeight = this.getStickyHeaderHeight();
423
+ const contentScrollTop = Math.max(0, container.scrollTop - headerHeight);
424
+ const { cumulativeOffsets } = this.state;
425
+ const newStartRow = cumulativeOffsets
426
+ ? this.findLineAtOffset(contentScrollTop, cumulativeOffsets)
427
+ : Math.floor(contentScrollTop / DiffViewer.ESTIMATED_ROW_HEIGHT);
428
+ // Only update state if the start row changed (avoid unnecessary re-renders)
429
+ if (newStartRow !== this.state.visibleStartRow) {
430
+ this.setState({ visibleStartRow: newStartRow });
431
+ }
432
+ };
433
+ /**
434
+ * Generates the entire diff view with virtualization support.
435
+ */
436
+ renderDiff = () => {
437
+ const { splitView, infiniteLoading, showDiffOnly } = this.props;
438
+ const { computedDiffResult, expandedBlocks, visibleStartRow, scrollableContainerRef, cumulativeOffsets } = this.state;
439
+ const cacheKey = this.getMemoisedKey();
440
+ const { lineInformation = [], lineBlocks = [], blocks = [] } = computedDiffResult[cacheKey] ?? {};
441
+ // Build Set for O(1) comment line lookups
442
+ const commentLineIdsSet = this.getCommentLineIdsSet(this.props.commentLineIds);
443
+ const hasComments = commentLineIdsSet.size > 0 && !!this.props.renderComment;
444
+ const hasRenderGutter = !!this.props.renderGutter;
445
+ // Build Set for O(1) highlight line lookups
446
+ const highlightLinesSet = this.getHighlightLinesSet(this.props.highlightLines);
447
+ // Calculate visible range for virtualization
448
+ let visibleRowStart = 0;
449
+ let visibleRowEnd = Infinity;
450
+ const buffer = 5; // render extra rows above/below viewport
451
+ if (infiniteLoading && scrollableContainerRef.current) {
452
+ const container = scrollableContainerRef.current;
453
+ // Account for sticky header height in scroll calculations
454
+ const headerHeight = this.getStickyHeaderHeight();
455
+ const contentScrollTop = Math.max(0, container.scrollTop - headerHeight);
456
+ if (cumulativeOffsets) {
457
+ // Variable height mode: use binary search to find visible range
458
+ const totalHeight = cumulativeOffsets[cumulativeOffsets.length - 1] || 0;
459
+ const lastRowIndex = cumulativeOffsets.length - 2;
460
+ visibleRowStart = Math.max(0, this.findLineAtOffset(contentScrollTop, cumulativeOffsets) - buffer);
461
+ visibleRowEnd = this.findLineAtOffset(contentScrollTop + container.clientHeight, cumulativeOffsets) + buffer;
462
+ // IMPORTANT: The calculated offsets may overestimate row heights (based on char count),
463
+ // but actual CSS rendering might produce shorter rows. To prevent empty space,
464
+ // ensure we render at least enough rows to fill the viewport using ESTIMATED_ROW_HEIGHT
465
+ // as a conservative minimum.
466
+ const minRowsToFillViewport = Math.ceil(container.clientHeight / DiffViewer.ESTIMATED_ROW_HEIGHT);
467
+ visibleRowEnd = Math.max(visibleRowEnd, visibleRowStart + minRowsToFillViewport + buffer);
468
+ // Also ensure we render all rows when near the bottom
469
+ if (contentScrollTop + container.clientHeight >= totalHeight - buffer * DiffViewer.ESTIMATED_ROW_HEIGHT) {
470
+ visibleRowEnd = lastRowIndex + buffer;
471
+ }
472
+ }
473
+ else {
474
+ // Fixed height fallback
475
+ const viewportRows = Math.ceil(container.clientHeight / DiffViewer.ESTIMATED_ROW_HEIGHT);
476
+ visibleRowStart = Math.max(0, visibleStartRow - buffer);
477
+ visibleRowEnd = visibleStartRow + viewportRows + buffer;
478
+ }
479
+ }
480
+ // First pass: build a map of lineIndex -> renderedRowIndex
481
+ // This accounts for code folding where some lines don't render or render as fold indicators
482
+ const lineToRowMap = new Map();
483
+ const seenBlocks = new Set();
484
+ let currentRow = 0;
485
+ for (let i = 0; i < lineInformation.length; i++) {
486
+ const blockIndex = lineBlocks[i];
487
+ if (showDiffOnly && blockIndex !== undefined) {
488
+ if (!expandedBlocks.includes(blockIndex)) {
489
+ // Line is in a collapsed block
490
+ const lastLineOfBlock = blocks[blockIndex].endLine === i;
491
+ if (!seenBlocks.has(blockIndex) && lastLineOfBlock) {
492
+ // This line renders as a fold indicator
493
+ seenBlocks.add(blockIndex);
494
+ lineToRowMap.set(i, currentRow);
495
+ currentRow++;
496
+ }
497
+ // Other lines in collapsed block don't render
498
+ }
499
+ else {
500
+ // Block is expanded, line renders normally
501
+ lineToRowMap.set(i, currentRow);
502
+ currentRow++;
503
+ }
504
+ }
505
+ else {
506
+ // Not in a block or showDiffOnly is false, line renders normally
507
+ lineToRowMap.set(i, currentRow);
508
+ currentRow++;
509
+ }
510
+ }
511
+ const totalRenderedRows = currentRow;
512
+ // Second pass: render only lines in the visible range
513
+ const diffNodes = [];
514
+ let topPadding = 0;
515
+ let firstVisibleFound = false;
516
+ let lastRenderedRowIndex = -1;
517
+ seenBlocks.clear();
518
+ for (let lineIndex = 0; lineIndex < lineInformation.length; lineIndex++) {
519
+ const line = lineInformation[lineIndex];
520
+ const rowIndex = lineToRowMap.get(lineIndex);
521
+ // Skip lines that don't render (hidden in collapsed blocks)
522
+ if (rowIndex === undefined)
523
+ continue;
524
+ // Skip lines before visible range
525
+ if (rowIndex < visibleRowStart) {
526
+ continue;
527
+ }
528
+ // Stop after visible range
529
+ if (rowIndex > visibleRowEnd) {
530
+ break;
531
+ }
532
+ // Calculate top padding from the first visible row
533
+ if (!firstVisibleFound) {
534
+ topPadding = cumulativeOffsets
535
+ ? cumulativeOffsets[rowIndex] || 0
536
+ : rowIndex * DiffViewer.ESTIMATED_ROW_HEIGHT;
537
+ firstVisibleFound = true;
538
+ }
539
+ // Track the last rendered row for bottom padding calculation
540
+ lastRenderedRowIndex = rowIndex;
541
+ // Render the line
542
+ if (showDiffOnly) {
543
+ const blockIndex = lineBlocks[lineIndex];
544
+ if (blockIndex !== undefined) {
545
+ const lastLineOfBlock = blocks[blockIndex].endLine === lineIndex;
546
+ if (!expandedBlocks.includes(blockIndex) &&
547
+ lastLineOfBlock) {
548
+ diffNodes.push(_jsx(SkippedLineIndicator, { num: blocks[blockIndex].lines, blockNumber: blockIndex, leftBlockLineNumber: line.left.lineNumber, rightBlockLineNumber: line.right.lineNumber, hideLineNumbers: this.props.hideLineNumbers, splitView: this.props.splitView, styles: this.styles, onBlockClick: this.onBlockExpand, codeFoldMessageRenderer: this.props.codeFoldMessageRenderer, renderGutter: this.props.renderGutter }, `fold-${lineIndex}`));
549
+ continue;
550
+ }
551
+ if (!expandedBlocks.includes(blockIndex)) {
552
+ continue;
553
+ }
554
+ }
555
+ }
556
+ // Compute word diff on-demand if deferred
557
+ const { leftValue, rightValue } = this.getWordDiffValues(line.left, line.right, lineIndex);
558
+ diffNodes.push(_jsx(DiffRow, { index: lineIndex, leftLineNumber: line.left.lineNumber, leftType: line.left.type, leftValue: leftValue, rightLineNumber: line.right.lineNumber, rightType: line.right.type, rightValue: rightValue, highlightLeft: highlightLinesSet.has(`L-${line.left.lineNumber}`), highlightRight: highlightLinesSet.has(`R-${line.right.lineNumber}`), splitView: splitView, hideLineNumbers: this.props.hideLineNumbers, styles: this.styles, onLineNumberClick: this.props.onLineNumberClick, renderContent: this.props.renderContent, renderGutter: this.props.renderGutter, compareMethod: this.props.compareMethod, contentColumnRef: this.contentColumnRef, hasCumulativeOffsets: !!cumulativeOffsets }, lineIndex));
559
+ // Inject comment rows after the DiffRow
560
+ if (hasComments) {
561
+ const leftLineId = line.left?.lineNumber ? `L-${line.left.lineNumber}` : null;
562
+ const rightLineId = line.right?.lineNumber ? `R-${line.right.lineNumber}` : null;
563
+ if (leftLineId && commentLineIdsSet.has(leftLineId)) {
564
+ diffNodes.push(_jsx(CommentRow, { lineId: leftLineId, lineNumber: line.left.lineNumber, prefix: LineNumberPrefix.LEFT, splitView: splitView, hideLineNumbers: this.props.hideLineNumbers, hasRenderGutter: hasRenderGutter, styles: this.styles, renderComment: this.props.renderComment, trRef: this.getCommentRowRef(leftLineId) }, `comment-${leftLineId}`));
565
+ }
566
+ if (rightLineId && rightLineId !== leftLineId && commentLineIdsSet.has(rightLineId)) {
567
+ diffNodes.push(_jsx(CommentRow, { lineId: rightLineId, lineNumber: line.right.lineNumber, prefix: LineNumberPrefix.RIGHT, splitView: splitView, hideLineNumbers: this.props.hideLineNumbers, hasRenderGutter: hasRenderGutter, styles: this.styles, renderComment: this.props.renderComment, trRef: this.getCommentRowRef(rightLineId) }, `comment-${rightLineId}`));
568
+ }
569
+ }
570
+ }
571
+ // Calculate total content height
572
+ const totalContentHeight = cumulativeOffsets
573
+ ? cumulativeOffsets[cumulativeOffsets.length - 1] || 0
574
+ : totalRenderedRows * DiffViewer.ESTIMATED_ROW_HEIGHT;
575
+ // Calculate bottom padding: space after the last rendered row
576
+ const bottomPadding = cumulativeOffsets && lastRenderedRowIndex >= 0
577
+ ? totalContentHeight - (cumulativeOffsets[lastRenderedRowIndex + 1] || totalContentHeight)
578
+ : 0;
579
+ return {
580
+ diffNodes,
581
+ blocks,
582
+ lineInformation,
583
+ totalRenderedRows,
584
+ topPadding,
585
+ bottomPadding,
586
+ totalContentHeight,
587
+ renderedCount: diffNodes.length,
588
+ // Debug info
589
+ debug: {
590
+ visibleRowStart,
591
+ visibleRowEnd,
592
+ totalRows: totalRenderedRows,
593
+ offsetsLength: cumulativeOffsets?.length ?? 0,
594
+ renderedCount: diffNodes.length,
595
+ scrollTop: scrollableContainerRef.current?.scrollTop ?? 0,
596
+ headerHeight: this.getStickyHeaderHeight(),
597
+ contentScrollTop: scrollableContainerRef.current
598
+ ? Math.max(0, scrollableContainerRef.current.scrollTop - this.getStickyHeaderHeight())
599
+ : 0,
600
+ clientHeight: scrollableContainerRef.current?.clientHeight ?? 0,
601
+ }
602
+ };
603
+ };
604
+ componentDidUpdate(prevProps) {
605
+ if (prevProps.oldValue !== this.props.oldValue ||
606
+ prevProps.newValue !== this.props.newValue ||
607
+ prevProps.compareMethod !== this.props.compareMethod ||
608
+ prevProps.disableWordDiff !== this.props.disableWordDiff ||
609
+ prevProps.linesOffset !== this.props.linesOffset) {
610
+ // Clear word diff cache when diff changes
611
+ this.wordDiffCache.clear();
612
+ // Reset scroll position to top
613
+ const container = this.state.scrollableContainerRef.current;
614
+ if (container) {
615
+ container.scrollTop = 0;
616
+ }
617
+ this.setState((prev) => ({
618
+ ...prev,
619
+ isLoading: true,
620
+ visibleStartRow: 0,
621
+ cumulativeOffsets: null,
622
+ }));
623
+ this.memoisedCompute();
624
+ }
625
+ // Recalculate offsets when commentLineIds change
626
+ if (!DiffViewer.shallowArrayEqual(prevProps.commentLineIds, this.props.commentLineIds)) {
627
+ // Clean stale entries from height measurement maps and ref cache
628
+ const currentSet = this.getCommentLineIdsSet(this.props.commentLineIds);
629
+ for (const lineId of this.commentRowHeights.keys()) {
630
+ if (!currentSet.has(lineId)) {
631
+ this.commentRowHeights.delete(lineId);
632
+ this.commentRowRefCache.delete(lineId);
633
+ const el = this.commentRowElements.get(lineId);
634
+ if (el) {
635
+ this.commentRowObserver?.unobserve(el);
636
+ this.commentRowElements.delete(lineId);
637
+ }
638
+ }
639
+ }
640
+ if (this.props.infiniteLoading) {
641
+ this.scheduleOffsetRecalc();
642
+ }
643
+ }
644
+ }
645
+ componentDidMount() {
646
+ this.setState((prev) => ({
647
+ ...prev,
648
+ isLoading: true
649
+ }));
650
+ this.memoisedCompute();
651
+ // Set up ResizeObserver for recalculating offsets on container resize
652
+ if (typeof ResizeObserver !== 'undefined' && this.props.infiniteLoading) {
653
+ this.resizeObserver = new ResizeObserver(() => {
654
+ requestAnimationFrame(() => this.recalculateOffsets());
655
+ });
656
+ const container = this.state.scrollableContainerRef.current;
657
+ if (container) {
658
+ this.resizeObserver.observe(container);
659
+ }
660
+ }
661
+ // Initialize comment row observer for height measurement
662
+ this.initCommentRowObserver();
663
+ }
664
+ componentWillUnmount() {
665
+ this.resizeObserver?.disconnect();
666
+ this.commentRowObserver?.disconnect();
667
+ }
668
+ render = () => {
669
+ const { oldValue, newValue, useDarkTheme, leftTitle, rightTitle, splitView, compareMethod, hideLineNumbers, nonce, } = this.props;
670
+ if (typeof compareMethod === "string" &&
671
+ compareMethod !== DiffMethod.JSON) {
672
+ if (typeof oldValue !== "string" || typeof newValue !== "string") {
673
+ throw Error('"oldValue" and "newValue" should be strings');
674
+ }
675
+ }
676
+ this.styles = this.computeStyles(this.props.styles, useDarkTheme, nonce);
677
+ const nodes = this.renderDiff();
678
+ let colSpanOnSplitView = 3;
679
+ let colSpanOnInlineView = 4;
680
+ if (hideLineNumbers) {
681
+ colSpanOnSplitView -= 1;
682
+ colSpanOnInlineView -= 1;
683
+ }
684
+ if (this.props.renderGutter) {
685
+ colSpanOnSplitView += 1;
686
+ colSpanOnInlineView += 1;
687
+ }
688
+ let deletions = 0;
689
+ let additions = 0;
690
+ for (const l of nodes.lineInformation) {
691
+ if (l.left.type === DiffType.ADDED) {
692
+ additions++;
693
+ }
694
+ if (l.right.type === DiffType.ADDED) {
695
+ additions++;
696
+ }
697
+ if (l.left.type === DiffType.REMOVED) {
698
+ deletions++;
699
+ }
700
+ if (l.right.type === DiffType.REMOVED) {
701
+ deletions++;
702
+ }
703
+ }
704
+ const totalChanges = deletions + additions;
705
+ const percentageAddition = Math.round((additions / totalChanges) * 100);
706
+ const blocks = [];
707
+ for (let i = 0; i < 5; i++) {
708
+ if (percentageAddition > i * 20) {
709
+ blocks.push(_jsx("span", { className: cn(this.styles.block, this.styles.blockAddition) }, i));
710
+ }
711
+ else {
712
+ blocks.push(_jsx("span", { className: cn(this.styles.block, this.styles.blockDeletion) }, i));
713
+ }
714
+ }
715
+ const allExpanded = this.state.expandedBlocks.length === nodes.blocks.length;
716
+ const LoadingElement = this.props.loadingElement;
717
+ const scrollDivStyle = this.props.infiniteLoading ? {
718
+ overflowY: 'scroll',
719
+ overflowX: 'hidden',
720
+ height: this.props.infiniteLoading.containerHeight
721
+ } : {};
722
+ // Only apply noWrap when infiniteLoading is enabled but we don't have cumulative offsets yet
723
+ // Once offsets are calculated, we enable pre-wrap for proper text wrapping
724
+ const shouldNoWrap = !!this.props.infiniteLoading && !this.state.cumulativeOffsets;
725
+ const tableElement = (_jsxs("table", { className: cn(this.styles.diffContainer, {
726
+ [this.styles.splitView]: splitView,
727
+ [this.styles.noWrap]: shouldNoWrap,
728
+ }), onMouseUp: () => {
729
+ const elements = document.getElementsByClassName("right");
730
+ for (let i = 0; i < elements.length; i++) {
731
+ const element = elements.item(i);
732
+ element.classList.remove(this.styles.noSelect);
733
+ }
734
+ const elementsLeft = document.getElementsByClassName("left");
735
+ for (let i = 0; i < elementsLeft.length; i++) {
736
+ const element = elementsLeft.item(i);
737
+ element.classList.remove(this.styles.noSelect);
738
+ }
739
+ }, children: [_jsxs("colgroup", { children: [!this.props.hideLineNumbers && _jsx("col", { width: "50px" }), !splitView && !this.props.hideLineNumbers && _jsx("col", { width: "50px" }), this.props.renderGutter && _jsx("col", { width: "50px" }), _jsx("col", { width: "28px" }), _jsx("col", { width: "auto" }), splitView && (_jsxs(_Fragment, { children: [!this.props.hideLineNumbers && _jsx("col", { width: "50px" }), this.props.renderGutter && _jsx("col", { width: "50px" }), _jsx("col", { width: "28px" }), _jsx("col", { width: "auto" })] }))] }), _jsx("tbody", { children: nodes.diffNodes })] }));
740
+ return (_jsxs("div", { style: { ...scrollDivStyle, position: 'relative' }, onScroll: this.onScroll, ref: this.state.scrollableContainerRef, children: [(!this.props.hideSummary || leftTitle || rightTitle) && (_jsxs("div", { ref: this.stickyHeaderRef, className: this.styles.stickyHeader, children: [!this.props.hideSummary && (_jsxs("div", { className: this.styles.summary, role: "banner", children: [_jsx("button", { type: "button", className: this.styles.allExpandButton, onClick: () => {
741
+ this.setState({
742
+ expandedBlocks: allExpanded
743
+ ? []
744
+ : nodes.blocks.map((b) => b.index),
745
+ }, () => this.recalculateOffsets());
746
+ }, children: allExpanded ? _jsx(Fold, {}) : _jsx(Expand, {}) }), " ", totalChanges, _jsx("div", { style: { display: "flex", gap: "1px" }, children: blocks }), this.props.summary ? _jsx("span", { children: this.props.summary }) : null] })), (leftTitle || rightTitle) && (_jsxs("div", { className: this.styles.columnHeaders, children: [_jsx("div", { className: this.styles.titleBlock, children: leftTitle ? (_jsx("pre", { className: this.styles.contentText, children: leftTitle })) : null }), splitView && (_jsx("div", { className: this.styles.titleBlock, children: rightTitle ? (_jsx("pre", { className: this.styles.contentText, children: rightTitle })) : null }))] }))] })), this.state.isLoading && LoadingElement && _jsx(LoadingElement, {}), this.props.infiniteLoading ? (_jsx("div", { style: {
747
+ height: nodes.totalContentHeight,
748
+ position: 'relative',
749
+ }, children: _jsx("div", { style: {
750
+ position: 'absolute',
751
+ top: nodes.topPadding,
752
+ left: 0,
753
+ right: 0,
754
+ }, children: tableElement }) })) : (tableElement), _jsx("span", { ref: this.charMeasureRef, style: {
755
+ position: 'absolute',
756
+ top: 0,
757
+ left: '-9999px',
758
+ visibility: 'hidden',
759
+ whiteSpace: 'pre',
760
+ fontFamily: 'monospace',
761
+ fontSize: 12,
762
+ }, "aria-hidden": "true", children: "M" }), this.props.infiniteLoading && this.props.showDebugInfo && (_jsxs("div", { style: {
763
+ position: 'fixed',
764
+ top: 10,
765
+ right: 10,
766
+ background: 'rgba(0,0,0,0.85)',
767
+ color: '#0f0',
768
+ padding: '10px',
769
+ fontFamily: 'monospace',
770
+ fontSize: '11px',
771
+ zIndex: 9999,
772
+ borderRadius: '4px',
773
+ maxWidth: '300px',
774
+ lineHeight: 1.4,
775
+ }, children: [_jsx("div", { style: { fontWeight: 'bold', marginBottom: '5px', color: '#fff' }, children: "Debug Info" }), _jsxs("div", { children: ["scrollTop: ", nodes.debug.scrollTop] }), _jsxs("div", { children: ["headerHeight: ", nodes.debug.headerHeight] }), _jsxs("div", { children: ["contentScrollTop: ", nodes.debug.contentScrollTop] }), _jsxs("div", { children: ["clientHeight: ", nodes.debug.clientHeight] }), _jsxs("div", { style: { marginTop: '5px', borderTop: '1px solid #444', paddingTop: '5px' }, children: [_jsxs("div", { children: ["visibleRowStart: ", nodes.debug.visibleRowStart] }), _jsxs("div", { children: ["visibleRowEnd: ", nodes.debug.visibleRowEnd] })] }), _jsxs("div", { style: { marginTop: '5px', borderTop: '1px solid #444', paddingTop: '5px' }, children: [_jsxs("div", { children: ["totalRows: ", nodes.debug.totalRows] }), _jsxs("div", { children: ["offsetsLength: ", nodes.debug.offsetsLength] }), _jsxs("div", { children: ["renderedCount: ", nodes.debug.renderedCount] })] }), _jsxs("div", { style: { marginTop: '5px', borderTop: '1px solid #444', paddingTop: '5px' }, children: [_jsxs("div", { children: ["topPadding: ", nodes.topPadding.toFixed(0)] }), _jsxs("div", { children: ["bottomPadding: ", nodes.bottomPadding.toFixed(0)] }), _jsxs("div", { children: ["totalContentHeight: ", nodes.totalContentHeight.toFixed(0)] })] }), _jsxs("div", { style: { marginTop: '5px', borderTop: '1px solid #444', paddingTop: '5px', color: '#ff0' }, children: [_jsxs("div", { children: ["cumulativeOffsets: ", this.state.cumulativeOffsets ? 'SET' : 'NULL'] }), _jsxs("div", { children: ["columnWidth: ", this.state.contentColumnWidth?.toFixed(0) ?? 'N/A', "px"] }), _jsxs("div", { children: ["charWidth: ", this.state.charWidth?.toFixed(2) ?? 'N/A', "px"] }), _jsxs("div", { children: ["charsPerRow: ", this.state.contentColumnWidth && this.state.charWidth ? Math.floor(this.state.contentColumnWidth / this.state.charWidth) : 'N/A'] })] }), this.state.cumulativeOffsets && (_jsxs("div", { style: { marginTop: '5px', borderTop: '1px solid #444', paddingTop: '5px', color: '#0ff', fontSize: '10px' }, children: [_jsxs("div", { children: ["offsets[", nodes.debug.visibleRowEnd, "]: ", this.state.cumulativeOffsets[nodes.debug.visibleRowEnd]?.toFixed(0) ?? 'N/A'] }), _jsxs("div", { children: ["offsets[", nodes.debug.totalRows - 1, "]: ", this.state.cumulativeOffsets[nodes.debug.totalRows - 1]?.toFixed(0) ?? 'N/A'] }), _jsxs("div", { children: ["offsets[", nodes.debug.totalRows, "]: ", this.state.cumulativeOffsets[nodes.debug.totalRows]?.toFixed(0) ?? 'N/A'] }), _jsxs("div", { style: { marginTop: '3px' }, children: ["viewportEnd: ", (nodes.debug.contentScrollTop + nodes.debug.clientHeight).toFixed(0)] }), _jsxs("div", { style: { marginTop: '3px', color: '#f0f' }, children: ["scrollHeight: ", this.state.scrollableContainerRef.current?.scrollHeight ?? 'N/A'] }), _jsxs("div", { children: ["maxScrollTop: ", (this.state.scrollableContainerRef.current?.scrollHeight ?? 0) - nodes.debug.clientHeight] })] }))] }))] }));
776
+ };
777
+ }
778
+ export default DiffViewer;
779
+ export { DiffMethod };
780
+ export { default as computeStyles } from "./styles.js";