Skip to content

Commit cd82400

Browse files
authored
Merge pull request #106 from ngdenterprise/editor-improvements
Allow arrays and objects as invocation parameters
2 parents b899c09 + 34c2603 commit cd82400

File tree

6 files changed

+45
-30
lines changed

6 files changed

+45
-30
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ All notable changes to the Neo N3 Visual DevTracker extension will be documented
44

55
Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how to structure this file.
66

7+
## Unreleased
8+
9+
### Changed
10+
11+
- The invoke file editor now supports editing invocation files that use arrays or objects as contract parameters
12+
713
## [2.1.49] - 2021-06-21
814

915
### Changed

src/panel/components/contracts/ArgumentInput.tsx

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,36 @@ import InputNonDraggable from "../InputNonDraggable";
44
import NeoType from "./NeoType";
55

66
type Props = {
7-
arg?: string | number;
7+
arg?: any;
88
autoSuggestListId: string;
99
isReadOnly: boolean;
1010
name: string;
1111
type?: string | number;
12-
onUpdate: (newArgument: string | number) => void;
12+
onUpdate: (newArgument: any) => void;
13+
};
14+
15+
const valueToString = (value: any) => {
16+
if (!value) {
17+
return "";
18+
} else if (Array.isArray(value) || typeof value === "object") {
19+
return JSON.stringify(value);
20+
} else {
21+
return `${value}`;
22+
}
23+
};
24+
25+
const stringToValue = (text: string) => {
26+
if (`${parseInt(text)}` === text) {
27+
return parseInt(text);
28+
} else if (`${parseFloat(text)}` === text) {
29+
return parseFloat(text);
30+
} else {
31+
try {
32+
return JSON.parse(text);
33+
} catch (e) {
34+
return `${text}`;
35+
}
36+
}
1337
};
1438

1539
export default function ArgumentInput({
@@ -20,7 +44,7 @@ export default function ArgumentInput({
2044
type,
2145
onUpdate,
2246
}: Props) {
23-
const [value, setValue] = useState(`${arg || ""}`);
47+
const [value, setValue] = useState(valueToString(arg));
2448
const inputStyle: React.CSSProperties = {
2549
color: "var(--vscode-input-foreground)",
2650
backgroundColor: "var(--vscode-input-background)",
@@ -31,15 +55,6 @@ export default function ArgumentInput({
3155
padding: 1,
3256
marginLeft: 15,
3357
};
34-
const coerceType = (text: string) => {
35-
if (`${parseInt(text)}` === text) {
36-
return parseInt(text);
37-
} else if (`${parseFloat(text)}` === text) {
38-
return parseFloat(text);
39-
} else {
40-
return `${text}`;
41-
}
42-
};
4358
return (
4459
<div style={{ marginLeft: 15, marginTop: 4 }}>
4560
<div>
@@ -57,12 +72,12 @@ export default function ArgumentInput({
5772
style={inputStyle}
5873
type="text"
5974
value={value}
60-
onBlur={(e) => onUpdate(coerceType(e.target.value))}
61-
onChange={(e) => setValue(e.target.value)}
75+
onBlur={(e) => onUpdate(stringToValue(e.target.value))}
76+
onChange={(e) => setValue(valueToString(e.target.value))}
6277
onKeyDown={(e) => {
6378
if (e.metaKey) {
6479
// User may be about to save
65-
onUpdate(coerceType(value));
80+
onUpdate(stringToValue(value));
6681
}
6782
}}
6883
/>

src/panel/components/contracts/ArgumentsInput.tsx

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ import * as neonSc from "@cityofzion/neon-core/lib/sc";
55
import ArgumentInput from "./ArgumentInput";
66

77
type Props = {
8-
args: (string | number)[];
8+
args: any[];
99
autoSuggestListId: string;
1010
isReadOnly: boolean;
1111
parameterDefinitions?: neonSc.ContractParameterDefinitionJson[];
1212
style?: React.CSSProperties;
13-
setArguments: (newArguments: (string | number)[]) => void;
13+
setArguments: (newArguments: any[]) => void;
1414
};
1515

1616
export default function ArgumentsInput({
@@ -42,7 +42,7 @@ export default function ArgumentsInput({
4242
key={`${i}_${_}`}
4343
name={(parameterDefinitions || [])[i]?.name || `Argument #${i + 1}`}
4444
type={(parameterDefinitions || [])[i]?.type}
45-
onUpdate={(arg: string | number) =>
45+
onUpdate={(arg) =>
4646
setArguments(
4747
args
4848
.map((__, j) => (i === j ? arg : __))
@@ -59,9 +59,7 @@ export default function ArgumentsInput({
5959
isReadOnly={isReadOnly}
6060
key={args.length}
6161
name={`Argument #${args.length + 1}`}
62-
onUpdate={(arg: string | number) =>
63-
setArguments(arg ? [...args, arg] : [...args])
64-
}
62+
onUpdate={(arg) => setArguments(arg ? [...args, arg] : [...args])}
6563
/>
6664
)}
6765
</div>

src/panel/components/contracts/InvocationStep.tsx

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ type Props = {
1212
i: number;
1313
contract?: string;
1414
operation?: string;
15-
args?: (string | number)[];
15+
args?: any[];
1616
autoCompleteData: AutoCompleteData;
1717
argumentSuggestionListId: string;
1818
forceFocus?: boolean;
@@ -23,11 +23,7 @@ type Props = {
2323
onDragStart: () => void;
2424
onDragEnd: () => void;
2525
onRun: () => void;
26-
onUpdate: (
27-
contract?: string,
28-
operation?: string,
29-
args?: (string | number)[]
30-
) => void;
26+
onUpdate: (contract?: string, operation?: string, args?: any[]) => void;
3127
};
3228

3329
export default function InvocationStep({
@@ -124,7 +120,7 @@ export default function InvocationStep({
124120
operations.find((_) => _.name === operation)?.parameters
125121
}
126122
style={{ marginBottom: 10 }}
127-
setArguments={(newArguments: (string | number)[]) =>
123+
setArguments={(newArguments) =>
128124
onUpdate(contract, operation, newArguments)
129125
}
130126
/>

src/shared/messages/invokeFileViewRequest.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ type InvokeFileViewRequest = {
1313
i: number;
1414
contract?: string;
1515
operation?: string;
16-
args?: (string | number)[];
16+
args?: any[];
1717
};
1818
updateJson?: string;
1919
};

src/shared/viewState/invokeFileViewState.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ type InvokeFileViewState = {
1111
fileContents: {
1212
contract?: string;
1313
operation?: string;
14-
args?: (string | number)[];
14+
args?: any[];
1515
}[];
1616
fileContentsJson: string;
1717
isPartOfDiffView: boolean;

0 commit comments

Comments
 (0)