1
1
import React from "react"
2
2
import { CodeAnnotation } from "../smooth-code"
3
- import { getColor , transparent , ColorName } from "../utils"
4
- import { LineWithElement } from "../smooth-code/partial-step-parser"
3
+ import { transparent } from "../utils"
4
+ import {
5
+ AnnotationProps ,
6
+ LineWithElement ,
7
+ } from "../smooth-code/partial-step-parser"
5
8
import { CopyButton } from "../smooth-code/copy-button"
9
+ import { CSSV } from "utils/light/css"
6
10
7
11
export function Annotation ( ) {
8
12
return (
@@ -36,35 +40,19 @@ function MultilineMark({
36
40
children,
37
41
data,
38
42
style,
39
- theme,
40
43
lines,
41
44
} : {
42
45
data : string
43
46
children : React . ReactNode
44
47
style ?: React . CSSProperties
45
- theme ?: any
46
48
lines ?: LineWithElement [ ]
47
49
} ) {
48
50
const content = getContent ( lines )
49
51
const className = `ch-code-multiline-mark ` + ( data ?? "" )
50
- const bg = getColor (
51
- theme ,
52
- ColorName . RangeHighlightBackground
53
- )
54
- const border = getColor (
55
- theme ,
56
- ColorName . EditorInfoForeground
57
- )
58
52
59
53
return (
60
- < div
61
- style = { { ...style , background : bg } }
62
- className = { className }
63
- >
64
- < span
65
- className = "ch-code-multiline-mark-border"
66
- style = { { background : border } }
67
- />
54
+ < div style = { style } className = { className } >
55
+ < span className = "ch-code-multiline-mark-border" />
68
56
{ children }
69
57
< CopyButton
70
58
className = "ch-code-button"
@@ -88,25 +76,15 @@ function getContent(lines: LineWithElement[]) {
88
76
. join ( "\n" )
89
77
}
90
78
91
- function InlineMark ( {
92
- children,
93
- data,
94
- theme,
95
- } : {
96
- data : any
97
- children : React . ReactNode
98
- theme : any
99
- } ) {
100
- const bg =
101
- tryGuessColor ( children ) ||
102
- transparent (
103
- getColor ( theme , ColorName . CodeForeground ) ,
104
- 0.2
105
- )
106
-
79
+ function InlineMark ( { children, data } : AnnotationProps ) {
107
80
const className = "ch-code-inline-mark " + ( data ?? "" )
108
81
return (
109
- < span className = { className } style = { { background : bg } } >
82
+ < span
83
+ className = { className }
84
+ style = { {
85
+ background : tryGuessColor ( children ) || undefined ,
86
+ } }
87
+ >
110
88
{ children }
111
89
</ span >
112
90
)
@@ -115,44 +93,34 @@ function InlineMark({
115
93
function tryGuessColor (
116
94
children : React . ReactNode
117
95
) : string | undefined {
118
- const child = React . Children . toArray ( children ) [ 0 ] as any
119
-
120
- const grandChild = React . Children . toArray (
121
- child ?. props ?. children || [ ]
122
- ) [ 0 ] as any
123
-
124
- const grandGrandChild = React . Children . toArray (
125
- grandChild ?. props ?. children || [ ]
126
- ) [ 0 ] as any
127
-
128
- const { color } = grandGrandChild ?. props ?. style || { }
129
-
130
- if ( color ) {
131
- return transparent ( color as string , 0.2 )
96
+ try {
97
+ const child = React . Children . toArray ( children ) [ 0 ] as any
98
+
99
+ const grandChild = React . Children . toArray (
100
+ child ?. props ?. children || [ ]
101
+ ) [ 0 ] as any
102
+
103
+ const grandGrandChild = React . Children . toArray (
104
+ grandChild ?. props ?. children || [ ]
105
+ ) [ 0 ] as any
106
+ const { color } = grandGrandChild ?. props ?. style || { }
107
+
108
+ if ( color ) {
109
+ return transparent ( color as string , 0.2 )
110
+ }
111
+ return undefined
112
+ } catch ( e ) {
113
+ return undefined
132
114
}
133
-
134
- return undefined
135
115
}
136
116
137
- function Box ( {
138
- children,
139
- data,
140
- theme,
141
- } : {
142
- data : any
143
- children : React . ReactNode
144
- theme : any
145
- } ) {
146
- const border =
147
- typeof data === "string"
148
- ? data
149
- : theme . tokenColors . find ( ( tc : any ) =>
150
- tc . scope ?. includes ( "string" )
151
- ) ?. settings ?. foreground || "yellow"
117
+ function Box ( { children, data } : AnnotationProps ) {
118
+ const outlineColor =
119
+ typeof data === "string" ? data : undefined
152
120
return (
153
121
< span
154
122
className = "ch-code-box-annotation"
155
- style = { { outline : `2px solid ${ border } ` } }
123
+ style = { { outlineColor } }
156
124
>
157
125
{ children }
158
126
</ span >
@@ -163,38 +131,16 @@ function WithClass({
163
131
children,
164
132
data,
165
133
style,
166
- theme,
167
- } : {
168
- data : any
169
- children : React . ReactNode
170
- style ?: React . CSSProperties
171
- theme : any
172
- } ) {
173
- const className = data as string
134
+ } : AnnotationProps ) {
174
135
return (
175
- < span style = { style } className = { className } >
136
+ < span style = { style } className = { data as string } >
176
137
{ children }
177
138
</ span >
178
139
)
179
140
}
180
141
181
- function Label ( {
182
- children,
183
- data,
184
- style,
185
- theme,
186
- } : {
187
- data : any
188
- children : React . ReactNode
189
- style ?: React . CSSProperties
190
- theme ?: any
191
- } ) {
192
- const bg = ( ( theme as any ) . colors [
193
- "editor.lineHighlightBackground"
194
- ] ||
195
- ( theme as any ) . colors [
196
- "editor.selectionHighlightBackground"
197
- ] ) as string
142
+ function Label ( { children, data, style } : AnnotationProps ) {
143
+ const bg = CSSV . editor . lineHighlightBackground
198
144
const [ hover , setHover ] = React . useState ( false )
199
145
200
146
return (
0 commit comments