|
1 | 1 | package react |
2 | 2 |
|
| 3 | +import ( |
| 4 | + "github.com/gopherjs/gopherjs/js" |
| 5 | + |
| 6 | + "github.com/gopherjs/gopherjs.github.io/playground/internal/common" |
| 7 | + "github.com/gopherjs/gopherjs.github.io/playground/internal/snippets" |
| 8 | +) |
| 9 | + |
3 | 10 | func Playground() *Element { |
4 | | - return CreateElement(playgroundComponent, nil) |
5 | | -} |
| 11 | + version := "vx.x.x" // TODO(grantnelson-wf): compiler.Version() |
| 12 | + preferslightTheme := getPrefersLightTheme() |
| 13 | + snippetsStore := snippets.NewStore() |
6 | 14 |
|
7 | | -func playgroundComponent() *Element { |
8 | | - code, setCode := UseState(``) |
9 | | - shareUrl, setShareUrl := UseState(``) |
10 | | - output, setOutput := UseState([]any{}) |
11 | | - fmtImports, setFmtImports := UseState(true) |
| 15 | + return CreateElement(func() *Element { |
| 16 | + code, setCode := UseState(``) |
| 17 | + shareUrl, setShareUrl := UseState(``) |
| 18 | + output, setOutput := UseState([]any{}) |
| 19 | + fmtImports, setFmtImports := UseState(true) |
| 20 | + lightTheme, setLightTheme := UseState(preferslightTheme) |
12 | 21 |
|
13 | | - _ = setShareUrl |
14 | | - _ = setOutput |
| 22 | + pa := &playgroundAssistant{ |
| 23 | + snippetStore: snippetsStore, |
| 24 | + code: code, |
| 25 | + setCode: setCode, |
| 26 | + setShareUrl: setShareUrl, |
| 27 | + setOutput: setOutput, |
| 28 | + fmtImports: fmtImports, |
| 29 | + } |
15 | 30 |
|
16 | | - version := `vx.x.x` |
| 31 | + UseEffect(func() { |
| 32 | + setDataTheme(lightTheme) |
| 33 | + }, lightTheme) |
17 | 34 |
|
18 | | - onRunClick := func() { |
19 | | - print("Run clicked") // TODO(grantnelson-wf): Implement |
20 | | - } |
| 35 | + UseEffect(func() { |
| 36 | + // code changed so clear share URL |
| 37 | + setShareUrl(``) |
| 38 | + getLocation().Set(`hash`, ``) |
| 39 | + println("Code changed, cleared share URL") // TODO(grantnelson-wf): Remove debug |
| 40 | + }, code) |
21 | 41 |
|
22 | | - onFormatClick := func() { |
23 | | - print("Format clicked", fmtImports) // TODO(grantnelson-wf): Implement |
24 | | - } |
| 42 | + UseEffect(pa.initCode) |
25 | 43 |
|
26 | | - onShareClick := func() { |
27 | | - print("Share clicked") // TODO(grantnelson-wf): Implement |
28 | | - } |
| 44 | + /* TODO(grantnelson-wf): Implement hashchange loading |
| 45 | + dom.GetWindow().Top().AddEventListener("hashchange", false, func(event dom.Event) { |
| 46 | + event.PreventDefault() |
| 47 | + callback() |
| 48 | + }) |
| 49 | + */ |
29 | 50 |
|
30 | | - return Fragment( |
31 | | - Div(Props{ |
32 | | - `id`: `banner`, |
33 | | - }, |
34 | | - BannerTitle(version), |
35 | | - Span(Props{ |
36 | | - `id`: `controls`, |
| 51 | + return Fragment( |
| 52 | + Div(Props{ |
| 53 | + `id`: `banner`, |
37 | 54 | }, |
38 | | - Button(`run-button`, `Run`, nil, onRunClick), |
39 | | - Button(`format-button`, `Format`, nil, onFormatClick), |
40 | | - FormatImportsControl(fmtImports, setFmtImports), |
41 | | - ShareUrlControl(shareUrl, onShareClick), |
| 55 | + BannerTitle(version), |
| 56 | + Span(Props{ |
| 57 | + `id`: `controls`, |
| 58 | + }, |
| 59 | + Button(`run-button`, `Run`, nil, pa.onRunClick), |
| 60 | + Button(`format-button`, `Format`, nil, pa.onFormatClick), |
| 61 | + ToggleBox(`format-imports`, `Rewrite imports on Format`, `Imports`, fmtImports, setFmtImports), |
| 62 | + ShareUrlControl(shareUrl, pa.onShareClick), |
| 63 | + // TODO(grantnelson-wf): Snippet selection control. |
| 64 | + ToggleBox(`color-theme`, `Change color-theme`, ``, lightTheme, setLightTheme), |
| 65 | + ), |
42 | 66 | ), |
43 | | - ), |
44 | | - Div(Props{ |
45 | | - `id`: `code-output-box`, |
46 | | - }, |
47 | 67 | Div(Props{ |
48 | | - `id`: `code-box-container`, |
| 68 | + `id`: `code-output-box`, |
49 | 69 | }, |
50 | | - CodeBox(code, setCode), |
| 70 | + Div(Props{ |
| 71 | + `id`: `code-box-container`, |
| 72 | + }, |
| 73 | + CodeBox(code, setCode), |
| 74 | + ), |
| 75 | + OutputBox(output), |
51 | 76 | ), |
52 | | - OutputBox(output), |
53 | | - ), |
54 | | - ) |
| 77 | + ) |
| 78 | + }, nil) |
| 79 | +} |
| 80 | + |
| 81 | +func getPrefersLightTheme() bool { |
| 82 | + return js.Global.Get(`window`).Call(`matchMedia`, `(prefers-color-scheme: light)`).Get(`matches`).Bool() |
| 83 | +} |
| 84 | + |
| 85 | +func setDataTheme(lightTheme bool) { |
| 86 | + theme := `dark` |
| 87 | + if lightTheme { |
| 88 | + theme = `light` |
| 89 | + } |
| 90 | + js.Global.Get(`document`).Get(`documentElement`).Call(`setAttribute`, `data-theme`, theme) |
| 91 | +} |
| 92 | + |
| 93 | +func getLocation() *js.Object { |
| 94 | + return js.Global.Get(`window`).Get(`top`).Get(`location`) |
| 95 | +} |
| 96 | + |
| 97 | +type playgroundAssistant struct { |
| 98 | + snippetStore common.SnippetStore |
| 99 | + code string |
| 100 | + setCode func(any) |
| 101 | + setShareUrl func(any) |
| 102 | + setOutput func(any) |
| 103 | + fmtImports bool |
| 104 | +} |
| 105 | + |
| 106 | +func (pa *playgroundAssistant) initCode() { |
| 107 | + // TODO(grantnelson-wf): Update |
| 108 | + hash := getLocation().Get(`hash`).String() |
| 109 | + code, err := pa.snippetStore.Read(hash) |
| 110 | + if err != nil { |
| 111 | + o := Output(pa.setOutput) |
| 112 | + o.Clear() |
| 113 | + o.AddError(err) |
| 114 | + } |
| 115 | + // even on error, set the code so the default code is shown. |
| 116 | + pa.setCode(code) |
| 117 | +} |
| 118 | + |
| 119 | +func (pa *playgroundAssistant) onRunClick() { |
| 120 | + println("Run clicked") // TODO(grantnelson-wf): Implement |
| 121 | +} |
| 122 | + |
| 123 | +func (pa *playgroundAssistant) onFormatClick() { |
| 124 | + println("Format clicked", pa.fmtImports) // TODO(grantnelson-wf): Implement |
| 125 | +} |
| 126 | + |
| 127 | +func (pa *playgroundAssistant) onShareClick() { |
| 128 | + println("Share URL Clicked") // TODO(grantnelson-wf): Implement |
55 | 129 | } |
0 commit comments