Skip to content

Commit f149daa

Browse files
kmcfaultlabaj
authored andcommitted
feat(CodeEditor): pass additional props to monaco-editor, update readme (patternfly#10080)
* feat(CodeEditor): pass through additional props to Editor * update readme
1 parent 9baed88 commit f149daa

File tree

2 files changed

+26
-26
lines changed

2 files changed

+26
-26
lines changed

packages/react-code-editor/README.md

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# @patternfly/react-code-editor
22

3-
This package provides a PatternFly wrapper for the Monaco code editor
3+
This package provides a PatternFly wrapper for the Monaco code editor, using the `@monaco-editor/react` package.
44

5-
### Prerequisite
5+
### Prerequisite
66

77
#### Node Environment
88

@@ -44,25 +44,25 @@ import { CodeEditor } from '@patternfly/react-code-editor';
4444
```
4545

4646
Install peer deps
47+
4748
```json
4849
"monaco-editor": "^0.21.3",
49-
"monaco-editor-webpack-plugin": "^2.1.0",
5050
"react": "^17 || ^18",
51-
"react-dom": "^17 || ^18",
52-
"react-monaco-editor": "^0.51.0"
51+
"react-dom": "^17 || ^18"
5352
```
5453

55-
To properly install the library `monaco-editor-webpack-plugin` be sure to follow the [plugin instructions](https://github.com/microsoft/monaco-editor/tree/main/webpack-plugin)
56-
5754
#### With create-react-app Projects
55+
5856
If you created your project with `create-react-app` you'll have some extra work to do, or you wont have syntax highlighting. Using the webpack plugin requires updating your webpack config, which `create-react-app` abstracts away. You can `npm eject` your project, but you may not want to do that. To keep your app set up in the `create-react-app` style but to get access to your webpack config you can use `react-app-rewired`.
5957

6058
First, install `react-app-rewired` as a development dependency:
59+
6160
```sh
6261
$ npm install -D react-app-rewired
6362
```
6463

6564
Next, replace all of the `react-script` references in your `package.json` `scripts` section with `react-app-required`:
65+
6666
```json
6767
"scripts": {
6868
"start": "react-app-rewired start",
@@ -72,32 +72,20 @@ Next, replace all of the `react-script` references in your `package.json` `scrip
7272
}
7373
```
7474

75-
Next, create a `config-overrides.js` file at the root of your project and add the following:
76-
77-
```javascript
78-
const MonacoWebpackPlugin = require('monaco-editor-webpack-plugin');
79-
80-
module.exports = function override(config, env) {
81-
config.plugins.push(new MonacoWebpackPlugin({
82-
languages: ['json', 'yaml', 'shell']
83-
}));
84-
return config;
85-
}
86-
```
87-
88-
Note: You should change the `languages` array based on your needs.
89-
9075
You can now start your app with `npm start` and syntax highlighting should work.
9176

9277
#### Enable YAML Syntax Highlighting
93-
The Monaco editor doesn't ship with full YAML support. You can configure your code editor with `Languages.yaml` but there will be no highlighting, even i you have the webpack plugin working correctly. To enable YAML support you need to do the following:
78+
79+
The Monaco editor doesn't ship with full YAML support. You can configure your code editor with `Languages.yaml` but there will be no highlighting. To enable YAML support you need to do the following:
9480

9581
First, install `monaco-yaml`:
82+
9683
```shell
9784
$ npm install --save monaco-yaml
9885
```
9986

10087
Next, at the entrypoint of your app enable it:
88+
10189
```javascript
10290
import { setDiagnosticsOptions } from 'monaco-yaml';
10391

@@ -107,8 +95,16 @@ setDiagnosticsOptions({
10795
completion: true,
10896
validate: true,
10997
format: true,
110-
schemas: [],
98+
schemas: []
11199
});
112100
```
113101

102+
Finally, to allow the `monaco-yaml` autocomplete to function properly with `@monaco-editor/react`, you should configure your YAML schema in a `beforeMount` call with the `monaco-yaml` `configureMonacoYaml` function. This `beforeMount` function should be passed to `CodeEditor` via the `editorProps` property like so:
103+
104+
```
105+
editorProps: {
106+
beforeMount: yourBeforeMountHandler
107+
}
108+
```
109+
114110
The `monaco-yaml` plugin has a lot of options so check out their docs to see what else you may want to add.

packages/react-code-editor/src/components/CodeEditor/CodeEditor.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {
1717
TooltipPosition,
1818
EmptyStateHeader
1919
} from '@patternfly/react-core';
20-
import Editor, { Monaco } from '@monaco-editor/react';
20+
import Editor, { EditorProps, Monaco } from '@monaco-editor/react';
2121
import { editor } from 'monaco-editor/esm/vs/editor/editor.api';
2222
import CopyIcon from '@patternfly/react-icons/dist/esm/icons/copy-icon';
2323
import UploadIcon from '@patternfly/react-icons/dist/esm/icons/upload-icon';
@@ -137,6 +137,8 @@ export interface CodeEditorProps extends Omit<React.HTMLProps<HTMLDivElement>, '
137137
downloadButtonToolTipText?: string;
138138
/** Name of the file if user downloads code to local file. */
139139
downloadFileName?: string;
140+
/** Additional props to pass to the monaco editor. */
141+
editorProps?: EditorProps;
140142
/** Content to display in space of the code editor when there is no code to display. */
141143
emptyState?: React.ReactNode;
142144
/** Override default empty state body text. */
@@ -499,7 +501,8 @@ class CodeEditor extends React.Component<CodeEditorProps, CodeEditorState> {
499501
shortcutsPopoverProps: shortcutsPopoverPropsProp,
500502
showEditor,
501503
options: optionsProp,
502-
overrideServices
504+
overrideServices,
505+
editorProps
503506
} = this.props;
504507
const shortcutsPopoverProps: PopoverProps = {
505508
...CodeEditor.defaultProps.shortcutsPopoverProps,
@@ -644,6 +647,7 @@ class CodeEditor extends React.Component<CodeEditorProps, CodeEditorState> {
644647
onChange={this.onChange}
645648
onMount={this.editorDidMount}
646649
theme={isDarkTheme ? 'vs-dark' : 'vs-light'}
650+
{...editorProps}
647651
/>
648652
</div>
649653
);

0 commit comments

Comments
 (0)