react-diff-viewer-continued 4.0.3 → 4.0.5

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.
@@ -1,12 +1,12 @@
1
1
  import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
2
- import * as React from 'react';
3
- import cn from 'classnames';
4
- import { computeLineInformation, DiffMethod, DiffType, } from './compute-lines';
5
- import computeStyles from './styles';
6
- import { computeHiddenBlocks } from "./compute-hidden-blocks";
7
- import { Expand } from "./expand";
8
- import memoize from 'memoize-one';
9
- import { Fold } from "./fold";
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, computeLineInformation, } from "./compute-lines.js";
7
+ import { Expand } from "./expand.js";
8
+ import computeStyles from "./styles.js";
9
+ import { Fold } from "./fold.js";
10
10
  export var LineNumberPrefix;
11
11
  (function (LineNumberPrefix) {
12
12
  LineNumberPrefix["LEFT"] = "L";
@@ -15,8 +15,8 @@ export var LineNumberPrefix;
15
15
  class DiffViewer extends React.Component {
16
16
  styles;
17
17
  static defaultProps = {
18
- oldValue: '',
19
- newValue: '',
18
+ oldValue: "",
19
+ newValue: "",
20
20
  splitView: true,
21
21
  highlightLines: [],
22
22
  disableWordDiff: false,
@@ -27,7 +27,7 @@ class DiffViewer extends React.Component {
27
27
  showDiffOnly: true,
28
28
  useDarkTheme: false,
29
29
  linesOffset: 0,
30
- nonce: '',
30
+ nonce: "",
31
31
  };
32
32
  constructor(props) {
33
33
  super(props);
@@ -87,16 +87,16 @@ class DiffViewer extends React.Component {
87
87
  */
88
88
  renderWordDiff = (diffArray, renderer) => {
89
89
  return diffArray.map((wordDiff, i) => {
90
- const content = renderer ? renderer(wordDiff.value) : wordDiff.value;
91
- if (typeof content !== 'string')
90
+ const content = renderer
91
+ ? renderer(wordDiff.value)
92
+ : wordDiff.value;
93
+ if (typeof content !== "string")
92
94
  return;
93
- return wordDiff.type === DiffType.ADDED ?
94
- _jsx("ins", { className: cn(this.styles.wordDiff, {
95
- [this.styles.wordAdded]: wordDiff.type === DiffType.ADDED,
96
- }), children: content }, i)
97
- : wordDiff.type === DiffType.REMOVED ? _jsx("del", { className: cn(this.styles.wordDiff, {
98
- [this.styles.wordRemoved]: wordDiff.type === DiffType.REMOVED,
99
- }), children: content }, i) : _jsx("span", { className: cn(this.styles.wordDiff), children: content }, i);
95
+ return wordDiff.type === DiffType.ADDED ? (_jsx("ins", { className: cn(this.styles.wordDiff, {
96
+ [this.styles.wordAdded]: wordDiff.type === DiffType.ADDED,
97
+ }), children: content }, i)) : wordDiff.type === DiffType.REMOVED ? (_jsx("del", { className: cn(this.styles.wordDiff, {
98
+ [this.styles.wordRemoved]: wordDiff.type === DiffType.REMOVED,
99
+ }), children: content }, i)) : (_jsx("span", { className: cn(this.styles.wordDiff), children: content }, i));
100
100
  });
101
101
  };
102
102
  /**
@@ -131,12 +131,12 @@ class DiffViewer extends React.Component {
131
131
  else {
132
132
  content = value;
133
133
  }
134
- let ElementType = 'div';
134
+ let ElementType = "div";
135
135
  if (added && !hasWordDiff) {
136
- ElementType = 'ins';
136
+ ElementType = "ins";
137
137
  }
138
138
  else if (removed && !hasWordDiff) {
139
- ElementType = 'del';
139
+ ElementType = "del";
140
140
  }
141
141
  return (_jsxs(_Fragment, { children: [!this.props.hideLineNumbers && (_jsx("td", { onClick: lineNumber && this.onLineNumberClickProxy(lineNumberTemplate), className: cn(this.styles.gutter, {
142
142
  [this.styles.emptyGutter]: !lineNumber,
@@ -167,7 +167,7 @@ class DiffViewer extends React.Component {
167
167
  [this.styles.diffRemoved]: removed,
168
168
  [this.styles.diffChanged]: changed,
169
169
  [this.styles.highlightedLine]: highlightLine,
170
- }), children: _jsxs("pre", { children: [added && '+', removed && '-'] }) }), _jsx("td", { className: cn(this.styles.content, {
170
+ }), children: _jsxs("pre", { children: [added && "+", removed && "-"] }) }), _jsx("td", { className: cn(this.styles.content, {
171
171
  [this.styles.emptyLine]: !content,
172
172
  [this.styles.diffAdded]: added,
173
173
  [this.styles.diffRemoved]: removed,
@@ -176,12 +176,16 @@ class DiffViewer extends React.Component {
176
176
  left: prefix === LineNumberPrefix.LEFT,
177
177
  right: prefix === LineNumberPrefix.RIGHT,
178
178
  }), onMouseDown: () => {
179
- const elements = document.getElementsByClassName(prefix === LineNumberPrefix.LEFT ? 'right' : 'left');
179
+ const elements = document.getElementsByClassName(prefix === LineNumberPrefix.LEFT ? "right" : "left");
180
180
  for (let i = 0; i < elements.length; i++) {
181
181
  const element = elements.item(i);
182
182
  element.classList.add(this.styles.noSelect);
183
183
  }
184
- }, title: added && !hasWordDiff ? "Added line" : removed && !hasWordDiff ? "Removed line" : undefined, children: _jsx(ElementType, { className: this.styles.contentText, children: content }) })] }));
184
+ }, title: added && !hasWordDiff
185
+ ? "Added line"
186
+ : removed && !hasWordDiff
187
+ ? "Removed line"
188
+ : undefined, children: _jsx(ElementType, { className: this.styles.contentText, children: content }) })] }));
185
189
  };
186
190
  /**
187
191
  * Generates lines for split view.
@@ -205,7 +209,7 @@ class DiffViewer extends React.Component {
205
209
  renderInlineView = ({ left, right }, index) => {
206
210
  let content;
207
211
  if (left.type === DiffType.REMOVED && right.type === DiffType.ADDED) {
208
- return (_jsxs(React.Fragment, { children: [_jsx("tr", { className: this.styles.line, children: this.renderLine(left.lineNumber, left.type, LineNumberPrefix.LEFT, left.value, null) }), _jsx("tr", { className: this.styles.line, children: this.renderLine(null, right.type, LineNumberPrefix.RIGHT, right.value, right.lineNumber) })] }, index));
212
+ return (_jsxs(React.Fragment, { children: [_jsx("tr", { className: this.styles.line, children: this.renderLine(left.lineNumber, left.type, LineNumberPrefix.LEFT, left.value, null) }), _jsx("tr", { className: this.styles.line, children: this.renderLine(null, right.type, LineNumberPrefix.RIGHT, right.value, right.lineNumber, LineNumberPrefix.RIGHT) })] }, index));
209
213
  }
210
214
  if (left.type === DiffType.REMOVED) {
211
215
  content = this.renderLine(left.lineNumber, left.type, LineNumberPrefix.LEFT, left.value, null);
@@ -235,8 +239,8 @@ class DiffViewer extends React.Component {
235
239
  */
236
240
  renderSkippedLineIndicator = (num, blockNumber, leftBlockLineNumber, rightBlockLineNumber) => {
237
241
  const { hideLineNumbers, splitView } = this.props;
238
- const message = this.props.codeFoldMessageRenderer ? (this.props.codeFoldMessageRenderer(num, leftBlockLineNumber, rightBlockLineNumber)) : (_jsxs("pre", { className: this.styles.codeFoldContent, children: ["Expand ", num, " lines ..."] }));
239
- const content = (_jsx("td", { className: this.styles.codeFoldContentContainer, children: _jsx("a", { onClick: this.onBlockClickProxy(blockNumber), tabIndex: 0, children: message }) }));
242
+ const message = this.props.codeFoldMessageRenderer ? (this.props.codeFoldMessageRenderer(num, leftBlockLineNumber, rightBlockLineNumber)) : (_jsxs("span", { className: this.styles.codeFoldContent, children: ["Expand ", num, " lines ..."] }));
243
+ const content = (_jsx("td", { className: this.styles.codeFoldContentContainer, children: _jsx("button", { type: "button", className: this.styles.codeFoldExpandButton, onClick: this.onBlockClickProxy(blockNumber), tabIndex: 0, children: message }) }));
240
244
  const isUnifiedViewWithoutLineNumbers = !splitView && !hideLineNumbers;
241
245
  return (_jsxs("tr", { className: this.styles.codeFold, children: [!hideLineNumbers && _jsx("td", { className: this.styles.codeFoldGutter }), this.props.renderGutter ? (_jsx("td", { className: this.styles.codeFoldGutter })) : null, _jsx("td", { className: cn({
242
246
  [this.styles.codeFoldGutter]: isUnifiedViewWithoutLineNumbers,
@@ -257,10 +261,11 @@ class DiffViewer extends React.Component {
257
261
  const blockIndex = lineBlocks[lineIndex];
258
262
  if (blockIndex !== undefined) {
259
263
  const lastLineOfBlock = blocks[blockIndex].endLine === lineIndex;
260
- if (!this.state.expandedBlocks.includes(blockIndex) && lastLineOfBlock) {
264
+ if (!this.state.expandedBlocks.includes(blockIndex) &&
265
+ lastLineOfBlock) {
261
266
  return (_jsx(React.Fragment, { children: this.renderSkippedLineIndicator(blocks[blockIndex].lines, blockIndex, line.left.lineNumber, line.right.lineNumber) }, lineIndex));
262
267
  }
263
- else if (!this.state.expandedBlocks.includes(blockIndex)) {
268
+ if (!this.state.expandedBlocks.includes(blockIndex)) {
264
269
  return null;
265
270
  }
266
271
  }
@@ -272,13 +277,14 @@ class DiffViewer extends React.Component {
272
277
  return {
273
278
  diffNodes,
274
279
  blocks,
275
- lineInformation
280
+ lineInformation,
276
281
  };
277
282
  };
278
283
  render = () => {
279
284
  const { oldValue, newValue, useDarkTheme, leftTitle, rightTitle, splitView, compareMethod, hideLineNumbers, nonce, } = this.props;
280
- if (typeof (compareMethod) === 'string' && compareMethod !== DiffMethod.JSON) {
281
- if (typeof oldValue !== 'string' || typeof newValue !== 'string') {
285
+ if (typeof compareMethod === "string" &&
286
+ compareMethod !== DiffMethod.JSON) {
287
+ if (typeof oldValue !== "string" || typeof newValue !== "string") {
282
288
  throw Error('"oldValue" and "newValue" should be strings');
283
289
  }
284
290
  }
@@ -294,8 +300,9 @@ class DiffViewer extends React.Component {
294
300
  colSpanOnSplitView += 1;
295
301
  colSpanOnInlineView += 1;
296
302
  }
297
- let deletions = 0, additions = 0;
298
- nodes.lineInformation.forEach((l) => {
303
+ let deletions = 0;
304
+ let additions = 0;
305
+ for (const l of nodes.lineInformation) {
299
306
  if (l.left.type === DiffType.ADDED) {
300
307
  additions++;
301
308
  }
@@ -308,7 +315,7 @@ class DiffViewer extends React.Component {
308
315
  if (l.right.type === DiffType.REMOVED) {
309
316
  deletions++;
310
317
  }
311
- });
318
+ }
312
319
  const totalChanges = deletions + additions;
313
320
  const percentageAddition = Math.round((additions / totalChanges) * 100);
314
321
  const blocks = [];
@@ -321,24 +328,26 @@ class DiffViewer extends React.Component {
321
328
  }
322
329
  }
323
330
  const allExpanded = this.state.expandedBlocks.length === nodes.blocks.length;
324
- return (_jsxs("div", { children: [_jsxs("div", { className: this.styles.summary, role: 'banner', children: [_jsx("a", { style: { cursor: 'pointer' }, onClick: () => {
331
+ return (_jsxs("div", { children: [_jsxs("div", { className: this.styles.summary, role: "banner", children: [_jsx("button", { type: "button", className: this.styles.allExpandButton, onClick: () => {
325
332
  this.setState({
326
- expandedBlocks: allExpanded ? [] : nodes.blocks.map(b => b.index)
333
+ expandedBlocks: allExpanded
334
+ ? []
335
+ : nodes.blocks.map((b) => b.index),
327
336
  });
328
- }, 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] }), _jsx("table", { className: cn(this.styles.diffContainer, {
337
+ }, 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] }), _jsx("table", { className: cn(this.styles.diffContainer, {
329
338
  [this.styles.splitView]: splitView,
330
339
  }), onMouseUp: () => {
331
- const elements = document.getElementsByClassName('right');
340
+ const elements = document.getElementsByClassName("right");
332
341
  for (let i = 0; i < elements.length; i++) {
333
342
  const element = elements.item(i);
334
343
  element.classList.remove(this.styles.noSelect);
335
344
  }
336
- const elementsLeft = document.getElementsByClassName('left');
345
+ const elementsLeft = document.getElementsByClassName("left");
337
346
  for (let i = 0; i < elementsLeft.length; i++) {
338
347
  const element = elementsLeft.item(i);
339
348
  element.classList.remove(this.styles.noSelect);
340
349
  }
341
- }, children: _jsxs("tbody", { children: [_jsxs("tr", { children: [!this.props.hideLineNumbers ? _jsx("td", { width: '50px' }) : null, !splitView && !this.props.hideLineNumbers ? _jsx("td", { width: '50px' }) : null, this.props.renderGutter ? _jsx("td", { width: '50px' }) : null, _jsx("td", { width: '28px' }), _jsx("td", { width: '100%' }), splitView ? _jsxs(_Fragment, { children: [!this.props.hideLineNumbers ? _jsx("td", { width: '50px' }) : null, this.props.renderGutter ? _jsx("td", { width: '50px' }) : null, _jsx("td", { width: '28px' }), _jsx("td", { width: '100%' })] }) : null] }), leftTitle || rightTitle ? _jsxs("tr", { children: [_jsx("td", { colSpan: (splitView ? colSpanOnSplitView : colSpanOnInlineView), className: cn(this.styles.titleBlock, this.styles.column), role: 'columnheader', children: leftTitle ? _jsx("pre", { className: this.styles.contentText, children: leftTitle }) : null }), splitView ? _jsx("td", { colSpan: colSpanOnSplitView, className: cn(this.styles.titleBlock, this.styles.column), role: 'columnheader', children: rightTitle ? _jsx("pre", { className: this.styles.contentText, children: rightTitle }) : null }) : null] }) : null, nodes.diffNodes] }) })] }));
350
+ }, children: _jsxs("tbody", { children: [_jsxs("tr", { children: [!this.props.hideLineNumbers ? _jsx("td", { width: "50px" }) : null, !splitView && !this.props.hideLineNumbers ? (_jsx("td", { width: "50px" })) : null, this.props.renderGutter ? _jsx("td", { width: "50px" }) : null, _jsx("td", { width: "28px" }), _jsx("td", { width: "100%" }), splitView ? (_jsxs(_Fragment, { children: [!this.props.hideLineNumbers ? _jsx("td", { width: "50px" }) : null, this.props.renderGutter ? _jsx("td", { width: "50px" }) : null, _jsx("td", { width: "28px" }), _jsx("td", { width: "100%" })] })) : null] }), leftTitle || rightTitle ? (_jsxs("tr", { children: [_jsx("th", { colSpan: splitView ? colSpanOnSplitView : colSpanOnInlineView, className: cn(this.styles.titleBlock, this.styles.column), children: leftTitle ? (_jsx("pre", { className: this.styles.contentText, children: leftTitle })) : null }), splitView ? (_jsx("th", { colSpan: colSpanOnSplitView, className: cn(this.styles.titleBlock, this.styles.column), children: rightTitle ? (_jsx("pre", { className: this.styles.contentText, children: rightTitle })) : null })) : null] })) : null, nodes.diffNodes] }) })] }));
342
351
  };
343
352
  }
344
353
  export default DiffViewer;