Skip to content

Commit

Permalink
Allow JSON function arguments on _Definitions_ page
Browse files Browse the repository at this point in the history
Provide a choice between Shesmu and raw JSON for function arguments in the _Try
It_ dialog.
  • Loading branch information
apmasell authored and avarsava committed Jan 30, 2024
1 parent 0fe022d commit ab8f5c1
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 17 deletions.
1 change: 1 addition & 0 deletions changes/change_tryit_format.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* Allow _Try It_ for functions on _Definitions_ page to take JSON or Shesmu input
71 changes: 55 additions & 16 deletions shesmu-server-ui/src/definitions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { fetchJsonWithBusyDialog, locallyStored } from "./io.js";
import {
fetchJsonWithBusyDialog,
locallyStored,
locallyStoredString,
} from "./io.js";
import {
DisplayElement,
IconName,
Expand All @@ -12,7 +16,7 @@ import {
hr,
indented,
inputSearch,
inputText,
inputTextArea,
italic,
link,
mono,
Expand All @@ -29,7 +33,7 @@ import {
} from "./html.js";
import { parse } from "./parser.js";
import * as valueParser from "./parser.js";
import { commonPathPrefix, mapModel } from "./util.js";
import { combineModels, commonPathPrefix, mapModel } from "./util.js";

export type Definition =
| ActionDefintion
Expand Down Expand Up @@ -689,12 +693,33 @@ function testFunction(func: FunctionDefinition): void {
const parsers = func.parameters.map(
(p) => parseDescriptor(p.type, valueParser)[0]
);
const inputs = func.parameters.map(() => inputText());
const savedFormat = locallyStoredString("shesmu_try_it_format", "shesmu");
const inputs = func.parameters.map(() => inputTextArea("", true));
const errors = func.parameters.map(() => pane("blank"));
dialog((_close) => [
"Test function ",
func.name,
br(),
"Argument Format: ",
dropdown(
(format, selected) => {
if (format == "json") {
return [{ type: "icon", icon: "braces" }, "JSON"];
} else {
return [{ type: "icon", icon: "card-text" }, "Shesmu"];
}
},
(format) => format == savedFormat.get(),
combineModels(),
{
synchronizer: savedFormat,
predicate: (recovered, item) => recovered == item,
extract: (x) => x,
},
"shesmu",
"json"
),
br(),
tableFromRows(
func.parameters.map((p, index) =>
tableRow(
Expand All @@ -715,21 +740,35 @@ function testFunction(func: FunctionDefinition): void {
() => {
const args: any[] = [];
if (
parsers.every((parser, index) =>
parse(
inputs[index].value,
parser,
(x) => {
args.push(x);
parsers.every((parser, index) => {
if (savedFormat.get() == "json") {
try {
args.push(JSON.parse(inputs[index].value));
errors[index].model.statusChanged(blank());
},
(message, position) =>
return true;
} catch (e) {
errors[index].model.statusChanged({
type: "b",
contents: `${position + 1}: ${message}`,
})
)
)
contents: `${e}`,
});
return false;
}
} else {
return parse(
inputs[index].value,
parser,
(x) => {
args.push(x);
errors[index].model.statusChanged(blank());
},
(message, position) =>
errors[index].model.statusChanged({
type: "b",
contents: `${position + 1}: ${message}`,
})
);
}
})
) {
fetchJsonWithBusyDialog(
"function",
Expand Down
8 changes: 7 additions & 1 deletion shesmu-server-ui/src/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2829,11 +2829,17 @@ export function inputText(initial?: string): InputField<string> {
/**
* Create a big text input box.
*/
export function inputTextArea(initial?: string): InputField<string> {
export function inputTextArea(
initial?: string,
short?: boolean
): InputField<string> {
const input = createUiFromTag("textarea");
if (initial) {
input.element.value = initial;
}
if (!short) {
input.element.classList.add("tall");
}
return {
ui: input,
get value() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -837,6 +837,8 @@ th {

textarea {
width: 100%;
}
textarea.tall {
height: 50vh;
}

Expand Down

0 comments on commit ab8f5c1

Please sign in to comment.