import React, {useState, useEffect, useLayoutEffect} from 'react'; import { IconButton, Dialog, Modal, Tooltip, DialogTitle, DialogContent, Typography, Paper, Menu, MenuItem, Button, } from '@material-ui/core'; import Checkbox from '@mui/material/Checkbox'; import { orange } from '@mui/material/colors'; import { isMobile } from "react-device-detect" import { GetParsedPaths } from "../views/Apps.jsx"; 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/addon/selection/mark-selection.js' import 'codemirror/theme/gruvbox-dark.css'; import 'codemirror/theme/duotone-light.css'; import { padding, textAlign } from '@mui/system'; const liquidFilters = [ {"name": "Size", "value": "size", "example": ""}, {"name": "Date", "value": `date: "%Y%M%d"`, "example": `{{ "now" | date: "%s" }}`}, ] const mathFilters = [ {"name": "Plus", "value": "plus: 1", "example": `{{ "1" | plus: 1 }}`}, {"name": "Minus", "value": "minus: 1", "example": `{{ "1" | minus: 1 }}`}, ] const pythonFilters = [ {"name": "Hello World", "value": `{% python %}\nprint("hello world")\n{% endpython %}`, "example": ``}, {"name": "Handle JSON", "value": `{% python %}\nimport json\njsondata = json.loads("""$nodename""")\n{% endpython %}`, "example": ``}, ] 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(""); const [anchorEl, setAnchorEl] = React.useState(null); const [anchorEl2, setAnchorEl2] = React.useState(null); const [anchorEl3, setAnchorEl3] = React.useState(null); const [mainVariables, setMainVariables] = React.useState([]); const [availableVariables, setAvailableVariables] = React.useState([]); const liquidOpen = Boolean(anchorEl); const mathOpen = Boolean(anchorEl2); const pythonOpen = Boolean(anchorEl3); useEffect(() => { var allVariables = [] var tmpVariables = [] for(var i=0; i < actionlist.length; i++){ allVariables.push('$'+actionlist[i].autocomplete.toLowerCase()) tmpVariables.push('$'+actionlist[i].autocomplete.toLowerCase()) var parsedPaths = [] if (typeof actionlist[i].example === "object") { parsedPaths = GetParsedPaths(actionlist[i].example, ""); } for (var key in parsedPaths) { const fullpath = "$"+actionlist[i].autocomplete.toLowerCase()+parsedPaths[key].autocomplete if (!allVariables.includes(fullpath)) { allVariables.push(fullpath) } } } setAvailableVariables(allVariables) setMainVariables(tmpVariables) }, []) 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) } } const findIndex = (line, loc) => { // var temp_arr = [] // for(var i=0; i { // value.markText({line:0, ch:2}, {line:0, ch:8}, {"css": "background-color: #f85a3e; border-radius: 4px; color: white"}) // value.markText({line:0, ch:13}, {line:0, ch:15}, {"css": "background-color: #f85a3e; border-radius: 4px; color: white"}) // value.markText({line:0, ch:19}, {line:0, ch:26}, {"css": "background-color: #f85a3e; border-radius: 4px; color: white"}) // value.markText({line:0, ch:31}, {line:0, ch:35}, {"css": "background-color: #f85a3e; border-radius: 4px; color: white"}) // value.markText({line:0, ch:69}, {line:0, ch:73}, {"css": "background-color: #f85a3e; border-radius: 4px; color: white"}) // var code_variables = value.getValue().match(/[$]{1}([a-zA-Z0-9_-]+\.?){1}([a-zA-Z0-9#_-]+\.?){0,}/g) // console.log(code_variables) // var regex = /[$]{1}([a-zA-Z0-9_-]+\.?){1}([a-zA-Z0-9#_-]+\.?){0,}/g // var code_variables_loc = regex.exec(value.getValue()) // console.log(code_variables_loc) // while ((code_variables_loc = regex.exec(value.getValue())) != null) { // console.log(code_variables_loc); // } // var code_variables_loc = regex.exec(value.getValue()) // console.log(code_variables_loc) var code_lines = localcodedata.split('\n') for (var i = 0; i < code_lines.length; i++){ var current_code_line = code_lines[i] // console.log(current_code_line) var variable_occurence = current_code_line.match(/[$]{1}([a-zA-Z0-9_-]+\.?){1}([a-zA-Z0-9#_-]+\.?){0,}/g) // console.log(variable_occurence) // console.log() // for (var j = 0; j < actionlist.length; j++) { // if(found[i].slice(1,).toLowerCase() === actionlist[j].autocomplete.toLowerCase()){ // input = input.replace(found[i], JSON.stringify(actionlist[j].example)); // console.log(input) // console.log(actionlist[j].example) // } // console.log(actionlist[j].autocomplete); // } var dollar_occurence = [] for(var ch=0; ch action.autocomplete.toLowerCase() === variable_occurence[occ].slice(1,).toLowerCase()) var correctVariable = availableVariables.includes(variable_occurence[occ].toLowerCase()) // console.log(actionlist) if(!correctVariable) { value.markText({line:i, ch:dollar_occurence[occ]}, {line:i, ch:dollar_occurence_len[occ]+dollar_occurence[occ]}, {"css": "background-color: rgb(248, 106, 62, 0.9); padding-top: 2px; padding-bottom: 2px; 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; padding-top: 2px; padding-bottom: 2px; color: white"}) } // console.log(correctVariables) } } catch (e) {} } } // function findlocation(arr) { // for(var i=0; i { // var updatedCode = localcodedata.slice(0,index) + "$" + str + localcodedata.slice(index+currentVariable.length+1,) // setlocalcodedata(updatedCode) // setEditorPopupOpen(false) // setCurrentLocation(0) // console.log(index) // console.log(currentLocation) var code_lines = localcodedata.split('\n') var parsedVariable = currentVariable if (currentVariable === undefined || currentVariable === null) { console.log("Location: ", currentLocation) parsedVariable= "$" } code_lines[currentLine] = code_lines[currentLine].slice(0,currentLocation[1]) + "$" + swapVariable + code_lines[currentLine].slice(currentLocation[1]+parsedVariable.length,) // console.log(code_lines) var updatedCode = code_lines.join('\n') // console.log(updatedCode) setlocalcodedata(updatedCode) } const expectedOutput = (input) => { const found = input.match(/[$]{1}([a-zA-Z0-9_-]+\.?){1}([a-zA-Z0-9#_-]+\.?){0,}/g) //console.log(found) try{ // When the found array is empty. for (var i = 0; i < found.length; i++) { // console.log(found[i]); for (var j = 0; j < actionlist.length; j++) { if(found[i].slice(1,).toLowerCase() === actionlist[j].autocomplete.toLowerCase()){ input = input.replace(found[i], JSON.stringify(actionlist[j].example)); // console.log(input) // console.log(actionlist[j].example) } // console.log(actionlist[j].autocomplete); } } } catch (e) {} const tmpValidation = validateJson(input.valueOf()) //setValidation(true) if (tmpValidation.valid === true) { setValidation(true) setExpOutput(tmpValidation.result) } else { setExpOutput(input.valueOf()) setValidation(false) } } const handleClick = (item) => { if (item === undefined || item.value === undefined || item.value === null) { return } if (!item.value.includes("{%")) { setlocalcodedata(localcodedata+" | "+item.value+" }}") } else { setlocalcodedata(localcodedata+item.value) } setAnchorEl(null) setAnchorEl2(null) setAnchorEl3(null) } return ( { console.log("In closer") changeActionParameterCodeMirror({target: {value: ""}}, fieldCount, localcodedata) //setExpansionModalOpen(false) }} PaperComponent={PaperComponent} PaperProps={{ style: { backgroundColor: theme.palette.surfaceColor, color: "white", minWidth: isMobile ? "100%" : 600, padding: isMobile ? "25px 10px 25px 10px" : 25, border: theme.palette.defaultBorder, zIndex: 10012, }, }} >
Code Editor { autoFormat(localcodedata) }} >
{ setAnchorEl(null); }} MenuListProps={{ 'aria-labelledby': 'basic-button', }} > {liquidFilters.map((item, index) => { return ( { handleClick(item) }}>{item.name} ) })} { setAnchorEl2(null); }} MenuListProps={{ 'aria-labelledby': 'basic-button', }} > {mathFilters.map((item, index) => { return ( { handleClick(item) }}>{item.name} ) })} { setAnchorEl3(null); }} MenuListProps={{ 'aria-labelledby': 'basic-button', }} > {pythonFilters.map((item, index) => { return ( { handleClick(item) }}>{item.name} ) })}
{ // 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(allVariable) // 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={{ styleSelectedText: true, theme: codeTheme, keyMap: 'sublime', mode: 'python', lineWrapping: linewrap, // mode: {codelang}, }} /> {editorPopupOpen ? {mainVariables.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], }, }} /> */}
{isMobile ? null : Output } {isMobile ? null : 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;