Adding support for text/select options in Prompt command

This commit is contained in:
Phil 2022-01-30 14:06:18 +00:00
parent 9ddb1c4379
commit 08d0b4cbae
3 changed files with 140 additions and 19 deletions

@ -2116,7 +2116,7 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
message = argsToString([message]);
SnackbarEvents.emit(message, variant, duration);
},
prompt: function (txt: any): any {
prompt: function (txt: any, options?: { type?: string; options?: string[] }): any {
if (!isString(txt)) {
txt = JSON.stringify(txt);
}
@ -2124,6 +2124,7 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
return new Promise(function (resolve) {
PromptEvent.emit({
txt: txt,
options,
resolve: resolve,
});
});

@ -649,7 +649,7 @@ export function Root(props: IProps): React.ReactElement {
if (serverScriptIndex === -1 || savedScriptCode !== server.scripts[serverScriptIndex as number].code) {
PromptEvent.emit({
txt: "Do you want to save changes to " + closingScript.fileName + "?",
resolve: (result: boolean) => {
resolve: (result: boolean | string) => {
if (result) {
// Save changes
closingScript.code = savedScriptCode;
@ -704,7 +704,7 @@ export function Root(props: IProps): React.ReactElement {
PromptEvent.emit({
txt: "Do you want to overwrite the current editor content with the contents of " +
openScript.fileName + " on the server? This cannot be undone.",
resolve: (result: boolean) => {
resolve: (result: boolean | string) => {
if (result) {
// Save changes
openScript.code = serverScriptCode;

@ -1,14 +1,19 @@
import React, { useState, useEffect } from "react";
import React, { useState, useEffect, Dispatch, SetStateAction } from "react";
import { EventEmitter } from "../../utils/EventEmitter";
import { Modal } from "../../ui/React/Modal";
import Typography from "@mui/material/Typography";
import Button from "@mui/material/Button";
import Select, { SelectChangeEvent } from "@mui/material/Select";
import TextField from '@mui/material/TextField';
import { KEY } from '../../utils/helpers/keyCodes';
import MenuItem from '@mui/material/MenuItem';
export const PromptEvent = new EventEmitter<[Prompt]>();
interface Prompt {
txt: string;
resolve: (result: boolean) => void;
options?: { type?: string; options?: string[] };
resolve: (result: boolean | string) => void;
}
export function PromptManager(): React.ReactElement {
@ -27,28 +32,143 @@ export function PromptManager(): React.ReactElement {
setPrompt(null);
}
function yes(): void {
if (prompt === null) return;
prompt.resolve(true);
setPrompt(null);
let promptRenderer;
switch (prompt?.options?.type) {
case 'text': {
promptRenderer = promptMenuText;
break;
}
function no(): void {
if (prompt === null) return;
prompt.resolve(false);
setPrompt(null);
case 'select': {
promptRenderer = promptMenuSelect;
break;
}
default: {
promptRenderer = promptMenuBoolean;
}
}
const valueState = useState('')
return (
<>
{prompt != null && (
<Modal open={true} onClose={close}>
<Typography>{prompt.txt}</Typography>
<div style={{ display: 'flex', justifyContent: 'center', alignItems: 'center', paddingTop: '10px' }}>
<Button style={{ marginRight: 'auto' }} onClick={yes}>Yes</Button>
<Button onClick={no}>No</Button>
</div>
{promptRenderer(prompt, setPrompt, valueState)}
</Modal>
)}
</>
);
}
function promptMenuBoolean(prompt: Prompt | null, setPrompt: Dispatch<SetStateAction<Prompt | null>>): React.ReactElement {
const yes = (): void => {
if (prompt !== null) {
prompt.resolve(true);
setPrompt(null);
}
}
const no = (): void => {
if (prompt !== null) {
prompt.resolve(false);
setPrompt(null);
}
}
return (
<>
<div style={{ display: 'flex', justifyContent: 'center', alignItems: 'center', paddingTop: '10px' }}>
<Button style={{ marginRight: 'auto' }} onClick={yes}>Yes</Button>
<Button onClick={no}>No</Button>
</div>
</>
);
}
function promptMenuText(prompt: Prompt | null, setPrompt: Dispatch<SetStateAction<Prompt | null>>, valueState: [string, Dispatch<SetStateAction<string>>]): React.ReactElement {
const [value, setValue] = valueState
const submit = (): void => {
if (prompt !== null) {
prompt.resolve(value);
setValue('')
setPrompt(null);
}
}
const onInput = (event: React.ChangeEvent<HTMLInputElement>): void => {
setValue(event.target.value);
}
const onKeyDown = (event: React.KeyboardEvent<HTMLInputElement>): void => {
event.stopPropagation();
if (prompt !== null && event.keyCode === KEY.ENTER) {
event.preventDefault();
submit();
}
}
return (
<>
<div style={{ display: 'flex', alignItems: 'center', paddingTop: '10px' }}>
<TextField
autoFocus
value={value}
onInput={onInput}
onKeyDown={onKeyDown}
style={{ flex: '1 0 auto' }}
InputProps={{
endAdornment: (
<Button
onClick={() => {
submit();
}}
>
Confirm
</Button>
),
}}
/>
</div>
</>
);
}
function promptMenuSelect(prompt: Prompt | null, setPrompt: Dispatch<SetStateAction<Prompt | null>>, valueState: [string, Dispatch<SetStateAction<string>>]): React.ReactElement {
const [value, setValue] = valueState
const submit = (): void => {
if (prompt !== null) {
prompt.resolve(value);
setValue('');
setPrompt(null);
}
}
const onChange = (event: SelectChangeEvent<string>): void => {
setValue(event.target.value);
}
const getItems = (options: string[] | { [key: string]: string }) : React.ReactElement[] => {
const content = [];
for (const i in options) {
// @ts-ignore
content.push(<MenuItem value={i}>{options[i]}</MenuItem>);
}
return content;
}
return (
<>
<div style={{ display: 'flex', alignItems: 'center', paddingTop: '10px' }}>
<Select onChange={onChange} value={value} style={{ flex: '1 0 auto' }}>
{getItems(prompt?.options?.options || [])}
</Select>
<Button onClick={submit}>Confirm</Button>
</div>
</>
);
}