Skip to content

Commit ba462e2

Browse files
committed
add option to clear the console log
1 parent edceb42 commit ba462e2

File tree

6 files changed

+81
-15
lines changed

6 files changed

+81
-15
lines changed

packages/sandcastle/public/Sandcastle-client.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,17 @@
1515
return "undefined";
1616
}
1717

18+
const originalClear = console.clear;
19+
console.clear = function () {
20+
originalClear();
21+
window.parent.postMessage(
22+
{
23+
type: "consoleClear",
24+
},
25+
"*",
26+
);
27+
};
28+
1829
const originalLog = console.log;
1930
console.log = function (d1) {
2031
originalLog.apply(console, arguments);

packages/sandcastle/src/App.tsx

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ export type SandcastleAction =
196196
function App() {
197197
const { settings, updateSettings } = useContext(SettingsContext);
198198
const rightSideRef = useRef<RightSideRef>(null);
199-
const consoleCollapsedHeight = 26;
199+
const consoleCollapsedHeight = 33;
200200
const [consoleExpanded, setConsoleExpanded] = useState(false);
201201

202202
const isStartingWithCode = !!(window.location.search || window.location.hash);
@@ -319,14 +319,27 @@ function App() {
319319
[consoleExpanded],
320320
);
321321

322-
const resetConsole = useCallback(() => {
323-
if (codeState.runNumber > 0) {
324-
// the console should only be cleared by the Bucket when the viewer page
325-
// has actually reloaded and stopped sending console statements
326-
// otherwise some could bleed into the "next run"
327-
setConsoleMessages([]);
328-
}
329-
}, [codeState.runNumber]);
322+
const resetConsole = useCallback(
323+
({ showMessage = false } = {}) => {
324+
if (codeState.runNumber > 0) {
325+
// the console should only be cleared by the Bucket when the viewer page
326+
// has actually reloaded and stopped sending console statements
327+
// otherwise some could bleed into the "next run"
328+
if (showMessage) {
329+
setConsoleMessages([
330+
{
331+
id: crypto.randomUUID(),
332+
type: "special",
333+
message: "Console was cleared",
334+
},
335+
]);
336+
} else {
337+
setConsoleMessages([]);
338+
}
339+
}
340+
},
341+
[codeState.runNumber],
342+
);
330343

331344
function runSandcastle() {
332345
dispatch({ type: "runSandcastle" });
@@ -634,6 +647,7 @@ function App() {
634647
logs={consoleMessages}
635648
expanded={consoleExpanded}
636649
toggleExpanded={() => rightSideRef.current?.toggleExpanded()}
650+
resetConsole={resetConsole}
637651
/>
638652
</Allotment.Pane>
639653
</RightSideAllotment>

packages/sandcastle/src/Bucket.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { ConsoleMessageType } from "./ConsoleMirror";
55

66
type SandcastleMessage =
77
| { type: "reload" }
8+
| { type: "consoleClear" }
89
| { type: "consoleLog"; log: string }
910
| { type: "consoleWarn"; warn: string }
1011
| { type: "consoleError"; error: string; lineNumber?: number; url?: string }
@@ -30,7 +31,7 @@ function Bucket({
3031
*/
3132
highlightLine: (lineNumber: number) => void;
3233
appendConsole: (type: ConsoleMessageType, message: string) => void;
33-
resetConsole: () => void;
34+
resetConsole: (options?: { showMessage?: boolean | undefined }) => void;
3435
}) {
3536
const bucket = useRef<HTMLIFrameElement>(null);
3637
const lastRunNumber = useRef<number>(Number.NEGATIVE_INFINITY);
@@ -172,6 +173,8 @@ function Bucket({
172173
// into the iframe, causing the demo to run there.
173174
activateBucketScripts(bucket.current, code, html);
174175
}
176+
} else if (e.data.type === "consoleClear") {
177+
resetConsole({ showMessage: true });
175178
} else if (e.data.type === "consoleLog") {
176179
// Console log messages from the iframe display in Sandcastle.
177180
appendConsole("log", e.data.log);

packages/sandcastle/src/ConsoleMirror.css

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,21 @@
1414
var(--stratakit-color-border-neutral-muted);
1515

1616
.title {
17-
/* Match the height of the badge to avoid flow shifts when adding them */
18-
height: 20px;
17+
/* Match the height of buttons to avoid flow shifts when adding them */
18+
height: 24px;
1919
align-content: center;
2020
margin-right: var(--stratakit-space-x1);
2121
}
22+
23+
.spacer {
24+
flex-grow: 100;
25+
}
26+
}
27+
28+
&:not(.expanded) .clear-button {
29+
display: none;
2230
}
31+
2332
.logs {
2433
overflow: auto;
2534
padding: 0 var(--stratakit-space-x2);
@@ -39,6 +48,7 @@
3948
/* "Filler" icon to take up the same space */
4049
width: 1rem;
4150
height: 1rem;
51+
flex-shrink: 0;
4252
}
4353
.message-index {
4454
user-select: none;
@@ -56,5 +66,9 @@
5666
.message.error {
5767
color: var(--stratakit-color-text-critical-base);
5868
}
69+
.message.special {
70+
font-style: italic;
71+
color: var(--stratakit-color-text-neutral-tertiary);
72+
}
5973
}
6074
}

packages/sandcastle/src/ConsoleMirror.tsx

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,17 @@ import classNames from "classnames";
22
import "./ConsoleMirror.css";
33
import { useLayoutEffect, useRef } from "react";
44
import useStayScrolled from "react-stay-scrolled";
5-
import { Badge } from "@stratakit/bricks";
6-
import { caretDown, caretUp, statusError, statusWarning } from "./icons";
5+
import { Badge, Button } from "@stratakit/bricks";
6+
import {
7+
caretDown,
8+
caretUp,
9+
deleteIcon,
10+
statusError,
11+
statusWarning,
12+
} from "./icons";
713
import { Icon } from "@stratakit/foundations";
814

9-
export type ConsoleMessageType = "log" | "warn" | "error";
15+
export type ConsoleMessageType = "log" | "warn" | "error" | "special";
1016
export type ConsoleMessage = {
1117
type: ConsoleMessageType;
1218
message: string;
@@ -27,10 +33,12 @@ export function ConsoleMirror({
2733
logs,
2834
expanded: consoleExpanded,
2935
toggleExpanded,
36+
resetConsole,
3037
}: {
3138
logs: ConsoleMessage[];
3239
expanded: boolean;
3340
toggleExpanded: () => void;
41+
resetConsole: (options?: { showMessage?: boolean }) => void;
3442
}) {
3543
const logsRef = useRef<HTMLDivElement>(document.createElement("div"));
3644
// TODO: determine if we need this lib or can implement ourselves. It's a little outdated
@@ -64,6 +72,19 @@ export function ConsoleMirror({
6472
{errors.length > 0 && (
6573
<Badge label={errors.length} tone="critical" variant="muted" />
6674
)}
75+
<div className="spacer"></div>
76+
<Button
77+
className="clear-button"
78+
variant="ghost"
79+
onClick={(e) => {
80+
e.preventDefault();
81+
e.stopPropagation();
82+
resetConsole({ showMessage: true });
83+
}}
84+
>
85+
<Icon href={deleteIcon} />
86+
Clear console
87+
</Button>
6788
</div>
6889
<div className="logs" ref={logsRef}>
6990
{logs.length === 0 && (
@@ -79,6 +100,7 @@ export function ConsoleMirror({
79100
className={classNames("message", {
80101
warning: log.type === "warn",
81102
error: log.type === "error",
103+
special: log.type === "special",
82104
})}
83105
>
84106
<ConsoleIcon type={log.type} />

packages/sandcastle/src/icons.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import documentation from "@stratakit/icons/documentation.svg";
2121
import info from "@stratakit/icons/info.svg";
2222
import copy from "@stratakit/icons/copy.svg";
2323
import retry from "@stratakit/icons/retry.svg";
24+
import deleteIcon from "@stratakit/icons/delete.svg";
2425

2526
export {
2627
add,
@@ -46,4 +47,5 @@ export {
4647
info,
4748
copy,
4849
retry,
50+
deleteIcon,
4951
};

0 commit comments

Comments
 (0)