Skip to content

Commit e5998e1

Browse files
committed
Fix themes story
1 parent 34ba7f5 commit e5998e1

File tree

1 file changed

+61
-32
lines changed

1 file changed

+61
-32
lines changed

packages/storybook/src/themes.story.js

Lines changed: 61 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import React from "react"
2-
import { MiniEditor as Editor } from "@code-hike/mini-editor"
32
import { Page } from "./utils"
43
import "@code-hike/mini-editor/dist/index.css"
54
import darkplus from "shiki/themes/dark-plus.json"
@@ -20,6 +19,8 @@ import slackdark from "shiki/themes/slack-dark.json"
2019
import slackochin from "shiki/themes/slack-ochin.json"
2120
import solarizeddark from "shiki/themes/solarized-dark.json"
2221
import solarizedlight from "shiki/themes/solarized-light.json"
22+
import { EditorSpring } from "@code-hike/mini-editor"
23+
import { highlight } from "@code-hike/highlighter"
2324

2425
const allThemes = {
2526
darkplus,
@@ -53,36 +54,63 @@ console.log(4)
5354
console.log(5)`
5455

5556
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 = [
5867
{ name: "foo.js", lang: "js", code },
5968
{ name: "index.js", lang: "js", code },
6069
{ 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+
]
7477

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])
78103

79104
return (
80105
<Page>
81106
<label style={{ textAlign: "center", margin: 18 }}>
82107
Theme:{" "}
83108
<select
84109
value={themeName}
85-
onChange={e => setTheme(e.currentTarget.value)}
110+
onChange={e => {
111+
setTheme(e.currentTarget.value)
112+
setProps(null)
113+
}}
86114
>
87115
{Object.keys(allThemes).map(themeName => (
88116
<option key={themeName} value={themeName}>
@@ -91,18 +119,19 @@ export const themes = () => {
91119
))}
92120
</select>
93121
</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+
)}
106135
</Page>
107136
)
108137
}

0 commit comments

Comments
 (0)