summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjmoenig <jens@moenig.org>2013-06-19 18:51:38 +0200
committerjmoenig <jens@moenig.org>2013-06-19 18:51:38 +0200
commite2aeaec7cfe0986ad13263b2eded6b8115b9b476 (patch)
treea16c2941996c3e7ba5a7b2f4d85f6e6281967b43
parentacc40a6d2a06ab1fcfbd488030e091bd9215a1b2 (diff)
downloadsnap-e2aeaec7cfe0986ad13263b2eded6b8115b9b476.tar.gz
snap-e2aeaec7cfe0986ad13263b2eded6b8115b9b476.zip
Store: persisting code mappings
in project and block library files
-rwxr-xr-xhistory.txt4
-rw-r--r--store.js63
2 files changed, 57 insertions, 10 deletions
diff --git a/history.txt b/history.txt
index 5e1d9c6..5c2d9cc 100755
--- a/history.txt
+++ b/history.txt
@@ -1737,3 +1737,7 @@ ______
130618
------
* Code mapping (generating textual code from blocks), first iteration
+
+130619
+------
+* Store: persisting code mappings in project and block library files
diff --git a/store.js b/store.js
index 061b1be..9736b6d 100644
--- a/store.js
+++ b/store.js
@@ -61,7 +61,7 @@ SyntaxElementMorph*/
// Global stuff ////////////////////////////////////////////////////////
-modules.store = '2013-April-25';
+modules.store = '2013-June-19';
// XML_Serializer ///////////////////////////////////////////////////////
@@ -204,7 +204,16 @@ XML_Serializer.prototype.format = function (string) {
return string.replace(/[@$%]([\d]+)?/g, function (spec, index) {
index = parseInt(index, 10);
- value = values[(isNaN(index) ? (i += 1) : index) + 1];
+
+ if (isNaN(index)) {
+ i += 1;
+ value = values[i + 1];
+ } else {
+ value = values[index + 1];
+ }
+ // original line of code - now frowned upon by JSLint:
+ // value = values[(isNaN(index) ? (i += 1) : index) + 1];
+
return spec === '@' ?
myself.escape(value)
: spec === '$' ?
@@ -303,10 +312,12 @@ SnapSerializer.prototype.load = function (xmlString) {
SnapSerializer.prototype.loadProjectModel = function (xmlNode) {
// public - answer a new Project represented by the given XML top node
var myself = this,
- project = this.project = {sprites: {}},
+ project = {sprites: {}},
model,
nameID;
+ this.project = project;
+
model = {project: xmlNode };
if (+xmlNode.attributes.version > this.version) {
throw 'Project uses newer version of Serializer';
@@ -379,6 +390,13 @@ SnapSerializer.prototype.loadProjectModel = function (xmlNode) {
);
}
+ model.codeMappings = model.project.childNamed('code');
+ if (model.codeMappings) {
+ model.codeMappings.children.forEach(function (xml) {
+ StageMorph.prototype.codeMappings[xml.tag] = xml.contents;
+ });
+ }
+
model.globalBlocks = model.project.childNamed('blocks');
if (model.globalBlocks) {
this.loadCustomBlocks(project.stage, model.globalBlocks, true);
@@ -654,7 +672,7 @@ SnapSerializer.prototype.loadCustomBlocks = function (
// private
var myself = this;
element.children.forEach(function (child) {
- var definition, names, inputs, comment, i;
+ var definition, names, inputs, code, comment, i;
if (child.tag !== 'block-definition') {
return;
}
@@ -687,11 +705,17 @@ SnapSerializer.prototype.loadCustomBlocks = function (
if (child.tag !== 'input') {
return;
}
- definition.declarations[names[i += 1]]
+ i += 1;
+ definition.declarations[names[i]]
= [child.attributes.type, child.contents];
});
}
+ code = child.childNamed('code');
+ if (code) {
+ definition.codeMapping = code.contents;
+ }
+
comment = child.childNamed('comment');
if (comment) {
definition.comment = myself.loadComment(comment);
@@ -1182,8 +1206,7 @@ SnapSerializer.prototype.loadColor = function (colorString) {
SnapSerializer.prototype.openProject = function (project, ide) {
var stage = ide.stage,
sprites = [],
- sprite,
- scripts;
+ sprite;
if (!project || !project.stage) {
return;
}
@@ -1195,7 +1218,8 @@ SnapSerializer.prototype.openProject = function (project, ide) {
if (stage) {
stage.destroy();
}
- ide.add(ide.stage = project.stage);
+ ide.add(project.stage);
+ ide.stage = project.stage;
sprites = ide.stage.children.filter(function (child) {
return child instanceof SpriteMorph;
});
@@ -1204,7 +1228,6 @@ SnapSerializer.prototype.openProject = function (project, ide) {
});
ide.sprites = new List(sprites);
sprite = sprites[0] || project.stage;
- scripts = sprite.scripts;
if (sizeOf(this.mediaDict) > 0) {
ide.hasChangedMedia = false;
@@ -1243,6 +1266,22 @@ StageMorph.prototype.toXML = function (serializer) {
thumbdata = null;
}
+ function codeMappings() {
+ var code = '';
+ Object.keys(StageMorph.prototype.codeMappings).forEach(
+ function (selector) {
+ code += (
+ '<' + selector + '>' +
+ XML_Element.prototype.escape(
+ StageMorph.prototype.codeMappings[selector]
+ ) +
+ '</' + selector + '>'
+ );
+ }
+ );
+ return code;
+ }
+
this.removeAllClones();
return serializer.format(
'<project name="@" app="@" version="@">' +
@@ -1258,6 +1297,7 @@ StageMorph.prototype.toXML = function (serializer) {
'<scripts>%</scripts><sprites>%</sprites>' +
'</stage>' +
'<hidden>$</hidden>' +
+ '<code>%</code>' +
'<blocks>%</blocks>' +
'<variables>%</variables>' +
'</project>',
@@ -1279,9 +1319,10 @@ StageMorph.prototype.toXML = function (serializer) {
serializer.store(this.scripts),
serializer.store(this.children),
Object.keys(StageMorph.prototype.hiddenPrimitives).reduce(
- function(a, b) {return a + ' ' + b; },
+ function (a, b) {return a + ' ' + b; },
''
),
+ codeMappings(),
serializer.store(this.globalBlocks),
(ide && ide.globalVariables) ?
serializer.store(ide.globalVariables) : ''
@@ -1546,12 +1587,14 @@ CustomBlockDefinition.prototype.toXML = function (serializer) {
return serializer.format(
'<block-definition s="@" type="@" category="@">' +
'%' +
+ '<code>@</code>' +
'<inputs>%</inputs>%%' +
'</block-definition>',
this.spec,
this.type,
this.category || 'other',
this.comment ? this.comment.toXML(serializer) : '',
+ this.codeMapping || '',
Object.keys(this.declarations).reduce(function (xml, decl) {
return xml + serializer.format(
'<input type="@">$</input>',