Skip to content

Commit

Permalink
memoize line calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
cristianoventura committed Nov 21, 2024
1 parent d89f015 commit bb64059
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/components/Monaco/ReactMonacoEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useTheme } from '@/hooks/useTheme'
import { Editor, EditorProps, loader } from '@monaco-editor/react'
import * as monaco from 'monaco-editor'
import { EditorToolbar, ToolbarState } from './EditorToolbar'
import { useState } from 'react'
import { useMemo, useState } from 'react'
import { Flex } from '@radix-ui/themes'

loader.config({ monaco })
Expand Down Expand Up @@ -42,7 +42,7 @@ export function ReactMonacoEditor({

// Monaco automatically applies word wrap if the content length of a line is >= 10000 characters
// In this case, we disable the word wrap button so Monaco's internal state is respected
const shouldEnableWordWrapButton = () => {
const shouldEnableWordWrapButton = useMemo(() => {
const lineCount = editor?.getModel()?.getLineCount()
if (!lineCount) return false

Expand All @@ -54,14 +54,14 @@ export function ReactMonacoEditor({
}

return true
}
}, [editor])

return (
<Flex height="100%" width="100%" direction="column">
{showToolbar && (
<EditorToolbar
getState={(state) => setToolbarState(state)}
actions={{ wordWrap: shouldEnableWordWrapButton() }}
actions={{ wordWrap: shouldEnableWordWrapButton }}
/>
)}
<Editor
Expand Down

0 comments on commit bb64059

Please sign in to comment.