diff --git a/src/components/EditorWrapper.js b/src/components/EditorWrapper.js index 2aeeb53..5df4762 100644 --- a/src/components/EditorWrapper.js +++ b/src/components/EditorWrapper.js @@ -1,123 +1,129 @@ -import React, { useEffect, useRef, useState } from 'react'; +import React, { useEffect, useRef, useState } from "react"; import Editor, { DiffEditor } from "@monaco-editor/react"; const cleanEditorContent = { - title: "index", - value: "", - inputType: "json" -} + title: "index", + value: "", + inputType: "json", +}; const defaultOptions = { readOnly: true, domReadOnly: true }; function EditorWrapper({ results }) { - const [editorContent, setEditorContent] = useState(cleanEditorContent); - const [editorOptions, setEditorOptions] = useState(defaultOptions); + const [editorContent, setEditorContent] = useState(cleanEditorContent); + const [editorOptions, setEditorOptions] = useState(defaultOptions); + const [cacheKey, setCacheKey] = useState(1); - const editorRef = useRef(null); + const editorRef = useRef(null); - useEffect(() => { - console.log("updated editor"); - editorRef.current?.focus(); - }, [editorContent?.id]); + useEffect(() => { + editorRef.current?.focus(); + }, [editorContent?.id]); - const firstResult = results[0]?.id + const firstResult = results[0]?.id; - useEffect(() => { - if (!editorContent.id && firstResult) setEditorContent(results[results.length - 1]) - if (!results.length && editorContent.id) setEditorContent(cleanEditorContent); - }, [firstResult, editorContent, results]) - console.log({editorContent}) + useEffect(() => { + if (!editorContent.id && firstResult) + setEditorContent(results[results.length - 1]); + if (!results.length && editorContent.id) { + setEditorContent(cleanEditorContent); + setCacheKey((prev) => prev + 1); + } + }, [firstResult, editorContent, results]); - return ( + return ( + <> + {firstResult && ( <> - {firstResult && ( - <> - - - Save display to File - - - - - Toggle word wrap - - - - )} - {results.map((r, n) => { - return ( - + + + Save display to File + + + + + Toggle word wrap + + - ); + )} + {results.map((r, n) => { + return ( + + ); + })} + {editorContent.value?.type === "diff" ? ( + (editorRef.current = editor)} + /> + ) : ( + { + editorRef.current = editor; + }} + /> + )} + + ); } -export default EditorWrapper \ No newline at end of file +export default EditorWrapper; diff --git a/src/lib/types.js b/src/lib/types.js index afaaf2a..ff9a0c7 100644 --- a/src/lib/types.js +++ b/src/lib/types.js @@ -1,91 +1,91 @@ const types = { - bool: { - name: "Boolean", - test: v => !!v === v, - }, - number: { - name: "Number", - test: v => typeof v === 'number', - }, - string: { - name: "string", - test: v => typeof v === 'string', - }, - nonNullObject: { - name: "Non-Null Object", - test: v => typeof v === 'object' && v !== null, + bool: { + name: "Boolean", + test: (v) => !!v === v, + }, + number: { + name: "Number", + test: (v) => typeof v === "number", + }, + string: { + name: "string", + test: (v) => typeof v === "string", + }, + nonNullObject: { + name: "Non-Null Object", + test: (v) => typeof v === "object" && v !== null, + subTypes: { + array: { + name: "Array", + test: (v) => Array.isArray(v), + }, + kvObject: { + name: "Key-Value Object", + test: (v) => !Array.isArray(v), subTypes: { - array: { - name: "Array", - test: v => Array.isArray(v), + pkStructureDocument: { + name: "Proskomma Structure Document", + pkValidator: { + type: "structure", + key: "document", + version: "0.2.1", + }, + }, + perfSequence: { + name: "Proskomma PERF Sequence", + pkValidator: { + type: "constraint", + key: "perfSequence", + version: "0.2.1", + }, + }, + perfDocument: { + name: "Proskomma PERF Document", + pkValidator: { + type: "constraint", + key: "perfDocument", + version: "0.2.1", }, - kvObject: { - name: "Key-Value Object", - test: v => !Array.isArray(v), - subTypes: { - pkStructureDocument: { - name: "Proskomma Structure Document", - pkValidator: { - type: 'structure', - key: 'document', - version: '0.2.1' - } - }, - perfSequence: { - name: "Proskomma PERF Sequence", - pkValidator: { - type: 'constraint', - key: 'perfSequence', - version: '0.2.1' - } - }, - perfDocument: { - name: "Proskomma PERF Document", - pkValidator: { - type: 'constraint', - key: 'perfDocument', - version: '0.2.1' - } - }, - sofriaDocument: { - name: "Proskomma SOFRIA Document", - pkValidator: { - type: 'constraint', - key: 'sofriaDocument', - version: '0.2.1' - } - }, - } - } - } + }, + sofriaDocument: { + name: "Proskomma SOFRIA Document", + pkValidator: { + type: "constraint", + key: "sofriaDocument", + version: "0.2.1", + }, + }, + }, + }, }, + }, }; const flattenTypes = (typesObject, ancestors, passedRet) => { - if (!ancestors) { - ancestors = []; + if (!ancestors) { + ancestors = []; + } + const ret = passedRet || {}; + for (const [key, value] of Object.entries(typesObject)) { + ret[key] = { name: value.name }; + if ("test" in value) { + ret[key].test = value.test; + } + if ("pkValidator" in value) { + ret[key].pkValidator = value.pkValidator; } - const ret = passedRet || {}; - for (const [key, value] of Object.entries(typesObject)) { - ret[key] = {name: value.name}; - if ("test" in value) { - ret[key].test = value.test; - } - if ("pkValidator" in value) { - ret[key].pkValidator = value.pkValidator; - } - if (ancestors.length > 0) { - ret[key].super = ancestors; - } - if ("subTypes" in value) { - flattenTypes(value.subTypes, [...ancestors, key], ret); - } + if (ancestors.length > 0) { + ret[key].super = ancestors; } - return ret; -} + if ("subTypes" in value) { + flattenTypes(value.subTypes, [...ancestors, key], ret); + } + } + return ret; +}; const flattenedTypes = flattenTypes(types); -console.log(flattenedTypes, null, 2); +// console.log(flattenedTypes, null, 2); export default flattenedTypes;