-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add JSON syntax highlighting and schema validator (#652)
Co-authored-by: Wojciech Kozyra <[email protected]>
- Loading branch information
1 parent
a2667c0
commit 63133c7
Showing
14 changed files
with
693 additions
and
349 deletions.
There are no files selected for viewing
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
Large diffs are not rendered by default.
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
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,4 +1,4 @@ | ||
.codeEditor{ | ||
.codeEditor { | ||
height: 100%; | ||
width: 100%; | ||
min-width: 300px; | ||
|
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,33 +1,92 @@ | ||
import styles from './PlaygroundCodeEditor.module.css'; | ||
import { ChangeEvent, useState } from 'react'; | ||
import React, { useState, useEffect, useCallback } from 'react'; | ||
import 'jsoneditor/dist/jsoneditor.css'; | ||
import './jsoneditor-dark.css'; | ||
import componentTypesJsonSchema from '../../component_types.schema.json'; | ||
import JSONEditor from '../jsonEditor'; | ||
import { jsonrepair } from 'jsonrepair'; | ||
import Ajv from 'ajv'; | ||
|
||
interface PlaygroundCodeEditorProps { | ||
onChange: (content: object | Error) => void; | ||
initialCodeEditorContent: string; | ||
initialCodeEditorContent: object; | ||
} | ||
|
||
function ajvInitialization(): Ajv.Ajv { | ||
const ajv = new Ajv({ | ||
allErrors: true, | ||
verbose: true, | ||
schemaId: 'auto', | ||
$data: true, | ||
}); | ||
|
||
ajv.addFormat('float', '^-?d+(.d+)?([eE][+-]?d+)?$'); | ||
ajv.addFormat('double', '^-?d+(.d+)?([eE][+-]?d+)?$'); | ||
ajv.addFormat('int32', '^-?d+$'); | ||
ajv.addFormat('uint32', '^d+$'); | ||
ajv.addFormat('uint', '^d+$'); | ||
|
||
return ajv; | ||
} | ||
|
||
function PlaygroundCodeEditor({ onChange, initialCodeEditorContent }: PlaygroundCodeEditorProps) { | ||
const [content, setContent] = useState<string>(initialCodeEditorContent); | ||
|
||
const handleChange = (event: ChangeEvent<HTMLTextAreaElement>) => { | ||
const codeContent = event.target.value; | ||
setContent(codeContent); | ||
try { | ||
const scene = JSON.parse(codeContent); | ||
onChange(scene); | ||
} catch (error) { | ||
onChange(error); | ||
const [jsonEditor, setJsonEditor] = useState<JSONEditor | null>(null); | ||
|
||
const editorContainer = useCallback((node: HTMLElement) => { | ||
if (node === null) { | ||
return; | ||
} | ||
}; | ||
|
||
return ( | ||
<textarea | ||
className={styles.codeEditor} | ||
name="inputArea" | ||
placeholder="Enter your code to try it out" | ||
value={content} | ||
onChange={handleChange} | ||
/> | ||
); | ||
const ajv = ajvInitialization(); | ||
const validate = ajv.compile(componentTypesJsonSchema); | ||
|
||
const editor = new JSONEditor(node, { | ||
mode: 'code', | ||
enableSort: false, | ||
enableTransform: false, | ||
statusBar: false, | ||
mainMenuBar: false, | ||
ajv, | ||
onChange: () => { | ||
try { | ||
const jsonContent = editor.get(); | ||
onChange(jsonContent); | ||
if (!validate(jsonContent)) { | ||
throw new Error('Invalid JSON!'); | ||
} | ||
} catch (error) { | ||
onChange(error); | ||
} | ||
}, | ||
onBlur: () => { | ||
try { | ||
const repaired = jsonrepair(editor.getText()); | ||
const formated = JSON.stringify(JSON.parse(repaired), null, 2); | ||
editor.updateText(formated); | ||
const jsonContent = editor.get(); | ||
onChange(jsonContent); | ||
if (!validate(jsonContent)) { | ||
throw new Error('Invalid JSON!'); | ||
} | ||
} catch (error) { | ||
onChange(error); | ||
} | ||
}, | ||
}); | ||
|
||
editor.setSchema(componentTypesJsonSchema); | ||
editor.set(initialCodeEditorContent); | ||
|
||
setJsonEditor(editor); | ||
}, []); | ||
|
||
useEffect(() => { | ||
return () => { | ||
if (jsonEditor) { | ||
jsonEditor.destroy(); | ||
} | ||
}; | ||
}, [jsonEditor]); | ||
|
||
return <div ref={editorContainer} style={{ height: '100%' }} />; | ||
} | ||
|
||
export default PlaygroundCodeEditor; |
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,28 @@ | ||
import { Tooltip } from 'react-tooltip'; | ||
|
||
function SubmitButton({ | ||
onSubmit, | ||
readyToSubmit, | ||
}: { | ||
onSubmit: () => Promise<void>; | ||
readyToSubmit: boolean; | ||
}) { | ||
return ( | ||
<div | ||
data-tooltip-id={readyToSubmit ? null : 'disableSubmit'} | ||
data-tooltip-content={readyToSubmit ? null : 'Invalid scene provided!'} | ||
data-tooltip-place={readyToSubmit ? null : 'top'}> | ||
<button | ||
className={`button ${ | ||
readyToSubmit ? 'button--outline button--primary' : 'disabled button--secondary' | ||
}`} | ||
style={readyToSubmit ? {} : { color: '#f5f5f5', backgroundColor: '#dbdbdb' }} | ||
onClick={onSubmit}> | ||
Submit | ||
</button> | ||
<Tooltip id="disableSubmit" /> | ||
</div> | ||
); | ||
} | ||
|
||
export default SubmitButton; |
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,119 @@ | ||
.jsoneditor { | ||
color: none; | ||
} | ||
div.jsoneditor { | ||
border-radius: 3px; | ||
} | ||
div.jsoneditor, | ||
div.jsoneditor-menu { | ||
border-color: #4b4b4b; | ||
color: #fcf5f5; | ||
} | ||
div.jsoneditor-menu { | ||
background-color: #4b4b4b; | ||
} | ||
|
||
[data-theme='dark'] .ace_editor { | ||
font-size: 16px; | ||
color: rgb(108, 103, 131); | ||
} | ||
|
||
[data-theme='light'] .ace_editor { | ||
font-size: 16px; | ||
color: rgb(0, 0, 0); | ||
} | ||
|
||
[data-theme='dark'] .ace_editor .ace_gutter { | ||
background: rgb(42, 39, 52); | ||
color: rgb(108, 103, 131); | ||
} | ||
|
||
[data-theme='light'] .ace_editor .ace_gutter { | ||
background: rgb(255, 255, 255); | ||
color: rgb(97, 97, 97); | ||
} | ||
[data-theme='dark'] .ace_content { | ||
background-color: rgb(42, 39, 52); | ||
} | ||
|
||
[data-theme='light'] .ace_content { | ||
background-color: rgb(255, 255, 255); | ||
} | ||
|
||
[data-theme='dark'] .ace_content .ace_variable { | ||
color: #9a86fd; | ||
} | ||
[data-theme='light'] .ace_content .ace_variable { | ||
color: #6f54fa; | ||
} | ||
|
||
[data-theme='dark'] .ace_line { | ||
color: rgb(108, 103, 131); | ||
} | ||
[data-theme='light'] .ace_line { | ||
color: rgb(97, 97, 97); | ||
} | ||
|
||
[data-theme='dark'] .ace_editor .ace_content .ace_string { | ||
color: rgb(255, 204, 153); | ||
} | ||
|
||
[data-theme='light'] .ace_editor .ace_content .ace_string { | ||
color: rgb(255, 153, 51); | ||
} | ||
|
||
[data-theme='dark'] .ace_editor .ace_content .ace_bool, | ||
.ace_editor .ace_content .ace_numeric { | ||
color: rgb(224, 145, 66); | ||
} | ||
|
||
[data-theme='light'] .ace_editor .ace_content .ace_bool, | ||
.ace_editor .ace_content .ace_numeric { | ||
color: rgb(224, 112, 0); | ||
} | ||
|
||
[data-theme='dark'] .ace-jsoneditor .ace_marker-layer .ace_active-line { | ||
color: rgb(108, 103, 131); | ||
background-color: rgb(62, 58, 76); | ||
} | ||
|
||
[data-theme='light'] .ace-jsoneditor .ace_marker-layer .ace_active-line { | ||
color: rgb(108, 103, 131); | ||
background-color: rgb(235, 235, 235); | ||
} | ||
|
||
[data-theme='dark'] .ace-jsoneditor .ace_gutter-active-line { | ||
color: rgb(108, 103, 131); | ||
background-color: rgb(62, 58, 76); | ||
} | ||
[data-theme='light'] .ace-jsoneditor .ace_gutter-active-line { | ||
color: rgb(108, 103, 131); | ||
background-color: rgb(235, 235, 235); | ||
} | ||
|
||
[data-theme='dark'] .ace_content .ace_cursor { | ||
border-color: #949494; | ||
} | ||
|
||
[data-theme='light'] .ace_content .ace_cursor { | ||
border-color: #000000; | ||
} | ||
|
||
[data-theme='dark'] .ace_editor .ace_marker-layer .ace_selection { | ||
background: rgb(98, 94, 113); | ||
} | ||
|
||
[data-theme='light'] .ace_editor .ace_marker-layer .ace_selection { | ||
background: rgb(222, 222, 222); | ||
} | ||
.ace_editor .ace_ .ace_editor .ace_marker-layer .ace_selected-word { | ||
border: none; | ||
} | ||
|
||
.ace-jsoneditor .ace_indent-guide { | ||
background: none; | ||
} | ||
|
||
.ace-jsoneditor .ace_marker-layer .ace_selected-word { | ||
border: none; | ||
} |
Oops, something went wrong.