Skip to content

Commit

Permalink
Pull out clearing innerHTML
Browse files Browse the repository at this point in the history
  • Loading branch information
WardBrian committed Jul 26, 2024
1 parent 8779e91 commit afeec3c
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 46 deletions.
12 changes: 5 additions & 7 deletions gui/src/app/Scripting/Analysis/AnalysisPyWindow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import { StanRun } from "@SpStanSampler/useStanSampler";
import { FileNames } from "@SpCore/FileMapping";
import { ProjectKnownFiles } from "@SpCore/ProjectDataModel";
import useTemplatedFillerText from "@SpScripting/useTemplatedFillerText";
import { writeConsoleOutToDiv } from "@SpScripting/ScriptEditor";
import {
clearOutputDivs,
writeConsoleOutToDiv,
} from "@SpScripting/OutputDivUtils";
import usePyodideWorker from "@SpScripting/pyodide/usePyodideWorker";
import PlottingScriptEditor from "@SpScripting/PlottingScriptEditor";
import useAnalysisState from "./useAnalysisState";
Expand Down Expand Up @@ -57,12 +60,7 @@ const AnalysisPyWindow: FunctionComponent<AnalysisWindowProps> = ({

const handleRun = useCallback(
(code: string) => {
if (consoleRef.current) {
consoleRef.current.innerHTML = "";
}
if (imagesRef.current) {
imagesRef.current.innerHTML = "";
}
clearOutputDivs(consoleRef, imagesRef);
run(code, spData, {
loadsDraws: true,
showsPlots: true,
Expand Down
8 changes: 2 additions & 6 deletions gui/src/app/Scripting/Analysis/AnalysisRWindow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { ProjectKnownFiles } from "@SpCore/ProjectDataModel";
import PlottingScriptEditor from "@SpScripting/PlottingScriptEditor";
import runR from "@SpScripting/webR/runR";
import useTemplatedFillerText from "@SpScripting/useTemplatedFillerText";
import { clearOutputDivs } from "@SpScripting/OutputDivUtils";
import loadDrawsCode from "@SpScripting/webR/sp_load_draws.R?raw";
import useAnalysisState from "./useAnalysisState";

Expand All @@ -29,12 +30,7 @@ const AnalysisRWindow: FunctionComponent<AnalysisWindowProps> = ({

const handleRun = useCallback(
async (userCode: string) => {
if (consoleRef.current) {
consoleRef.current.innerHTML = "";
}
if (imagesRef.current) {
imagesRef.current.innerHTML = "";
}
clearOutputDivs(consoleRef, imagesRef);
const code = loadDrawsCode + userCode;
await runR({
code,
Expand Down
8 changes: 2 additions & 6 deletions gui/src/app/Scripting/Analysis/useAnalysisState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
InterpreterStatus,
isInterpreterBusy,
} from "@SpScripting/InterpreterTypes";
import { clearOutputDivs } from "@SpScripting/OutputDivUtils";
import { StanRun } from "@SpStanSampler/useStanSampler";
import { useEffect, useMemo, useRef, useState } from "react";

Expand All @@ -19,12 +20,7 @@ const useAnalysisState = (latestRun: StanRun) => {
const imagesRef = useRef<HTMLDivElement | null>(null);

useEffect(() => {
if (imagesRef.current) {
imagesRef.current.innerHTML = "";
}
if (consoleRef.current) {
consoleRef.current.innerHTML = "";
}
clearOutputDivs(consoleRef, imagesRef);
}, [latestRun.draws]);

const { draws, paramNames, samplingOpts, status: samplerStatus } = latestRun;
Expand Down
10 changes: 6 additions & 4 deletions gui/src/app/Scripting/DataGeneration/DataPyWindow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import { FunctionComponent, useCallback, useMemo } from "react";
import { FileNames } from "@SpCore/FileMapping";
import { ProjectKnownFiles } from "@SpCore/ProjectDataModel";
import useTemplatedFillerText from "@SpScripting/useTemplatedFillerText";
import ScriptEditor, { writeConsoleOutToDiv } from "@SpScripting/ScriptEditor";
import ScriptEditor from "@SpScripting/ScriptEditor";
import {
clearOutputDivs,
writeConsoleOutToDiv,
} from "@SpScripting/OutputDivUtils";
import usePyodideWorker from "@SpScripting/pyodide/usePyodideWorker";
import useDataGenState from "./useDataGenState";

Expand Down Expand Up @@ -34,9 +38,7 @@ const DataPyWindow: FunctionComponent<Props> = () => {

const handleRun = useCallback(
(code: string) => {
if (consoleRef.current) {
consoleRef.current.innerHTML = "";
}
clearOutputDivs(consoleRef);
run(
code,
{},
Expand Down
5 changes: 2 additions & 3 deletions gui/src/app/Scripting/DataGeneration/DataRWindow.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { FunctionComponent, useCallback } from "react";
import ScriptEditor from "@SpScripting/ScriptEditor";
import { clearOutputDivs } from "@SpScripting/OutputDivUtils";
import { FileNames } from "@SpCore/FileMapping";
import { ProjectKnownFiles } from "@SpCore/ProjectDataModel";
import runR from "@SpScripting/webR/runR";
Expand All @@ -22,9 +23,7 @@ const DataRWindow: FunctionComponent<Props> = () => {

const handleRun = useCallback(
async (code: string) => {
if (consoleRef.current) {
consoleRef.current.innerHTML = "";
}
clearOutputDivs(consoleRef);
await runR({ code, consoleRef, onStatus, onData });
},
[consoleRef, onData, onStatus],
Expand Down
2 changes: 1 addition & 1 deletion gui/src/app/Scripting/DataGeneration/useDataGenState.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useCallback, useContext, useRef, useState } from "react";
import { ProjectKnownFiles } from "@SpCore/ProjectDataModel";
import { writeConsoleOutToDiv } from "@SpScripting/ScriptEditor";
import { writeConsoleOutToDiv } from "@SpScripting/OutputDivUtils";
import { InterpreterStatus } from "@SpScripting/InterpreterTypes";
import { ProjectContext } from "@SpCore/ProjectContextProvider";

Expand Down
25 changes: 25 additions & 0 deletions gui/src/app/Scripting/OutputDivUtils.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { RefObject } from "react";

type ConsoleOutType = "stdout" | "stderr";

export const writeConsoleOutToDiv = (
parentDiv: RefObject<HTMLDivElement>,
x: string,
type: ConsoleOutType,
) => {
if (x === "") return;
if (!parentDiv.current) return;
const styleClass = type === "stdout" ? "WorkerStdout" : "WorkerStderr";
const preElement = document.createElement("pre");
preElement.textContent = x;
const divElement = document.createElement("div");
divElement.className = styleClass;
divElement.appendChild(preElement);
parentDiv.current.appendChild(divElement);
};

export const clearOutputDivs = (...parentDiv: RefObject<HTMLDivElement>[]) => {
for (const div of parentDiv) {
if (div.current) div.current.innerHTML = "";
}
};
18 changes: 0 additions & 18 deletions gui/src/app/Scripting/ScriptEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -184,22 +184,4 @@ const ConsoleOutputWindow: FunctionComponent<ConsoleOutputWindowProps> = ({
return <div className="ConsoleOutputArea" ref={consoleRef} />;
};

type ConsoleOutType = "stdout" | "stderr";

export const writeConsoleOutToDiv = (
parentDiv: RefObject<HTMLDivElement>,
x: string,
type: ConsoleOutType,
) => {
if (x === "") return;
if (!parentDiv.current) return;
const styleClass = type === "stdout" ? "WorkerStdout" : "WorkerStderr";
const preElement = document.createElement("pre");
preElement.textContent = x;
const divElement = document.createElement("div");
divElement.className = styleClass;
divElement.appendChild(preElement);
parentDiv.current.appendChild(divElement);
};

export default ScriptEditor;
2 changes: 1 addition & 1 deletion gui/src/app/Scripting/webR/runR.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { RefObject } from "react";
import { RString, WebR } from "webr";
import { InterpreterStatus } from "@SpScripting/InterpreterTypes";
import { writeConsoleOutToDiv } from "@SpScripting/ScriptEditor";
import { writeConsoleOutToDiv } from "@SpScripting/OutputDivUtils";

let webR: WebR | null = null;
export const loadWebRInstance = async (
Expand Down

0 comments on commit afeec3c

Please sign in to comment.