replace monaco editor with compiled version (#290) (#291)

* rename exported functions

* replace monaco editor with compiled version

* fixes

* cancel old API requests
This commit is contained in:
Ritesh Kumar authored and GitHub committed 2021-06-27 23:24:08 +05:30
1 parent 3b1eb48149
commit 4ebf5472cd
77 files changed
+869 -235534

No files matched your search

+4 -84
View File
@@ -1,10 +1,8 @@
import { editor } from "monaco-editor";
import {
Button,
FilePicker,
Heading,
HTMLInputEvent,
Icon,
IconButton,
Pane,
Popover,
@@ -16,7 +14,6 @@ import {
import React, { useCallback, useEffect, useRef, useState } from "react";
import dynamic from "next/dynamic";
import copy from "clipboard-copy";
import { getWorker, Wrapper } from "@utils/workerWrapper";
import Npm from "@assets/svgs/Npm";
import { supportedLanguages } from "@utils/prettier";
import { useDropzone } from "react-dropzone";
@@ -46,12 +43,6 @@ export interface EditorPanelProps {
};
}
let prettierWorker: Wrapper;
if (IN_BROWSER) {
const PrettierWorker = require("@workers/prettier.worker");
prettierWorker = getWorker(PrettierWorker);
}
const Monaco = dynamic(() => import("../components/Monaco"), {
ssr: false,
loading: () => (
@@ -67,16 +58,14 @@ const Monaco = dynamic(() => import("../components/Monaco"), {
)
});
export default function({
export default function EditorPanel({
editable = true,
title,
settingElement,
hasLoad,
acceptFiles,
hasClear,
previewElement,
hasCopy = true,
hasPrettier = true,
topNotifications,
language,
defaultValue,
@@ -85,12 +74,10 @@ export default function({
packageDetails
}: EditorPanelProps) {
const [showSettingsDialogue, setSettingsDialog] = useState(false);
const [showPreview, setPreviewVisibility] = useState(true);
const [value, setValue] = useState(defaultValue);
const editorRef = useRef(null);
const [fetchingUrl, setFetchingUrl] = useState("");
const options: editor.IEditorOptions = {
const options = {
fontSize: 14,
readOnly: !editable,
codeLens: false,
@@ -99,7 +86,8 @@ export default function({
enabled: false
},
quickSuggestions: false,
lineNumbers: "on"
lineNumbers: "on",
renderValidationDecorations: "off"
};
const _toggleSettingsDialog = useCallback(
@@ -162,19 +150,6 @@ export default function({
});
}, [value]);
const prettify = useCallback(() => {
prettierWorker
.send({
language,
value
})
.then(setValue);
}, [language, value]);
useEffect(() => {
editorRef.current && editorRef.current.reLayout();
}, [showPreview]);
const fetchFile = useCallback(
close => {
(async () => {
@@ -277,18 +252,6 @@ export default function({
</Tooltip>
)}
{previewElement && (
<Tooltip content="Preview">
<IconButton
height={28}
icon={"eye-open"}
marginRight={10}
isActive={showPreview}
onClick={() => setPreviewVisibility(!showPreview)}
/>
</Tooltip>
)}
{packageDetails && (
<a
href={packageDetails.url}
@@ -314,12 +277,6 @@ export default function({
Copy
</Button>
)}
{hasPrettier && supportedLanguages.includes(language) && (
<Button appearance="primary" onClick={prettify} height={28}>
Prettify
</Button>
)}
</Pane>
<div
@@ -345,45 +302,8 @@ export default function({
setValue(value);
onChange(value);
}}
innerRef={editorRef}
/>
</div>
{IN_BROWSER && showPreview && previewElement && (
<Pane display="flex" flex={1} flexDirection="column" overflow="hidden">
<Pane
height={50}
backgroundColor="#fff"
zIndex={2}
borderTop
position={"relative"}
alignItems={"center"}
display="flex"
paddingX={20}
>
<Heading size={500} flex={1}>
Preview
</Heading>
<Icon
size={20}
cursor="pointer"
icon="cross"
onClick={() => setPreviewVisibility(false)}
/>
</Pane>
<Pane
height={"40vh"}
display="flex"
width={"100%"}
borderTop
backgroundColor="#fff"
zIndex={2}
overflow="hidden"
>
{previewElement(value)}
</Pane>
</Pane>
)}
</Pane>
);
}
+25 -161
View File
@@ -1,39 +1,5 @@
import React from "react";
import { editor, languages } from "monaco-editor";
import { Pane } from "evergreen-ui";
import { lightEditor } from "@utils/theme";
languages.typescript.typescriptDefaults.setDiagnosticsOptions({
noSemanticValidation: true,
noSyntaxValidation: true
});
languages.css.cssDefaults.setDiagnosticsOptions({
lint: {
unknownAtRules: "ignore"
} as any
});
// @ts-ignore
self.MonacoEnvironment = {
getWorkerUrl: function(_moduleId, label) {
if (label === "json") {
return `/static/monaco/json.worker.js`;
}
if (label === "css") {
return `/static/monaco/css.worker.js`;
}
if (label === "html") {
return `/static/monaco/html.worker.js`;
}
if (label === "typescript" || label === "javascript") {
return `/static/monaco/typescript.worker.js`;
}
return `/static/monaco/editor.worker.js`;
}
};
function noop() {}
import Editor from "@monaco-editor/react";
export function processSize(size) {
return !/^\d+$/.test(size) ? size : `${size}px`;
@@ -45,133 +11,31 @@ interface MonacoProps {
value?: string;
width?: number | string;
height?: number | string;
options?: editor.IEditorOptions;
options?: any;
defaultValue?: string;
editorDidMount?: (editor: editor.IStandaloneCodeEditor) => void;
onChange: (value: string, event: editor.IModelContentChangedEvent) => void;
onChange: (value: string) => 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;
export const Monaco: React.FC<MonacoProps> = ({
language,
value,
defaultValue,
height,
width,
options,
onChange
}) => {
return (
<Editor
defaultLanguage={language}
defaultValue={defaultValue}
value={value}
height={height}
width={width}
options={options}
onChange={onChange}
/>
);
};
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} />
)
);
export default Monaco;
+1 -1
View File
@@ -5,7 +5,7 @@ import Link from "next/link";
import { useRouter } from "next/router";
import SearchBox from "@components/Searchbox";
export default function() {
export default function Navigator() {
const router = useRouter();
return (