Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(monaco-editor): fix theme conflict #1848

Merged
merged 2 commits into from
Dec 30, 2024
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
4 changes: 1 addition & 3 deletions apps/desktop/src/renderer/src/pages/log.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,5 @@ logger.mark('mark')
</script>

<template>
<div>
<LogView :log="logMemory" />
</div>
<LogView :log="logMemory" />
</template>
1 change: 1 addition & 0 deletions apps/web/auto-imports.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
export {}
declare global {
const EffectScope: typeof import('vue')['EffectScope']
const ElMessage: typeof import('element-plus/es')['ElMessage']
const acceptHMRUpdate: typeof import('pinia')['acceptHMRUpdate']
const computed: typeof import('vue')['computed']
const createApp: typeof import('vue')['createApp']
Expand Down
4 changes: 1 addition & 3 deletions apps/web/src/pages/log.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,5 @@ logger.mark('mark')
</script>

<template>
<div>
<LogView :log="logMemory" />
</div>
<LogView :log="logMemory" />
</template>
1 change: 1 addition & 0 deletions packages/ui/auto-imports.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
export {}
declare global {
const EffectScope: typeof import('vue')['EffectScope']
const ElMessage: typeof import('element-plus/es')['ElMessage']
const acceptHMRUpdate: typeof import('pinia')['acceptHMRUpdate']
const computed: typeof import('vue')['computed']
const createApp: typeof import('vue')['createApp']
Expand Down
3 changes: 3 additions & 0 deletions packages/ui/components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,19 @@ declare module 'vue' {
ElButton: typeof import('element-plus/es')['ElButton']
ElCascaderPanel: typeof import('element-plus/es')['ElCascaderPanel']
ElCol: typeof import('element-plus/es')['ElCol']
ElDialog: typeof import('element-plus/es')['ElDialog']
ElDivider: typeof import('element-plus/es')['ElDivider']
ElIconDelete: typeof import('@element-plus/icons-vue')['Delete']
ElIconDownload: typeof import('@element-plus/icons-vue')['Download']
ElIconPrinter: typeof import('@element-plus/icons-vue')['Printer']
ElIconRight: typeof import('@element-plus/icons-vue')['Right']
ElIconUpload: typeof import('@element-plus/icons-vue')['Upload']
ElIconWarning: typeof import('@element-plus/icons-vue')['Warning']
ElInput: typeof import('element-plus/es')['ElInput']
ElInputNumber: typeof import('element-plus/es')['ElInputNumber']
ElMain: typeof import('element-plus/es')['ElMain']
ElOption: typeof import('element-plus/es')['ElOption']
ElPopover: typeof import('element-plus/es')['ElPopover']
ElRow: typeof import('element-plus/es')['ElRow']
ElScrollbar: typeof import('element-plus/es')['ElScrollbar']
ElSelect: typeof import('element-plus/es')['ElSelect']
Expand Down
39 changes: 21 additions & 18 deletions packages/ui/src/components/MonacoEditor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ import * as monaco from 'monaco-editor/esm/vs/editor/editor.api'

import { useSettingsStore } from '../stores'

import EditorDark from '../styles/themes/custom/editor-dark'
import EditorNight from '../styles/themes/custom/editor-night'
import { logEditorDark, logEditorLight, logEditorNight } from '../styles/themes/custom/log-themes'
import { editorDark, editorLight, editorNight } from '../styles/themes/custom/editor-themes'

const props = withDefaults(
defineProps<{
Expand Down Expand Up @@ -36,6 +34,7 @@ let editor: monaco.editor.IStandaloneCodeEditor | null = null
const defaultOptions: monaco.editor.IStandaloneEditorConstructionOptions = {
lineHeight: 1,
fontSize: 14,
tabSize: 2,
automaticLayout: true,
scrollBeyondLastLine: false,
lineNumbers: 'off',
Expand Down Expand Up @@ -64,21 +63,10 @@ const editorTheme = computed(() => {
dark: 'editor-dark',
night: 'editor-night',
}
const logThemeOptions: Record<Theme, string> = {
light: 'editor-log',
dark: 'editor-log-dark',
night: 'editor-log-night',
}
if (language.value === 'log') {
return logThemeOptions[settings!.currentTheme]
}
return themeOptions[settings!.currentTheme]
})

function defineLogLanguage() {
monaco.editor.defineTheme('editor-log', logEditorLight)
monaco.editor.defineTheme('editor-log-dark', logEditorDark)
monaco.editor.defineTheme('editor-log-night', logEditorNight)
languages.register({ id: 'log' })
languages.setMonarchTokensProvider('log', {
tokenizer: {
Expand All @@ -97,8 +85,12 @@ function defineLogLanguage() {
}

function defineDefaultTheme() {
monaco.editor.defineTheme('editor-dark', EditorDark)
monaco.editor.defineTheme('editor-night', EditorNight)
// Multiple editors cannot have different themes because Monaco Editor sets the theme styles globally
// Therefore, an additional Log theme cannot be defined, and the Log theme styles need to be merged into the default theme
// Issue: https://github.com/Microsoft/monaco-editor/issues/338
monaco.editor.defineTheme('vs', editorLight)
monaco.editor.defineTheme('editor-dark', editorDark)
monaco.editor.defineTheme('editor-night', editorNight)
}

function initMonacoEditor() {
Expand All @@ -124,9 +116,20 @@ watch(value, (newValue) => {
}
})

watch(language, (newLanguage) => {
if (editor?.getModel()) {
monaco.editor.setModelLanguage(editor.getModel()!, newLanguage)
}
})

watch(options, (newOptions) => {
editor?.updateOptions(newOptions)
}, { deep: true })

defineDefaultTheme()
defineLogLanguage()

onMounted(() => {
defineDefaultTheme()
defineLogLanguage()
initMonacoEditor()
})

Expand Down
25 changes: 24 additions & 1 deletion packages/ui/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,43 @@
import type { App } from 'vue'

import AboutDataCollectionPolicy from './components/about/DataCollectionPolicy.vue'
import AboutFooter from './components/about/Footer.vue'
import AboutInfo from './components/about/Info.vue'
import AboutView from './components/about/View.vue'
import EmptyView from './components/common/EmptyView.vue'
import LeftMenu from './components/common/LeftMenu.vue'
import MainView from './components/common/MainView.vue'
import SplitView from './components/common/SplitView.vue'
import ConnectionDetailsView from './components/connections/DetailsView.vue'
import ConnectionListView from './components/connections/ListView.vue'
import HelpView from './components/HelpView.vue'
import LogView from './components/LogView.vue'
import MonacoEditor from './components/MonacoEditor.vue'
import MyDialog from './components/MyDialog.vue'
import SettingsView from './components/SettingsView.vue'

export * from './components/monacoEnvironment'

export * from './i18n'
export * from './stores'

export { ConnectionDetailsView, ConnectionListView, EmptyView, HelpView, LeftMenu, MainView, SettingsView, SplitView }
export {
AboutDataCollectionPolicy,
AboutFooter,
AboutInfo,
AboutView,
ConnectionDetailsView,
ConnectionListView,
EmptyView,
HelpView,
LeftMenu,
LogView,
MainView,
MonacoEditor,
MyDialog,
SettingsView,
SplitView,
}

export default {
install: (app: App) => {
Expand Down
49 changes: 0 additions & 49 deletions packages/ui/src/styles/themes/custom/editor-dark.ts

This file was deleted.

49 changes: 0 additions & 49 deletions packages/ui/src/styles/themes/custom/editor-night.ts

This file was deleted.

115 changes: 115 additions & 0 deletions packages/ui/src/styles/themes/custom/editor-themes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import type { editor } from 'monaco-editor/esm/vs/editor/editor.api'

export const logRules: editor.ITokenThemeRule[] = [
{ token: 'log-TRACE', foreground: 'a9a9a9', background: '1e1e1e' },
{ token: 'log-DEBUG', foreground: '00aaaa', background: '1e1e1e' },
{ token: 'log-INFO', foreground: '00aa00', background: '1e1e1e' },
{ token: 'log-WARN', foreground: 'aa5500', background: '1e1e1e' },
{ token: 'log-ERROR', foreground: 'ff5555', background: '1e1e1e' },
{ token: 'log-FATAL', foreground: 'aa00aa', background: '1e1e1e' },
{ token: 'log-MARK', foreground: '555555', background: '1e1e1e' },
{ token: 'log-date', foreground: '008888', background: '2d2d2d' },
]

export const editorLight: editor.IStandaloneThemeData = {
base: 'vs',
inherit: true,
rules: [
...logRules,
],
colors: {},
}

export const editorDark: editor.IStandaloneThemeData = {
base: 'vs-dark',
inherit: true,
rules: [
...logRules,
{ foreground: '75715e', token: 'comment' },
{ foreground: 'e6db74', token: 'string' },
{ foreground: 'ae81ff', token: 'constant.numeric' },
{ foreground: 'ae81ff', token: 'constant.language' },
{ foreground: 'ae81ff', token: 'constant.character' },
{ foreground: 'ae81ff', token: 'constant.other' },
{ foreground: 'f92672', token: 'keyword' },
{ foreground: 'f92672', token: 'storage' },
{ foreground: '66d9ef', fontStyle: 'italic', token: 'storage.type' },
{ foreground: 'a6e22e', fontStyle: 'underline', token: 'entity.name.class' },
{ foreground: 'a6e22e', fontStyle: 'italic underline', token: 'entity.other.inherited-class' },
{ foreground: 'a6e22e', token: 'entity.name.function' },
{ foreground: 'fd971f', fontStyle: 'italic', token: 'variable.parameter' },
{ foreground: 'f92672', token: 'entity.name.tag' },
{ foreground: 'a6e22e', token: 'entity.other.attribute-name' },
{ foreground: '66d9ef', token: 'support.function' },
{ foreground: '66d9ef', token: 'support.constant' },
{ foreground: '66d9ef', fontStyle: 'italic', token: 'support.type' },
{ foreground: '66d9ef', fontStyle: 'italic', token: 'support.class' },
{ foreground: 'f8f8f0', background: 'f92672', token: 'invalid' },
{ foreground: 'f8f8f0', background: 'ae81ff', token: 'invalid.deprecated' },
{ foreground: 'cfcfc2', token: 'meta.structure.dictionary.json string.quoted.double.json' },
{ foreground: '75715e', token: 'meta.diff' },
{ foreground: '75715e', token: 'meta.diff.header' },
{ foreground: 'f92672', token: 'markup.deleted' },
{ foreground: 'a6e22e', token: 'markup.inserted' },
{ foreground: 'e6db74', token: 'markup.changed' },
{ foreground: 'ae81ffa0', token: 'constant.numeric.line-number.find-in-files - match' },
{ foreground: 'e6db74', token: 'entity.name.filename.find-in-files' },
],
colors: {
'editor.foreground': '#F8F8F2',
'editor.background': '#282828',
'editor.selectionBackground': '#49483E',
'editor.lineHighlightBackground': '#3E3D32',
'editorCursor.foreground': '#F8F8F0',
'editorWhitespace.foreground': '#3B3A32',
'editorIndentGuide.activeBackground': '#9D550FB0',
'editor.selectionHighlightBorder': '#222218',
},
}

export const editorNight: editor.IStandaloneThemeData = {
base: 'vs-dark',
inherit: true,
rules: [
...logRules,
{ foreground: '75715e', token: 'comment' },
{ foreground: 'e6db74', token: 'string' },
{ foreground: 'ae81ff', token: 'constant.numeric' },
{ foreground: 'ae81ff', token: 'constant.language' },
{ foreground: 'ae81ff', token: 'constant.character' },
{ foreground: 'ae81ff', token: 'constant.other' },
{ foreground: 'f92672', token: 'keyword' },
{ foreground: 'f92672', token: 'storage' },
{ foreground: '66d9ef', fontStyle: 'italic', token: 'storage.type' },
{ foreground: 'a6e22e', fontStyle: 'underline', token: 'entity.name.class' },
{ foreground: 'a6e22e', fontStyle: 'italic underline', token: 'entity.other.inherited-class' },
{ foreground: 'a6e22e', token: 'entity.name.function' },
{ foreground: 'fd971f', fontStyle: 'italic', token: 'variable.parameter' },
{ foreground: 'f92672', token: 'entity.name.tag' },
{ foreground: 'a6e22e', token: 'entity.other.attribute-name' },
{ foreground: '66d9ef', token: 'support.function' },
{ foreground: '66d9ef', token: 'support.constant' },
{ foreground: '66d9ef', fontStyle: 'italic', token: 'support.type' },
{ foreground: '66d9ef', fontStyle: 'italic', token: 'support.class' },
{ foreground: 'f8f8f0', background: 'f92672', token: 'invalid' },
{ foreground: 'f8f8f0', background: 'ae81ff', token: 'invalid.deprecated' },
{ foreground: 'cfcfc2', token: 'meta.structure.dictionary.json string.quoted.double.json' },
{ foreground: '75715e', token: 'meta.diff' },
{ foreground: '75715e', token: 'meta.diff.header' },
{ foreground: 'f92672', token: 'markup.deleted' },
{ foreground: 'a6e22e', token: 'markup.inserted' },
{ foreground: 'e6db74', token: 'markup.changed' },
{ foreground: 'ae81ffa0', token: 'constant.numeric.line-number.find-in-files - match' },
{ foreground: 'e6db74', token: 'entity.name.filename.find-in-files' },
],
colors: {
'editor.foreground': '#F8F8F2',
'editor.background': '#292B33',
'editor.selectionBackground': '#49483E',
'editor.lineHighlightBackground': '#3E3D32',
'editorCursor.foreground': '#F8F8F0',
'editorWhitespace.foreground': '#3B3A32',
'editorIndentGuide.activeBackground': '#9D550FB0',
'editor.selectionHighlightBorder': '#222218',
},
}
Loading
Loading