fix(webui): keep numbered code tokens in one wrapping flow
This commit is contained in:
1 parent
219c8bb2bc
commit
e981e97f10
5 files changed
+100
-1
No files matched your search
@@ -35,9 +35,11 @@ const LazyHighlightedCode = lazy(async () => {
|
||||
const [
|
||||
{ default: SyntaxHighlighter },
|
||||
{ ViewportCodeRows },
|
||||
{ WrappedCodeRows },
|
||||
] = await Promise.all([
|
||||
import("react-syntax-highlighter/dist/esm/prism-async-light"),
|
||||
import("@/components/ViewportCodeRows"),
|
||||
import("@/components/WrappedCodeRows"),
|
||||
]);
|
||||
|
||||
return {
|
||||
@@ -84,7 +86,8 @@ const LazyHighlightedCode = lazy(async () => {
|
||||
PreTag="pre"
|
||||
showLineNumbers={showLineNumbers && !viewportHighlight}
|
||||
wrapLongLines={wrapLongLines}
|
||||
renderer={viewportHighlight ? props => <ViewportCodeRows {...props} /> : undefined}
|
||||
renderer={viewportHighlight ? props => <ViewportCodeRows {...props} />
|
||||
: showLineNumbers && wrapLongLines ? props => <WrappedCodeRows {...props} /> : undefined}
|
||||
>
|
||||
{code}
|
||||
</SyntaxHighlighter>
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
import type { SyntaxHighlighterProps } from "react-syntax-highlighter";
|
||||
import createElement from "react-syntax-highlighter/dist/esm/create-element";
|
||||
|
||||
type RowsProps = Parameters<NonNullable<SyntaxHighlighterProps["renderer"]>>[0];
|
||||
|
||||
/** Keep inline tokens in one wrapping flow beside the line-number gutter. */
|
||||
export function WrappedCodeRows({ rows, ...props }: RowsProps) {
|
||||
return rows.map((row, index) => {
|
||||
const [lineNumber, ...content] = row.children!;
|
||||
return createElement({
|
||||
...props,
|
||||
key: index,
|
||||
node: {
|
||||
...row,
|
||||
children: [lineNumber, {
|
||||
type: "element",
|
||||
tagName: "span",
|
||||
properties: { className: ["min-w-0", "flex-1"] },
|
||||
children: content,
|
||||
}],
|
||||
},
|
||||
});
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user