How to use react-diff-view - 10 common examples

To help you get started, we’ve selected a few react-diff-view examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github mozilla / addons-code-manager / src / utils.spec.tsx View on Github external
it('returns a flattened list of all changes', () => {
      const diff = parseDiff(diffWithDeletions)[0];
      const changes = getAllHunkChanges(diff.hunks);

      // Check a line from the first hunk:
      expect(changes.filter((c) => c.lineNumber === 2)[0].content).toEqual(
        "import { Diff, DiffProps, parseDiff } from 'react-diff-view';",
      );
      // Check a line from the second hunk:
      expect(changes.filter((c) => c.lineNumber === 24)[0].content).toEqual(
        '    console.log({ hunk });',
      );
      // Check a line from the third hunk:
      expect(changes.filter((c) => c.lineNumber === 50)[0].content).toEqual(
        '          ',
      );
    });
  });
github theforeman / foreman / webpack / assets / javascripts / react_app / components / DiffView / DiffView.js View on Github external
threshold: 30,
    markLongDistanceDiff: true,
  });

  // old,new Text
  if (patch === '') {
    const gitDiff = getDiff(oldText, newText);
    const files = parseDiff(gitDiff);
    const hunk = files[0].hunks;

    return (
      hunk && 
    );
  }
  // Patch
  const files = parseDiff(
    patch
      .split('\n')
      .slice(1)
      .join('\n')
  );
  // eslint-disable-next-line react/prop-types
  const renderFile = ({ oldRevision, newRevision, type, hunks }) => (
    
  );
