-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
be98d1d
commit da004ea
Showing
5 changed files
with
237 additions
and
3 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
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,90 @@ | ||
import { EditorProps } from "@/os/tools"; | ||
import { isValidAutomergeUrl } from "@automerge/automerge-repo"; | ||
import { useDocument } from "@automerge/automerge-repo-react-hooks"; | ||
import ReactJson from "@microlink/react-json-view"; | ||
import { useCallback } from "react"; | ||
import "react-error-boundary"; | ||
|
||
export const JSONEditor: React.FC<EditorProps<never, never>> = ({ docUrl }) => { | ||
const [doc, changeDoc] = useDocument(docUrl); | ||
|
||
/* | ||
const onSelectAutomergeUrl = useCallback( | ||
(url) => { | ||
setHistory([documentUrl, ...history]); | ||
changeDocumentUrl(url); | ||
}, | ||
[history, setHistory, changeDocumentUrl] | ||
); | ||
*/ | ||
|
||
const onEdit = useCallback( | ||
(args) => { | ||
const { namespace, new_value, name } = args; | ||
changeDoc(function (doc) { | ||
let current = doc; | ||
for ( | ||
let _i = 0, namespace_1 = namespace; | ||
_i < namespace_1.length; | ||
_i++ | ||
) { | ||
const key = namespace_1[_i]; | ||
current = current[key]; | ||
} | ||
current[name] = new_value; | ||
}); | ||
}, | ||
[changeDoc] | ||
); | ||
|
||
const onAdd = useCallback(function () { | ||
return true; | ||
}, []); | ||
|
||
const onDelete = useCallback( | ||
function ({ namespace, name }) { | ||
changeDoc(function (doc) { | ||
let current = doc; | ||
for ( | ||
let _i = 0, namespace_2 = namespace; | ||
_i < namespace_2.length; | ||
_i++ | ||
) { | ||
const key = namespace_2[_i]; | ||
current = current[key]; | ||
} | ||
delete current[name]; | ||
}); | ||
}, | ||
[changeDoc] | ||
); | ||
|
||
const onSelect = useCallback(function (arg) { | ||
console.log("select", arg); | ||
const { value } = arg; | ||
if (!(typeof value === "string")) { | ||
return; | ||
} | ||
|
||
if (isValidAutomergeUrl(value)) { | ||
//onSelectAutomergeUrl(value); | ||
} | ||
}, []); | ||
|
||
if (!doc) { | ||
return; | ||
} | ||
|
||
return ( | ||
<div className="h-full w-full overflow-auto px-5 py-4"> | ||
<ReactJson | ||
collapsed={3} | ||
src={doc} | ||
onEdit={onEdit} | ||
onAdd={onAdd} | ||
onDelete={onDelete} | ||
onSelect={onSelect} | ||
/> | ||
</div> | ||
); | ||
}; |
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,12 @@ | ||
import { Module } from "@/os/modules"; | ||
import { Tool, ToolMetaData } from "@/os/tools"; | ||
|
||
export default new Module<ToolMetaData, Tool>({ | ||
metadata: { | ||
id: "json", | ||
name: "JSON", | ||
supportedDatatypes: ["*"], | ||
}, | ||
|
||
load: () => import("./tool").then(({ JSONTool }) => JSONTool), | ||
}); |
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,6 @@ | ||
import { Tool } from "@/os/tools"; | ||
import { JSONEditor } from "./JSONEditor"; | ||
|
||
export const JSONTool: Tool = { | ||
editorComponent: JSONEditor, | ||
}; |
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