|
| 1 | +/* |
| 2 | + * Copyright The Backstage Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import React from 'react'; |
| 18 | + |
| 19 | +import { Progress } from '@backstage/core-components'; |
| 20 | + |
| 21 | +import { useTheme } from '@mui/material/styles'; |
| 22 | +import Editor, { loader, OnChange, OnMount } from '@monaco-editor/react'; |
| 23 | +import * as monacoEditor from 'monaco-editor'; |
| 24 | +import type MonacoEditor from 'monaco-editor'; |
| 25 | + |
| 26 | +loader.config({ monaco: monacoEditor }); |
| 27 | + |
| 28 | +const defaultOptions: MonacoEditor.editor.IEditorConstructionOptions = { |
| 29 | + minimap: { enabled: false }, |
| 30 | +}; |
| 31 | + |
| 32 | +interface CodeEditorContextValue { |
| 33 | + getEditor: () => MonacoEditor.editor.ICodeEditor | null; |
| 34 | + setEditor: (editor: MonacoEditor.editor.ICodeEditor) => void; |
| 35 | + /** short for getEditor()?.getValue() */ |
| 36 | + getValue: () => string | undefined; |
| 37 | + /** short for getEditor()?.setValue() and getEditor()?.focus() */ |
| 38 | + setValue: (value: string, autoFocus?: boolean) => void; |
| 39 | +} |
| 40 | + |
| 41 | +const CodeEditorContext = React.createContext<CodeEditorContextValue>( |
| 42 | + undefined as any as CodeEditorContextValue, |
| 43 | +); |
| 44 | + |
| 45 | +export const CodeEditorContextProvider = (props: { |
| 46 | + children: React.ReactNode; |
| 47 | +}) => { |
| 48 | + const editorRef = React.useRef<MonacoEditor.editor.ICodeEditor | null>(null); |
| 49 | + const contextValue = React.useMemo<CodeEditorContextValue>( |
| 50 | + () => ({ |
| 51 | + getEditor: () => editorRef.current, |
| 52 | + setEditor: (editor: MonacoEditor.editor.ICodeEditor) => { |
| 53 | + editorRef.current = editor; |
| 54 | + }, |
| 55 | + getValue: () => editorRef.current?.getValue(), |
| 56 | + setValue: (value: string, autoFocus = true) => { |
| 57 | + editorRef.current?.setValue(value); |
| 58 | + if (autoFocus) { |
| 59 | + editorRef.current?.focus(); |
| 60 | + } |
| 61 | + }, |
| 62 | + }), |
| 63 | + [], |
| 64 | + ); |
| 65 | + |
| 66 | + return ( |
| 67 | + <CodeEditorContext.Provider value={contextValue}> |
| 68 | + {props.children} |
| 69 | + </CodeEditorContext.Provider> |
| 70 | + ); |
| 71 | +}; |
| 72 | + |
| 73 | +export const useCodeEditor = () => { |
| 74 | + const contextValue = React.useContext(CodeEditorContext); |
| 75 | + if (!contextValue) { |
| 76 | + throw new Error( |
| 77 | + 'useCodeEditor must be used within a CodeEditorContextProvider', |
| 78 | + ); |
| 79 | + } |
| 80 | + return contextValue; |
| 81 | +}; |
| 82 | + |
| 83 | +export interface CodeEditorProps { |
| 84 | + defaultLanguage: 'yaml'; // We enforce YAML for now |
| 85 | + defaultValue?: string; |
| 86 | + onChange?: OnChange; |
| 87 | + onLoaded?: () => void; |
| 88 | +} |
| 89 | + |
| 90 | +/** |
| 91 | + * Wrapper around Editor from @monaco-editor/react. |
| 92 | + * It automaticallty sets the editor into the current |
| 93 | + * CodeEditorContext so that it can be accessed via useCodeEditor |
| 94 | + * and we can have a uncontrolled input component. |
| 95 | + */ |
| 96 | +export const CodeEditor = ({ |
| 97 | + defaultLanguage, |
| 98 | + onChange, |
| 99 | + onLoaded, |
| 100 | + ...otherProps |
| 101 | +}: CodeEditorProps) => { |
| 102 | + const theme = useTheme().palette.mode === 'dark' ? 'vs-dark' : 'vs-light'; |
| 103 | + |
| 104 | + const codeEditor = useCodeEditor(); |
| 105 | + |
| 106 | + const onMount = React.useCallback<OnMount>( |
| 107 | + (editor, _monaco) => { |
| 108 | + codeEditor.setEditor(editor); |
| 109 | + onLoaded?.(); |
| 110 | + }, |
| 111 | + [codeEditor, onLoaded], |
| 112 | + ); |
| 113 | + |
| 114 | + return ( |
| 115 | + <Editor |
| 116 | + theme={theme} |
| 117 | + defaultLanguage={defaultLanguage} |
| 118 | + onChange={onChange} |
| 119 | + onMount={onMount} |
| 120 | + loading={<Progress />} |
| 121 | + options={defaultOptions} |
| 122 | + {...otherProps} |
| 123 | + /> |
| 124 | + ); |
| 125 | +}; |
0 commit comments