Support multiple Blockly instances.

This commit is contained in:
Neil Fraser
2015-04-28 13:51:25 -07:00
parent 67140282a0
commit 17961f7f58
43 changed files with 1412 additions and 1403 deletions

View File

@@ -80,6 +80,12 @@ Code.LANGUAGE_NAME = {
*/
Code.LANGUAGE_RTL = ['ar', 'fa', 'he'];
/**
* Blockly's main workspace.
* @type Blockly.WorkspaceSvg
*/
Code.workspace = null;
/**
* Extracts a parameter from the URL.
* If the parameter is absent default_value is returned.
@@ -132,11 +138,11 @@ Code.loadBlocks = function(defaultXml) {
// Language switching stores the blocks during the reload.
delete window.sessionStorage.loadOnceBlocks;
var xml = Blockly.Xml.textToDom(loadOnce);
Blockly.Xml.domToWorkspace(Blockly.mainWorkspace, xml);
Blockly.Xml.domToWorkspace(Code.workspace, xml);
} else if (defaultXml) {
// Load the editor with default starting blocks.
var xml = Blockly.Xml.textToDom(defaultXml);
Blockly.Xml.domToWorkspace(Blockly.mainWorkspace, xml);
Blockly.Xml.domToWorkspace(Code.workspace, xml);
} else if ('BlocklyStorage' in window) {
// Restore saved blocks in a separate thread so that subsequent
// initialization is not affected from a failed load.
@@ -153,7 +159,7 @@ Code.changeLanguage = function() {
// not load Blockly.
// MSIE 11 does not support sessionStorage on file:// URLs.
if (typeof Blockly != 'undefined' && window.sessionStorage) {
var xml = Blockly.Xml.workspaceToDom(Blockly.mainWorkspace);
var xml = Blockly.Xml.workspaceToDom(Code.workspace);
var text = Blockly.Xml.domToText(xml);
window.sessionStorage.loadOnceBlocks = text;
}
@@ -262,8 +268,8 @@ Code.tabClick = function(clickedName) {
}
}
if (xmlDom) {
Blockly.mainWorkspace.clear();
Blockly.Xml.domToWorkspace(Blockly.mainWorkspace, xmlDom);
Code.workspace.clear();
Blockly.Xml.domToWorkspace(Code.workspace, xmlDom);
}
}
@@ -292,12 +298,12 @@ Code.renderContent = function() {
// Initialize the pane.
if (content.id == 'content_xml') {
var xmlTextarea = document.getElementById('content_xml');
var xmlDom = Blockly.Xml.workspaceToDom(Blockly.mainWorkspace);
var xmlDom = Blockly.Xml.workspaceToDom(Code.workspace);
var xmlText = Blockly.Xml.domToPrettyText(xmlDom);
xmlTextarea.value = xmlText;
xmlTextarea.focus();
} else if (content.id == 'content_javascript') {
var code = Blockly.JavaScript.workspaceToCode();
var code = Blockly.JavaScript.workspaceToCode(Code.workspace);
content.textContent = code;
if (typeof prettyPrintOne == 'function') {
code = content.innerHTML;
@@ -305,7 +311,7 @@ Code.renderContent = function() {
content.innerHTML = code;
}
} else if (content.id == 'content_python') {
code = Blockly.Python.workspaceToCode();
code = Blockly.Python.workspaceToCode(Code.workspace);
content.textContent = code;
if (typeof prettyPrintOne == 'function') {
code = content.innerHTML;
@@ -313,7 +319,7 @@ Code.renderContent = function() {
content.innerHTML = code;
}
} else if (content.id == 'content_dart') {
code = Blockly.Dart.workspaceToCode();
code = Blockly.Dart.workspaceToCode(Code.workspace);
content.textContent = code;
if (typeof prettyPrintOne == 'function') {
code = content.innerHTML;
@@ -357,16 +363,16 @@ Code.init = function() {
el.style.width = (2 * bBox.width - el.offsetWidth) + 'px';
}
// Make the 'Blocks' tab line up with the toolbox.
if (Blockly.mainWorkspace.toolbox_.width) {
if (Code.workspace.toolbox_.width) {
document.getElementById('tab_blocks').style.minWidth =
(Blockly.mainWorkspace.toolbox_.width - 38) + 'px';
(Code.workspace.toolbox_.width - 38) + 'px';
// Account for the 19 pixel margin and on each side.
}
};
window.addEventListener('resize', onresize, false);
var toolbox = document.getElementById('toolbox');
Blockly.inject(document.getElementById('content_blocks'),
Code.workspace = Blockly.inject(document.getElementById('content_blocks'),
{grid:
{spacing: 25,
length: 3,
@@ -490,10 +496,10 @@ Code.runJS = function() {
* Discard all blocks from the workspace.
*/
Code.discard = function() {
var count = Blockly.mainWorkspace.getAllBlocks().length;
var count = Code.workspace.getAllBlocks().length;
if (count < 2 ||
window.confirm(MSG['discard'].replace('%1', count))) {
Blockly.mainWorkspace.clear();
Code.workspace.clear();
window.location.hash = '';
}
};