AncrelDocumentationv0.3.0

Search documentation

    Documentation / frontend

    Unified diff block

    Pass unified diff text directly to UnifiedDiffBlock from @ancrel/workspace/diff. File and hunk headers stay visible. Each hunk starts its own old and new line numbers. The left gutter advances on removals, the right gutter advances on additions, and context advances both. Literal plus, minus and space prefixes identify the operation when colors are unavailable.

    Live example

    Open playground ↗
    --- a/notes.txt
    +++ b/notes.txt
    @@ -1,2 +1,2 @@
     Title
    -First edition
    +Second edition

    App.tsx

    'use client'
    
    import { useState } from 'react'
    import { UnifiedDiffBlock, computeClientFileDiffs } from '@ancrel/workspace/diff'
    
    const serverDiff = '--- a/notes.txt\n+++ b/notes.txt\n@@ -1,2 +1,2 @@\n Title\n-First edition\n+Second edition'
    const files = computeClientFileDiffs({ 'notes.txt': 'Title\nFirst edition\n' },
      { 'notes.txt': 'Title\nWorking draft\n' })
    
    export default function App() {
      const [local, setLocal] = useState(false)
      return <>
        <label><input type="checkbox" checked={local} onChange={event => setLocal(event.target.checked)} />
          Show local preview</label>
        <UnifiedDiffBlock label={local ? 'Local preview' : 'Server diff'} diff={local ? files[0].diff : serverDiff} />
      </>
    }
    

    Expected behavior

    The server diff shows the change from First edition to Second edition. Select Show local preview to see Working draft as the added line. Both views retain literal change signs and line numbers.

    Local previews

    Use computeUnifiedDiffText(before, after, path?) to preview edits to a single text file. It looks ahead at most five lines, includes two lines of context around each change and merges overlapping context. Identical strings produce an empty preview.

    Use computeClientFileDiffs(before, after) for two maps of path to text. It returns changed paths in sorted order, each with a change kind and a preview produced by the same bounded algorithm. Both helpers are preview-only. Repeated lines or moves beyond the lookahead can produce a different alignment from a version-control tool. Generate authoritative patches on the server and pass that text to the component for review.

    Presentation

    Supply a descriptive label when a page contains several diffs. Each region supports horizontal scrolling and keyboard focus. Load the token and workspace stylesheets to apply the mono font, tabular line numbers and change colors.

    --ac-sys-diff-add-bg --ac-sys-diff-remove-bg

    Props

    PropTypeRequiredDefault
    classNamestringNo—
    diffstringYes—
    keyReact.Key | null | undefinedNo—
    labelstringNo—
    refReact.Ref<HTMLPreElement> | undefinedNo—

    Constraints

    • Use authoritative unified diff text for saved or reviewed patches.

    Anti-patterns

    • Applying the bounded-lookahead preview as a repository patch.