Skip to content

Commit 39190e1

Browse files
authored
Merge pull request #49 from code-hike/scrollbars
Add scrollbars
2 parents 0983368 + d3183ba commit 39190e1

File tree

8 files changed

+145
-34
lines changed

8 files changed

+145
-34
lines changed

packages/mini-editor/src/code.tsx

+7-12
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ type CodeProps = {
1616
language: string
1717
parentHeight?: number
1818
minColumns: number
19+
minZoom: number
20+
maxZoom: number
1921
}
2022

2123
export function Code({
@@ -27,6 +29,8 @@ export function Code({
2729
language,
2830
parentHeight,
2931
minColumns,
32+
minZoom,
33+
maxZoom,
3034
}: CodeProps) {
3135
const {
3236
prevLines,
@@ -53,17 +57,7 @@ export function Code({
5357
<pre
5458
ref={ref}
5559
className={`language-${language}`}
56-
style={{
57-
position: "absolute",
58-
top: 0,
59-
bottom: 0,
60-
right: 16,
61-
left: 16,
62-
padding: 0,
63-
margin: 0,
64-
overflow: "hidden",
65-
opacity: dimensions ? 1 : 0,
66-
}}
60+
style={{ opacity: dimensions ? 1 : 0 }}
6761
>
6862
<code>
6963
{dimensions ? (
@@ -85,7 +79,8 @@ export function Code({
8579
}
8680
prevFocus={prevFocusIndexes}
8781
nextFocus={nextFocusIndexes}
88-
maxZoom={1}
82+
minZoom={minZoom}
83+
maxZoom={maxZoom}
8984
/>
9085
) : (
9186
<>

packages/mini-editor/src/index.scss

+12-1
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,22 @@
4343
height: 100%;
4444
color: #cccccc;
4545
font-size: 15px;
46-
padding: 5px 10px;
46+
// padding: 5px 10px;
4747
line-height: 1.1rem;
4848
box-sizing: border-box;
4949
}
5050

51+
.ch-editor-body pre {
52+
position: absolute;
53+
top: 0;
54+
bottom: 0;
55+
left: 0;
56+
right: 0;
57+
padding: 0 16px;
58+
margin: 0;
59+
overflow: auto;
60+
}
61+
5162
.ch-editor-body code {
5263
line-height: 20px;
5364
}

packages/mini-editor/src/mini-editor.tsx

+12
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ export type MiniEditorProps = {
3131
steps?: MiniEditorStep[]
3232
height?: number
3333
minColumns?: number
34+
minZoom?: number
35+
maxZoom?: number
3436
button?: React.ReactNode
3537
classes?: Classes
3638
} & React.PropsWithoutRef<JSX.IntrinsicElements["div"]>
@@ -46,6 +48,8 @@ function MiniEditor(props: MiniEditorProps) {
4648
steps: ogSteps,
4749
tabs: ogTabs,
4850
minColumns = 50,
51+
minZoom = 0.2,
52+
maxZoom = 1,
4953
height,
5054
...rest
5155
} = props
@@ -101,6 +105,8 @@ function MiniEditor(props: MiniEditorProps) {
101105
steps={contentSteps}
102106
parentHeight={height}
103107
minColumns={minColumns}
108+
minZoom={minZoom}
109+
maxZoom={maxZoom}
104110
/>
105111
)}
106112
</EditorFrame>
@@ -155,6 +161,8 @@ type ContentProps = {
155161
steps: ContentStep[]
156162
parentHeight?: number
157163
minColumns: number
164+
minZoom: number
165+
maxZoom: number
158166
}
159167

160168
function EditorContent({
@@ -163,6 +171,8 @@ function EditorContent({
163171
steps,
164172
parentHeight,
165173
minColumns,
174+
minZoom,
175+
maxZoom,
166176
}: ContentProps) {
167177
const fwdTransitions = useForwardTransitions(steps)
168178
const bwdTransitions = useBackwardTransitions(steps)
@@ -188,6 +198,8 @@ function EditorContent({
188198
progress={progress - transitionIndex + 1}
189199
parentHeight={parentHeight}
190200
minColumns={minColumns}
201+
minZoom={minZoom}
202+
maxZoom={maxZoom}
191203
/>
192204
)
193205
}

packages/mini-editor/src/use-dimensions.tsx

+19-4
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ function useDimensions<T extends HTMLElement>(
3131

3232
useLayoutEffect(() => {
3333
if (ref.current) {
34-
const rect = ref.current.getBoundingClientRect()
35-
3634
const pll = ref.current.querySelector(
3735
".prev-longest-line"
3836
)
@@ -50,8 +48,8 @@ function useDimensions<T extends HTMLElement>(
5048
? plw! / (pll.textContent?.length || 1)
5149
: nlw! / (nll!.textContent?.length || 1)
5250
setDimensions({
53-
width: rect.width,
54-
height: rect.height,
51+
width: getWidthWithoutPadding(ref.current),
52+
height: getHeightWithoutPadding(ref.current),
5553
lineWidths: [
5654
plw || nlw || DEFAULT_WIDTH,
5755
nlw || plw || DEFAULT_WIDTH,
@@ -73,6 +71,23 @@ function useDimensions<T extends HTMLElement>(
7371
}
7472
}
7573

74+
function getWidthWithoutPadding(element: HTMLElement) {
75+
const computedStyle = getComputedStyle(element)
76+
return (
77+
element.clientWidth -
78+
parseFloat(computedStyle.paddingLeft) -
79+
parseFloat(computedStyle.paddingRight)
80+
)
81+
}
82+
function getHeightWithoutPadding(element: HTMLElement) {
83+
const computedStyle = getComputedStyle(element)
84+
return (
85+
element.clientHeight -
86+
parseFloat(computedStyle.paddingTop) -
87+
parseFloat(computedStyle.paddingBottom)
88+
)
89+
}
90+
7691
function depsChanged(
7792
oldDeps: React.DependencyList,
7893
newDeps: React.DependencyList

packages/smooth-lines/src/index.tsx

+75-16
Original file line numberDiff line numberDiff line change
@@ -35,41 +35,51 @@ function SmoothLines({
3535
prevFocus,
3636
nextFocus,
3737
center,
38-
minZoom = 0, // TODO use minZoom
38+
minZoom = 0,
3939
maxZoom = 1.2,
4040
}: Props) {
4141
const lines = useLineTransitions(prevLines, nextLines)
4242

43-
const focusWidth = Array.isArray(lineWidth)
44-
? tweenProp(lineWidth[0], lineWidth[1], progress)
45-
: lineWidth
46-
4743
const prevFocusKeys = prevFocus.map(
4844
index => prevLines[index]?.key
4945
)
5046
const nextFocusKeys = nextFocus.map(
5147
index => nextLines[index]?.key
5248
)
5349

54-
const [prevZoom, prevDX, prevDY] = getContentProps({
50+
const [
51+
prevZoom,
52+
prevDX,
53+
prevDY,
54+
prevContentHeight,
55+
prevContentWidth,
56+
] = getContentProps({
5557
containerWidth,
5658
containerHeight,
5759
lineWidth: Array.isArray(lineWidth)
5860
? lineWidth[0]
5961
: lineWidth,
6062
lineHeight,
63+
minZoom,
6164
maxZoom,
6265
horizontalCenter: !!center,
6366
focusLineIndexList: prevFocus,
6467
originalContentHeight: prevLines.length * lineHeight,
6568
})
66-
const [nextZoom, nextDX, nextDY] = getContentProps({
69+
const [
70+
nextZoom,
71+
nextDX,
72+
nextDY,
73+
nextContentHeight,
74+
nextContentWidth,
75+
] = getContentProps({
6776
containerWidth,
6877
containerHeight,
6978
lineWidth: Array.isArray(lineWidth)
7079
? lineWidth[1]
7180
: lineWidth,
7281
lineHeight,
82+
minZoom,
7383
maxZoom,
7484
horizontalCenter: !!center,
7585
focusLineIndexList: nextFocus,
@@ -79,13 +89,29 @@ function SmoothLines({
7989
const zoom = tweenProp(prevZoom, nextZoom, progress)
8090
const dx = tweenProp(prevDX, nextDX, progress)
8191
const dy = tweenProp(prevDY, nextDY, progress)
92+
const focusHeight = tweenProp(
93+
prevContentHeight,
94+
nextContentHeight,
95+
progress
96+
)
97+
const focusWidth = tweenProp(
98+
prevContentWidth,
99+
nextContentWidth,
100+
progress
101+
)
82102

83103
return (
84104
<Container
85105
width={containerWidth}
86106
height={containerHeight}
87107
>
88-
<Content dx={dx} dy={dy} scale={zoom}>
108+
<Content
109+
dx={dx}
110+
dy={dy}
111+
scale={zoom}
112+
height={Math.max(focusHeight, containerHeight)}
113+
width={Math.max(focusWidth, containerWidth)}
114+
>
89115
<Lines
90116
lines={lines}
91117
prevFocusKeys={prevFocusKeys}
@@ -104,6 +130,7 @@ function getContentProps({
104130
containerHeight,
105131
lineWidth,
106132
lineHeight,
133+
minZoom,
107134
maxZoom,
108135
focusLineIndexList,
109136
originalContentHeight,
@@ -113,6 +140,7 @@ function getContentProps({
113140
containerHeight: number
114141
lineWidth: number
115142
lineHeight: number
143+
minZoom: number
116144
maxZoom: number
117145
focusLineIndexList: number[]
118146
originalContentHeight: number
@@ -124,32 +152,45 @@ function getContentProps({
124152
]
125153
const originalFocusHeight =
126154
(extremes[1] - extremes[0] + 3) * lineHeight
127-
const zoom = Math.min(
128-
containerWidth / lineWidth,
129-
containerHeight / originalFocusHeight,
130-
maxZoom
155+
const zoom = Math.max(
156+
Math.min(
157+
containerWidth / lineWidth,
158+
containerHeight / originalFocusHeight,
159+
maxZoom
160+
),
161+
minZoom
131162
)
132163

133164
const contentHeight = originalContentHeight * zoom
134165

135166
const focusStart = (extremes[0] - 1) * lineHeight * zoom
136167
const focusEnd = (extremes[1] + 2) * lineHeight * zoom
137168
const focusCenter = (focusEnd + focusStart) / 2
169+
const focusHeight = focusEnd - focusStart
138170

139171
const dy =
140172
containerHeight > contentHeight
141173
? (containerHeight - contentHeight) / 2
142174
: clamp(
143175
containerHeight / 2 - focusCenter,
144-
containerHeight - contentHeight,
176+
Math.max(
177+
containerHeight - contentHeight,
178+
-focusStart // to ensure first focus line is shown when focus is bigger than container
179+
),
145180
0
146181
)
147182

148183
const dx = horizontalCenter
149184
? containerWidth / 2 - (lineWidth * zoom) / 2
150185
: 0
151186

152-
return [zoom, dx, dy] as const
187+
return [
188+
zoom,
189+
dx,
190+
dy,
191+
focusHeight,
192+
lineWidth * zoom,
193+
] as const
153194
}
154195

155196
function Container({
@@ -167,6 +208,7 @@ function Container({
167208
width,
168209
height,
169210
position: "relative",
211+
// overflow: "auto",
170212
}}
171213
>
172214
{children}
@@ -178,11 +220,15 @@ function Content({
178220
dx,
179221
dy,
180222
scale,
223+
height,
224+
width,
181225
children,
182226
}: {
183227
dx: number
184228
dy: number
185229
scale: number
230+
height: number
231+
width: number
186232
children: React.ReactNode
187233
}) {
188234
return (
@@ -191,10 +237,23 @@ function Content({
191237
position: "absolute",
192238
top: 0,
193239
left: 0,
194-
transform: `translateX(${dx}px) translateY(${dy}px) scale(${scale})`,
240+
transformOrigin: "top left",
241+
width: width,
242+
height: height,
243+
overflow: "hidden",
195244
}}
196245
>
197-
{children}
246+
<div
247+
style={{
248+
position: "absolute",
249+
top: 0,
250+
left: 0,
251+
transform: `translateX(${dx}px) translateY(${dy}px) scale(${scale})`,
252+
transformOrigin: "top left",
253+
}}
254+
>
255+
{children}
256+
</div>
198257
</div>
199258
)
200259
}

packages/storybook/src/mini-editor.story.js

+2
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ console.log(1)`
6969
progress={progress}
7070
backward={backward}
7171
minColumns={10}
72+
minZoom={1}
7273
/>
7374
)}
7475
</WithProgress>
@@ -127,6 +128,7 @@ console.log(8)`
127128
steps={steps}
128129
progress={progress}
129130
backward={backward}
131+
minZoom={0.8}
130132
/>
131133
)}
132134
</WithProgress>

0 commit comments

Comments
 (0)