imguolao / monaco-vue

Use monaco-editor loaded from CDN in Vue 2&3, no need to bundling.
https://imguolao.github.io/monaco-vue/
MIT License
234 stars 21 forks source link

Change Background Color or Theme #46

Closed rohitasare7 closed 7 months ago

rohitasare7 commented 10 months ago

Hello, is there any way I can change background color of editor or any other theme?

Please let me know, thanks

rohitasare7 commented 10 months ago

I want to do something like this -->

import Editor, { loader } from '@monaco-editor/react';

loader.init().then((monaco) => {
    monaco.editor.defineTheme('myTheme', {
        base: 'vs',
        inherit: true,
        rules: [],
        colors: {
            'editor.background': '#efefef',
        },
    });
});
rohitasare7 commented 10 months ago

@imguolao kindly suggest the approach.

imguolao commented 10 months ago

You can copy the fllowing code into an html files to debug this editor.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>UMD Example</title>
  <script src="https://unpkg.com/vue@3.3.7/dist/vue.global.js"></script>
  <script src="https://unpkg.com/vue-demi@0.14.6/lib/index.iife.js"></script>
  <script src="https://unpkg.com/@monaco-editor/loader@1.4.0/lib/umd/monaco-loader.js"></script>
  <script src="https://unpkg.com/@guolao/vue-monaco-editor@1.4.0/lib/umd/monaco-vue.js"></script>
  <style>
    html,
    body,
    #app {
      margin: 0;
      padding: 0;
      height: 100%;
    }
  </style>
</head>
<body>
  <div id="app">
    <!-- Will render here -->
  </div>
</body>

  <script>
    const { createApp, ref } = Vue
    const { VueMonacoEditor, install } = monaco_vue

    const app = createApp({
      components: {
        VueMonacoEditor,
      },

      template: `
        <vue-monaco-editor
          v-model:value="code"
          :options="MONACO_EDITOR_OPTIONS"
          :theme="theme"
          @beforeMount="handleBeforeMount"
        />
      `,

      setup() {
        const MONACO_EDITOR_OPTIONS = {
          automaticLayout: true,
          formatOnType: true,
          formatOnPaste: true,
        }

        const code = ref('// some code...')
        const theme = ref()
        const handleBeforeMount = monacoInstance => {

          // your action
          monacoInstance.editor.defineTheme('myTheme', {
            base: 'vs',
            inherit: true,
            rules: [],
            colors: {
              'editor.background': '#FA8072',
            },
          })

          theme.value = 'myTheme'
        }

        return {
          MONACO_EDITOR_OPTIONS,
          code,
          theme,
          handleBeforeMount,
        }
      }
    })

    app.use(install, {
      paths: {
        vs: 'https://cdn.jsdelivr.net/npm/monaco-editor@0.38.0/min/vs'
      },
    })

    app.mount('#app')
</script>
</html>
imguolao commented 10 months ago

You can also read the documentation here in detail: https://github.com/imguolao/monaco-vue?tab=readme-ov-file#props--events--slots

imguolao commented 10 months ago

Hope this help you.

rohitasare7 commented 7 months ago

Thanks alot!