2026-01-04 09:57:55 +08:00
|
|
|
import { checksum } from "@opencode-ai/util/encode"
|
2025-12-16 00:56:03 +08:00
|
|
|
import { FileDiff } from "@pierre/diffs"
|
2025-12-08 20:24:19 +08:00
|
|
|
import { createEffect, createMemo, onCleanup, splitProps } from "solid-js"
|
2025-12-02 20:50:16 +08:00
|
|
|
import { createDefaultOptions, type DiffProps, styleVariables } from "../pierre"
|
2025-12-31 23:23:24 +08:00
|
|
|
import { getWorkerPool } from "../pierre/worker"
|
2025-10-24 21:26:17 +08:00
|
|
|
|
2025-10-25 00:23:32 +08:00
|
|
|
export function Diff<T>(props: DiffProps<T>) {
|
|
|
|
|
let container!: HTMLDivElement
|
2025-11-08 09:59:02 +08:00
|
|
|
const [local, others] = splitProps(props, ["before", "after", "class", "classList", "annotations"])
|
2025-10-24 21:26:17 +08:00
|
|
|
|
2026-01-04 09:57:55 +08:00
|
|
|
const options = createMemo(() => ({
|
|
|
|
|
...createDefaultOptions(props.diffStyle),
|
|
|
|
|
...others,
|
|
|
|
|
}))
|
|
|
|
|
|
|
|
|
|
let instance: FileDiff<T> | undefined
|
2025-12-08 20:24:19 +08:00
|
|
|
|
|
|
|
|
createEffect(() => {
|
2026-01-04 09:57:55 +08:00
|
|
|
const opts = options()
|
|
|
|
|
const workerPool = getWorkerPool(props.diffStyle)
|
|
|
|
|
const annotations = local.annotations
|
2026-01-06 19:22:47 +08:00
|
|
|
const beforeContents = typeof local.before?.contents === "string" ? local.before.contents : ""
|
|
|
|
|
const afterContents = typeof local.after?.contents === "string" ? local.after.contents : ""
|
2026-01-04 09:57:55 +08:00
|
|
|
|
|
|
|
|
instance?.cleanUp()
|
|
|
|
|
instance = new FileDiff<T>(opts, workerPool)
|
|
|
|
|
|
2025-12-08 20:24:19 +08:00
|
|
|
container.innerHTML = ""
|
2026-01-04 09:57:55 +08:00
|
|
|
instance.render({
|
|
|
|
|
oldFile: {
|
|
|
|
|
...local.before,
|
2026-01-06 19:22:47 +08:00
|
|
|
contents: beforeContents,
|
|
|
|
|
cacheKey: checksum(beforeContents),
|
2026-01-04 09:57:55 +08:00
|
|
|
},
|
|
|
|
|
newFile: {
|
|
|
|
|
...local.after,
|
2026-01-06 19:22:47 +08:00
|
|
|
contents: afterContents,
|
|
|
|
|
cacheKey: checksum(afterContents),
|
2026-01-04 09:57:55 +08:00
|
|
|
},
|
|
|
|
|
lineAnnotations: annotations,
|
2025-11-25 21:40:11 +08:00
|
|
|
containerWrapper: container,
|
|
|
|
|
})
|
2026-01-04 09:57:55 +08:00
|
|
|
})
|
2025-11-25 21:40:11 +08:00
|
|
|
|
2026-01-04 09:57:55 +08:00
|
|
|
onCleanup(() => {
|
|
|
|
|
instance?.cleanUp()
|
2025-10-24 21:26:17 +08:00
|
|
|
})
|
|
|
|
|
|
2025-12-02 20:50:16 +08:00
|
|
|
return <div data-component="diff" style={styleVariables} ref={container} />
|
2025-10-24 21:26:17 +08:00
|
|
|
}
|