Skip to content

Support console.clear in playground #1578

Description

@mootari

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

  1. 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);
    };
    });
  2. 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];
    }
    });

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions