From ed6d9c7f49567e099dff79e6207ec46b6d0ef04e Mon Sep 17 00:00:00 2001 From: cha0s Date: Sun, 31 Jan 2021 12:23:11 -0600 Subject: [PATCH] feat: literal hints --- .../src/behavior-components/expression.jsx | 17 ++- .../expression/invocation.jsx | 129 ++++++++++++------ .../behavior-components/expression/key.jsx | 1 - .../src/behavior-components/literal.jsx | 20 +-- 4 files changed, 102 insertions(+), 65 deletions(-) diff --git a/packages/entity/src/behavior-components/expression.jsx b/packages/entity/src/behavior-components/expression.jsx index 023fafc..85424cc 100644 --- a/packages/entity/src/behavior-components/expression.jsx +++ b/packages/entity/src/behavior-components/expression.jsx @@ -199,10 +199,19 @@ const Expression = ({ type: 'invoke', args: description.vararg ? [] - : description.args.map(() => ({ - type: 'literal', - value: null, - })), + : description.args.map((arg) => { + let value = null; + if ('expressions' === arg.type) { + value = { + type: 'expressions', + expressions: [], + }; + } + return { + type: 'literal', + value, + }; + }), }, }); })(description, opPath)} diff --git a/packages/entity/src/behavior-components/expression/invocation.jsx b/packages/entity/src/behavior-components/expression/invocation.jsx index ca08104..868448e 100644 --- a/packages/entity/src/behavior-components/expression/invocation.jsx +++ b/packages/entity/src/behavior-components/expression/invocation.jsx @@ -1,6 +1,12 @@ import {join} from 'path'; -import {PropTypes, React} from '@latus/react'; +import { + PropTypes, + React, + useEffect, + useRef, + useState, +} from '@latus/react'; import {useJsonPatcher} from '@persea/json'; import Variant from '../variant'; @@ -13,50 +19,91 @@ const Invocation = ({ op, path, }) => { + const literalTypeRef = useRef(); + const [literalType, setLiteralType] = useState(false); + useEffect(() => { + literalTypeRef.current?.focus(); + }); const patch = useJsonPatcher(); const argComponent = (arg, i) => { const argPath = join(path, 'args', i.toString()); if ('undefined' === arg.type) { - return ( - { - if ('[literal]' === value) { - patch({ - op: 'replace', - path: argPath, - value: { - type: 'literal', - value: null, - }, - }); - } - else { - patch({ - type: 'add', - path: argPath, - value: { - type: 'expression', - ops: [ - { - type: 'key', - }, - ], - }, - }); - onChange(false, value, join(argPath, 'ops/0/key')); - } - }} - path={argPath} - value="..." - /> - ); + const confirmLiteralType = () => { + let value = null; + switch (literalType) { + case 'expressions': + value = { + type: 'expressions', + expressions: [], + }; + break; + case 'number': value = 0; break; + case 'string': value = ''; break; + case 'array': value = []; break; + case 'object': value = {}; break; + case 'vector': value = [0, 0]; break; + default: + } + patch({ + op: 'replace', + path: argPath, + value: { + type: 'literal', + value, + }, + }); + setLiteralType(false); + }; + return false === literalType + ? ( + { + if ('[literal]' === value) { + setLiteralType(''); + } + else { + patch({ + type: 'add', + path: argPath, + value: { + type: 'expression', + ops: [ + { + type: 'key', + }, + ], + }, + }); + onChange(false, value, join(argPath, 'ops/0/key')); + } + }} + path={argPath} + value="..." + /> + ) + : ( + { + setLiteralType(value); + }} + onKeyPress={(event) => { + if ('Enter' === event.key) { + confirmLiteralType(event); + } + }} + ref={literalTypeRef} + value={literalType} + /> + ); } const j = description.vararg ? Math.min(description?.args?.length - 1, i) : i; const options = description?.args?.[j].options; diff --git a/packages/entity/src/behavior-components/expression/key.jsx b/packages/entity/src/behavior-components/expression/key.jsx index 0a6ef2d..5f390e0 100644 --- a/packages/entity/src/behavior-components/expression/key.jsx +++ b/packages/entity/src/behavior-components/expression/key.jsx @@ -116,7 +116,6 @@ const Key = ({ ref={customizeRef} value={customization} /> - ) } diff --git a/packages/entity/src/behavior-components/literal.jsx b/packages/entity/src/behavior-components/literal.jsx index e14cff2..f44ef3f 100644 --- a/packages/entity/src/behavior-components/literal.jsx +++ b/packages/entity/src/behavior-components/literal.jsx @@ -50,25 +50,7 @@ const Literal = ({ null === value ? { type: 'expressions', - expressions: [ - { - type: 'expression', - ops: [ - { - type: 'key', - key: 'Flow', - }, - { - type: 'key', - key: 'nop', - }, - { - type: 'invoke', - args: [], - }, - ], - }, - ], + expressions: [], } : value }