1
1
import React from "react"
2
- import { MiniEditor as Editor } from "@code-hike/mini-editor"
3
2
import { Page } from "./utils"
4
3
import "@code-hike/mini-editor/dist/index.css"
5
4
import darkplus from "shiki/themes/dark-plus.json"
@@ -20,6 +19,8 @@ import slackdark from "shiki/themes/slack-dark.json"
20
19
import slackochin from "shiki/themes/slack-ochin.json"
21
20
import solarizeddark from "shiki/themes/solarized-dark.json"
22
21
import solarizedlight from "shiki/themes/solarized-light.json"
22
+ import { EditorSpring } from "@code-hike/mini-editor"
23
+ import { highlight } from "@code-hike/highlighter"
23
24
24
25
const allThemes = {
25
26
darkplus,
@@ -53,36 +54,63 @@ console.log(4)
53
54
console.log(5)`
54
55
55
56
export const themes = ( ) => {
56
- const props = {
57
- files : [
57
+ const [ themeName , setTheme ] = React . useState (
58
+ "materialdarker"
59
+ )
60
+
61
+ const theme = allThemes [ themeName ]
62
+
63
+ const [ props , setProps ] = React . useState ( null )
64
+
65
+ React . useEffect ( ( ) => {
66
+ const files = [
58
67
{ name : "foo.js" , lang : "js" , code } ,
59
68
{ name : "index.js" , lang : "js" , code } ,
60
69
{ name : "bar.js" , lang : "js" , code } ,
61
- { name : "app.js" , lang : "js" , code } ,
62
- ] ,
63
- northPanel : {
64
- active : "app.js" ,
65
- tabs : [ "foo.js" , "app.js" ] ,
66
- heightRatio : 0.5 ,
67
- } ,
68
- southPanel : {
69
- active : "index.js" ,
70
- tabs : [ "bar.js" , "index.js" ] ,
71
- heightRatio : 0.5 ,
72
- } ,
73
- }
70
+ {
71
+ name : "app.js" ,
72
+ lang : "js" ,
73
+ code,
74
+ annotations : [ { focus : "2:3" } ] ,
75
+ } ,
76
+ ]
74
77
75
- const [ themeName , setTheme ] = React . useState (
76
- "materialdarker"
77
- )
78
+ Promise . all (
79
+ files . map ( file =>
80
+ highlight ( {
81
+ code : file . code ,
82
+ lang : file . lang ,
83
+ theme,
84
+ } )
85
+ )
86
+ ) . then ( ( codes , i ) => {
87
+ setProps ( {
88
+ files : files . map ( ( file , i ) => ( {
89
+ ...file ,
90
+ code : codes [ i ] ,
91
+ } ) ) ,
92
+ northPanel : {
93
+ active : "app.js" ,
94
+ tabs : [ "foo.js" , "app.js" ] ,
95
+ } ,
96
+ southPanel : {
97
+ active : "index.js" ,
98
+ tabs : [ "bar.js" , "index.js" ] ,
99
+ } ,
100
+ } )
101
+ } )
102
+ } , [ theme ] )
78
103
79
104
return (
80
105
< Page >
81
106
< label style = { { textAlign : "center" , margin : 18 } } >
82
107
Theme:{ " " }
83
108
< select
84
109
value = { themeName }
85
- onChange = { e => setTheme ( e . currentTarget . value ) }
110
+ onChange = { e => {
111
+ setTheme ( e . currentTarget . value )
112
+ setProps ( null )
113
+ } }
86
114
>
87
115
{ Object . keys ( allThemes ) . map ( themeName => (
88
116
< option key = { themeName } value = { themeName } >
@@ -91,18 +119,19 @@ export const themes = () => {
91
119
) ) }
92
120
</ select >
93
121
</ label >
94
- < Editor
95
- { ...props }
96
- codeProps = { {
97
- maxZoom : 1.2 ,
98
- minColumns : 10 ,
99
- theme : allThemes [ themeName ] ,
100
- } }
101
- frameProps = { { height : 500 } }
102
- />
103
- { /* <pre>
104
- {JSON.stringify(allThemes[themeName], null, 2)}
105
- </pre> */ }
122
+ { props ? (
123
+ < EditorSpring
124
+ { ...props }
125
+ frameProps = { { height : 500 } }
126
+ codeConfig = { {
127
+ maxZoom : 1.2 ,
128
+ minColumns : 10 ,
129
+ theme,
130
+ } }
131
+ />
132
+ ) : (
133
+ < div > Loading...</ div >
134
+ ) }
106
135
</ Page >
107
136
)
108
137
}
0 commit comments