Skip to content

Commit

Permalink
merging json editor
Browse files Browse the repository at this point in the history
  • Loading branch information
aidansunbury committed Nov 8, 2024
2 parents bcc8d81 + 2caa68f commit 55dbbfc
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 13,707 deletions.
1 change: 1 addition & 0 deletions packages/dev-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"@trpc/react-query": "^11.0.0-next-beta.264",
"@trpc/server": "^11.0.0-next-beta.264",
"autoprefixer": "^10.4.14",
"jsoneditor": "^9.10.3",
"next": "^13.2.4",
"next-transpile-modules": "^10.0.0",
"postcss": "^8.4.21",
Expand Down
1 change: 1 addition & 0 deletions packages/dev-app/src/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { type AppType } from "next/app";
import { api } from "~/utils/api";

import "~/styles/globals.css";
import 'jsoneditor/dist/jsoneditor.css';

const MyApp: AppType = ({ Component, pageProps }) => {
return <Component {...pageProps} />;
Expand Down
2 changes: 2 additions & 0 deletions packages/trpc-panel/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"@trpc/server": "^11.0.0-next-beta.264",
"@types/jest": "^29.2.4",
"@types/json-bigint": "^1.0.1",
"@types/jsoneditor": "^9.9.1",
"@types/react": "^18.0.21",
"@types/react-dom": "^18.0.6",
"ajv": "^8.11.2",
Expand All @@ -66,6 +67,7 @@
"gulp-replace": "^1.1.3",
"jest": "^29.3.1",
"json-bigint": "^1.0.0",
"jsoneditor": "^9.10.3",
"postcss": "^8.4.19",
"react": "18.2.0",
"react-dom": "18.2.0",
Expand Down
5 changes: 3 additions & 2 deletions packages/trpc-panel/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ export default [
// commonjs(),
],
output: [
{ file: "lib/index.js", format: "cjs" },
{ file: "lib/index.mjs", format: "es" },
{ file: "lib/index.js", format: "cjs", inlineDynamicImports: true },
{ file: "lib/index.mjs", format: "es", inlineDynamicImports: true },
],
},
{
Expand All @@ -35,6 +35,7 @@ export default [
format: "umd",
sourcemap: true,
name: "trpc-panel",
inlineDynamicImports: true,
},
plugins: [
postcss({
Expand Down
44 changes: 44 additions & 0 deletions packages/trpc-panel/src/react-app/components/form/JSONEditor.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import React, { useEffect, useRef } from 'react';
import type JSONEditor from 'jsoneditor';

const jsonEditorImport = import('jsoneditor').then(({ default: JSONEditor }) => JSONEditor);

export default function JSONEditorDemo ({
value,
onChange,
}: {
value: any;
onChange: (value: any) => void;
}) {
const editorRef = useRef(null);

useEffect(() => {
let abort = false;
let newEditor: JSONEditor;
jsonEditorImport.then(JSONEditor => {
if (!abort && editorRef.current) {
newEditor = new JSONEditor(editorRef.current, {
mode: 'code',
history: false,
onChange: () => {
try {
onChange(newEditor.get());
} catch (err) {}
},
});
newEditor.set(value);
}
});

return () => {
abort = true;
if (newEditor) {
newEditor.destroy();
}
};
}, [ editorRef.current, value ]);

return (
<div className="jsoneditor-react-container" ref={editorRef} />
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ import { Error } from "./Error";
import { RequestResult } from "./RequestResult";
import { CollapsableSection } from "@src/react-app/components/CollapsableSection";
import { CloseIcon } from "@src/react-app/components/icons/CloseIcon";
import { ToggleJsonIcon } from "@src/react-app/components/icons/ToggleJsonIcon";
import { ObjectField } from "@src/react-app/components/form/fields/ObjectField";
import { fullFormats } from "ajv-formats/dist/formats";
import type { ParsedInputNode } from "@src/parse/parseNodeTypes";
import { DocumentationSection } from "@src/react-app/components/form/ProcedureForm/DescriptionSection";
import { Field } from "@src/react-app/components/form/Field";
import { ProcedureFormContextProvider } from "@src/react-app/components/form/ProcedureForm/ProcedureFormContext";
import getSize from "string-byte-length";
import JSONEditor from "../JSONEditor";

const TRPCErrorSchema = z.object({
meta: z.object({
Expand Down Expand Up @@ -121,6 +123,8 @@ export function ProcedureForm({
control,
reset: resetForm,
handleSubmit,
getValues,
setValue,
} = useForm({
resolver: ajvResolver(wrapJsonSchema(procedure.inputSchema as any), {
formats: fullFormats,
Expand Down Expand Up @@ -171,6 +175,12 @@ export function ProcedureForm({

const fieldName = procedure.node.path.join(".");

const [useRawInput, setUseRawInput] = useState(false);
function toggleRawInput() {
console.log(getValues());
setUseRawInput(!useRawInput);
}

return (
<ProcedureFormContextProvider path={procedure.pathFromRootRouter.join(".")}>
<CollapsableSection
Expand All @@ -193,24 +203,38 @@ export function ProcedureForm({

<FormSection
title="Input"
topRightElement={<XButton control={control} reset={reset} />}
topRightElement={
<div className="flex">
<XButton control={control} reset={reset} />
<ToggleRawInput onClick={toggleRawInput} />
</div>
}
>
{procedure.node.type === "object" ? (
Object.keys(procedure.node.children).length > 0 && (
<ObjectField
node={
procedure.node as ParsedInputNode & {
type: "object";
}
}
label={fieldName}
control={control}
topLevel
/>
)
) : (
<Field inputNode={procedure.node} control={control} />
{useRawInput && (
<JSONEditor
value={getValues().vals}
onChange={(values) => {
setValue("vals", values);
}}
/>
)}
{!useRawInput &&
(procedure.node.type === "object" ? (
Object.keys(procedure.node.children).length > 0 && (
<ObjectField
node={
procedure.node as ParsedInputNode & {
type: "object";
}
}
label={fieldName}
control={control}
topLevel
/>
)
) : (
<Field inputNode={procedure.node} control={control} />
))}

<ProcedureFormButton
text={`Execute ${name}`}
Expand Down Expand Up @@ -263,6 +287,16 @@ function XButton({
);
}

function ToggleRawInput({ onClick }: { onClick: () => void }) {
return (
<div className="w-6 h-6">
<button type="button" onClick={onClick}>
<ToggleJsonIcon className="w-6 h-6" />
</button>
</div>
);
}

function wrapJsonSchema(jsonSchema: any) {
delete jsonSchema["$schema"];

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import React from "react";

export function ToggleJsonIcon({ className }: { className?: string }) {
return (
<svg
id="Layer_1"
xmlns="http://www.w3.org/2000/svg"
width="24"
height="24"
viewBox="0 0 24 24"
className={className}
>
<defs>
<style>{`.cls-1{fill:#444;fill-rule:evenodd;}`}</style>
</defs>
<title>toggle raw JSON input</title>
<path
className="cls-1"
d="M8 3a2 2 0 0 0-2 2v4a2 2 0 0 1-2 2H3v2h1a2 2 0 0 1 2 2v4a2 2 0 0 0 2 2h2v-2H8v-5a2 2 0 0 0-2-2a2 2 0 0 0 2-2V5h2V3m6 0a2 2 0 0 1 2 2v4a2 2 0 0 0 2 2h1v2h-1a2 2 0 0 0-2 2v4a2 2 0 0 1-2 2h-2v-2h2v-5a2 2 0 0 1 2-2a2 2 0 0 1-2-2V5h-2V3h2Z"
/>
</svg>
);
}
1 change: 1 addition & 0 deletions packages/trpc-panel/src/react-app/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import React from "react";
import ReactDOM from "react-dom/client";
import { RootComponent } from "./Root";
import "./index.css";
import 'jsoneditor/dist/jsoneditor.css';
import { RenderOptions } from "@src/render";
import { trpc } from "@src/react-app/trpc";

Expand Down
Loading

0 comments on commit 55dbbfc

Please sign in to comment.