Skip to content

Commit

Permalink
feat(interactive-examples): get ready for JS migration (#12570)
Browse files Browse the repository at this point in the history
* correct heights
* add name attribute
* use old string quoting logic
* log non-structured-cloneable objects better
  • Loading branch information
LeoMcA authored Feb 11, 2025
1 parent 79f6f72 commit 33de34d
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 34 deletions.
6 changes: 3 additions & 3 deletions client/src/lit/interactive-example.global.scss
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
interactive-example {
display: block;
height: 513px;
height: 444px;
margin: 1rem 0;

&[height="shorter"] {
height: 433px;
height: 364px;
}

&[height="taller"] {
height: 725px;
height: 654px;
}
}
11 changes: 10 additions & 1 deletion client/src/lit/interactive-example.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,17 @@ import styles from "./interactive-example.scss?css" with { type: "css" };
*/

export class InteractiveExample extends GleanMixin(LitElement) {
static properties = {
name: { type: String },
};

static styles = styles;

constructor() {
super();
this.name = "";
}

/** @type {Ref<PlayController>} */
_controller = createRef();

Expand Down Expand Up @@ -71,7 +80,7 @@ export class InteractiveExample extends GleanMixin(LitElement) {
return html`
<play-controller ${ref(this._controller)}>
<div class="template-javascript">
<h4>JavaScript Demo:</h4>
<h4>${this.name}</h4>
<play-editor id="editor" language="javascript"></play-editor>
<div class="buttons">
<button id="execute" @click=${this._run}>Run</button>
Expand Down
3 changes: 2 additions & 1 deletion client/src/lit/interactive-example.scss
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ play-console {
"editor editor"
"buttons console";
grid-template-columns: max-content 1fr;
grid-template-rows: max-content 1fr;
grid-template-rows: max-content 1fr 8rem;
height: 100%;

play-runner {
Expand All @@ -63,6 +63,7 @@ play-console {
"buttons"
"console";
grid-template-columns: 1fr;
grid-template-rows: max-content 1fr max-content 8rem;

.buttons {
flex-direction: row;
Expand Down
3 changes: 3 additions & 0 deletions client/src/lit/play/console-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ export function formatObject(input) {
}

if (objectName === "Object") {
if (input?._MDNPlaySerializedObject) {
return input._MDNPlaySerializedObject;
}
let formattedChild = "";
let start = true;
for (const key in input) {
Expand Down
5 changes: 1 addition & 4 deletions client/src/lit/play/console.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,7 @@ class VirtualConsole {
}
this.#host._messages = [
...this.#host._messages,
(args.every((x) => typeof x === "string")
? args
: args.map((x) => formatOutput(x))
).join(" "),
args.map((x) => formatOutput(x)).join(" "),
];
}

Expand Down
2 changes: 0 additions & 2 deletions client/src/lit/play/console.scss
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
display: flex;
flex-direction: column;
font-size: 0.875rem;
height: 6rem;
margin: 0;
max-height: 6rem;
overflow: auto;
width: 100%;
}
Expand Down
44 changes: 21 additions & 23 deletions libs/play/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,37 +248,35 @@ export function renderHtml(state = null) {
{
typ: "console",
prop,
args: args.map((x) => JSON.parse(JSON.stringify(x))),
args: args.map((x) => {
try {
window.structuredClone(x);
return x;
} catch {
return {
_MDNPlaySerializedObject: x.toString(),
};
}
}),
},
"*"
);
} catch {
try {
window.parent.postMessage(
{
typ: "console",
prop,
args: args.map((x) => x.toString()),
},
"*"
);
} catch {
window.parent.postMessage(
{
typ: "console",
prop: "warn",
args: [
"[Playground] Unsupported console message (see browser console)",
],
},
"*"
);
}
window.parent.postMessage(
{
typ: "console",
prop: "warn",
args: [
"[Playground] Unsupported console message (see browser console)",
],
},
"*"
);
}
}
target[prop](...args);
};
};
}
return target[prop];
},
});
Expand Down

0 comments on commit 33de34d

Please sign in to comment.