|
| 1 | +## A Cli DiffView Component like GitHub, Easy to use and feature complete. |
| 2 | + |
| 3 | +### Usage |
| 4 | + |
| 5 | +#### There are two ways to use this component: |
| 6 | + |
| 7 | +1. Use the `DiffView` component directly. |
| 8 | + |
| 9 | +```tsx |
| 10 | +import { DiffView, DiffModeEnum } from "@git-diff-view/cli"; |
| 11 | + |
| 12 | +<DiffView<string> |
| 13 | + // use data |
| 14 | + data={{ |
| 15 | + oldFile?: { fileName?: string | null; fileLang?: string | null; content?: string | null }; |
| 16 | + newFile?: { fileName?: string | null; fileLang?: string | null; content?: string | null }; |
| 17 | + hunks: string[]; |
| 18 | + }} |
| 19 | + width={number} |
| 20 | + extendData={{oldFile: {10: {data: 'foo'}}, newFile: {20: {data: 'bar'}}}} |
| 21 | + renderExtendLine={({ data }) => ReactNode} |
| 22 | + diffViewHighlight={boolean} |
| 23 | + diffViewTabSpace={boolean} |
| 24 | + diffViewTabWidth={"small" | "medium" | "large"} |
| 25 | + diffViewMode={DiffModeEnum.Split | DiffModeEnum.Unified} |
| 26 | + diffViewTheme={'light' | 'dark'} |
| 27 | +/> |
| 28 | + |
| 29 | +``` |
| 30 | + |
| 31 | +2. Use the `DiffView` component with `@git-diff-view/core` or `@git-diff-view/file` |
| 32 | + |
| 33 | +```tsx |
| 34 | +// with @git-diff-view/file |
| 35 | +import { DiffFile, generateDiffFile } from "@git-diff-view/file"; |
| 36 | +const file = generateDiffFile( |
| 37 | + data?.oldFile?.fileName || "", |
| 38 | + data?.oldFile?.content || "", |
| 39 | + data?.newFile?.fileName || "", |
| 40 | + data?.newFile?.content || "", |
| 41 | + data?.oldFile?.fileLang || "", |
| 42 | + data?.newFile?.fileLang || "" |
| 43 | +); |
| 44 | +file.init(); |
| 45 | +file.buildSplitDiffLines(); |
| 46 | +file.buildUnifiedDiffLines(); |
| 47 | + |
| 48 | +// with @git-diff-view/core |
| 49 | +import { DiffFile } from "@git-diff-view/core"; |
| 50 | +const file = new DiffFile( |
| 51 | + data?.oldFile?.fileName || "", |
| 52 | + data?.oldFile?.content || "", |
| 53 | + data?.newFile?.fileName || "", |
| 54 | + data?.newFile?.content || "", |
| 55 | + data?.hunks || [], |
| 56 | + data?.oldFile?.fileLang || "", |
| 57 | + data?.newFile?.fileLang || "" |
| 58 | +); |
| 59 | +file.init(); |
| 60 | +file.buildSplitDiffLines(); |
| 61 | +file.buildUnifiedDiffLines(); |
| 62 | + |
| 63 | +// use current data to render |
| 64 | +<DiffView diffFile={file} {...props} />; |
| 65 | +// or use the bundle data to render, eg: postMessage/httpRequest |
| 66 | +const bundle = file.getBundle(); |
| 67 | +const diffFile = DiffFile.createInstance(data || {}, bundle); |
| 68 | +<DiffView diffFile={diffFile} {...props} />; |
| 69 | +``` |
0 commit comments