diff.tsx 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. import {
  2. type FileContents,
  3. FileDiff,
  4. type DiffLineAnnotation,
  5. type HunkData,
  6. FileDiffOptions,
  7. } from "@pierre/precision-diffs"
  8. import { ComponentProps, createEffect, splitProps } from "solid-js"
  9. export type DiffProps<T = {}> = FileDiffOptions<T> & {
  10. before: FileContents
  11. after: FileContents
  12. annotations?: DiffLineAnnotation<T>[]
  13. class?: string
  14. classList?: ComponentProps<"div">["classList"]
  15. }
  16. // interface ThreadMetadata {
  17. // threadId: string
  18. // }
  19. export function Diff<T>(props: DiffProps<T>) {
  20. let container!: HTMLDivElement
  21. const [local, others] = splitProps(props, ["before", "after", "class", "classList", "annotations"])
  22. // const lineAnnotations: DiffLineAnnotation<ThreadMetadata>[] = [
  23. // {
  24. // side: "additions",
  25. // // The line number specified for an annotation is the visual line number
  26. // // you see in the number column of a diff
  27. // lineNumber: 16,
  28. // metadata: { threadId: "68b329da9893e34099c7d8ad5cb9c940" },
  29. // },
  30. // ]
  31. // If you ever want to update the options for an instance, simple call
  32. // 'setOptions' with the new options. Bear in mind, this does NOT merge
  33. // existing properties, it's a full replace
  34. // instance.setOptions({
  35. // ...instance.options,
  36. // theme: "pierre-dark",
  37. // themes: undefined,
  38. // })
  39. //
  40. // When ready to render, simply call .render with old/new file, optional
  41. // annotations and a container element to hold the diff
  42. createEffect(() => {
  43. const instance = new FileDiff<T>({
  44. theme: "OpenCode",
  45. // When using the 'themes' prop, 'themeType' allows you to force 'dark'
  46. // or 'light' theme, or inherit from the OS ('system') theme.
  47. themeType: "system",
  48. // Disable the line numbers for your diffs, generally not recommended
  49. disableLineNumbers: false,
  50. // Whether code should 'wrap' with long lines or 'scroll'.
  51. overflow: "wrap",
  52. // Normally you shouldn't need this prop, but if you don't provide a
  53. // valid filename or your file doesn't have an extension you may want to
  54. // override the automatic detection. You can specify that language here:
  55. // https://shiki.style/languages
  56. // lang?: SupportedLanguages;
  57. // 'diffStyle' controls whether the diff is presented side by side or
  58. // in a unified (single column) view
  59. diffStyle: "unified",
  60. // Line decorators to help highlight changes.
  61. // 'bars' (default):
  62. // Shows some red-ish or green-ish (theme dependent) bars on the left
  63. // edge of relevant lines
  64. //
  65. // 'classic':
  66. // shows '+' characters on additions and '-' characters on deletions
  67. //
  68. // 'none':
  69. // No special diff indicators are shown
  70. diffIndicators: "bars",
  71. // By default green-ish or red-ish background are shown on added and
  72. // deleted lines respectively. Disable that feature here
  73. disableBackground: false,
  74. // Diffs are split up into hunks, this setting customizes what to show
  75. // between each hunk.
  76. //
  77. // 'line-info' (default):
  78. // Shows a bar that tells you how many lines are collapsed. If you are
  79. // using the oldFile/newFile API then you can click those bars to
  80. // expand the content between them
  81. //
  82. // 'metadata':
  83. // Shows the content you'd see in a normal patch file, usually in some
  84. // format like '@@ -60,6 +60,22 @@'. You cannot use these to expand
  85. // hidden content
  86. //
  87. // 'simple':
  88. // Just a subtle bar separator between each hunk
  89. // hunkSeparators: "line-info",
  90. hunkSeparators(hunkData: HunkData) {
  91. const fragment = document.createDocumentFragment()
  92. const numCol = document.createElement("div")
  93. numCol.innerHTML = `<svg data-slot="diff-hunk-separator-line-number-icon" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M8.97978 14.0204L8.62623 13.6668L9.33334 12.9597L9.68689 13.3133L9.33333 13.6668L8.97978 14.0204ZM12 16.3335L12.3535 16.6871L12 17.0406L11.6464 16.687L12 16.3335ZM14.3131 13.3133L14.6667 12.9597L15.3738 13.6668L15.0202 14.0204L14.6667 13.6668L14.3131 13.3133ZM12.5 16.0002V16.5002H11.5V16.0002H12H12.5ZM9.33333 13.6668L9.68689 13.3133L12.3535 15.9799L12 16.3335L11.6464 16.687L8.97978 14.0204L9.33333 13.6668ZM12 16.3335L11.6464 15.9799L14.3131 13.3133L14.6667 13.6668L15.0202 14.0204L12.3535 16.6871L12 16.3335ZM6.5 8.00016V7.50016H8.5V8.00016V8.50016H6.5V8.00016ZM9.5 8.00016V7.50016H11.5V8.00016V8.50016H9.5V8.00016ZM12.5 8.00016V7.50016H14.5V8.00016V8.50016H12.5V8.00016ZM15.5 8.00016V7.50016H17.5V8.00016V8.50016H15.5V8.00016ZM12 10.5002H12.5V16.0002H12H11.5V10.5002H12Z" fill="currentColor"/></svg> `
  94. numCol.dataset["slot"] = "diff-hunk-separator-line-number"
  95. fragment.appendChild(numCol)
  96. const contentCol = document.createElement("div")
  97. contentCol.dataset["slot"] = "diff-hunk-separator-content"
  98. const span = document.createElement("span")
  99. span.dataset["slot"] = "diff-hunk-separator-content-span"
  100. span.textContent = `${hunkData.lines} unmodified lines`
  101. contentCol.appendChild(span)
  102. fragment.appendChild(contentCol)
  103. return fragment
  104. },
  105. // On lines that have both additions and deletions, we can run a
  106. // separate diff check to mark parts of the lines that change.
  107. // 'none':
  108. // Do not show these secondary highlights
  109. //
  110. // 'char':
  111. // Show changes at a per character granularity
  112. //
  113. // 'word':
  114. // Show changes but rounded up to word boundaries
  115. //
  116. // 'word-alt' (default):
  117. // Similar to 'word', however we attempt to minimize single character
  118. // gaps between highlighted changes
  119. lineDiffType: "word-alt",
  120. // If lines exceed these character lengths then we won't perform the
  121. // line lineDiffType check
  122. maxLineDiffLength: 1000,
  123. // If any line in the diff exceeds this value then we won't attempt to
  124. // syntax highlight the diff
  125. maxLineLengthForHighlighting: 1000,
  126. // Enabling this property will hide the file header with file name and
  127. // diff stats.
  128. disableFileHeader: true,
  129. // You can optionally pass a render function for rendering out line
  130. // annotations. Just return the dom node to render
  131. // renderAnnotation(annotation: DiffLineAnnotation<T>): HTMLElement {
  132. // // Despite the diff itself being rendered in the shadow dom,
  133. // // annotations are inserted via the web components 'slots' api and you
  134. // // can use all your normal normal css and styling for them
  135. // const element = document.createElement("div")
  136. // element.innerText = annotation.metadata.threadId
  137. // return element
  138. // },
  139. ...others,
  140. })
  141. container.innerHTML = ""
  142. instance.render({
  143. oldFile: local.before,
  144. newFile: local.after,
  145. lineAnnotations: local.annotations,
  146. containerWrapper: container,
  147. })
  148. })
  149. return (
  150. <div
  151. data-component="diff"
  152. style={{
  153. "--pjs-font-family": "var(--font-family-mono)",
  154. "--pjs-font-size": "var(--font-size-small)",
  155. "--pjs-line-height": "24px",
  156. "--pjs-tab-size": 2,
  157. "--pjs-font-features": "var(--font-family-mono--font-feature-settings)",
  158. "--pjs-header-font-family": "var(--font-family-sans)",
  159. "--pjs-gap-block": 0,
  160. "--pjs-min-number-column-width": "4ch",
  161. }}
  162. ref={container}
  163. />
  164. )
  165. }