Skip to content

Commit 1758cb8

Browse files
feat: add a scenario selector (#39)
1 parent a3c3646 commit 1758cb8

File tree

3 files changed

+80
-5
lines changed

3 files changed

+80
-5
lines changed

test/index.html

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@
5959
margin-top: 10px;
6060
}
6161

62+
#scenarioForm {
63+
padding-top: 7px;
64+
}
65+
6266
#announceWrapper {
6367
overflow-y: scroll;
6468
height: 400px;
@@ -80,6 +84,18 @@
8084
<button id="run">Run Code!</button>
8185
<div id="output">
8286
<div id="p5output"></div>
87+
<div id="scenarioForm">
88+
<form id="options">
89+
<label for="scenarioSelect">Scenario:</label>
90+
<select
91+
name="scenario"
92+
id="scenarioSelect"
93+
onchange="document.forms.options.submit()">
94+
<option value="sun">sun</option>
95+
<option value="blank">blank canvas</option>
96+
</select>
97+
</form>
98+
</div>
8399
<h3 id="description"></h3>
84100
<p>
85101
This page is an active site for development of keyboard navigation

test/index.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,24 @@ import {javascriptGenerator} from 'blockly/javascript';
1919
import {load} from './loadTestBlocks';
2020
import {runCode, registerRunCodeShortcut} from './runCode';
2121

22+
/**
23+
* Load initial workspace state based on the value in the scenario dropdown.
24+
*
25+
* @param workspace The workspace to load blocks into.
26+
*/
27+
function loadScenario(workspace: Blockly.WorkspaceSvg) {
28+
const scenarioSelector = location.search.match(/scenario=([^&]+)/);
29+
// Default to the sunny day example.
30+
const scenarioString = scenarioSelector ? scenarioSelector[1] : 'sun';
31+
const selector = document.getElementById(
32+
'scenarioSelect',
33+
) as HTMLSelectElement;
34+
selector.value = scenarioString;
35+
36+
// Load the initial state from storage and run the code.
37+
load(workspace, scenarioString);
38+
}
39+
2240
/**
2341
* Create the workspace, including installing keyboard navigation and
2442
* change listeners.
@@ -39,8 +57,7 @@ function createWorkspace(): Blockly.WorkspaceSvg {
3957
// Disable blocks that aren't inside the setup or draw loops.
4058
workspace.addChangeListener(Blockly.Events.disableOrphans);
4159

42-
// Load the initial state from storage and run the code.
43-
load(workspace);
60+
loadScenario(workspace);
4461
runCode();
4562

4663
return workspace;

test/loadTestBlocks.js

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import * as Blockly from 'blockly/core';
88

9-
const defaultData = {
9+
const sunnyDay = {
1010
'blocks': {
1111
'languageVersion': 0,
1212
'blocks': [
@@ -140,12 +140,54 @@ const defaultData = {
140140
},
141141
};
142142

143+
const blankCanvas = {
144+
'blocks': {
145+
'languageVersion': 0,
146+
'blocks': [
147+
{
148+
'type': 'p5_setup',
149+
'id': '5.{;T}3Qv}Awi:1M$:ut',
150+
'x': 0,
151+
'y': 75,
152+
'deletable': false,
153+
'inputs': {
154+
'STATEMENTS': {
155+
'block': {
156+
'type': 'p5_canvas',
157+
'id': 'spya_H-5F=K8+DhedX$y',
158+
'deletable': false,
159+
'movable': false,
160+
'fields': {
161+
'WIDTH': 400,
162+
'HEIGHT': 400,
163+
},
164+
},
165+
},
166+
},
167+
},
168+
{
169+
'type': 'p5_draw',
170+
'id': '3iI4f%2#Gmk}=OjI7(8h',
171+
'x': 0,
172+
'y': 332,
173+
'deletable': false,
174+
},
175+
],
176+
},
177+
};
178+
143179
/**
144180
* Loads saved state from local storage into the given workspace.
145181
* @param {Blockly.Workspace} workspace Blockly workspace to load into.
182+
* @param {string} scenarioString Which scenario to load.
146183
*/
147-
export const load = function (workspace) {
148-
const data = JSON.stringify(defaultData);
184+
export const load = function (workspace, scenarioString) {
185+
const scenarioMap = {
186+
'blank': blankCanvas,
187+
'sun': sunnyDay,
188+
};
189+
190+
const data = JSON.stringify(scenarioMap[scenarioString]);
149191
// Don't emit events during loading.
150192
Blockly.Events.disable();
151193
Blockly.serialization.workspaces.load(JSON.parse(data), workspace, false);

0 commit comments

Comments
 (0)