summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README16
-rw-r--r--snip.js60
2 files changed, 76 insertions, 0 deletions
diff --git a/README b/README
new file mode 100644
index 0000000..eb55e71
--- /dev/null
+++ b/README
@@ -0,0 +1,16 @@
+SnipSnapLib
+===========
+
+The SnipSnapLib allows you to add your own custom block to Snap! without the need of the `javascript function` block.
+
+All you need to do is
+* fork the repository
+* edit the `snip.js` to add the desired function
+* bookmarkletify it, I recommend [this tool] (https://chriszarate.github.io/bookmarkleter/) **Important** You should not minify the script because this could cause incompatibilities if you have more than one SnipSnapLib active
+* drag&drop it in the URL bar of the Snap!-project and enjoy!
+
+Bugs
+====
+
+[ ] The bookmarklet makes the browser ask to leave the site
+[ ] If a category is selected, an inserted block is not instantly visible there
diff --git a/snip.js b/snip.js
new file mode 100644
index 0000000..5ea1bc4
--- /dev/null
+++ b/snip.js
@@ -0,0 +1,60 @@
+document.customBlocks = document.customBlocks || [];
+document.customBlockList = document.customBlockList || [];
+
+///////////////////////////////////////////////////
+// -> insert custom block definitions here //
+document.customBlocks.push({test: {type: 'command', category: 'looks', spec: 'test %s'}});
+document.customBlockList.push({category: 'looks', block: 'test'});
+SpriteMorph.prototype.test = function (text) {
+ alert("It works! " + text);
+};
+// end of insert, you can ignore the stuff below //
+///////////////////////////////////////////////////
+
+// helper functions
+function objects_merge(a, b) {
+ var res = {};
+ for (var aattrname in a) {res[aattrname] = a[aattrname];}
+ for (var battrname in b) {res[battrname] = b[battrname];}
+ return res;
+}
+
+function block(selector) {
+ var newBlock = SpriteMorph.prototype.blockForSelector(selector, true);
+ newBlock.isTemplate = true;
+ return newBlock;
+}
+
+if(!SpriteMorph.prototype.wasBlockTemplates) {
+ SpriteMorph.prototype.wasBlockTemplates = SpriteMorph.prototype.blockTemplates;
+ // re-define template function to inject custom blocks
+ SpriteMorph.prototype.blockTemplates = function (category) {
+ blocks = this.wasBlockTemplates(category);
+ document.customBlockList.forEach(function (cblock) {
+ if(cblock.category == category) {
+ blocks.push(block(cblock.block));
+ }
+ });
+ return blocks;
+ };
+}
+
+if(!SpriteMorph.prototype.wasInitBlocks) {
+ SpriteMorph.prototype.wasInitBlocks = SpriteMorph.prototype.initBlocks;
+ // re-define block initialization function
+ SpriteMorph.prototype.initBlocks = function () {
+ this.wasInitBlocks();
+ document.customBlocks.forEach(function (extBlock) {
+ SpriteMorph.prototype.blocks = objects_merge(SpriteMorph.prototype.blocks, extBlock);
+ });
+ };
+}
+
+// refresh the palettes
+world.children.forEach(function (child) {
+ child.flushPaletteCache(); // TODO cache clearing does not work like expected
+ child.refreshPalette();
+ child.sprites.asArray().forEach(function (sprite) {
+ sprite.initBlocks();
+ });
+});