Skip to content

Commit 9c7ebdd

Browse files
fixing line recalc
1 parent ad14c73 commit 9c7ebdd

File tree

3 files changed

+60
-56
lines changed

3 files changed

+60
-56
lines changed

playground/internal/react/bindings.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,3 +242,12 @@ func UseEffect(effect func(), deps []any) {
242242
func UseEffectWithCleanup(effect func() func(), deps ...any) {
243243
react().Call(`useEffect`, effect, deps)
244244
}
245+
246+
// UseMemo memoizes the result of a computation function.
247+
// The computation is only re-run when any of the dependencies change.
248+
//
249+
// See: https://react.dev/reference/react/useMemo
250+
func UseMemo[T any](compute func() T, deps []any) T {
251+
r := react().Call(`useMemo`, compute, deps)
252+
return r.Interface().(T)
253+
}

playground/internal/react/codeBox.go

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,22 @@ func codeBoxComponent(props Props) *Element {
2525
textAreaRef: UseRef(),
2626
lineNumsRef: UseRef(),
2727
}
28+
2829
lineCount := strings.Count(cba.curCode, "\n") + 1
30+
lineNumbers := UseMemo(func() string {
31+
return getLineNumbers(lineCount)
32+
}, []any{lineCount})
2933

3034
return Div(Props{
3135
`id`: `code-box`,
3236
},
33-
codeLineBox(lineCount, cba.lineNumsRef),
37+
TextArea(Props{
38+
`id`: `line-nums`,
39+
`ref`: cba.lineNumsRef,
40+
`value`: lineNumbers,
41+
`readOnly`: true,
42+
`disable`: `true`,
43+
}),
3444
TextArea(Props{
3545
`id`: `code`,
3646
`ref`: cba.textAreaRef,
@@ -47,36 +57,6 @@ func codeBoxComponent(props Props) *Element {
4757
)
4858
}
4959

50-
func codeLineBox(lineCount int, ref *Ref) *Element {
51-
return CreateElement(codeLineBoxComponent, Props{
52-
`lineCount`: lineCount,
53-
`ref`: ref,
54-
})
55-
}
56-
57-
func codeLineBoxComponent(props Props) *Element {
58-
lineCount := int(As[float64](props, `lineCount`))
59-
ref := props[`ref`]
60-
61-
var sb strings.Builder
62-
for i := 1; i <= lineCount; i++ {
63-
sb.WriteString(strconv.Itoa(i))
64-
if i < lineCount {
65-
sb.WriteString("\n")
66-
}
67-
}
68-
69-
print(lineCount) // TODO(grantnelson-wf): Remove
70-
71-
return TextArea(Props{
72-
`id`: `line-nums`,
73-
`ref`: ref,
74-
`value`: sb.String(),
75-
`readOnly`: true,
76-
`disable`: `true`,
77-
})
78-
}
79-
8060
type codeBoxAssistant struct {
8161
curCode string
8262
setCode func(any)
@@ -318,6 +298,17 @@ func (cba *codeBoxAssistant) findMatchingOpeningBrace(caret int) int {
318298
return -1
319299
}
320300

301+
func getLineNumbers(lineCount int) string {
302+
var sb strings.Builder
303+
for i := 1; i <= lineCount; i++ {
304+
sb.WriteString(strconv.Itoa(i))
305+
if i < lineCount {
306+
sb.WriteString("\n")
307+
}
308+
}
309+
return sb.String()
310+
}
311+
321312
func (cba *codeBoxAssistant) getSelection() (int, int) {
322313
start := cba.textAreaRef.Get(`selectionStart`).Int()
323314
end := cba.textAreaRef.Get(`selectionEnd`).Int()

playground/playground.js

Lines changed: 29 additions & 25 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)