diff --git a/sites/shared/components/workbench/edit.js b/sites/shared/components/workbench/edit.js new file mode 100644 index 00000000000..48f904a9a7a --- /dev/null +++ b/sites/shared/components/workbench/edit.js @@ -0,0 +1,67 @@ +import yaml from 'js-yaml' +import hljs from 'highlight.js/lib/common' +import defaultSettings from './default-settings' +import validateGist from './gist-validator' +import { useEffect, useState, useRef } from 'react' + +const countLines = (txt) => txt.split('\n').length +const Edit = (props) => { + let { gist, setGist, design } = props + + const inputRef = useRef(null) + const [gistAsYaml, setGistAsYaml] = useState(null) + const [numLines, setNumLines] = useState(0) + + useEffect(() => { + setGistAsYaml(yaml.dump(gist)) + }, [gist]) + useEffect(() => { + if (gistAsYaml) setNumLines(countLines(gistAsYaml)) + }, [gistAsYaml]) + + const onChange = (e) => { + const editedAsJson = yaml.load(inputRef.current.value) + if (validateGist(editedAsJson, design).valid) { + setGist({ ...defaultSettings, ...editedAsJson, design: gist.design }) + } + } + + const onKeyUp = (e) => { + setNumLines(countLines(e.target.value)) + } + + return ( +
+

Edit Pattern Configuration

+
+
+
+ {Array.from({ length: numLines }, (_, i) => ( + + {' '} + {i + 1}{' '} + + ))} +
+