|
1
|
"use client" |
|
2
|
|
|
3
|
import { |
|
4
|
LineInputProps, |
|
5
|
LineOutputProps, |
|
6
|
Token, |
|
7
|
TokenInputProps, |
|
8
|
TokenOutputProps, |
|
9
|
} from "prism-react-renderer" |
|
10
|
import React, { useCallback, useMemo, useRef } from "react" |
|
11
|
import { CodeBlockCollapsibleLines } from "../../components/CodeBlock/Collapsible/Lines" |
|
12
|
import { useCollapsible } from "../use-collapsible" |
|
13
|
|
|
14
|
export type HighlightProps = { |
|
15
|
getLineProps: (input: LineInputProps) => LineOutputProps |
|
16
|
getTokenProps: (input: TokenInputProps) => TokenOutputProps |
|
17
|
} |
|
18
|
|
|
19
|
export type CollapsibleCodeLines = { |
|
20
|
collapsibleLinesStr?: string |
|
21
|
getLines: ( |
|
22
|
token: Token[][], |
|
23
|
highlightProps: HighlightProps, |
|
24
|
lineNumberOffset?: number |
|
25
|
) => React.JSX.Element[] |
|
26
|
} |
|
27
|
|
|
28
|
export type CollapsedCodeLinesPosition = "start" | "end" |
|
29
|
|
|
30
|
export const useCollapsibleCodeLines = ({ |
|
31
|
collapsibleLinesStr, |
|
32
|
getLines, |
|
33
|
}: CollapsibleCodeLines) => { |
|
34
|
const collapsedRange: |
|
35
|
| { |
|
36
|
start: number |
|
37
|
end: number | undefined |
|
38
|
} |
|
39
|
| undefined = useMemo(() => { |
|
40
|
if (!collapsibleLinesStr) { |
|
41
|
return |
|
42
|
} |
|
43
|
|
|
44
|
const splitCollapsedLines = collapsibleLinesStr |
|
45
|
.split("-") |
|
46
|
.map((lineNumber) => parseInt(lineNumber)) |
|
47
|
|
|
48
|
if ( |
|
49
|
!splitCollapsedLines.length || |
|
50
|
(splitCollapsedLines.length >= 2 && |
|
51
|
splitCollapsedLines[0] !== 1 && |
|
52
|
splitCollapsedLines[1] < 2) |
|
53
|
) { |
|
54
|
return |
|
55
|
} |
|
56
|
|
|
57
|
return { |
|
58
|
start: splitCollapsedLines[0], |
|
59
|
end: splitCollapsedLines[1], |
|
60
|
} |
|
61
|
}, [collapsibleLinesStr]) |
|
62
|
|
|
63
|
const isCollapsible = useCallback( |
|
64
|
(tokens: Token[][]) => { |
|
65
|
return collapsedRange && collapsedRange.start < tokens.length |
|
66
|
}, |
|
67
|
[collapsedRange] |
|
68
|
) |
|
69
|
|
|
70
|
const type: CollapsedCodeLinesPosition | undefined = useMemo(() => { |
|
71
|
if (!collapsedRange) { |
|
72
|
return undefined |
|
73
|
} |
|
74
|
return collapsedRange.start === 1 ? "start" : "end" |
|
75
|
}, [collapsedRange]) |
|
76
|
|
|
77
|
const ref = useRef(null) |
|
78
|
const collapsibleHookResult = useCollapsible({ |
|
79
|
unmountOnExit: false, |
|
80
|
translateEnabled: false, |
|
81
|
heightAnimation: true, |
|
82
|
childrenRef: ref, |
|
83
|
}) |
|
84
|
|
|
85
|
const getCollapsedLinesElm = useCallback( |
|
86
|
({ |
|
87
|
tokens, |
|
88
|
highlightProps, |
|
89
|
}: { |
|
90
|
tokens: Token[][] |
|
91
|
highlightProps: HighlightProps |
|
92
|
}) => { |
|
93
|
if (!collapsedRange || !type || !isCollapsible(tokens)) { |
|
94
|
return <></> |
|
95
|
} |
|
96
|
|
|
97
|
const startIndex = |
|
98
|
type === "start" ? collapsedRange.start - 1 : collapsedRange.start |
|
99
|
|
|
100
|
const lines = tokens.slice( |
|
101
|
startIndex, |
|
102
|
collapsedRange.end |
|
103
|
? Math.min(collapsedRange.end, tokens.length) |
|
104
|
: tokens.length |
|
105
|
) |
|
106
|
|
|
107
|
return ( |
|
108
|
<CodeBlockCollapsibleLines {...collapsibleHookResult} type={type}> |
|
109
|
{getLines(lines, highlightProps, startIndex)} |
|
110
|
</CodeBlockCollapsibleLines> |
|
111
|
) |
|
112
|
}, |
|
113
|
[collapsedRange, collapsibleHookResult, isCollapsible, type] |
|
114
|
) |
|
115
|
|
|
116
|
const getNonCollapsedLinesElm = useCallback( |
|
117
|
({ |
|
118
|
tokens, |
|
119
|
highlightProps, |
|
120
|
}: { |
|
121
|
tokens: Token[][] |
|
122
|
highlightProps: HighlightProps |
|
123
|
}) => { |
|
124
|
if (!collapsedRange || !isCollapsible(tokens)) { |
|
125
|
return getLines(tokens, highlightProps) |
|
126
|
} |
|
127
|
|
|
128
|
const isCollapseBeginning = collapsedRange.start === 1 |
|
129
|
const lines = tokens.slice( |
|
130
|
isCollapseBeginning ? collapsedRange.end || tokens.length : 0, |
|
131
|
isCollapseBeginning ? undefined : collapsedRange.start |
|
132
|
) |
|
133
|
|
|
134
|
return getLines( |
|
135
|
lines, |
|
136
|
highlightProps, |
|
137
|
isCollapseBeginning ? collapsedRange.end : 0 |
|
138
|
) |
|
139
|
}, |
|
140
|
[collapsedRange, collapsibleHookResult, isCollapsible] |
|
141
|
) |
|
142
|
|
|
143
|
return { |
|
144
|
getCollapsedLinesElm, |
|
145
|
getNonCollapsedLinesElm, |
|
146
|
type, |
|
147
|
isCollapsible, |
|
148
|
...collapsibleHookResult, |
|
149
|
} |
|
150
|
} |