Files
transform/components/Monaco.tsx
T
Ritesh Kumar 85ffa616cd fix(static): remove from gitignore (#132)
* HTML to Pug converter (#127)

* feat(pug): add HTML to pug converter

* fix(pug): added package details

* feat(analyze): added webpack bundle analyzer

*  fix(rust): handle special characters and duplicate keys (#131)

* remove duplicate google tag manager (#122)

* Update _app.tsx (#124)

* docs: add timneutkens as a contributor (#125)

* docs: update README.md

* docs: update .all-contributorsrc

* Upgrade Next.js to 9 (#126)

* Upgrade Next.js to 9

* Add back engines key

* Add globalObject

Brings in suggestion from https://github.com/zeit/next.js/issues/7873#issuecomment-512795482

* Add temporary fix for window issue

* fix(font): add fallback font for non mac browsers (#130)

closes #129

* fix(rust): handle special characters and duplicate keys

closes #97

* fix(rust): handle special chars

closes #97, closes #95, closes #89, closes #62

* chore(rust): remove settings

* feat(kotlin): added JSON to kotlin and json schema

* fixes

* fixes

* fixes

* fixes

* fixes

* added favicon

* social media tags

* fix(static): remove from gitignore

* fixes
2019-07-25 00:59:20 +05:30

167 lines
3.9 KiB
TypeScript

import React from "react";
import { editor } from "monaco-editor";
import { Pane } from "evergreen-ui";
import { lightEditor } from "@utils/theme";
// @ts-ignore
self.MonacoEnvironment = {
getWorkerUrl: function(_moduleId, label) {
if (label === "json") {
return `/static/json.worker.js`;
}
if (label === "css") {
return `/static/css.worker.js`;
}
if (label === "html") {
return `/static/html.worker.js`;
}
if (label === "typescript" || label === "javascript") {
return `/static/typescript.worker.js`;
}
return `/static/editor.worker.js`;
}
};
function noop() {}
export function processSize(size) {
return !/^\d+$/.test(size) ? size : `${size}px`;
}
interface MonacoProps {
theme?: string;
language?: string;
value?: string;
width?: number | string;
height?: number | string;
options?: editor.IEditorOptions;
defaultValue?: string;
editorDidMount?: (editor: editor.IStandaloneCodeEditor) => void;
onChange: (value: string, event: editor.IModelContentChangedEvent) => void;
}
export class MonacoEditor extends React.PureComponent<MonacoProps> {
private editor: editor.IStandaloneCodeEditor;
private containerElement: React.RefObject<HTMLDivElement> = React.createRef();
private __prevent_trigger_change_event: boolean;
static defaultProps = {
width: "100%",
height: "100%",
value: null,
language: "javascript",
options: {},
editorDidMount: noop,
theme: "light"
};
public reLayout = () => {
this.editor && this.editor.layout();
};
componentDidMount() {
this.initMonaco();
window.addEventListener("resize", this.reLayout);
}
componentDidUpdate(prevProps: MonacoProps) {
const { value, language, theme, width, height, options } = this.props;
if (value !== prevProps.value) {
this.__prevent_trigger_change_event = true;
this.editor.setValue(typeof value === "string" ? value : "");
this.__prevent_trigger_change_event = false;
}
if (prevProps.language !== language) {
editor.setModelLanguage(this.editor.getModel(), language);
}
if (prevProps.theme !== theme) {
editor.setTheme(theme);
}
if (
this.editor &&
(width !== prevProps.width || height !== prevProps.height)
) {
this.reLayout();
}
if (prevProps.options !== options) {
this.editor.updateOptions(options);
}
}
componentWillUnmount() {
this.destroyMonaco();
window.removeEventListener("resize", this.reLayout);
}
destroyMonaco() {
if (this.editor) {
this.editor.dispose();
}
}
initMonaco() {
const { value, language, theme, options } = this.props;
if (this.containerElement.current) {
this.editor = editor.create(this.containerElement.current, {
value,
language,
...options
});
editor.defineTheme("light", lightEditor);
if (theme) {
editor.setTheme(theme);
}
// After initializing monaco editor
this.editorDidMount(this.editor);
}
}
editorDidMount(editor) {
this.props.editorDidMount(editor);
editor.onDidChangeModelContent(event => {
const value = editor.getValue();
// Only invoking when user input changed
if (!this.__prevent_trigger_change_event) {
this.props.onChange(value, event);
}
});
}
render() {
const { width, height } = this.props;
const fixedWidth = processSize(width);
const fixedHeight = processSize(height);
const style = {
width: fixedWidth,
height: fixedHeight
};
return (
<Pane display={"flex"} flex={1} backgroundColor={"#fafafa"}>
<div
ref={this.containerElement}
style={style}
className="react-monaco-editor-container"
/>
</Pane>
);
}
}
export default React.memo(
({
innerRef,
...props
}: MonacoProps & { innerRef: React.RefObject<MonacoEditor> }) => (
<MonacoEditor {...props} ref={innerRef} />
)
);