Clearing the playground log output currently requires clicking the Clear button or pressing Meta+Backspace.
When prototyping it's a fairly common practice to include console.clear() in one's code to automatically clear the console on every rerun. Sadly this has no effect in the playground because the iframe only forwards standard console levels (log, warn, etc).
Proposed solution
- In the iframe context, override
console.clear to instead send a message of type "clear" to the parent context:
|
// Forward logs to parent window |
|
['log', 'info', 'debug', 'warn', 'error'].forEach((level) => { |
|
const original = console[level]; |
|
console[level] = (...args) => { |
|
parent.postMessage({ type: 'log', log: [level, stringify(args)] }, '*'); |
|
original(...args); |
|
}; |
|
}); |
- When receiving a message of type
"clear" in the parent context, invoke clearLogs():
|
const captureLogs = $((event: MessageEvent<MessageEventData>) => { |
|
if (event.data.type === 'log') { |
|
logs.value = [...logs.value, event.data.log]; |
|
} |
|
}); |
Clearing the playground log output currently requires clicking the Clear button or pressing Meta+Backspace.
When prototyping it's a fairly common practice to include
console.clear()in one's code to automatically clear the console on every rerun. Sadly this has no effect in the playground because the iframe only forwards standard console levels (log,warn, etc).Proposed solution
console.clearto instead send a message of type"clear"to the parent context:valibot/website/src/routes/playground/iframeCode.js
Lines 129 to 136 in e789528
"clear"in the parent context, invokeclearLogs():valibot/website/src/routes/playground/index.tsx
Lines 170 to 174 in e789528