Skip to content

Commit 23761a7

Browse files
committed
Add line number option
1 parent 9ec80dc commit 23761a7

File tree

6 files changed

+85
-110
lines changed

6 files changed

+85
-110
lines changed

packages/smooth-code/src/code-tween.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
import React from "react"
22
import { useDimensions, Dimensions } from "./use-dimensions"
33
import { IRawTheme } from "vscode-textmate"
4-
import { DEFAULT_THEME } from "./themes"
54
import {
65
FullTween,
76
Code,
87
map,
98
FocusString,
9+
getCodeColors,
1010
} from "@code-hike/utils"
1111
import {
1212
useStepParser,
1313
CodeAnnotation,
1414
CodeShift,
1515
} from "./partial-step-parser"
1616
import { SmoothLines } from "./smooth-lines"
17-
import { getThemeDefaultColors } from "./themes"
1817

1918
type HTMLProps = React.DetailedHTMLProps<
2019
React.HTMLAttributes<HTMLPreElement>,
@@ -40,6 +39,7 @@ export type CodeConfig = {
4039
maxZoom?: number
4140
horizontalCenter?: boolean
4241
theme: IRawTheme
42+
lineNumbers?: boolean
4343
}
4444

4545
function useCodeShift({
@@ -74,6 +74,7 @@ export function CodeTween({
7474
stepInfo.code,
7575
map(tween, tween => tween.focus),
7676
config.minColumns || DEFAULT_MIN_COLUMNS,
77+
config.lineNumbers || false,
7778
[config.parentHeight]
7879
)
7980
// return (
@@ -122,8 +123,7 @@ function AfterDimensions({
122123
minZoom = 1,
123124
maxZoom = 1,
124125
horizontalCenter = false,
125-
126-
theme = (DEFAULT_THEME as unknown) as IRawTheme,
126+
theme,
127127
},
128128
dimensions,
129129
stepInfo,
@@ -136,8 +136,7 @@ function AfterDimensions({
136136
progress: number
137137
htmlProps: HTMLProps
138138
}) {
139-
const { bg, fg } = getThemeDefaultColors(theme)
140-
139+
const { bg, fg } = getCodeColors(theme)
141140
return (
142141
<Wrapper
143142
htmlProps={htmlProps}
@@ -151,6 +150,7 @@ function AfterDimensions({
151150
minZoom={minZoom}
152151
maxZoom={maxZoom}
153152
center={horizontalCenter}
153+
theme={theme}
154154
/>
155155
</Wrapper>
156156
)

packages/smooth-code/src/smooth-container.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ export function SmoothContainer({
5252
progress
5353
)
5454

55-
const leftPad = 16
55+
const lineNumberPad =
56+
(dimensions?.lineNumberWidth || 0) * zoom
57+
const leftPad = 16 + lineNumberPad
5658

5759
const width = Math.max(
5860
focusWidth + leftPad,
@@ -221,6 +223,7 @@ function getContentProps({
221223
(extremes[1] - extremes[0] + 3) * lineHeight
222224
const zoom = Math.max(
223225
Math.min(
226+
// TODO consider line number width
224227
(containerWidth - 16 * 2) / lineWidth,
225228
containerHeight / originalFocusHeight,
226229
maxZoom

packages/smooth-code/src/smooth-lines.tsx

Lines changed: 52 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@ import {
66
} from "./partial-step-parser"
77
import { SmoothContainer } from "./smooth-container"
88
import { tween } from "./tween"
9-
import { FullTween, map, Tween } from "@code-hike/utils"
9+
import {
10+
FullTween,
11+
Tween,
12+
getColor,
13+
ColorName,
14+
EditorTheme,
15+
} from "@code-hike/utils"
1016

1117
type SmoothLinesProps = {
1218
progress: number
@@ -15,6 +21,7 @@ type SmoothLinesProps = {
1521
maxZoom?: number
1622
center?: boolean
1723
codeStep: CodeShift
24+
theme: EditorTheme
1825
}
1926

2027
export function SmoothLines(props: SmoothLinesProps) {
@@ -26,7 +33,11 @@ export function SmoothLines(props: SmoothLinesProps) {
2633
focusWidth={focusWidth}
2734
lineHeight={props.dimensions!.lineHeight}
2835
progress={props.progress}
36+
theme={props.theme}
2937
startX={startX}
38+
lineNumberWidth={
39+
props.dimensions!.lineNumberWidth
40+
}
3041
/>
3142
)}
3243
</SmoothContainer>
@@ -39,12 +50,16 @@ function Lines({
3950
focusWidth,
4051
lineHeight,
4152
startX,
53+
theme,
54+
lineNumberWidth,
4255
}: {
4356
codeStep: CodeShift
4457
focusWidth: number
4558
lineHeight: number
4659
progress: number
4760
startX: number
61+
lineNumberWidth: number
62+
theme: EditorTheme
4863
}) {
4964
const groups =
5065
progress < 0.5
@@ -63,6 +78,8 @@ function Lines({
6378
lineHeight={lineHeight}
6479
startX={startX}
6580
key={i}
81+
theme={theme}
82+
lineNumberWidth={lineNumberWidth}
6683
/>
6784
)
6885
}
@@ -99,6 +116,8 @@ function Lines({
99116
lineHeight={lineHeight}
100117
startY={startY}
101118
startX={startX}
119+
theme={theme}
120+
lineNumberWidth={lineNumberWidth}
102121
/>
103122
</Component>
104123
)
@@ -116,13 +135,17 @@ function LineGroup({
116135
t,
117136
startX,
118137
startY = 0,
138+
theme,
139+
lineNumberWidth,
119140
}: {
120141
lines: CodeLine[]
121142
focusWidth: number
122143
lineHeight: number
123144
t: number
124145
startX: number
125146
startY?: number
147+
theme: EditorTheme
148+
lineNumberWidth: number
126149
}) {
127150
return (
128151
<>
@@ -134,18 +157,34 @@ function LineGroup({
134157

135158
return (
136159
<React.Fragment key={key}>
137-
{/* <span
138-
style={{
139-
top: 0,
140-
left: 0,
141-
transform: `translate(${
142-
dx * focusWidth
143-
}px, ${(dy - startY) * lineHeight}px)`,
144-
position: "absolute",
145-
}}
146-
>
147-
13
148-
</span> */}
160+
{lineNumberWidth ? (
161+
<span
162+
style={{
163+
top: 0,
164+
left: 0,
165+
transform: `translate(${
166+
dx * focusWidth
167+
}px, ${(dy - startY) * lineHeight}px)`,
168+
position: "absolute",
169+
userSelect: "none",
170+
width: lineNumberWidth,
171+
textAlign: "right",
172+
display: "inline-block",
173+
boxSizing: "border-box",
174+
paddingRight: "1ch",
175+
opacity,
176+
fontVariantNumeric: "tabular-nums",
177+
color: getColor(
178+
theme,
179+
ColorName.LineNumberForeground
180+
),
181+
}}
182+
>
183+
{t < 0.5
184+
? line.lineNumber.prev
185+
: line.lineNumber.next}
186+
</span>
187+
) : undefined}
149188
<LineContainer
150189
dx={startX + dx * focusWidth}
151190
dy={(dy - startY) * lineHeight}

packages/smooth-code/src/themes.ts

Lines changed: 0 additions & 82 deletions
This file was deleted.

packages/smooth-code/src/use-dimensions.tsx

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ type Dimensions = {
1313
lineWidth: [number, number]
1414
lineHeight: number
1515
colWidth: number
16+
lineNumberWidth: number
1617
} | null
1718

1819
const useLayoutEffect =
@@ -36,6 +37,7 @@ function useDimensions(
3637
code: Tween<string>,
3738
focus: Tween<FocusString>,
3839
minColumns: number,
40+
lineNumbers: boolean,
3941
deps: React.DependencyList
4042
): { element: React.ReactNode; dimensions: Dimensions } {
4143
const [
@@ -63,6 +65,8 @@ function useDimensions(
6365
.trim()
6466
.split(newlineRe)
6567

68+
const lineCount = lines.length
69+
6670
const element = (
6771
<code style={{ display: "block" }}>
6872
<br />
@@ -75,6 +79,14 @@ function useDimensions(
7579
}
7680
key={i}
7781
>
82+
<span
83+
style={{
84+
display: "inline-block",
85+
width: lineNumbers ? "auto" : 0,
86+
}}
87+
>
88+
{lineCount}0
89+
</span>
7890
<div style={{ display: "inline-block" }}>
7991
<span>{line}</span>
8092
</div>
@@ -100,16 +112,20 @@ function useDimensions(
100112
const codeElement = pll?.parentElement!
101113

102114
// TODO is it clientWidth or clientRect?
115+
const lineContentDiv = pll?.querySelector(
116+
":scope > div"
117+
) as HTMLElement
118+
119+
const lineNumberSpan = pll?.querySelector(
120+
":scope > span"
121+
) as HTMLElement
122+
const lnw = getWidthWithoutPadding(lineNumberSpan)
103123

104-
const plw = getWidthWithoutPadding(
105-
pll?.firstElementChild as HTMLElement
106-
)
124+
const plw = getWidthWithoutPadding(lineContentDiv)
107125
const colWidth = plw / prevLongestLine.length || 1
108126
const nlw = nextLongestLine.length * colWidth
109127
const lineHeight =
110-
getHeightWithoutPadding(
111-
pll?.firstElementChild as HTMLElement
112-
) ?? 20
128+
getHeightWithoutPadding(lineContentDiv) ?? 20
113129

114130
const d: Dimensions = {
115131
containerWidth: getWidthWithoutPadding(
@@ -134,6 +150,7 @@ function useDimensions(
134150
],
135151
lineHeight,
136152
colWidth,
153+
lineNumberWidth: lnw,
137154
deps: allDeps,
138155
}
139156
setDimensions(d)

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,6 @@ function TestTransition({
218218
return "Loading..."
219219
}
220220

221-
console.log(files)
222-
223221
const [
224222
prevNorthTabs,
225223
nextNorthTabs,

0 commit comments

Comments
 (0)