@jbrowse/plugin-config
Version:
JBrowse 2 config utilities
69 lines (68 loc) • 3.48 kB
JavaScript
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
import { useEffect, useState } from 'react';
import { useDebounce } from '@jbrowse/core/util';
import { stringToJexlExpression } from '@jbrowse/core/util/jexlStrings';
import HelpIcon from '@mui/icons-material/Help';
import { IconButton, TextField, Tooltip } from '@mui/material';
import { observer } from 'mobx-react';
import { getEnv } from 'mobx-state-tree';
import { makeStyles } from 'tss-react/mui';
const fontFamily = 'Consolas, "Andale Mono WT", "Andale Mono", "Lucida Console", "Lucida Sans Typewriter", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Liberation Mono", "Nimbus Mono L", Monaco, "Courier New", Courier, monospace';
const useStyles = makeStyles()(theme => ({
callbackEditor: {
marginTop: '16px',
borderBottom: `1px solid ${theme.palette.divider}`,
width: '100%',
fontFamily,
},
textAreaFont: {
fontFamily,
},
callbackContainer: {
width: '100%',
overflowX: 'auto',
},
error: {
color: 'red',
fontSize: '0.8em',
},
}));
const CallbackEditor = observer(function ({ slot, }) {
const { classes } = useStyles();
const [code, setCode] = useState(slot.value);
const [error, setCodeError] = useState();
const debouncedCode = useDebounce(code, 400);
useEffect(() => {
var _a;
try {
const jexlDebouncedCode = debouncedCode.startsWith('jexl:')
? debouncedCode
: `jexl:${debouncedCode}`;
if (jexlDebouncedCode === 'jexl:') {
throw new Error('Empty jexl expression is not valid');
}
stringToJexlExpression(jexlDebouncedCode, (_a = getEnv(slot).pluginManager) === null || _a === void 0 ? void 0 : _a.jexl);
slot.set(jexlDebouncedCode);
setCodeError(undefined);
}
catch (e) {
console.error({ e });
setCodeError(e);
}
}, [debouncedCode, slot]);
return (_jsxs(_Fragment, { children: [error ? _jsx("p", { className: classes.error, children: `${error}` }) : null, _jsxs("div", { className: classes.callbackContainer, children: [_jsx(TextField, { multiline: true, className: classes.callbackEditor, value: code.startsWith('jexl:') ? code.split('jexl:')[1] : code, onChange: event => {
setCode(event.target.value);
}, style: { background: error ? '#fdd' : undefined }, slotProps: {
input: {
classes: {
input: classes.textAreaFont,
},
},
} }), _jsx("p", { children: slot.description }), _jsx(Tooltip, { title: _jsxs("div", { children: ["Callbacks are written in Jexl format. Click to learn more.", _jsx("br", {}), " Names of available context items: ", slot.contextVariable] }), arrow: true, children: _jsx(IconButton, { color: "primary", onClick: () => {
const newWindow = window.open('https://github.com/TomFrost/Jexl', '_blank', 'noopener,noreferrer');
if (newWindow) {
newWindow.opener = null;
}
}, children: _jsx(HelpIcon, {}) }) })] })] }));
});
export default CallbackEditor;