github mozilla / addons-code-manager / src / components / DiffView / index.spec.tsx View on Github external
it('renders multiple inline messages', () => {
    const externalMessages = [
      // Add a message to line 9 in the first hunk.
      { line: 9, uid: 'first' },
      // Add a message to line 23 in the second hunk.
      { line: 23, uid: 'second' },
    ];

    const diff = parseDiff(diffWithDeletions)[0];
    const widgets = renderAndGetWidgets({
      diff,
      selectedMessageMap: createFakeLinterMessagesByPath({
        messages: externalMessages,
      }),
    });

    const { hunks } = diff;

    const firstWidget = renderWidget(hunks, widgets, externalMessages[0].line);
    expect(firstWidget.find(LinterMessage)).toHaveLength(1);
    expect(firstWidget.find(LinterMessage)).toHaveProp(
      'message',
      expect.objectContaining({
        uid: externalMessages[0].uid,
      }),
github mozilla / addons-code-manager / src / reducers / versions.spec.tsx View on Github external
const createFakeDiffWithChanges = (
    testHunks: TestHunkChange[][],
  ): DiffInfo => {
    // The fixture is only used to initialize some fields. All of the
    // hunks/changes will be overwritten.
    const diffSample = parseDiff(diffWithDeletions)[0];

    const hunks = testHunks.map((hunk) => {
      return {
        changes: hunk.map((change) => {
          return {
            content: 'the content of the change is irrelevant to the tests',
            isDelete: change.type === 'delete',
            isInsert: change.type === 'insert',
            isNormal: change.type === 'normal',
            lineNumber: change.lineNumber,
            oldLineNumber:
              change.type === 'normal' ? change.lineNumber : undefined,
            type: change.type,
          };
        }),
        content: 'the content of hunks is irrelevant to the tests',
github argoproj / argo-cd / ui / src / app / applications / components / application-resources-diff / application-resources-diff.tsx View on Github external
};
                })
                .filter(i => !i.hook)
                .filter(i => i.a !== i.b)
                .map(i => {
                    const context = pref.appDetails.compactDiff ? 2 : Number.MAX_SAFE_INTEGER;
                    // react-diff-view, awesome as it is, does not accept unidiff format, you must add a git header section
                    return `diff --git a/${i.name} b/${i.name}
index 6829b8a2..4c565f1b 100644
${formatLines(diffLines(i.a, i.b), {context, aname: `a/${name}}`, bname: `b/${i.name}`})}`;
                })
                .join('\n');
            // assume that if you only have one file, we don't need the file path
            const whiteBox = props.states.length > 1 ? 'white-box' : '';
            const showPath = props.states.length > 1;
            const files = parseDiff(diffText);
            const viewType = pref.appDetails.inlineDiff ? 'unified' : 'split';
            return (
                <div>
                    <div>
                        
                                services.viewPreferences.updatePreferences({
                                    appDetails: {
                                        ...pref.appDetails,
                                        compactDiff: !pref.appDetails.compactDiff
                                    }
                                })
                            }
                        /&gt;</div></div>
github mozilla / addons-code-manager / src / reducers / versions.tsx View on Github external
export const getDiffAnchors = (diff: DiffInfo): string[] => {
  const anchors: string[] = [];

  for (const hunk of diff.hunks) {
    let seekingChange = true;
    for (const change of hunk.changes) {
      if (change.type === 'normal') {
        seekingChange = true;
      } else if (seekingChange) {
        // This is the first change in a block.
        anchors.push(getChangeKey(change));
        seekingChange = false;
      }
    }
  }
  return anchors;
};
github mozilla / addons-code-manager / src / components / DiffView / index.tsx View on Github external
getWidgets = (
    hunks: Hunks,
    selectedMessageMap: LinterProviderInfo['selectedMessageMap'],
  ) =&gt; {
    const { _changeCanBeCommentedUpon, enableCommenting, version } = this.props;

    const allWidgets: WidgetMap = {};

    // We only want widgets for delete, insert and normal changes, not eofnl changes.
    const changesAllowingWidgets = getAllHunkChanges(hunks).filter(
      (change) =&gt; change.isDelete || change.isInsert || change.isNormal,
    );
    for (const change of changesAllowingWidgets) {
      const changeKey = getChangeKey(change);
      const line = change.lineNumber;

      let messages;
      if (line &amp;&amp; selectedMessageMap) {
        messages = selectedMessageMap.byLine[line];
      }

      let widget =
        enableCommenting &amp;&amp; line &amp;&amp; _changeCanBeCommentedUpon(change) ? (
          
            {(allComments) =&gt; allComments}
github otakustay / react-diff-view / demo / selectors / index.js View on Github external
(canExpand, hunks) =&gt; {
        if (canExpand) {
            // Expand a normal section to demostrate util function
            return expandCollapsedBlockBy(
                expandFromRawCode(hunks, rawCode, 18, 22),
                rawCode,
                lines =&gt; lines &lt;= 10
            );
        }

        return hunks;
    }
);
github theforeman / foreman / webpack / assets / javascripts / react_app / components / DiffView / DiffView.js View on Github external
const DiffView = ({ oldText, newText, viewType, patch }) =&gt; {
  const markEdits = markCharacterEdits({
    threshold: 30,
    markLongDistanceDiff: true,
  });

  // old,new Text
  if (patch === '') {
    const gitDiff = getDiff(oldText, newText);
    const files = parseDiff(gitDiff);
    const hunk = files[0].hunks;

    return (
      hunk &amp;&amp; 
    );
  }
  // Patch
  const files = parseDiff(
github patternfly / patternfly-react / packages / patternfly-3 / patternfly-react-extensions / src / components / DiffView / DiffView.js View on Github external
const DiffView = ({
  oldText,
  newText,
  className,
  viewType,
  patch,
  markEditsByWord,
  emptyState,
  markThreshold,
  markLongDistanceDiff,
  ...props
}) =&gt; {
  const markEditProps = { markThreshold, markLongDistanceDiff };
  const markEdits = markEditsByWord ? markWordEdits(markEditProps) : markCharacterEdits(markEditProps);
  const classes = classNames('diff-pf', className);

  // Old, New Text
  if (!patch) {
    const gitDiff = getDiff(oldText, newText);
    const files = parseDiff(gitDiff);
    const hunk = files[0].hunks;

    if (hunk.length === 0) {
      return emptyState;
    }
    return hunk &amp;&amp; ;
  }

  // Patch
  const files = parseDiff(patch);