remove ns2 example doing DOM manip

This commit is contained in:
Olivier Gagnon 2021-09-20 00:14:30 -04:00
parent 7355f4212b
commit a3e624deba
7 changed files with 36 additions and 41 deletions

@ -117,30 +117,6 @@ that are loaded.
Examples Examples
-------- --------
**DOM Manipulation (tprintColored.ns)**
Directly alter the game's terminal and print colored text::
export function tprintColored(txt, color) {
let terminalInput = document.getElementById("terminal-input");
let rowElement = document.createElement("tr");
let cellElement = document.createElement("td");
rowElement.classList.add("posted");
cellElement.classList.add("terminal-line");
cellElement.style.color = color;
cellElement.innerText = txt;
rowElement.appendChild(cellElement);
terminalInput.before(rowElement);
}
export async function main(ns) {
tprintColored("Red Text!", "red");
tprintColored("Blue Text!", "blue");
tprintColored("Use Hex Codes!", "#3087E3");
}
**Script Scheduler (scriptScheduler.ns)** **Script Scheduler (scriptScheduler.ns)**
This script shows some of the new functionality that is available in NetscriptJS, This script shows some of the new functionality that is available in NetscriptJS,

@ -5,7 +5,7 @@ import { HacknetServer } from "../Hacknet/HacknetServer";
import { BaseServer } from "../Server/BaseServer"; import { BaseServer } from "../Server/BaseServer";
import { Programs } from "../Programs/Programs"; import { Programs } from "../Programs/Programs";
import { CodingContractResult } from "../CodingContracts"; import { CodingContractResult } from "../CodingContracts";
import { TerminalEvents } from "./TerminalEvents"; import { TerminalEvents, TerminalClearEvents } from "./TerminalEvents";
import { TextFile } from "../TextFile"; import { TextFile } from "../TextFile";
import { Script } from "../Script/Script"; import { Script } from "../Script/Script";
@ -480,6 +480,7 @@ export class Terminal implements ITerminal {
// TODO: remove this once we figure out the height issue. // TODO: remove this once we figure out the height issue.
this.outputHistory = [new Output(`Bitburner v${CONSTANTS.Version}`, "primary")]; this.outputHistory = [new Output(`Bitburner v${CONSTANTS.Version}`, "primary")];
TerminalEvents.emit(); TerminalEvents.emit();
TerminalClearEvents.emit();
} }
prestige(): void { prestige(): void {

@ -1,2 +1,3 @@
import { EventEmitter } from "../utils/EventEmitter"; import { EventEmitter } from "../utils/EventEmitter";
export const TerminalEvents = new EventEmitter<[]>(); export const TerminalEvents = new EventEmitter<[]>();
export const TerminalClearEvents = new EventEmitter<[]>();

@ -11,7 +11,7 @@ import { ITerminal, Output, Link } from "../ITerminal";
import { IRouter } from "../../ui/Router"; import { IRouter } from "../../ui/Router";
import { IPlayer } from "../../PersonObjects/IPlayer"; import { IPlayer } from "../../PersonObjects/IPlayer";
import { TerminalInput } from "./TerminalInput"; import { TerminalInput } from "./TerminalInput";
import { TerminalEvents } from "../TerminalEvents"; import { TerminalEvents, TerminalClearEvents } from "../TerminalEvents";
interface IActionTimerProps { interface IActionTimerProps {
terminal: ITerminal; terminal: ITerminal;
@ -51,13 +51,17 @@ interface IProps {
export function TerminalRoot({ terminal, router, player }: IProps): React.ReactElement { export function TerminalRoot({ terminal, router, player }: IProps): React.ReactElement {
const scrollHook = useRef<HTMLDivElement>(null); const scrollHook = useRef<HTMLDivElement>(null);
const setRerender = useState(0)[1]; const setRerender = useState(0)[1];
const [key, setKey] = useState(0);
function rerender(): void { function rerender(): void {
setRerender((old) => old + 1); setRerender((old) => old + 1);
} }
useEffect(() => { function clear(): void {
return TerminalEvents.subscribe(rerender); setKey((key) => key + 1);
}, []); }
useEffect(() => TerminalEvents.subscribe(rerender), []);
useEffect(() => TerminalClearEvents.subscribe(clear), []);
function doScroll(): void { function doScroll(): void {
const hook = scrollHook.current; const hook = scrollHook.current;
@ -76,7 +80,7 @@ export function TerminalRoot({ terminal, router, player }: IProps): React.ReactE
return ( return (
<> <>
<Box width="100%" minHeight="100vh" display={"flex"} alignItems={"flex-end"}> <Box width="100%" minHeight="100vh" display={"flex"} alignItems={"flex-end"}>
<List id="terminal" classes={{ root: classes.list }}> <List key={key} id="terminal" classes={{ root: classes.list }}>
{terminal.outputHistory.map((item, i) => { {terminal.outputHistory.map((item, i) => {
if (item instanceof Output) if (item instanceof Output)
return ( return (

@ -22,8 +22,8 @@ export function ActiveScriptsRoot(props: IProps): React.ReactElement {
} }
useEffect(() => { useEffect(() => {
const id = setInterval(rerender, 20); // const id = setInterval(rerender, 20);
return () => clearInterval(id); // return () => clearInterval(id);
}, []); }, []);
return ( return (

@ -21,10 +21,17 @@ export function ServerAccordionContent(props: IProps): React.ReactElement {
setPage(0); setPage(0);
}; };
let safePage = page;
while (safePage * rowsPerPage + 1 > props.workerScripts.length) {
safePage--;
}
if (safePage != page) setPage(safePage);
return ( return (
<> <>
<List dense disablePadding> <List dense disablePadding>
{props.workerScripts.slice(page * rowsPerPage, page * rowsPerPage + rowsPerPage).map((ws) => ( {props.workerScripts.slice(safePage * rowsPerPage, safePage * rowsPerPage + rowsPerPage).map((ws) => (
<WorkerScriptAccordion key={`${ws.name}_${ws.args}`} workerScript={ws} /> <WorkerScriptAccordion key={`${ws.name}_${ws.args}`} workerScript={ws} />
))} ))}
</List> </List>
@ -33,7 +40,7 @@ export function ServerAccordionContent(props: IProps): React.ReactElement {
component="div" component="div"
count={props.workerScripts.length} count={props.workerScripts.length}
rowsPerPage={rowsPerPage} rowsPerPage={rowsPerPage}
page={page} page={safePage}
onPageChange={handleChangePage} onPageChange={handleChangePage}
onRowsPerPageChange={handleChangeRowsPerPage} onRowsPerPageChange={handleChangeRowsPerPage}
ActionsComponent={TablePaginationActionsAll} ActionsComponent={TablePaginationActionsAll}

@ -35,13 +35,6 @@ export function ServerAccordions(props: IProps): React.ReactElement {
const [page, setPage] = useState(0); const [page, setPage] = useState(0);
const [rowsPerPage, setRowsPerPage] = useState(10); const [rowsPerPage, setRowsPerPage] = useState(10);
const setRerender = useState(false)[1]; const setRerender = useState(false)[1];
function rerender(): void {
setRerender((old) => !old);
}
useEffect(() => {
return WorkerScriptStartStopEventEmitter.subscribe(rerender);
}, []);
const handleChangePage = (event: unknown, newPage: number): void => { const handleChangePage = (event: unknown, newPage: number): void => {
setPage(newPage); setPage(newPage);
@ -79,6 +72,19 @@ export function ServerAccordions(props: IProps): React.ReactElement {
const filtered = Object.values(serverToScriptMap).filter((data) => data && data.server.hostname.includes(filter)); const filtered = Object.values(serverToScriptMap).filter((data) => data && data.server.hostname.includes(filter));
function rerender(): void {
setRerender((old) => !old);
let safePage = page;
while (safePage * rowsPerPage + 1 >= filtered.length) {
safePage--;
}
if (safePage != page) setPage(safePage);
}
useEffect(() => WorkerScriptStartStopEventEmitter.subscribe(rerender));
return ( return (
<> <>
<TextField <TextField