From a296b57c02c4a4011613e2e5424e3581b9a6a2ea Mon Sep 17 00:00:00 2001 From: cha0s Date: Fri, 29 Jan 2021 22:38:11 -0600 Subject: [PATCH] refactor: changes --- .../src/behavior-components/expression.jsx | 64 ++++++++++--------- 1 file changed, 35 insertions(+), 29 deletions(-) diff --git a/packages/entity/src/behavior-components/expression.jsx b/packages/entity/src/behavior-components/expression.jsx index 0b4725f..20f4f63 100644 --- a/packages/entity/src/behavior-components/expression.jsx +++ b/packages/entity/src/behavior-components/expression.jsx @@ -1,4 +1,4 @@ -import {join} from 'path'; +import {join, relative} from 'path'; import {isInvocation, isKey} from '@avocado/behavior'; import {PropTypes, React} from '@latus/react'; @@ -23,38 +23,18 @@ const Expression = ({ // eslint-disable-next-line react/destructuring-assignment let description = context.constructor.descriptionFor(walk); const onChange = (event, value, localPath) => { - if ('.' === value) { - const parts = localPath.split('/'); - parts.pop(); - patch({ - op: 'remove', - path: parts.join('/'), - }); - return; - } - if ('' === 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}, - ]; + const patches = []; + const j = parseInt(relative(path, localPath).split('/')[1], 10); if (j < ops.length - 1) { 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) { - patches.unshift({ + patches.push({ op: 'add', path: join(path, 'ops', j.toString()), value: { @@ -62,6 +42,10 @@ const Expression = ({ }, }); } + patches.push({ + path: localPath, + value, + }); patch(patches); }; do { @@ -93,7 +77,29 @@ const Expression = ({ ...description.children, }} key={opKey} - onChange={onChange} + onChange={(event, value, localPath) => { + if ('.' === value) { + const parts = localPath.split('/'); + parts.pop(); + patch({ + op: 'remove', + path: parts.join('/'), + }); + } + else if ('' === value) { + patch({ + op: 'replace', + path, + value: { + type: 'literal', + value: null, + }, + }); + } + else { + onChange(event, value, localPath); + } + }} op={op} path={opPath} />,