Skip to content

Commit aeb8790

Browse files
authored
Merge pull request #235 from code-hike/next
v0.7.1
2 parents 7f7d589 + 4ad6b0a commit aeb8790

File tree

3 files changed

+50
-25
lines changed

3 files changed

+50
-25
lines changed

packages/mdx/src/mini-editor/code-browser.scss

+12
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
font-size: 1rem;
2323
line-height: 1.2rem;
2424
letter-spacing: 0px;
25+
position: relative;
2526
}
2627

2728
.ch-code-browser-content ::selection {
@@ -42,3 +43,14 @@
4243
background-color: var(--ch-hover-background);
4344
color: var(--ch-hover-foreground);
4445
}
46+
47+
.ch-code-browser-button {
48+
width: 1.5em;
49+
height: 1.5em;
50+
cursor: pointer;
51+
min-width: 1.5em;
52+
min-height: 1.5em;
53+
position: absolute;
54+
right: 0.8em;
55+
top: 0.8em;
56+
}

packages/mdx/src/mini-editor/code-browser.tsx

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import { CodeFile } from "./editor-shift"
22
import { IRawTheme } from "vscode-textmate"
3-
import { ColorName, getColor, getColorScheme } from "utils"
3+
import {
4+
codeToText,
5+
ColorName,
6+
getColor,
7+
getColorScheme,
8+
} from "utils"
49
import React from "react"
10+
import { CopyButton } from "smooth-code/copy-button"
511

612
export function CodeBrowser({
713
files,
@@ -192,6 +198,10 @@ function Content({
192198
colorScheme: getColorScheme(theme),
193199
}}
194200
>
201+
<CopyButton
202+
className="ch-code-browser-button"
203+
content={codeToText(file.code)}
204+
/>
195205
{file.code.lines.map((line, i) => (
196206
<div key={i}>
197207
{line.tokens.length === 0 ? (

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

+27-24
Original file line numberDiff line numberDiff line change
@@ -56,40 +56,43 @@ function useDimensions(
5656
code.next,
5757
focus.next
5858
)
59+
5960
const lines = (code.prev || code.next!)
6061
.trimEnd()
6162
.split(newlineRe)
6263

6364
const lineCount = lines.length
6465

66+
// avod setting the ref more than once https://github.com/code-hike/codehike/issues/232
67+
let prevLineRefSet = false
6568
const element = (
6669
<code className="ch-code-scroll-parent">
6770
<br />
68-
{lines.map((line, i) => (
69-
<div
70-
ref={
71-
line === prevLongestLine
72-
? prevLineRef
73-
: undefined
74-
}
75-
key={i}
76-
>
77-
{lineNumbers ? (
78-
<span className="ch-code-line-number">
79-
_{lineCount}
80-
</span>
81-
) : undefined}
82-
<div
83-
style={{
84-
display: "inline-block",
85-
// leftPad
86-
marginLeft: 16,
87-
}}
88-
>
89-
<span>{line}</span>
71+
{lines.map((line, i) => {
72+
const ref =
73+
!prevLineRefSet && line === prevLongestLine
74+
? prevLineRef
75+
: undefined
76+
prevLineRefSet = prevLineRefSet || ref != null
77+
return (
78+
<div ref={ref} key={i}>
79+
{lineNumbers ? (
80+
<span className="ch-code-line-number">
81+
_{lineCount}
82+
</span>
83+
) : undefined}
84+
<div
85+
style={{
86+
display: "inline-block",
87+
// leftPad
88+
marginLeft: 16,
89+
}}
90+
>
91+
<span>{line}</span>
92+
</div>
9093
</div>
91-
</div>
92-
))}
94+
)
95+
})}
9396
<br />
9497
</code>
9598
)

0 commit comments

Comments
 (0)