Skip to content

Commit

Permalink
feat: Add prop to optionally disable lazy loading language modes (#132)
Browse files Browse the repository at this point in the history
  • Loading branch information
nickgirardo authored Aug 11, 2021
1 parent c60f93f commit fb5526f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
26 changes: 25 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const code = 'const a = 0;';
/>
```

requiring codemirror resources, This is often the case when specifying certain language modes and themes. Just set the mode, the language resources will automatically lazy loading.
requiring codemirror resources. This is often done when specifying certain language modes and themes. Language resources do not need to be imported, just set the mode and they will be automatically lazy loaded.

```jsx
import CodeMirror from '@uiw/react-codemirror';
Expand All @@ -67,6 +67,29 @@ const code = 'const a = 0;';
/>
```

If you do not want to lazy load the language resources, you can set the prop `lazyLoadMode` to `false`. You will need to load the language resources yourself in this case.

```jsx
import CodeMirror from '@uiw/react-codemirror';
import 'codemirror/keymap/sublime';
import 'codemirror/theme/monokai.css';
// Manually loading the language resources here
import 'codemirror/mode/javascript/javascript';

const code = 'console.log("hello world!");';

<CodeMirror
value={code}
lazyLoadMode={false}
options={{
theme: 'monokai',
tabSize: 2,
keyMap: 'sublime',
mode: 'js',
}}
/>
```

## Change Theme

```diff
Expand Down Expand Up @@ -94,6 +117,7 @@ const code = 'const a = 0;';
- `width` width of editor. Defaults to `100%`.
- `height` height of editor. Defaults to `100%`.
- `value` value of the auto created model in the editor.
- `lazyLoadMode` should the mode by automatically lazy loaded. If this is set to false you will need to load the mode yourself. Defaults to true.
- `options` refer to [codemirror options](https://codemirror.net/doc/manual.html#config).

## Props Events
Expand Down
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const defaultOptions = {
}

function ReactCodeMirror(props = {}, ref) {
const { options = {}, value = '', width = '100%', height = '100%' } = props;
const { options = {}, value = '', width = '100%', height = '100%', lazyLoadMode = true } = props;
const [editor, setEditor] = useState();
const textareaRef = useRef();
const lastestProps = useRef(props);
Expand Down Expand Up @@ -44,7 +44,7 @@ function ReactCodeMirror(props = {}, ref) {
async function setOptions(instance, opt = {}) {
if (typeof opt === 'object' && window) {
const mode = CodeMirror.findModeByName(opt.mode || '');
if (mode && mode.mode) {
if (lazyLoadMode && mode && mode.mode) {
await import(`codemirror/mode/${mode.mode}/${mode.mode}.js`);
}
if (mode) {
Expand Down
4 changes: 4 additions & 0 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ export interface IReactCodemirror extends IDOMEvent {
* height of editor. Defaults to 100%.
*/
height?: any;
/**
* should modes be loaded dynamically. Defaults to true.
*/
lazyLoadMode?: boolean;
/**
* refer to codemirror options.
*/
Expand Down

0 comments on commit fb5526f

Please sign in to comment.