2025-12-16 00:56:03 +08:00
|
|
|
import { type FileContents, File, FileOptions, LineAnnotation } from "@pierre/diffs"
|
2025-12-08 20:24:19 +08:00
|
|
|
import { ComponentProps, createEffect, createMemo, splitProps } from "solid-js"
|
2025-12-02 20:50:16 +08:00
|
|
|
import { createDefaultOptions, styleVariables } from "../pierre"
|
2025-12-31 23:23:24 +08:00
|
|
|
import { getWorkerPool } from "../pierre/worker"
|
2025-11-04 04:44:25 +08:00
|
|
|
|
|
|
|
|
export type CodeProps<T = {}> = FileOptions<T> & {
|
|
|
|
|
file: FileContents
|
|
|
|
|
annotations?: LineAnnotation<T>[]
|
|
|
|
|
class?: string
|
|
|
|
|
classList?: ComponentProps<"div">["classList"]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function Code<T>(props: CodeProps<T>) {
|
|
|
|
|
let container!: HTMLDivElement
|
|
|
|
|
const [local, others] = splitProps(props, ["file", "class", "classList", "annotations"])
|
|
|
|
|
|
2025-12-08 20:24:19 +08:00
|
|
|
const file = createMemo(
|
|
|
|
|
() =>
|
|
|
|
|
new File<T>(
|
|
|
|
|
{
|
|
|
|
|
...createDefaultOptions<T>("unified"),
|
|
|
|
|
...others,
|
|
|
|
|
},
|
2025-12-31 23:23:24 +08:00
|
|
|
getWorkerPool("unified"),
|
2025-12-08 20:24:19 +08:00
|
|
|
),
|
|
|
|
|
)
|
2025-11-04 04:44:25 +08:00
|
|
|
|
2025-12-08 20:24:19 +08:00
|
|
|
createEffect(() => {
|
2025-11-06 01:55:31 +08:00
|
|
|
container.innerHTML = ""
|
2025-12-08 20:24:19 +08:00
|
|
|
file().render({
|
2025-11-06 01:55:31 +08:00
|
|
|
file: local.file,
|
2025-11-04 04:44:25 +08:00
|
|
|
lineAnnotations: local.annotations,
|
|
|
|
|
containerWrapper: container,
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div
|
|
|
|
|
data-component="code"
|
2025-11-26 09:59:37 +08:00
|
|
|
style={styleVariables}
|
2025-11-04 04:44:25 +08:00
|
|
|
classList={{
|
|
|
|
|
...(local.classList || {}),
|
|
|
|
|
[local.class ?? ""]: !!local.class,
|
|
|
|
|
}}
|
|
|
|
|
ref={container}
|
|
|
|
|
/>
|
|
|
|
|
)
|
|
|
|
|
}
|