-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add JSON syntax highlighting and schema validator #652
Merged
wkazmierczak
merged 7 commits into
playground
from
@wkazmierczak/json-syntax-highlighting
Aug 13, 2024
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
db75dcc
syntax highlighting, json schema validator
wkazmierczak 393d370
add proper json schema, disable button
wkazmierczak dc16cff
move imports to useEffect
wkazmierczak de3d95d
refactor
wkazmierczak 9e99359
further refactor
wkazmierczak 2c90141
Fix jsoneditor types + add autorepair
wkozyra95 0c07fbe
hotfix
wkazmierczak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add format when changing focus |
||
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add
@types/jsoneditor
package to have type support