Skip to content

Commit ab608d8

Browse files
committed
switcher
1 parent a11995f commit ab608d8

File tree

13 files changed

+394
-105
lines changed

13 files changed

+394
-105
lines changed

package-lock.json

Lines changed: 140 additions & 73 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/tdb-documents-ui/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,9 @@
118118
"mermaid": "^9.3.0",
119119
"prismjs": "^1.28.0",
120120
"react-bootstrap": "^2.7.0",
121+
"react-bootstrap-theme-switcher": "^1.0.7",
121122
"react-bootstrap-typeahead": "^6.0.0-alpha.9",
123+
"react-bootswatch-select": "^1.0.11",
122124
"react-calendar": "^4.2.1",
123125
"react-codemirror2": "^7.2.1",
124126
"react-css-theme-switcher": "^0.3.0",
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import React, {useState} from 'react'
2+
import SyntaxHighlighter from 'react-syntax-highlighter'
3+
import { vs2015 } from 'react-syntax-highlighter/dist/esm/styles/hljs'
4+
5+
export const DisplayCode = ({ codeString }) => {
6+
return <SyntaxHighlighter language="javascript" style={vs2015} wrapLines={true}>
7+
{codeString}
8+
</SyntaxHighlighter>
9+
}

packages/tdb-documents-ui/sandbox/src/Layout.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ import { View } from "./View"
44
import { ModeBar } from "./ModeBar"
55
import { DocumentTypes } from "./DocumentTypes"
66
import { Editor } from './Editors';
7-
7+
import { MoreInfo } from "./MoreInfoCanvas"
88

