Commit aed4d8f5 authored by Neil Fraser's avatar Neil Fraser

Add optional data property to blocks and XML.

parent 1e88c35a
......@@ -1620,15 +1620,15 @@ Blockly.Trashcan.prototype.animateLid_=function(){this.lidOpen_+=this.isOpen?.2:
// Copyright 2012 Google Inc. Apache License 2.0
Blockly.Xml={};Blockly.Xml.workspaceToDom=function(a){var b;Blockly.RTL&&(b=a.getWidth());var c=goog.dom.createDom("xml");a=a.getTopBlocks(!0);for(var d=0,e;e=a[d];d++){var f=Blockly.Xml.blockToDom_(e);e=e.getRelativeToSurfaceXY();f.setAttribute("x",Blockly.RTL?b-e.x:e.x);f.setAttribute("y",e.y);c.appendChild(f)}return c};
Blockly.Xml.blockToDom_=function(a){var b=goog.dom.createDom("block");b.setAttribute("type",a.type);b.setAttribute("id",a.id);if(a.mutationToDom){var c=a.mutationToDom();c&&b.appendChild(c)}for(var c=0,d;d=a.inputList[c];c++)for(var e=0,f;f=d.fieldRow[e];e++)if(f.name&&f.EDITABLE){var g=goog.dom.createDom("field",null,f.getValue());g.setAttribute("name",f.name);b.appendChild(g)}if(c=a.getCommentText())c=goog.dom.createDom("comment",null,c),"object"==typeof a.comment&&(c.setAttribute("pinned",a.comment.isVisible()),
d=a.comment.getBubbleSize(),c.setAttribute("h",d.height),c.setAttribute("w",d.width)),b.appendChild(c);e=!1;for(c=0;d=a.inputList[c];c++){var h;f=!0;d.type!=Blockly.DUMMY_INPUT&&(g=d.connection.targetBlock(),d.type==Blockly.INPUT_VALUE?(h=goog.dom.createDom("value"),e=!0):d.type==Blockly.NEXT_STATEMENT&&(h=goog.dom.createDom("statement")),g&&(h.appendChild(Blockly.Xml.blockToDom_(g)),f=!1),h.setAttribute("name",d.name),f||b.appendChild(h))}e&&b.setAttribute("inline",a.inputsInline);a.isCollapsed()&&
b.setAttribute("collapsed",!0);a.disabled&&b.setAttribute("disabled",!0);a.isDeletable()||b.setAttribute("deletable",!1);a.isMovable()||b.setAttribute("movable",!1);a.isEditable()||b.setAttribute("editable",!1);if(a=a.getNextBlock())h=goog.dom.createDom("next",null,Blockly.Xml.blockToDom_(a)),b.appendChild(h);return b};Blockly.Xml.domToText=function(a){return(new XMLSerializer).serializeToString(a)};
d=a.comment.getBubbleSize(),c.setAttribute("h",d.height),c.setAttribute("w",d.width)),b.appendChild(c);a.data&&(c=goog.dom.createDom("data",null,a.data),b.appendChild(c));e=!1;for(c=0;d=a.inputList[c];c++){var h;f=!0;d.type!=Blockly.DUMMY_INPUT&&(g=d.connection.targetBlock(),d.type==Blockly.INPUT_VALUE?(h=goog.dom.createDom("value"),e=!0):d.type==Blockly.NEXT_STATEMENT&&(h=goog.dom.createDom("statement")),g&&(h.appendChild(Blockly.Xml.blockToDom_(g)),f=!1),h.setAttribute("name",d.name),f||b.appendChild(h))}e&&
b.setAttribute("inline",a.inputsInline);a.isCollapsed()&&b.setAttribute("collapsed",!0);a.disabled&&b.setAttribute("disabled",!0);a.isDeletable()||b.setAttribute("deletable",!1);a.isMovable()||b.setAttribute("movable",!1);a.isEditable()||b.setAttribute("editable",!1);if(a=a.getNextBlock())h=goog.dom.createDom("next",null,Blockly.Xml.blockToDom_(a)),b.appendChild(h);return b};Blockly.Xml.domToText=function(a){return(new XMLSerializer).serializeToString(a)};
Blockly.Xml.domToPrettyText=function(a){a=Blockly.Xml.domToText(a).split("<");for(var b="",c=1;c<a.length;c++){var d=a[c];"/"==d[0]&&(b=b.substring(2));a[c]=b+"<"+d;"/"!=d[0]&&"/>"!=d.slice(-2)&&(b+=" ")}a=a.join("\n");a=a.replace(/(<(\w+)\b[^>]*>[^\n]*)\n *<\/\2>/g,"$1</$2>");return a.replace(/^\n/,"")};
Blockly.Xml.textToDom=function(a){a=(new DOMParser).parseFromString(a,"text/xml");if(!a||!a.firstChild||"xml"!=a.firstChild.nodeName.toLowerCase()||a.firstChild!==a.lastChild)throw"Blockly.Xml.textToDom did not obtain a valid XML tree.";return a.firstChild};
Blockly.Xml.domToWorkspace=function(a,b){var c;Blockly.RTL&&(c=a.getWidth());for(var d=b.childNodes.length,e=0;e<d;e++){var f=b.childNodes[e];if("block"==f.nodeName.toLowerCase()){var g=Blockly.Xml.domToBlock(a,f),h=parseInt(f.getAttribute("x"),10),f=parseInt(f.getAttribute("y"),10);isNaN(h)||isNaN(f)||g.moveBy(Blockly.RTL?c-h:h,f)}}};
Blockly.Xml.domToBlock=function(a,b,c){var d=Blockly.Xml.domToBlockHeadless_(a,b,c);if(a.rendered){d.setConnectionsHidden(!0);a=d.getDescendants();for(b=a.length-1;0<=b;b--)a[b].initSvg();for(b=a.length-1;0<=b;b--)a[b].render(!1);setTimeout(function(){d.setConnectionsHidden(!1)},1);d.updateDisabled();Blockly.fireUiEvent(window,"resize")}return d};
Blockly.Xml.domToBlockHeadless_=function(a,b,c){var d=null,e=b.getAttribute("type");if(!e)throw"Block type unspecified: \n"+b.outerHTML;var f=b.getAttribute("id");if(c&&f){d=Blockly.Block.getById(f,a);if(!d)throw"Couldn't get Block with id: "+f;f=d.getParent();d.workspace&&d.dispose(!0,!1,!0);d.fill(a,e);d.parent_=f}else d=Blockly.Block.obtain(a,e);for(var g=null,f=0,h;h=b.childNodes[f];f++)if(3!=h.nodeType||!h.data.match(/^\s*$/)){for(var g=null,k=0,l;l=h.childNodes[k];k++)3==l.nodeType&&l.data.match(/^\s*$/)||
(g=l);k=h.getAttribute("name");switch(h.nodeName.toLowerCase()){case "mutation":d.domToMutation&&(d.domToMutation(h),d.initSvg&&d.initSvg());break;case "comment":d.setCommentText(h.textContent);var p=h.getAttribute("pinned");p&&setTimeout(function(){d.comment&&d.comment.setVisible&&d.comment.setVisible("true"==p)},1);g=parseInt(h.getAttribute("w"),10);h=parseInt(h.getAttribute("h"),10);!isNaN(g)&&!isNaN(h)&&d.comment&&d.comment.setVisible&&d.comment.setBubbleSize(g,h);break;case "title":case "field":d.setFieldValue(h.textContent,
k);break;case "value":case "statement":h=d.getInput(k);if(!h)throw"Input "+k+" does not exist in block "+e;if(g&&"block"==g.nodeName.toLowerCase())if(g=Blockly.Xml.domToBlockHeadless_(a,g,c),g.outputConnection)h.connection.connect(g.outputConnection);else if(g.previousConnection)h.connection.connect(g.previousConnection);else throw"Child block does not have output or previous statement.";break;case "next":if(g&&"block"==g.nodeName.toLowerCase()){if(!d.nextConnection)throw"Next statement does not exist.";
(g=l);k=h.getAttribute("name");switch(h.nodeName.toLowerCase()){case "mutation":d.domToMutation&&(d.domToMutation(h),d.initSvg&&d.initSvg());break;case "comment":d.setCommentText(h.textContent);var p=h.getAttribute("pinned");p&&setTimeout(function(){d.comment&&d.comment.setVisible&&d.comment.setVisible("true"==p)},1);g=parseInt(h.getAttribute("w"),10);h=parseInt(h.getAttribute("h"),10);!isNaN(g)&&!isNaN(h)&&d.comment&&d.comment.setVisible&&d.comment.setBubbleSize(g,h);break;case "data":d.data=h.textContent;
break;case "title":case "field":d.setFieldValue(h.textContent,k);break;case "value":case "statement":h=d.getInput(k);if(!h)throw"Input "+k+" does not exist in block "+e;if(g&&"block"==g.nodeName.toLowerCase())if(g=Blockly.Xml.domToBlockHeadless_(a,g,c),g.outputConnection)h.connection.connect(g.outputConnection);else if(g.previousConnection)h.connection.connect(g.previousConnection);else throw"Child block does not have output or previous statement.";break;case "next":if(g&&"block"==g.nodeName.toLowerCase()){if(!d.nextConnection)throw"Next statement does not exist.";
if(d.nextConnection.targetConnection)throw"Next statement is already connected.";g=Blockly.Xml.domToBlockHeadless_(a,g,c);if(!g.previousConnection)throw"Next block does not have previous statement.";d.nextConnection.connect(g.previousConnection)}break;default:console.log("Ignoring unknown tag: "+h.nodeName)}}(a=b.getAttribute("inline"))&&d.setInputsInline("true"==a);(a=b.getAttribute("disabled"))&&d.setDisabled("true"==a);(a=b.getAttribute("deletable"))&&d.setDeletable("true"==a);(a=b.getAttribute("movable"))&&
d.setMovable("true"==a);(a=b.getAttribute("editable"))&&d.setEditable("true"==a);(b=b.getAttribute("collapsed"))&&d.setCollapsed("true"==b);return d};Blockly.Xml.deleteNext=function(a){for(var b=0,c;c=a.childNodes[b];b++)if("next"==c.nodeName.toLowerCase()){a.removeChild(c);break}};goog.global.Blockly||(goog.global.Blockly={});goog.global.Blockly.Xml||(goog.global.Blockly.Xml={});goog.global.Blockly.Xml.domToText=Blockly.Xml.domToText;goog.global.Blockly.Xml.domToWorkspace=Blockly.Xml.domToWorkspace;
goog.global.Blockly.Xml.textToDom=Blockly.Xml.textToDom;goog.global.Blockly.Xml.workspaceToDom=Blockly.Xml.workspaceToDom;
......
......@@ -95,6 +95,13 @@ Blockly.Xml.blockToDom_ = function(block) {
element.appendChild(commentElement);
}
if (block.data) {
// Optional text data that round-trips beween blocks and XML.
// Has no effect. May be used by 3rd parties for meta information.
var dataElement = goog.dom.createDom('data', null, block.data);
element.appendChild(dataElement);
}
var hasValues = false;
for (var i = 0, input; input = block.inputList[i]; i++) {
var container;
......@@ -357,6 +364,11 @@ Blockly.Xml.domToBlockHeadless_ =
block.comment.setBubbleSize(bubbleW, bubbleH);
}
break;
case 'data':
// Optional text data that round-trips beween blocks and XML.
// Has no effect. May be used by 3rd parties for meta information.
block.data = xmlChild.textContent;
break;
case 'title':
// Titles were renamed to field in December 2013.
// Fall through.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment