Skip to content

Commit

Permalink
impr: sample output support
Browse files Browse the repository at this point in the history
  • Loading branch information
nalgeon committed Oct 21, 2023
1 parent d0779bc commit 14b1ad1
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/snippet.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,15 @@ class CodapiSnippet extends HTMLElement {
this.editor,
this.execute.bind(this)
);

this.toolbar = this.querySelector("codapi-toolbar");
const actions = this.getAttribute("actions");
this.toolbar.addActions(actions ? actions.split(" ") : null);

this.output = this.querySelector("codapi-output");
if (this.hasAttribute("output")) {
this.showSampleOutput(this.getAttribute("output"));
}
}

// listen allows running and editing the code.
Expand Down Expand Up @@ -156,6 +161,21 @@ class CodapiSnippet extends HTMLElement {
}
}

// showSampleOutput shows the predefined output for the snippet
// (if any, according to the `output` attribute).
showSampleOutput(selector) {
let sample =
selector == ""
? this.nextElementSibling
: document.querySelector(selector);
if (!sample) {
return;
}
sample = sample.querySelector("code") || sample;
this.output.showMessage(sample.innerHTML);
sample.parentElement.removeChild(sample);
}

// showStatus shows a custom message in the status bar.
showStatus(message) {
this.toolbar.showStatus(message);
Expand Down

0 comments on commit 14b1ad1

Please sign in to comment.