99
const App= (props) =>{
1010

11+
1112
return <Container>
1213
<DocumentTypes/>
14+
<MoreInfo/>
1315
<ModeBar/>
1416
<Row>
1517
<Col md={5}><Editor/></Col>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import React, { useState } from 'react'
2+
import Stack from 'react-bootstrap/Stack'
3+
import Offcanvas from 'react-bootstrap/Offcanvas'
4+
import {DisplayCode} from "./DisplayCode"
5+
/*import {Button} from "react-bootstrap"
6+
import {BsClipboard} from "react-icons/bs"
7+
import {copyToClipboard} from "./utils"*/
8+
import { FrameObj } from "./frameInit"
9+
10+
export const MoreInfo = () => {
11+
const {
12+
frames,
13+
type,
14+
mode,
15+
code,
16+
setShowCode,
17+
showCode
18+
} = FrameObj()
19+
20+
//if(!showCode) return <div/>
21+
22+
const handleClose = () => setShowCode(false)
23+
24+
return <Offcanvas show={showCode} onHide={handleClose} key={"more-info"} placement={"end"} name={"more-info"} style={{width: "900px"}}>
25+
<Offcanvas.Header closeButton className="bg-light text-dark">
26+
<Offcanvas.Title>Code</Offcanvas.Title>
27+
</Offcanvas.Header>
28+
<Offcanvas.Body>
29+
<Stack direction="horizontal" gap={3} className="mt-4 mb-4">
30+
{/*<h6>Click <a href={link}> here </a> to check out documentation</h6>*/}
31+
{/*<Button variant="primary"
32+
title="Copy to clipboard"
33+
className="btn btn-sm ms-auto"
34+
style={{float: "right"}}
35+
onClick={(e) => copyToClipboard(exampleCode)}>
36+
<BsClipboard className="mr-2"/> <label>Copy Code</label>
37+
</Button>*/}
38+
</Stack>
39+
<DisplayCode codeString={code}/>
40+
</Offcanvas.Body>
41+
</Offcanvas>
42+
}
43+
44+

packages/tdb-documents-ui/sandbox/src/View.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,35 @@ import Card from "react-bootstrap/Card"
33
import { FrameObj } from "./frameInit"
44
import { FrameViewer } from '@terminusdb/terminusdb-documents-ui'
55
import { getFormData, handleTraverse } from "./controller"
6+
import Button from "react-bootstrap/Button"
7+
import { Stack } from "react-bootstrap"
68

79
export const View = () => {
810

911
const {
1012
frames,
1113
type,
1214
mode,
13-
setData
15+
setData,
16+
setShowCode
1417
} = FrameObj()
1518

1619
function handleSubmit(data) {
1720
setData(data)
1821
}
1922

2023
return <Card className="w-100">
21-
<Card.Header className="d-flex">
22-
<div> {`Document Type - `}</div>
23-
<div className="text-warning fw-bolder">{type}</div>
24+
<Card.Header className="w-100">
25+
<Stack direction="horizontal">
26+
<div> {`Document Type - `}</div>
27+
<div className="text-warning fw-bolder">{type}</div>
28+
<Button className="ms-auto btn btn-sm" onClick={(e) => setShowCode(Date.now())}>View Code</Button>
29+
</Stack>
2430
</Card.Header>
2531
<Card.Body>
2632
<FrameViewer frame={frames}
2733
mode={mode}
28-
formData={getFormData(mode, type)}
34+
formData={getFormData(mode, type, setData)}
2935
onTraverse={handleTraverse}
3036
//onSelect={handleSelect}
3137
theme="darkly"

packages/tdb-documents-ui/sandbox/src/controller.js

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,19 @@ import { CREATE } from "./constants"
22
import * as lego from "./lego.constants"
33

44

5-
export function getFormData(mode, type) {
5+
export function getFormData(mode, type, setData) {
66
if(mode === CREATE) return {}
7-
8-
if(type === "Color") return lego.COLOR_DATA
9-
else if(type === "Element") return lego.ELEMENT_DATA
10-
else if(type === "Inventory") return lego.INVENTORY_DATA
11-
else if(type === "LegoSet") return lego.LEGO_SET_DATA
12-
else if(type === "Minifig") return lego.MINIFIG_DATA
13-
else if(type === "Part") return lego.PART_DATA
14-
else if(type === "PartRelation") return lego.PART_RELATION_DATA
15-
else if(type === "Theme") return lego.THEME_DATA
7+
let data = null
8+
if(type === "Color") data = lego.COLOR_DATA
9+
else if(type === "Element") data = lego.ELEMENT_DATA
10+
else if(type === "Inventory") data = lego.INVENTORY_DATA
11+
else if(type === "LegoSet") data = lego.LEGO_SET_DATA
12+
else if(type === "Minifig") data = lego.MINIFIG_DATA
13+
else if(type === "Part") data = lego.PART_DATA
14+
else if(type === "PartRelation") data = lego.PART_RELATION_DATA
15+
else if(type === "Theme") data = lego.THEME_DATA
16+
if(setData) setData(data)
17+
return data
1618
}
1719

1820
/**

packages/tdb-documents-ui/sandbox/src/frameInit.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ export const FrameContext = React.createContext()
33
export const FrameObj = () => useContext(FrameContext)
44
import { CREATE, LEGO } from "./constants"
55
import { LEGO_FRAMES } from "./lego.constants"
6+
import { generateFrameViewerCode } from "./generateCode"
67

78
export const FrameProvider = ({ children, config }) => {
89

@@ -16,6 +17,16 @@ export const FrameProvider = ({ children, config }) => {
1617
const [type, setType] = useState("Theme")
1718
// constants to store data
1819
const [data, setData] = useState({})
20+
// consttant to store display of code
21+
const [showCode, setShowCode] = useState(false)
22+
// consttant to store code
23+
const [code, setCode] = useState(false)
24+
25+
useEffect(() => {
26+
if(showCode) {
27+
setCode(generateFrameViewerCode(data, mode, type))
28+
}
29+
}, [showCode])
1930

2031

2132
return (
@@ -30,7 +41,11 @@ export const FrameProvider = ({ children, config }) => {
3041
formData,
3142
setFormData,
3243
data,
33-
setData
44+
setData,
45+
showCode,
46+
setShowCode,
47+
code,
48+
setCode
3449
}}
3550
>
3651
{children}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/** Generates Frame Viewer code with current params
2+
* @param {*} uiFrames - uiFrames to alter look and feel of form
3+
* @param {*} data - instance data to view form in Edit/View Mode
4+
* @param {*} mode - mode in which form is to be displayed - Create, Edit or View
5+
* @param {*} type - type of Document being displayed in form
6+
*/
7+
export const generateFrameViewerCode = function(data, mode, type){
8+
return `
9+
10+
/***
11+
* frames - JSON frames of all document types (mandatory)
12+
* type - document type to be displayed (mandatory)
13+
* data - data to be displayed in form (mandatory for EDIT/ VIEW mode - if nothing to display pass {})
14+
* mode - the mode in which form is to be displayed - Create/ Edit or View
15+
* onTraverse - a js function which gets back the ID of a document on click
16+
*/
17+
export const Form = ({ frames, type, data, mode }) => {
18+
19+
/* Callback submit function which extracts all user input fields */
20+
function handleSubmit(data) {
21+
console.log("Data submitted from form", data)
22+
}
23+
24+
/* filled data */
25+
let fData= '${JSON.stringify(data, null, 4)}'
26+
27+
if(!frames) return <>LOADING ... <>
28+
29+
return <FrameViewer
30+
frame={frames} // frames
31+
type={"${type}"} // type of document to display in form
32+
formData={fData} // instance data
33+
mode={"${mode}"} // mode in which to display the form
34+
hideSubmit={mode==="View" ? true : false} // hide Submit button when in VIEW mode
35+
onSubmit={handleSubmit} // Callback submit function
36+
/>
37+
}
38+
`
39+
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
import React, { useEffect, useState } from "react"
2+
import { getProperties } from "./FrameHelpers"
3+
import * as CONST from "./constants"
4+
import { Alert } from "react-bootstrap"
5+
import * as util from "./utils"
6+
import { Viewer } from "./Viewer"
7+
import { HelperMessages } from "./HelperMessages"
8+
import { constructFormParams } from "./constructFormParams"
9+
import { loadTheme } from "./formActions"
10+
11+
/*
12+
** frame - full json schema of a document
13+
** uiFrame - ui json of a document
14+
** type - document type of interest
15+
** mode - create/ edit/ view
16+
** submitButton - submit button configuration json object
17+
** formData - filled value of the document
18+
** onSubmit - a function with have custom logic to process data submitted
19+
** onSelect - a js function which gets back the selected value from selects
20+
** onTraverse - a js function which gets back the ID of a document on click
21+
** compareFormData - used for diff viewers to compare against original or changed data
22+
** language - language code parameters to support a wide variety of languages in Ui as defined in schema
23+
*/
24+
export function FrameViewer(props){
25+
26+
let { frame, uiFrame, type, mode, formData, compareFormData, onSubmit, onTraverse, onSelect, language, theme } = props
27+
28+
// schema constants
29+
const [schema, setSchema]=useState(false)
30+
// ui schema constants
31+
const [uiSchema, setUISchema]=useState(false)
32+
// display constants which holds schema & ui Schema to refresh Viewer
33+
const [display, setDisplay]=useState(false)
34+
// read only constants (used in VIEW mode)
35+
const [readOnly, setReadOnly]=useState(false)
36+
// language support constants
37+
const [lang, setLanguage]=useState(false)
38+
// error constants
39+
const [error, setError]=useState(false)
40+
// documentation constants
41+
const [documentation, setDocumentation]=useState(false)
42+
// form data constants
43+
const [data, setData]=useState(formData)
44+
// message constants
45+
const [message, setMessage]=useState(false)
46+
// look up constants which maintains a reference to all of the class definitions
47+
const [reference, setReference]=useState({})
48+
// constants to update form props
49+
const [update, setUpdate] = useState(false)
50+
51+
52+
function clear() {
53+
// reset everything on change of form props
54+
setDisplay(false)
55+
setSchema(false)
56+
setUISchema(false)
57+
setReadOnly(false)
58+
setLanguage(false)
59+
}
60+
61+
useEffect(() => {
62+
if(theme) {
63+
loadTheme(theme)
64+
}
65+
}, [theme])
66+
67+
68+
useEffect(() => {
69+
//try{
70+
if(frame && type && mode) {
71+
clear()
72+
// update form
73+
setUpdate(Date.now())
74+
}
75+
//}
76+
//catch(e) {
77+
//setError(`An error has occured in generating frames. Err - ${e}`)
78+
//}
79+
80+
}, [frame, uiFrame, type, mode, formData, language])
81+
82+
useEffect(() => {
83+
// on update construct form params
84+
if(update) {
85+
constructFormParams(props, setDocumentation, reference, setReference, setReadOnly, setMessage, display, setDisplay)
86+
}
87+
}, [update])
88+
89+
90+
91+
if(error) {
92+
return <Alert variant="danger">{error}</Alert>
93+
}
94+
95+
return <div className="tdb__frame__viewer ">
96+
<HelperMessages frame={frame} mode={mode} type={type} formData={formData}/>
97+
<Viewer display={display}
98+
message={message}
99+
mode={mode}
100+
type={type}
101+
language={language}
102+
onSubmit={onSubmit}
103+
readOnly={readOnly}
104+
data={formData}
105+
setData={setData}
106+
documentation={documentation}/>
107+
</div>
108+
109+
}
110+
111+

0 commit comments

Comments
 (0)