diff --git a/src/MonacoJSONEditor.tsx b/src/MonacoJSONEditor.tsx index f4a6f2de..7423f947 100644 --- a/src/MonacoJSONEditor.tsx +++ b/src/MonacoJSONEditor.tsx @@ -9,6 +9,7 @@ import _ from "lodash"; import { JSONSchema4 } from "json-schema"; import schema from "@open-rpc/meta-schema"; import { IUISchema } from "./UISchema"; +import examplesList from "./examplesList"; interface IProps { defaultValue?: string; @@ -46,16 +47,14 @@ export default class MonacoJSONEditor extends React.Component { this.metaSchema = schema as JSONSchema4; const defaultV = _.isEmpty(this.props.defaultValue) ? null : JSON.stringify(this.props.defaultValue, undefined, " "); - const emptySchema = JSON.stringify({ - openrpc: "1.0.0-rc1", - info: { - title: "", - version: "", - }, - methods: [], - }, undefined, "\t"); + const ex = examplesList.find((e) => e.name! === "petstore"); + const defaultSchema = await fetch(ex!.url).then((res) => res.text()); const localStorageSchema = window.localStorage.getItem("schema"); - const defaultValue = defaultV || localStorageSchema || emptySchema; + let defaultValue = (defaultV || localStorageSchema || defaultSchema).trim(); + + if (defaultValue === "{}" || defaultValue === "") { + defaultValue = defaultSchema; + } model = monaco.editor.createModel(defaultValue, "json", modelUri); monaco.languages.json.jsonDefaults.setDiagnosticsOptions({ diff --git a/src/SearchBar/SearchBar.tsx b/src/SearchBar/SearchBar.tsx index 9425ed46..1e1082d6 100644 --- a/src/SearchBar/SearchBar.tsx +++ b/src/SearchBar/SearchBar.tsx @@ -15,6 +15,7 @@ import { Grid, } from "@material-ui/core"; import { IUISchema } from "../UISchema"; +import suggestions from "../examplesList"; const styles = (theme: Theme) => ({ title: { @@ -31,33 +32,8 @@ interface IProps extends WithStyles { onSplitViewChange?: any; } + function getSuggestion(query: string | null) { - const suggestions = [ - { - name: "api-with-examples", - url: "https://raw.githubusercontent.com/open-rpc/examples/master/service-descriptions/api-with-examples-openrpc.json", //tslint:disable-line - }, - { - name: "link-example", - url: "https://raw.githubusercontent.com/open-rpc/examples/master/service-descriptions/link-example-openrpc.json", //tslint:disable-line - }, - { - name: "params-by-name-petstore", - url: "https://raw.githubusercontent.com/open-rpc/examples/master/service-descriptions/params-by-name-petstore-openrpc.json", //tslint:disable-line - }, - { - name: "petstore-expanded", - url: "https://raw.githubusercontent.com/open-rpc/examples/master/service-descriptions/petstore-expanded-openrpc.json", //tslint:disable-line - }, - { - name: "petstore", - url: "https://raw.githubusercontent.com/open-rpc/examples/master/service-descriptions/petstore-openrpc.json", //tslint:disable-line - }, - { - name: "simple-math", - url: "https://raw.githubusercontent.com/open-rpc/examples/master/service-descriptions/simple-math-openrpc.json", //tslint:disable-line - }, - ]; if (!query) { return suggestions; } diff --git a/src/examplesList.tsx b/src/examplesList.tsx new file mode 100644 index 00000000..6a4898a2 --- /dev/null +++ b/src/examplesList.tsx @@ -0,0 +1,26 @@ +export default [ + { + name: "api-with-examples", + url: "https://raw.githubusercontent.com/open-rpc/examples/master/service-descriptions/api-with-examples-openrpc.json", //tslint:disable-line + }, + { + name: "link-example", + url: "https://raw.githubusercontent.com/open-rpc/examples/master/service-descriptions/link-example-openrpc.json", //tslint:disable-line + }, + { + name: "params-by-name-petstore", + url: "https://raw.githubusercontent.com/open-rpc/examples/master/service-descriptions/params-by-name-petstore-openrpc.json", //tslint:disable-line + }, + { + name: "petstore-expanded", + url: "https://raw.githubusercontent.com/open-rpc/examples/master/service-descriptions/petstore-expanded-openrpc.json", //tslint:disable-line + }, + { + name: "petstore", + url: "https://raw.githubusercontent.com/open-rpc/examples/master/service-descriptions/petstore-openrpc.json", //tslint:disable-line + }, + { + name: "simple-math", + url: "https://raw.githubusercontent.com/open-rpc/examples/master/service-descriptions/simple-math-openrpc.json", //tslint:disable-line + }, +];