Files
Ritesh Kumar 1bd559514b Transform 2.0 (#101)
* feat(init): added EditorPanel with prettier, copy and settings support

* chore(static): ignore static

* feat: added navigator

* feat:

* feat(settings): added sessionStorage support for settings

* feat(message): add messaging support in editors

* feat(preview): added support for preview panel

* fix(settings): use localstorage for settings instead of session storage

* feat:

* feat(css-to-js): implemented React section

* feat(schema): add json to schema conversions

* feat(others): implemented xml, yaml and markdown conversions

* feat(babel): added flow to typescript converter

* feat(graphql): implemented graphql to typescript

* feat(graphql): added some transformations

* fix: add support for different default value in one language

* feat(graphql): added components, resolver signatures and mongodb

* feat:

* scala case class

* fix(memory): worker-loader patch applied to fix memory overflow crash

* feat: added json schema to typescript

*     icon: <GraphQLIcon />,

* feat(navigator): shorter naming and new categories

* route fixes

* navigator

* perf(workers): create separate file and remove fallback

* fix(navigator): completed

* build(snack):

* fixes

* fixes

* fix(safari):

* added json-to-mobx-state tree

* fix(native): snacksession

* fixes

* added logo

* navigator

* fixes

* custom header

* fixes

* refactor(settings): move settings to left side

* feat: set up redirects

* feat(seo): added title and description

* feat(error): added custom error page

* ga-script

* feat:

* fixes

* fixe

* fixes

* fixes

* fix:
2019-07-18 19:07:48 +05:30

148 lines
3.5 KiB
TypeScript

import React from "react";
import { editor } from "monaco-editor";
import { Pane } from "evergreen-ui";
import { lightEditor } from "@utils/theme";
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(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} />
)
);