opencode/packages/ui/src/components/code.tsx

49 lines
1.2 KiB
TypeScript
Raw Normal View History

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"
import { createDefaultOptions, styleVariables } from "../pierre"
2025-12-08 20:24:19 +08:00
import { workerPool } 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,
},
workerPool,
),
)
2025-11-04 04:44:25 +08:00
2025-12-08 20:24:19 +08:00
createEffect(() => {
container.innerHTML = ""
2025-12-08 20:24:19 +08:00
file().render({
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}
/>
)
}