feat: literal <-> expr

This commit is contained in:
cha0s 2021-01-29 15:03:09 -06:00
parent d57feed17e
commit 78d11634ca
2 changed files with 101 additions and 44 deletions

View File

@ -42,18 +42,27 @@ const Expression = ({
}, },
}); });
} }
if ('.' !== patches[0].value) { if ('.' === patches[patches.length - 1].value) {
// eslint-disable-next-line babel/no-unused-expressions const parts = patches[patches.length - 1].path.split('/');
patch(patches);
}
else {
const parts = patches[0].path.split('/');
parts.pop(); parts.pop();
patch({ patch({
op: 'remove', op: 'remove',
path: parts.join('/'), path: parts.join('/'),
}); });
} }
else if ('<literal>' === patches[patches.length - 1].value) {
patch({
op: 'replace',
path,
value: {
type: 'literal',
value: null,
},
});
}
else {
patch(patches);
}
}; };
do { do {
const op = ops[i]; const op = ops[i];
@ -61,10 +70,21 @@ const Expression = ({
const opPath = join(path, 'ops', opsCount.toString()); const opPath = join(path, 'ops', opsCount.toString());
const nextOp = i === ops.length - 1 ? undefined : ops[i + 1]; const nextOp = i === ops.length - 1 ? undefined : ops[i + 1];
if (isKey(op)) { if (isKey(op)) {
const isFirst = 0 === i;
const isLast = i === ops.length - 1 ? true : isInvocation(ops[i + 1]); const isLast = i === ops.length - 1 ? true : isInvocation(ops[i + 1]);
Renderables.push( Renderables.push(
<Key <Key
childrenDescription={{ childrenDescription={{
...(
isFirst && 'void' !== type
? {
'<literal>': {
label: '',
type,
},
}
: {}
),
...( ...(
isLast isLast
? {'.': {label: '', type: description.type}} ? {'.': {label: '', type: description.type}}
@ -132,7 +152,7 @@ const Expression = ({
type: 'invoke', type: 'invoke',
args: description.args.map(() => ({ args: description.args.map(() => ({
type: 'literal', type: 'literal',
value: 69, value: null,
})), })),
}; };
patch({ patch({

View File

@ -1,8 +1,10 @@
import {join} from 'path'; import {join} from 'path';
import {PropTypes, React} from '@latus/react'; import {PropTypes, React} from '@latus/react';
import {useJsonPatcher} from '@persea/json';
import Literal from '../literal'; import Literal from '../literal';
import Key from './key';
const Invocation = ({ const Invocation = ({
context, context,
@ -11,45 +13,80 @@ const Invocation = ({
onChange, onChange,
op, op,
path, path,
}) => ( }) => {
<div className="invocation"> // eslint-disable-next-line react/destructuring-assignment
( const contextDescription = context.describe();
{ const patch = useJsonPatcher();
op.args.map((arg, i) => ( return (
<div <div className="invocation">
key={JSON.stringify(arg)} (
className="invocation__arg" {
> op.args.map((arg, i) => (
{ <div
'literal' === arg.type key={join(path, 'args', i.toString())}
? ( className="invocation__arg"
<Literal >
onChange={onChange} {
path={join(path, 'args', i.toString(), 'value')} 'literal' === arg.type
type={description.args[i].type} ? (
value={arg} <>
/> <Key
) childrenDescription={{
: ( '<literal>': {
<Expression label: '',
context={context} type: arg.type,
expression={arg} },
onChange={onChange} ...contextDescription.children,
path={join(path, 'args', i.toString())} }}
type={description.args[i].type} onChange={(event, value) => {
/> patch({
) op: 'replace',
} path: join(path, 'args', i.toString()),
{i < op.args.length - 1 && <span className="invocation__arg-sep">, </span>} value: {
</div> type: 'expression',
)) ops: [
} {
) type: 'key',
</div> key: value,
); },
],
},
});
}}
op={op}
path={path}
/>
<Literal
onChange={onChange}
path={join(path, 'args', i.toString(), 'value')}
type={description.args[i].type}
value={arg}
/>
</>
)
: (
<Expression
context={context}
expression={arg}
onChange={onChange}
path={join(path, 'args', i.toString())}
type={description.args[i].type}
/>
)
}
{i < op.args.length - 1 && <span className="invocation__arg-sep">, </span>}
</div>
))
}
)
</div>
);
};
Invocation.propTypes = { Invocation.propTypes = {
context: PropTypes.shape({}).isRequired, context: PropTypes.shape({
describe: PropTypes.func,
}).isRequired,
description: PropTypes.shape({ description: PropTypes.shape({
args: PropTypes.arrayOf( args: PropTypes.arrayOf(
PropTypes.shape({ PropTypes.shape({