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

WISH: Add the monaco editor to sample #10

Open
ejfasting opened this issue Apr 29, 2024 · 1 comment
Open

WISH: Add the monaco editor to sample #10

ejfasting opened this issue Apr 29, 2024 · 1 comment

Comments

@ejfasting
Copy link

Recent threads has shown there are issues with getting the @volar/monaco package to work from version 2.x of volar:
volarjs/volar.js#151

It would be a good idea to add the implementation of the monaco-client to the starter

@AssadIKhan
Copy link

Addressing the Issue with @volar/monaco and Implementing monaco-client in a Starter

The issue with @volar/monaco in Volar 2.x stems from its dependency management and potential compatibility issues. To address this and improve the reliability and maintainability of Monaco Editor integration in a starter project, we can directly include monaco-editor and implement a custom solution.

  1. Project Setup (within your starter project)

    • Install monaco-editor:
      npm install monaco-editor
  2. Create a MonacoProvider
    Create a reusable component or utility function (e.g., MonacoProvider) to handle the initialization and management of the Monaco Editor instance. This promotes code reusability and better organization within your starter project.

  3. Implement MonacoProvider

    • Import monaco-editor
      import * as monaco from 'monaco-editor/esm/vs/editor/editor.api';
    • Handle Editor Creation:
      const createMonacoEditor = (container, options) => { return monaco.editor.create(container, options);};
  4. Integrate into Starter Components

       <div ref="editorContainer"></div> 
       </template>
    
       <script>
       import { ref, onMounted, onBeforeUnmount } from 'vue';
       import { createMonacoEditor } from './MonacoProvider'; // Import from your provider
    
       export default {
         props: {
           value: { 
             type: String,
             default: '' 
           },
           // ... other props
         },
         setup(props, { emit }) {
           const editorContainer = ref(null);
           let monacoEditorInstance = null;
       
           onMounted(() => {
             if (editorContainer.value) { 
               monacoEditorInstance = createMonacoEditor(editorContainer.value, { 
                 value: props.value,
                 // ... other options
               });
       
               monacoEditorInstance.onDidChangeModelContent(() => {
                 emit('update:modelValue', monacoEditorInstance.getValue());
               });
             }
           });
    
           onBeforeUnmount(() => {
             if (monacoEditorInstance) {
               monacoEditorInstance.dispose();
             }
           });
    
           return { editorContainer }; 
         }
       };
       </script> 
    

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants