Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/MonacoEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, { ForwardedRef, forwardRef, ReactElement, useEffect, useMemo, useR
import debounce from 'lodash.debounce'
import { monaco, createEditor, getMonacoLanguage, updateEditorKeybindingsMode, registerEditorOpenHandler } from '@codingame/monaco-editor-wrapper'
import { IEditorOptions } from 'vscode/service-override/modelEditor'
import { useDeepMemo, useLastValueRef, useLastVersion, useColorTheme } from './hooks'
import { useDeepMemo, useLastValueRef, useLastVersion, useThemeColors } from './hooks'
import './style'

const STATUS_BAR_HEIGHT = 20
Expand Down Expand Up @@ -123,7 +123,7 @@ function MonacoEditor ({
const preventTriggerChangeEventRef = useRef<boolean>(false)

const [height, setHeight] = useState<number | string>(requestedHeight !== 'auto' ? requestedHeight : 50)
const colorTheme = useColorTheme()
const [statusBarBackground, statusBarForeground, statusBarBorder] = useThemeColors(['statusBar.background', 'statusBar.foreground', 'statusBar.border'])

const containerRef = useRef<HTMLDivElement>(null)
const statusBarRef = useRef<HTMLDivElement>(null)
Expand Down Expand Up @@ -334,11 +334,11 @@ function MonacoEditor ({

const statusBarStyle = useMemo(() => {
return {
backgroundColor: colorTheme.getColor('statusBar.background')?.toString() ?? '#007ACC',
color: colorTheme.getColor('statusBar.foreground')?.toString() ?? '#FFFFFF',
borderTop: `1px solid ${colorTheme.getColor('statusBar.border')?.toString() ?? '#FFFFFF'}`
backgroundColor: statusBarBackground ?? '#007ACC',
color: statusBarForeground ?? '#FFFFFF',
borderTop: `1px solid ${statusBarBorder ?? '#FFFFFF'}`
}
}, [colorTheme])
}, [statusBarBackground, statusBarBorder, statusBarForeground])

return (
<div className='react-monaco-editor-react-container' style={{ height }}>
Expand Down
24 changes: 14 additions & 10 deletions src/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
import { MutableRefObject, useCallback, useEffect, useMemo, useRef, useState } from 'react'
import { MutableRefObject, useCallback, useEffect, useRef, useState } from 'react'
import deepEqual from 'deep-equal'
import { getConfiguration, onConfigurationChanged, monaco } from '@codingame/monaco-editor-wrapper'
import { StandaloneServices, IThemeService, IColorTheme } from 'vscode/services'
import { StandaloneServices, IThemeService } from 'vscode/services'

export function useColorTheme (): IColorTheme {
const themeService = useMemo(() => StandaloneServices.get(IThemeService), [])
const [theme, setTheme] = useState(themeService.getColorTheme())
function getCurrentThemeColor (color: string): string | undefined {
const themeService = StandaloneServices.get(IThemeService)
return themeService.getColorTheme().getColor(color)?.toString()
}

export function useThemeColors (colors: string[]): (string | undefined)[] {
const [colorValues, setColorValues] = useState(colors.map(getCurrentThemeColor))
useEffect(() => {
const disposable = themeService.onDidColorThemeChange(() => {
setTheme(themeService.getColorTheme())
const disposable = StandaloneServices.get(IThemeService).onDidColorThemeChange(() => {
setColorValues(colors.map(getCurrentThemeColor))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The onDidColorThemeChange is called when the user configuration workbench.colorCustomizations is changed but the IColorTheme instance doesn't change, so the setTheme doesn't trigger a new render

})
// Since useEffect is asynchronous, the theme may have changed between the initialization of state and now
// Let's update the state just in case
setTheme(themeService.getColorTheme())
setColorValues(colors.map(getCurrentThemeColor))
return () => {
disposable.dispose()
}
}, [themeService])
}, [colors])

return theme
return colorValues
}

export function useUserConfiguration (programmingLanguageId?: string): Partial<monaco.editor.IEditorOptions> {
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { loadLanguage, monaco, updateKeybindings, updateUserConfiguration } from '@codingame/monaco-editor-wrapper'
import { useColorTheme, useUserConfiguration } from './hooks'
import { useThemeColors, useUserConfiguration } from './hooks'
import MonacoEditor, { MonacoEditorProps } from './MonacoEditor'

export default MonacoEditor
export {
monaco,
useColorTheme,
useThemeColors,
useUserConfiguration,
updateKeybindings,
updateUserConfiguration,
Expand Down