File tree 3 files changed +50
-25
lines changed
3 files changed +50
-25
lines changed Original file line number Diff line number Diff line change 22
22
font-size : 1rem ;
23
23
line-height : 1.2rem ;
24
24
letter-spacing : 0px ;
25
+ position : relative ;
25
26
}
26
27
27
28
.ch-code-browser-content ::selection {
42
43
background-color : var (--ch-hover-background );
43
44
color : var (--ch-hover-foreground );
44
45
}
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
+ }
Original file line number Diff line number Diff line change 1
1
import { CodeFile } from "./editor-shift"
2
2
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"
4
9
import React from "react"
10
+ import { CopyButton } from "smooth-code/copy-button"
5
11
6
12
export function CodeBrowser ( {
7
13
files,
@@ -192,6 +198,10 @@ function Content({
192
198
colorScheme : getColorScheme ( theme ) ,
193
199
} }
194
200
>
201
+ < CopyButton
202
+ className = "ch-code-browser-button"
203
+ content = { codeToText ( file . code ) }
204
+ />
195
205
{ file . code . lines . map ( ( line , i ) => (
196
206
< div key = { i } >
197
207
{ line . tokens . length === 0 ? (
Original file line number Diff line number Diff line change @@ -56,40 +56,43 @@ function useDimensions(
56
56
code . next ,
57
57
focus . next
58
58
)
59
+
59
60
const lines = ( code . prev || code . next ! )
60
61
. trimEnd ( )
61
62
. split ( newlineRe )
62
63
63
64
const lineCount = lines . length
64
65
66
+ // avod setting the ref more than once https://github.com/code-hike/codehike/issues/232
67
+ let prevLineRefSet = false
65
68
const element = (
66
69
< code className = "ch-code-scroll-parent" >
67
70
< 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 >
90
93
</ div >
91
- </ div >
92
- ) ) }
94
+ )
95
+ } ) }
93
96
< br />
94
97
</ code >
95
98
)
You can’t perform that action at this time.
0 commit comments