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

support @monaco-editor/react #758

Open
4 tasks done
leermao opened this issue Aug 24, 2024 · 5 comments
Open
4 tasks done

support @monaco-editor/react #758

leermao opened this issue Aug 24, 2024 · 5 comments
Labels

Comments

@leermao
Copy link

leermao commented Aug 24, 2024

Clear and concise description of the problem

image

import Editor, { loader } from '@monaco-editor/react';
import { Box } from '@mui/material';
import { shikiToMonaco } from '@shikijs/monaco';
import * as monaco from 'monaco-editor-core';
import { useEffect } from 'react';
import { createHighlighter } from 'shiki';

// @ts-ignore
loader.config({ monaco });

function TestCode({ language, value }: { language: string; value: string }) {
  const init = async () => {
    // Create the highlighter, it can be reused
    const highlighter = await createHighlighter({
      themes: ['vitesse-dark', 'vitesse-light'],
      langs: [language],
    });

    monaco.languages.register({ id: language });

    shikiToMonaco(highlighter, monaco);

    const container = document.getElementById('container');
    if (container) {
      const editor = monaco.editor.create(container, {
        value,
        language,
        theme: 'vitesse-dark',
      });

      console.log(editor);
    }
  };
  useEffect(() => {
    init();
  }, []);
  return (
    <>
      <Box id="container" height={300} />
      <Box component={Editor} height={300} value={value} language={language} />
    </>
  );
}

export default TestCode;

Currently this version does not seem to integrate with @monaco-editor/react ?

Suggested solution

Currently this version does not seem to integrate with @monaco-editor/react ?

Alternative

No response

Additional context

No response

Validations

Contributes

  • If this feature request is accepted, I am willing to submit a PR to fix this issue
@antfu
Copy link
Member

antfu commented Aug 24, 2024

I don't know react nor the project to investigate. Counting on community to help on this.

@Zxilly
Copy link

Zxilly commented Aug 25, 2024

create a beforeMount hook like this

export function setupCangjie(monaco: Monaco) {
  monaco.languages.register({id: 'cangjie'});
  // debugger
  // monaco.languages.setLanguageConfiguration('cangjie', langConf as any);

  (async function() {
    const highlighter = await createHighlighterCore({
      themes: [
        theme as any,
      ],
      langs: [
        grammer as any,
      ],
      loadWasm: getWasm
    })

    shikiToMonaco(highlighter, monaco)
  })()
}

@leermao
Copy link
Author

leermao commented Aug 25, 2024

Have you tried? I tried to use it this way and kept reporting errors in screenshots, When there are multiple editors, or switch pages and re-render the editor.

@Zxilly
Copy link

Zxilly commented Aug 25, 2024

I only have one monaco instance, this works for me.

@leermao
Copy link
Author

leermao commented Aug 26, 2024

It will report an error when there are multiple instances

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

No branches or pull requests

3 participants