refactor: changes

This commit is contained in:
cha0s 2021-01-29 22:38:11 -06:00
parent 4a8e1b7515
commit a296b57c02

View File

@ -1,4 +1,4 @@
import {join} from 'path'; import {join, relative} from 'path';
import {isInvocation, isKey} from '@avocado/behavior'; import {isInvocation, isKey} from '@avocado/behavior';
import {PropTypes, React} from '@latus/react'; import {PropTypes, React} from '@latus/react';
@ -23,38 +23,18 @@ const Expression = ({
// eslint-disable-next-line react/destructuring-assignment // eslint-disable-next-line react/destructuring-assignment
let description = context.constructor.descriptionFor(walk); let description = context.constructor.descriptionFor(walk);
const onChange = (event, value, localPath) => { const onChange = (event, value, localPath) => {
if ('.' === value) { const patches = [];
const parts = localPath.split('/'); const j = parseInt(relative(path, localPath).split('/')[1], 10);
parts.pop();
patch({
op: 'remove',
path: parts.join('/'),
});
return;
}
if ('<literal>' === value) {
patch({
op: 'replace',
path,
value: {
type: 'literal',
value: null,
},
});
return;
}
const relative = localPath.slice(path.length + 1);
const j = parseInt(relative.split('/')[1], 10);
const patches = [
{path: localPath, value},
];
if (j < ops.length - 1) { if (j < ops.length - 1) {
for (let k = j + 1; k < ops.length; ++k) { for (let k = j + 1; k < ops.length; ++k) {
patches.unshift({op: 'remove', path: join(path, 'ops', k.toString())}); patches.push({
op: 'remove',
path: join(path, 'ops', k.toString()),
});
} }
} }
else if (j === ops.length) { else if (j === ops.length) {
patches.unshift({ patches.push({
op: 'add', op: 'add',
path: join(path, 'ops', j.toString()), path: join(path, 'ops', j.toString()),
value: { value: {
@ -62,6 +42,10 @@ const Expression = ({
}, },
}); });
} }
patches.push({
path: localPath,
value,
});
patch(patches); patch(patches);
}; };
do { do {
@ -93,7 +77,29 @@ const Expression = ({
...description.children, ...description.children,
}} }}
key={opKey} key={opKey}
onChange={onChange} onChange={(event, value, localPath) => {
if ('.' === value) {
const parts = localPath.split('/');
parts.pop();
patch({
op: 'remove',
path: parts.join('/'),
});
}
else if ('<literal>' === value) {
patch({
op: 'replace',
path,
value: {
type: 'literal',
value: null,
},
});
}
else {
onChange(event, value, localPath);
}
}}
op={op} op={op}
path={opPath} path={opPath}
/>, />,