-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathCodeEditor.tsx
More file actions
188 lines (162 loc) · 5.69 KB
/
CodeEditor.tsx
File metadata and controls
188 lines (162 loc) · 5.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import { useRef, useEffect, useMemo, useState, useCallback } from 'react'
import type { ImperativePanelGroupHandle, PanelOnResize } from 'react-resizable-panels'
import { useCodeMirror, type VisibleLineRange } from '@/hooks/use-codemirror'
import { TocPanel } from './TocPanel'
import { ResizablePanelGroup, ResizablePanel, ResizableHandle } from '@/components/ui/resizable'
import { useTocSettingsStore } from '@/stores/toc-settings-store'
import { TOC_MAX_WIDTH, TOC_MIN_WIDTH } from '@/types/settings'
interface CodeEditorProps {
filePath: string
content: string
language: string
readOnly?: boolean
isVisible: boolean
onChange: (content: string) => void
onCursorChange: (line: number, col: number) => void
onScrollChange: (scrollTop: number) => void
}
function getTocPercentBounds(panelWidth: number): { minPercent: number; maxPercent: number } {
const minPercent = (TOC_MIN_WIDTH / panelWidth) * 100
const maxPercent = (TOC_MAX_WIDTH / panelWidth) * 100
return {
minPercent,
maxPercent: Math.max(minPercent, maxPercent)
}
}
export function CodeEditor({
content,
language,
readOnly = false,
isVisible,
onChange,
onCursorChange,
onScrollChange
}: CodeEditorProps): React.JSX.Element {
const containerRef = useRef<HTMLDivElement>(null)
const layoutRef = useRef<HTMLDivElement>(null)
const panelGroupRef = useRef<ImperativePanelGroupHandle>(null)
const [visibleRange, setVisibleRange] = useState<VisibleLineRange | undefined>()
const [layoutWidth, setLayoutWidth] = useState(0)
const { isTocHydrated, isTocVisible, tocWidth, setTocWidth } = useTocSettingsStore((state) => ({
isTocHydrated: state.isLoaded || state.loadFailed,
isTocVisible: state.settings.isVisible,
tocWidth: state.settings.width,
setTocWidth: state.setWidth
}))
const { view, setContent, scrollToLine } = useCodeMirror(containerRef, {
content,
language,
readOnly,
onChange,
onCursorChange,
onScrollChange,
onVisibleRangeChange: setVisibleRange
})
const getPanelWidth = useCallback((): number => {
return layoutWidth || layoutRef.current?.clientWidth || 1000
}, [layoutWidth])
const getTocPanelSizePercent = useCallback((): number => {
const panelWidth = getPanelWidth()
const { minPercent, maxPercent } = getTocPercentBounds(panelWidth)
const widthRatio = panelWidth > 0 ? tocWidth / panelWidth : 0
return Math.min(maxPercent, Math.max(minPercent, widthRatio * 100))
}, [getPanelWidth, tocWidth])
const tocPanelBounds = useMemo(() => getTocPercentBounds(getPanelWidth()), [getPanelWidth])
const tocPanelDefaultSize = useMemo(() => getTocPanelSizePercent(), [getTocPanelSizePercent])
const canRenderToc = isTocHydrated && isTocVisible && language === 'markdown'
const handleTocResize = useCallback<PanelOnResize>(
(size, prevSize): void => {
const panelWidth = getPanelWidth()
const { minPercent, maxPercent } = getTocPercentBounds(panelWidth)
const clampedSize = Math.min(maxPercent, Math.max(minPercent, size))
const nextPixels = Math.round((clampedSize / 100) * panelWidth)
if (prevSize !== size) {
setTocWidth(nextPixels)
}
},
[getPanelWidth, setTocWidth]
)
// Update content when it changes from external source (file reload)
const prevContentRef = useRef(content)
useEffect(() => {
if (content !== prevContentRef.current) {
setContent(content)
prevContentRef.current = content
}
}, [content, setContent])
useEffect(() => {
const element = layoutRef.current
if (!element) {
return
}
const updateLayoutWidth = (): void => {
setLayoutWidth(element.clientWidth)
}
updateLayoutWidth()
const observer = new ResizeObserver(() => {
updateLayoutWidth()
})
observer.observe(element)
return () => observer.disconnect()
}, [])
// Focus when becoming visible
useEffect(() => {
if (isVisible && view) {
view.focus()
}
}, [isVisible, view])
useEffect(() => {
if (!canRenderToc) {
return
}
const group = panelGroupRef.current
if (!group) {
return
}
const tocSize = getTocPanelSizePercent()
const currentTocSize = group.getLayout()[1]
if (currentTocSize !== undefined && Math.abs(currentTocSize - tocSize) < 0.5) {
return
}
group.setLayout([100 - tocSize, tocSize])
}, [canRenderToc, getTocPanelSizePercent])
return (
<div className="h-full w-full" style={{ display: isVisible ? 'block' : 'none' }}>
<div ref={layoutRef} className="h-full w-full">
<ResizablePanelGroup ref={panelGroupRef} direction="horizontal">
<ResizablePanel
defaultSize={canRenderToc ? 100 - tocPanelDefaultSize : 100}
minSize={60}
>
<div ref={containerRef} className="w-full h-full overflow-hidden" />
</ResizablePanel>
{canRenderToc && (
<>
<ResizableHandle />
<ResizablePanel
defaultSize={tocPanelDefaultSize}
minSize={tocPanelBounds.minPercent}
maxSize={tocPanelBounds.maxPercent}
onResize={handleTocResize}
>
<div
className="h-full"
style={{ minWidth: TOC_MIN_WIDTH, maxWidth: TOC_MAX_WIDTH, width: '100%' }}
>
<TocPanel
editorMode="codemirror"
codemirror={{
content,
scrollToLine,
visibleRange
}}
/>
</div>
</ResizablePanel>
</>
)}
</ResizablePanelGroup>
</div>
</div>
)
}