Skip to content

Commit b41cb98

Browse files
author
Ke, Mingze
committed
Auto detect and open corresponding demo area
1 parent 042b2fc commit b41cb98

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

code.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1227,6 +1227,20 @@ Code.exportImage = function () {
12271227
saveSvgAsPng(Code.workspace.getCanvas(), 'webduino-blocks.png');
12281228
};
12291229

1230+
Code.getBlockDemo = function (type) {
1231+
var toolbox = Code.rawToolbox;
1232+
var block = toolbox.querySelector('block[type=' + type + ']');
1233+
var attr;
1234+
if (block) {
1235+
while (block = block.parentNode) {
1236+
if (block.tagName === 'CATEGORY' && (attr = block.getAttribute('demo'))) {
1237+
return attr;
1238+
}
1239+
}
1240+
}
1241+
return null;
1242+
};
1243+
12301244
Blockly.JavaScript['procedures_defnoreturn'] = function (block) {
12311245
// Define a procedure with a return value.
12321246
var funcName = Blockly.JavaScript.variableDB_.getName(
@@ -1301,6 +1315,24 @@ Blockly.JavaScript['workspaceToCode'] = function (workspace) {
13011315
return code;
13021316
};
13031317

1318+
Blockly.Xml._domToWorkspace = Blockly.Xml.domToWorkspace;
1319+
1320+
Blockly.Xml.domToWorkspace = function () {
1321+
Blockly.Xml._domToWorkspace.apply(this, arguments);
1322+
var xml = Blockly.Xml.workspaceToDom(Code.workspace);
1323+
var blocks = slice.call(xml.querySelectorAll('block'));
1324+
for (var i = 0; i < blocks.length; i++) {
1325+
var type = blocks[i].getAttribute('type');
1326+
var demo = Code.getBlockDemo(type);
1327+
if (demo) {
1328+
if (demo !== Code.queryString.get('demo')) {
1329+
Code.queryString.set('demo', demo, true);
1330+
}
1331+
return;
1332+
}
1333+
}
1334+
};
1335+
13041336
Blockly.JavaScript.scrub_ = function (block, code) {
13051337
var commentCode = '';
13061338
// Only collect comments for blocks that aren't inline.

0 commit comments

Comments
 (0)