// Copyright 2024, Command Line Inc. // SPDX-License-Identifier: Apache-2.0 import * as React from "react"; import Frame from "react-frame-component"; type IJsonNode = { tag: string; props?: Record; children?: (IJsonNode | string)[]; }; const TagMap: Record> = {}; function convertNodeToTag(node: IJsonNode | string, idx?: number): JSX.Element | string { if (node == null) { return null; } if (idx == null) { idx = 0; } if (typeof node === "string") { return node; } let key = node.props?.key ?? "child-" + idx; let TagComp = TagMap[node.tag]; if (!TagComp) { return
Unknown tag:{node.tag}
; } return ; } function IJsonHtmlTag({ node }: { node: IJsonNode }) { let { tag, props, children } = node; let divProps = {}; if (props != null) { for (let [key, val] of Object.entries(props)) { if (key.startsWith("on")) { divProps[key] = (e: any) => { console.log("handler", key, val); }; } else { divProps[key] = val; } } } let childrenComps: (string | JSX.Element)[] = []; if (children != null) { for (let idx = 0; idx < children.length; idx++) { let comp = convertNodeToTag(children[idx], idx); if (comp != null) { childrenComps.push(comp); } } } return React.createElement(tag, divProps, childrenComps); } TagMap["div"] = IJsonHtmlTag; TagMap["b"] = IJsonHtmlTag; TagMap["i"] = IJsonHtmlTag; TagMap["p"] = IJsonHtmlTag; TagMap["s"] = IJsonHtmlTag; TagMap["span"] = IJsonHtmlTag; TagMap["a"] = IJsonHtmlTag; TagMap["img"] = IJsonHtmlTag; TagMap["h1"] = IJsonHtmlTag; TagMap["h2"] = IJsonHtmlTag; TagMap["h3"] = IJsonHtmlTag; TagMap["h4"] = IJsonHtmlTag; TagMap["h5"] = IJsonHtmlTag; TagMap["h6"] = IJsonHtmlTag; TagMap["ul"] = IJsonHtmlTag; TagMap["ol"] = IJsonHtmlTag; TagMap["li"] = IJsonHtmlTag; TagMap["input"] = IJsonHtmlTag; TagMap["button"] = IJsonHtmlTag; TagMap["textarea"] = IJsonHtmlTag; TagMap["select"] = IJsonHtmlTag; TagMap["option"] = IJsonHtmlTag; TagMap["form"] = IJsonHtmlTag; function IJsonView({ rootNode }: { rootNode: IJsonNode }) { // TODO fix this huge inline style return (
{convertNodeToTag(rootNode)}
); } export { IJsonView };