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

@ -1654,7 +1654,7 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
}else{ }else{
workerScript.log("purchaseServer", () => `Invalid argument: ram='${ram}' must be a positive power of 2`); workerScript.log("purchaseServer", () => `Invalid argument: ram='${ram}' must be a positive power of 2`);
} }
return ""; return "";
} }
@ -2116,7 +2116,7 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
message = argsToString([message]); message = argsToString([message]);
SnackbarEvents.emit(message, variant, duration); SnackbarEvents.emit(message, variant, duration);
}, },
prompt: function (txt: any): any { prompt: function (txt: any, options?: { type?: string; options?: string[] }): any {
if (!isString(txt)) { if (!isString(txt)) {
txt = JSON.stringify(txt); txt = JSON.stringify(txt);
} }
@ -2124,6 +2124,7 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
return new Promise(function (resolve) { return new Promise(function (resolve) {
PromptEvent.emit({ PromptEvent.emit({
txt: txt, txt: txt,
options,
resolve: resolve, resolve: resolve,
}); });
}); });

@ -649,7 +649,7 @@ export function Root(props: IProps): React.ReactElement {
if (serverScriptIndex === -1 || savedScriptCode !== server.scripts[serverScriptIndex as number].code) { if (serverScriptIndex === -1 || savedScriptCode !== server.scripts[serverScriptIndex as number].code) {
PromptEvent.emit({ PromptEvent.emit({
txt: "Do you want to save changes to " + closingScript.fileName + "?", txt: "Do you want to save changes to " + closingScript.fileName + "?",
resolve: (result: boolean) => { resolve: (result: boolean | string) => {
if (result) { if (result) {
// Save changes // Save changes
closingScript.code = savedScriptCode; closingScript.code = savedScriptCode;
@ -704,7 +704,7 @@ export function Root(props: IProps): React.ReactElement {
PromptEvent.emit({ PromptEvent.emit({
txt: "Do you want to overwrite the current editor content with the contents of " + txt: "Do you want to overwrite the current editor content with the contents of " +
openScript.fileName + " on the server? This cannot be undone.", openScript.fileName + " on the server? This cannot be undone.",
resolve: (result: boolean) => { resolve: (result: boolean | string) => {
if (result) { if (result) {
// Save changes // Save changes
openScript.code = serverScriptCode; 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 { EventEmitter } from "../../utils/EventEmitter";
import { Modal } from "../../ui/React/Modal"; import { Modal } from "../../ui/React/Modal";
import Typography from "@mui/material/Typography"; import Typography from "@mui/material/Typography";
import Button from "@mui/material/Button"; 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]>(); export const PromptEvent = new EventEmitter<[Prompt]>();
interface Prompt { interface Prompt {
txt: string; txt: string;
resolve: (result: boolean) => void; options?: { type?: string; options?: string[] };
resolve: (result: boolean | string) => void;
} }
export function PromptManager(): React.ReactElement { export function PromptManager(): React.ReactElement {
@ -27,28 +32,143 @@ export function PromptManager(): React.ReactElement {
setPrompt(null); setPrompt(null);
} }
function yes(): void { let promptRenderer;
if (prompt === null) return; switch (prompt?.options?.type) {
prompt.resolve(true); case 'text': {
setPrompt(null); promptRenderer = promptMenuText;
} break;
function no(): void { }
if (prompt === null) return;
prompt.resolve(false); case 'select': {
setPrompt(null); promptRenderer = promptMenuSelect;
break;
}
default: {
promptRenderer = promptMenuBoolean;
}
} }
const valueState = useState('')
return ( return (
<> <>
{prompt != null && ( {prompt != null && (
<Modal open={true} onClose={close}> <Modal open={true} onClose={close}>
<Typography>{prompt.txt}</Typography> <Typography>{prompt.txt}</Typography>
<div style={{ display: 'flex', justifyContent: 'center', alignItems: 'center', paddingTop: '10px' }}> {promptRenderer(prompt, setPrompt, valueState)}
<Button style={{ marginRight: 'auto' }} onClick={yes}>Yes</Button>
<Button onClick={no}>No</Button>
</div>
</Modal> </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>
</>
);
}