esolang/ui/code-editor/use-editor-lang-config.ts
2022-01-30 20:47:33 +05:30

29 lines
753 B
TypeScript

import React from "react";
import { useMonaco } from "@monaco-editor/react";
import { MonacoTokensProvider } from "../../languages/types";
type ConfigParams = {
languageId: string;
tokensProvider?: MonacoTokensProvider;
};
/** Add custom language and relevant providers to Monaco */
export const useEditorLanguageConfig = (params: ConfigParams) => {
const monaco = useMonaco();
React.useEffect(() => {
if (!monaco) return;
// Register language
monaco.languages.register({ id: params.languageId });
// If provided, register token provider for language
if (params.tokensProvider) {
monaco.languages.setMonarchTokensProvider(
params.languageId,
params.tokensProvider
);
}
}, [monaco]);
};