-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#1463: Add monaco editor to playground
- Loading branch information
1 parent
b9ceb6f
commit 6ad170d
Showing
10 changed files
with
257 additions
and
32 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 125 additions & 0 deletions
125
src-playground/src/components/diff-editor/code-editor/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
import * as monaco from "monaco-editor"; | ||
import React from "react"; | ||
import { SortierWorkerInputData } from "../../../hooks/use-sortier"; | ||
import "./styles.css"; | ||
|
||
export type CodeEditorProps = { | ||
className?: string; | ||
fileType: SortierWorkerInputData["type"]; | ||
value: string; | ||
onChange?: (newValue: string) => void; | ||
}; | ||
|
||
function fileTypeToJsx(fileType: SortierWorkerInputData["type"]): boolean { | ||
switch (fileType) { | ||
case "jsx": | ||
case "tsx": | ||
return true; | ||
default: | ||
return false; | ||
} | ||
} | ||
|
||
function fileTypeToLanguage(fileType: SortierWorkerInputData["type"]): string { | ||
switch (fileType) { | ||
case "js": | ||
case "jsx": | ||
return "javascript"; | ||
case "ts": | ||
case "tsx": | ||
return "typescript"; | ||
default: | ||
return fileType; | ||
} | ||
} | ||
|
||
export function CodeEditor(props: CodeEditorProps) { | ||
const { className, fileType, onChange, value } = props; | ||
|
||
const ref = React.useRef<HTMLDivElement>(null); | ||
|
||
// Calculated values | ||
const readOnly = onChange == null; | ||
const isJsx = fileTypeToJsx(fileType); | ||
const language = fileTypeToLanguage(fileType); | ||
const monacoInstance = React.useMemo(() => { | ||
const { current } = ref; | ||
if (current == null) { | ||
return null; | ||
} | ||
const instance = monaco.editor.create(current, { | ||
automaticLayout: true, | ||
language: language, | ||
readOnly: readOnly, | ||
value: value, | ||
}); | ||
|
||
return instance; | ||
}, [ref.current]); | ||
|
||
// Effects from if props change | ||
React.useEffect(() => { | ||
if (monacoInstance == null) { | ||
return; | ||
} | ||
const newModel = monaco.editor.createModel(value, language); | ||
monacoInstance.setModel(newModel); | ||
|
||
if (isJsx) { | ||
monaco.languages.typescript.typescriptDefaults.setCompilerOptions({ | ||
jsx: monaco.languages.typescript.JsxEmit.React, | ||
}); | ||
monaco.languages.typescript.javascriptDefaults.setCompilerOptions({ | ||
jsx: monaco.languages.typescript.JsxEmit.React, | ||
}); | ||
} else { | ||
monaco.languages.typescript.typescriptDefaults.setCompilerOptions({ | ||
jsx: monaco.languages.typescript.JsxEmit.None, | ||
}); | ||
monaco.languages.typescript.javascriptDefaults.setCompilerOptions({ | ||
jsx: monaco.languages.typescript.JsxEmit.None, | ||
}); | ||
} | ||
}, [monacoInstance, language, isJsx]); | ||
|
||
React.useEffect(() => { | ||
if (monacoInstance == null) { | ||
return; | ||
} | ||
|
||
const model = monacoInstance.getModel(); | ||
if (model == null) { | ||
return; | ||
} | ||
|
||
if (model.getValue() !== value) { | ||
model.setValue(value); | ||
} | ||
}), | ||
[monacoInstance, value]; | ||
|
||
React.useEffect(() => { | ||
if (monacoInstance == null) { | ||
return; | ||
} | ||
if (onChange == null) { | ||
return; | ||
} | ||
|
||
const listener = monacoInstance.onDidChangeModelContent(() => { | ||
onChange(monacoInstance.getValue()); | ||
}); | ||
|
||
return () => { | ||
listener.dispose(); | ||
}; | ||
}), | ||
[monacoInstance, onChange]; | ||
|
||
// Render | ||
return ( | ||
<div className="site--diff-editor--code-editor"> | ||
<div className={className} ref={ref}></div> | ||
</div> | ||
); | ||
} |
11 changes: 11 additions & 0 deletions
11
src-playground/src/components/diff-editor/code-editor/styles.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
.site--diff-editor--code-editor { | ||
position: relative; | ||
} | ||
|
||
.site--diff-editor--code-editor > * { | ||
bottom: 0px; | ||
left: 0px; | ||
position: absolute; | ||
right: 0px; | ||
top: 0px; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>Page Title</title> | ||
</head> | ||
<body> | ||
<h1>Hello world!</h1> | ||
<p>Welcome to sortier!</p> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,24 @@ | ||
type Options = { | ||
import React from "react"; | ||
|
||
export type OptionsProps = { | ||
sleep?: number; | ||
write?: boolean; | ||
logLevel?: "quiet" | "normal" | "diagnostic"; | ||
}; | ||
|
||
const options: Options = { | ||
sleep: 30, | ||
write: true, | ||
logLevel: "diagnostic", | ||
}; | ||
export function OptionsViewer(props: OptionsProps) { | ||
const { logLevel, sleep, write } = props; | ||
|
||
return ( | ||
<div> | ||
<dl> | ||
<dt>Log Level</dt> | ||
<dl>{logLevel}</dl> | ||
<dt>Sleep</dt> | ||
<dl>{sleep}</dl> | ||
<dt>Write</dt> | ||
<dl>{write}</dl> | ||
</dl> | ||
</div> | ||
); | ||
} |
Oops, something went wrong.