import React, {useState, useEffect, useLayoutEffect} from 'react'; import { IconButton, Dialog, Modal, Tooltip, DialogTitle, DialogContent, Typography, Paper } from '@material-ui/core'; import Checkbox from '@mui/material/Checkbox'; import { orange } from '@mui/material/colors'; import { FullscreenExit as FullscreenExitIcon, } from "@material-ui/icons"; import { AutoFixHigh as AutoFixHighIcon, CompressOutlined, } from '@mui/icons-material'; import { useTheme } from '@material-ui/core/styles'; import { validateJson } from "../views/Workflows.jsx"; import ReactJson from "react-json-view"; import PaperComponent from "../components/PaperComponent.jsx"; import CodeMirror from '@uiw/react-codemirror'; import 'codemirror/keymap/sublime'; import 'codemirror/theme/gruvbox-dark.css'; import 'codemirror/theme/duotone-light.css'; import { padding, textAlign } from '@mui/system'; const CodeEditor = (props) => { const { fieldCount, setFieldCount, actionlist, changeActionParameterCodeMirror, expansionModalOpen, setExpansionModalOpen, codedata, setcodedata } = props const [localcodedata, setlocalcodedata] = React.useState(codedata === undefined || codedata === null || codedata.length === 0 ? "" : codedata); // const {codelang, setcodelang} = props const theme = useTheme(); const [validation, setValidation] = React.useState(false); const [expOutput, setExpOutput] = React.useState(" "); const [linewrap, setlinewrap] = React.useState(true); const [codeTheme, setcodeTheme] = React.useState("gruvbox-dark"); const [editorPopupOpen, setEditorPopupOpen] = React.useState(false); const [currentCharacter, setCurrentCharacter] = React.useState(-1); const [currentLine, setCurrentLine] = React.useState(-1); const [variableOccurences, setVariableOccurences] = React.useState([]); const [currentLocation, setCurrentLocation] = React.useState([]); const [currentVariable, setCurrentVariable] = React.useState(""); // useEffect(() => { // console.log(currentLocation) // }, [currentLocation]) const autoFormat = (input) => { if (validation !== true) { return } try { input = JSON.stringify(JSON.parse(input), null, 4) } catch (e) { console.log("Failed magic JSON stringification: ", e) } if (input !== localcodedata) { setlocalcodedata(input) } } function findIndex(line, loc) { // var temp_arr = [] // for(var i=0; i action.autocomplete.toLowerCase() === variable_occurence[occ].slice(1,).toLowerCase()) if(correctVariable == undefined) { value.markText({line:i, ch:dollar_occurence[occ]}, {line:i, ch:dollar_occurence_len[occ]+dollar_occurence[occ]}, {"css": "background-color: red; border-radius: 4px; color: white"}) } else{ value.markText({line:i, ch:dollar_occurence[occ]}, {line:i, ch:dollar_occurence_len[occ]+dollar_occurence[occ]}, {"css": "background-color: #8b8e26; border-radius: 4px; color: white"}) } // console.log(correctVariables) } } catch (e) {} } } // function findlocation(arr) { // for(var i=0; i { console.log("In closer") changeActionParameterCodeMirror({target: {value: ""}}, fieldCount, localcodedata) //setExpansionModalOpen(false) }} PaperComponent={PaperComponent} aria-labelledby="draggable-dialog-title" PaperProps={{ style: { backgroundColor: theme.palette.surfaceColor, color: "white", minWidth: 600, padding: 25, border: theme.palette.defaultBorder, zIndex: 10012, }, }} >
Code Editor { autoFormat(localcodedata) }} >
{ // console.log(value.getCursor()) setCurrentCharacter(value.getCursor().ch) setCurrentLine(value.getCursor().line) // console.log(value.getCursor().ch, value.getCursor().line) findIndex(value.getCursor().line, value.getCursor().ch) highlight_variables(value) }} onChange={(value) => { setlocalcodedata(value.getValue()) expectedOutput(value.getValue()) // console.log(value.getValue().split('\n')[value.getCursor().line]) // console.log(value.getCursor()) // console.log(value) // console.log(value.getValue().indexOf('$')) // console.log(value.display.input.prevInput) if(value.display.input.prevInput.startsWith('$') || value.display.input.prevInput.endsWith('$')){ setEditorPopupOpen(true) // console.log(findIndex(value.getValue())) // console.log(findlocation(findIndex(value.getValue()))) // setCurrentLocation(findlocation(findIndex(value.getValue()))) // console.log(currentLocation) // console.log(findVariables(findlocation(findIndex(value.getValue())), value.getValue())) // setCurrentVariable(findVariables(findlocation(findIndex(value.getValue())), value.getValue())) // console.log(actionlist) } // setVariableOccurences(findIndex(value.getValue())) // setCurrentVariable(findVariables(value.getValue())) // console.log(currentLocation) // console.log(currentVariable) // console.log(findIndex(value.getValue())) // highlight_variables(value) }} options={{ theme: codeTheme, keyMap: 'sublime', mode: 'javascript', lineWrapping: linewrap, // mode: {codelang}, }} /> {editorPopupOpen ? {actionlist.map((data, index) => { // console.log(data) return (
) })}
: null}
Line Wrap { if (linewrap) { setlinewrap(false) } if (!linewrap){ setlinewrap(true) } }} defaultChecked size="small" sx={{ color: orange[600], '&.Mui-checked': { color: orange[800], }, }} /> Dark Theme { if (codeTheme == "gruvbox-dark") { setcodeTheme("duotone-light") } if (codeTheme == "duotone-light"){ setcodeTheme("gruvbox-dark") } }} defaultChecked size="small" sx={{ color: orange[600], '&.Mui-checked': { color: orange[800], }, }} />
Output {validation === true ? { //handleReactJsonClipboard(copy); }} displayDataTypes={false} onSelect={(select) => { //HandleJsonCopy(validate.result, select, "exec"); }} name={"JSON autocompletion"} /> :

{expOutput}

}

JSON Validation: {validation ? "Correct" : "Incorrect"}

) } export default CodeEditor;