diff --git a/packages/editor-sample/src/App/TemplatePanel/ImportJson/ImportJsonDialog.tsx b/packages/editor-sample/src/App/TemplatePanel/ImportJson/ImportJsonDialog.tsx new file mode 100644 index 0000000..0b7c3c4 --- /dev/null +++ b/packages/editor-sample/src/App/TemplatePanel/ImportJson/ImportJsonDialog.tsx @@ -0,0 +1,60 @@ +import React, { useState } from 'react'; + +import { Button, Dialog, DialogActions, DialogContent, DialogTitle, Input } from '@mui/material'; + +import { resetDocument } from '../../../documents/editor/EditorContext'; + +type ImportJsonDialogProps = { + onClose: () => void; +}; +export default function ImportJsonDialog({ onClose }: ImportJsonDialogProps) { + const [value, setValue] = useState(''); + const [error, setError] = useState(null); + + const handleChange: React.ChangeEventHandler = (ev) => { + const v = ev.currentTarget.value; + setValue(v); + try { + JSON.parse(v); + setError(null); + } catch { + setError('There was a parsing error'); + } + }; + + return ( + + Import JSON +
{ + ev.preventDefault(); + try { + const doc = JSON.parse(value); + resetDocument(doc); + onClose(); + } catch {} + }} + > + + + + + + + +
+
+ ); +} diff --git a/packages/editor-sample/src/App/TemplatePanel/ImportJson/index.tsx b/packages/editor-sample/src/App/TemplatePanel/ImportJson/index.tsx index e3a8be1..88ca13b 100644 --- a/packages/editor-sample/src/App/TemplatePanel/ImportJson/index.tsx +++ b/packages/editor-sample/src/App/TemplatePanel/ImportJson/index.tsx @@ -1,41 +1,21 @@ import React, { useState } from 'react'; -import { Button, Dialog, DialogActions, DialogContent, DialogTitle, Input } from '@mui/material'; +import { Button } from '@mui/material'; -import { resetDocument } from '../../../documents/editor/EditorContext'; +import ImportJsonDialog from './ImportJsonDialog'; export default function ImportJson() { const [open, setOpen] = useState(false); + let dialog = null; + if (open) { + dialog = setOpen(false)} />; + } + return ( <> - setOpen(false)}> - Import JSON -
{ - ev.preventDefault(); - const data = new FormData(ev.currentTarget); - const json = data.get('json'); - if (typeof json !== 'string') { - return; - } - try { - const doc = JSON.parse(json); - resetDocument(doc); - setOpen(false); - } catch {} - }} - > - - - - - - - -
-
+ {dialog} ); }