1- package react
1+ package page
22
33import (
44 "strconv"
@@ -8,35 +8,36 @@ import (
88
99 "github.com/gopherjs/gopherjs.github.io/playground/internal/common"
1010 "github.com/gopherjs/gopherjs.github.io/playground/internal/editor"
11+ "github.com/gopherjs/gopherjs.github.io/playground/internal/react"
1112)
1213
13- func CodeBox (code string , setCode Setter , onSave Setter , onEscape Setter ) * Element {
14- return CreateElement (codeBoxComponent , Props {
14+ func CodeBox (code string , setCode react. Setter , onSave react. Setter , onEscape react. Setter ) * react. Element {
15+ return react . CreateElement (codeBoxComponent , react. Props {
1516 `curCode` : code ,
1617 `setCode` : setCode ,
1718 `onSave` : onSave ,
1819 `onEscape` : onEscape ,
1920 })
2021}
2122
22- func codeBoxComponent (props Props ) * Element {
23+ func codeBoxComponent (props react. Props ) * react. Element {
2324 var (
2425 curCode = props .GetString (`curCode` )
2526 setCode = props .GetFunc (`setCode` )
2627 onSave = props .GetFunc (`onSave` )
2728 onEscape = props .GetFunc (`onEscape` )
28- textAreaRef = UseRef ()
29- lineNumsRef = UseRef ()
29+ textAreaRef = react . UseRef ()
30+ lineNumsRef = react . UseRef ()
3031 )
3132
32- onInput := UseCallback (func (e * js.Object ) {
33+ onInput := react . UseCallback (func (e * js.Object ) {
3334 newCode := e .Get (`target` ).Get (`value` ).String ()
3435 sel := getSelection (textAreaRef )
3536 globals .UndoRedo ().RecordCodeChange (sel , curCode , newCode )
3637 setCode (newCode )
3738 }, []any {curCode , setCode , textAreaRef })
3839
39- onKeyDown := UseCallback (func (e * js.Object ) {
40+ onKeyDown := react . UseCallback (func (e * js.Object ) {
4041 key := e .Get (`key` ).String ()
4142 shift := e .Get (`shiftKey` ).Bool ()
4243 ctrl := e .Get (`metaKey` ).Bool () || e .Get (`ctrlKey` ).Bool ()
@@ -53,12 +54,12 @@ func codeBoxComponent(props Props) *Element {
5354 }
5455 }, []any {curCode , setCode , onSave , onEscape , textAreaRef })
5556
56- onScroll := UseCallback (func (e * js.Object ) {
57+ onScroll := react . UseCallback (func (e * js.Object ) {
5758 scrollTop := e .Get (`target` ).Get (`scrollTop` ).Int ()
5859 lineNumsRef .Current ().Set (`scrollTop` , scrollTop )
5960 }, []any {lineNumsRef })
6061
61- onSelect := UseCallback (func (e * js.Object ) {
62+ onSelect := react . UseCallback (func (e * js.Object ) {
6263 // Don't normalize the selection so that the direction is preserved.
6364 globals .UndoRedo ().RecordSelectionChange (getSelection (textAreaRef ))
6465 }, []any {textAreaRef })
@@ -67,28 +68,28 @@ func codeBoxComponent(props Props) *Element {
6768 // then maybe we could indent the pasted code automatically to match
6869 // the indent of where it is being pasted.
6970
70- UseEffect (func () {
71+ react . UseEffect (func () {
7172 // On first render, focus the code textarea.
7273 textAreaRef .Current ().Call (`focus` )
7374 setSelection (textAreaRef , common.Selection {})
7475 }, []any {})
7576
7677 lineCount := strings .Count (curCode , "\n " ) + 1
77- lineNumbers := UseMemo (func () string {
78+ lineNumbers := react . UseMemo (func () string {
7879 return getLineNumbers (lineCount )
7980 }, []any {lineCount })
8081
81- return Div (Props {
82+ return react . Div (react. Props {
8283 `id` : `code-box` ,
8384 },
84- TextArea (Props {
85+ react . TextArea (react. Props {
8586 `id` : `line-nums` ,
8687 `ref` : lineNumsRef ,
8788 `value` : lineNumbers ,
8889 `readOnly` : true ,
8990 `disable` : `true` ,
9091 }),
91- TextArea (Props {
92+ react . TextArea (react. Props {
9293 `id` : `code` ,
9394 `ref` : textAreaRef ,
9495 `value` : curCode ,
@@ -107,10 +108,10 @@ func codeBoxComponent(props Props) *Element {
107108
108109type codeBoxAssistant struct {
109110 curCode string
110- setCode Func
111- onSave Func
112- onEscape Func
113- textAreaRef * Ref
111+ setCode react. Func
112+ onSave react. Func
113+ onEscape react. Func
114+ textAreaRef * react. Ref
114115}
115116
116117var _ common.CodeBoxWrapper = (* codeBoxAssistant )(nil )
@@ -166,7 +167,7 @@ func (cba *codeBoxAssistant) SetCode(sel common.Selection, code string) {
166167 horizontallyAutoScroll (cba .textAreaRef , sel .End , code )
167168}
168169
169- func verticallyAutoScroll (textAreaRef * Ref , caret int , code string ) {
170+ func verticallyAutoScroll (textAreaRef * react. Ref , caret int , code string ) {
170171 textArea := textAreaRef .Current ()
171172 totalHeight := textArea .Get (`scrollHeight` ).Int ()
172173 visibleHeight := textArea .Get (`clientHeight` ).Int ()
@@ -186,7 +187,7 @@ func verticallyAutoScroll(textAreaRef *Ref, caret int, code string) {
186187 }
187188}
188189
189- func horizontallyAutoScroll (textAreaRef * Ref , caret int , code string ) {
190+ func horizontallyAutoScroll (textAreaRef * react. Ref , caret int , code string ) {
190191 textArea := textAreaRef .Current ()
191192 totalWidth := textArea .Get (`scrollWidth` ).Int ()
192193 visibleWidth := textArea .Get (`clientWidth` ).Int ()
@@ -207,15 +208,15 @@ func horizontallyAutoScroll(textAreaRef *Ref, caret int, code string) {
207208 }
208209}
209210
210- func getSelection (textAreaRef * Ref ) common.Selection {
211+ func getSelection (textAreaRef * react. Ref ) common.Selection {
211212 textArea := textAreaRef .Current ()
212213 start := textArea .Get (`selectionStart` ).Int ()
213214 end := textArea .Get (`selectionEnd` ).Int ()
214215 return common.Selection {Start : start , End : end }
215216}
216217
217218// setSelection sets the selection on the given textarea ref.
218- func setSelection (textAreaRef * Ref , sel common.Selection ) {
219+ func setSelection (textAreaRef * react. Ref , sel common.Selection ) {
219220 textArea := textAreaRef .Current ()
220221 textArea .Set (`selectionStart` , sel .Start )
221222 textArea .Set (`selectionEnd` , sel .End )
0 commit comments