Skip to content

Commit 2cdf6c0

Browse files
committed
feat: edit-only mode
1 parent b2998c3 commit 2cdf6c0

File tree

3 files changed

+55
-8
lines changed

3 files changed

+55
-8
lines changed

src/snippet.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,15 @@ class CodapiSnippet extends HTMLElement {
6161
// init initializes the component state.
6262
init() {
6363
const filesStr = this.getAttribute("files");
64-
this.executor = new Executor({
65-
engine: this.getAttribute("engine"),
66-
sandbox: this.getAttribute("sandbox"),
67-
command: this.getAttribute("command"),
68-
template: this.getAttribute("template"),
69-
files: filesStr ? filesStr.split(" ") : null,
70-
});
64+
this.executor = this.hasAttribute("sandbox")
65+
? new Executor({
66+
engine: this.getAttribute("engine"),
67+
sandbox: this.getAttribute("sandbox"),
68+
command: this.getAttribute("command"),
69+
template: this.getAttribute("template"),
70+
files: filesStr ? filesStr.split(" ") : null,
71+
})
72+
: null;
7173
this.dependsOn = this.getAttribute("depends-on");
7274
this.state = State.unknown;
7375
}
@@ -80,6 +82,7 @@ class CodapiSnippet extends HTMLElement {
8082

8183
this._toolbar = this.querySelector("codapi-toolbar");
8284
const actions = this.getAttribute("actions");
85+
this._toolbar.runnable = this.executor != null;
8386
this._toolbar.addActions(actions ? actions.split(" ") : null);
8487

8588
const status = this._toolbar.querySelector("codapi-status");
@@ -197,6 +200,9 @@ class CodapiSnippet extends HTMLElement {
197200

198201
// execute runs the code.
199202
async execute(command = undefined) {
203+
if (!this.executor) {
204+
return;
205+
}
200206
if (this.snippet.isEmpty) {
201207
this._output.showMessage("(empty)");
202208
return;

src/toolbar.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Action } from "./action.js";
44

55
const template = document.createElement("template");
66
template.innerHTML = `
7-
<button>Run</button>
7+
<button hidden>Run</button>
88
<a href="#edit" hidden>Edit</a>
99
<codapi-status></codapi-status>
1010
`;
@@ -87,6 +87,18 @@ class CodapiToolbar extends HTMLElement {
8787
this.status.showMessage(message);
8888
}
8989

90+
get runnable() {
91+
return !this.run.hasAttribute("hidden");
92+
}
93+
94+
set runnable(value) {
95+
if (value) {
96+
this.run.removeAttribute("hidden");
97+
} else {
98+
this.run.setAttribute("hidden", "");
99+
}
100+
}
101+
90102
get editable() {
91103
return !this.edit.hasAttribute("hidden");
92104
}

tests/snippet.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ async function runTests() {
1515
await testAttachSelector();
1616
await testAttachSelectorPrev();
1717

18+
await testRunnable();
19+
await testNotRunnable();
20+
1821
await testEditorOff();
1922
await testEditorBasic();
2023
await testEditorExternal();
@@ -271,6 +274,32 @@ async function testRunError() {
271274
});
272275
}
273276

277+
async function testRunnable() {
278+
return new Promise((resolve, reject) => {
279+
t.log("testRunnable...");
280+
const ui = createSnippet(`
281+
<pre><code>print("hello")</code></pre>
282+
<codapi-snippet sandbox="python"></codapi-snippet>
283+
`);
284+
t.assert("runnable", ui.toolbar.runnable);
285+
t.assert("run button", !ui.toolbar.run.hasAttribute("hidden"));
286+
resolve();
287+
});
288+
}
289+
290+
async function testNotRunnable() {
291+
return new Promise((resolve, reject) => {
292+
t.log("testNotRunnable...");
293+
const ui = createSnippet(`
294+
<pre><code>print("hello")</code></pre>
295+
<codapi-snippet></codapi-snippet>
296+
`);
297+
t.assert("runnable", !ui.toolbar.runnable);
298+
t.assert("run button", ui.toolbar.run.hasAttribute("hidden"));
299+
resolve();
300+
});
301+
}
302+
274303
async function testEditAndRun() {
275304
return new Promise((resolve, reject) => {
276305
t.log("testEditAndRun...");

0 commit comments

Comments
 (0)