Skip to content

Commit d0fed74

Browse files
author
ColinTree
committed
0.2.1: inprove block identify system
1 parent e9b0ae3 commit d0fed74

File tree

2 files changed

+16
-13
lines changed

2 files changed

+16
-13
lines changed

assets/plugin.js

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ var TEXT_DO = 'do';
1212
var TEXT_CALL = 'call';
1313
var TEXT_SET = 'set';
1414

15+
// will be increased automatically by getBlock(json)
16+
var blockIndex = 0;
17+
var blockId = 'block0';
18+
1519
require(['gitbook', 'jQuery'], function(gitbook, $) {
1620

1721
gitbook.events.bind("start", function(e, config) {
@@ -28,11 +32,10 @@ require(['gitbook', 'jQuery'], function(gitbook, $) {
2832
var name = block['name'];
2933
var arg = block['arg'] || [];
3034
var output = block['output']===true;
31-
var divId = 'method_'+name;
3235

33-
$(this).attr('id', divId).show();
36+
$(this).attr('id', blockId).show();
3437

35-
Blockly.Blocks['dynamicCreated_'+divId] = {
38+
Blockly.Blocks['dynamicCreated_'+blockId] = {
3639
init: function() {
3740
this.appendDummyInput().appendField(TEXT_CALL).appendField(new Blockly.FieldDropdown([[ComponentName, 'OPTIONNAME']]), 'COMPONENT_SELECTOR').appendField('.'+name);
3841
for (var i=0; i<arg.length; i++) {
@@ -51,7 +54,7 @@ require(['gitbook', 'jQuery'], function(gitbook, $) {
5154
}
5255
};
5356

54-
newBlockAndWorkspace(divId).moveBy(output?8:0, 0);
57+
newBlockAndWorkspace(blockId).moveBy(output?8:0, 0);
5558
});
5659

5760
// EVENT
@@ -60,11 +63,10 @@ require(['gitbook', 'jQuery'], function(gitbook, $) {
6063

6164
var name = block['name'];
6265
var arg = block['arg'] || [];
63-
var divId = 'event_'+name;
6466

65-
$(this).attr('id', divId).show();
67+
$(this).attr('id', blockId).show();
6668

67-
Blockly.Blocks['dynamicCreated_'+divId] = {
69+
Blockly.Blocks['dynamicCreated_'+blockId] = {
6870
init: function() {
6971
this.appendDummyInput('').appendField(TEXT_WHEN).appendField(new Blockly.FieldDropdown([[ComponentName, 'OPTIONNAME']]), "COMPONENT_SELECTOR").appendField('.' + name);
7072
if (arg.length > 0) {
@@ -83,7 +85,7 @@ require(['gitbook', 'jQuery'], function(gitbook, $) {
8385
}
8486
};
8587

86-
newBlockAndWorkspace(divId);
88+
newBlockAndWorkspace(blockId);
8789
});
8890

8991
// PROPERTY
@@ -95,11 +97,10 @@ require(['gitbook', 'jQuery'], function(gitbook, $) {
9597
if (getter !== true && getter !== false) {
9698
getter = true;
9799
}
98-
var divId = 'property_'+(getter?'getter':'setter')+'_'+name;
99100

100-
$(this).attr('id', divId).show();
101+
$(this).attr('id', blockId).show();
101102

102-
Blockly.Blocks['dynamicCreated_'+divId] = {
103+
Blockly.Blocks['dynamicCreated_'+blockId] = {
103104
init: function() {
104105
var input;
105106
if (getter) {
@@ -119,7 +120,7 @@ require(['gitbook', 'jQuery'], function(gitbook, $) {
119120
}
120121
};
121122

122-
newBlockAndWorkspace(divId).moveBy((getter?8:0), 0);
123+
newBlockAndWorkspace(blockId).moveBy((getter?8:0), 0);
123124
});
124125
});
125126
});
@@ -130,6 +131,8 @@ require(['gitbook', 'jQuery'], function(gitbook, $) {
130131
* @returns object or null
131132
*/
132133
function getBlock(json) {
134+
blockIndex++;
135+
blockId = 'block' + blockIndex;
133136
var blockData = JSON.parse(json);
134137
if (typeof(blockData) != "object") {
135138
console.error("block info is not a json object");

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,5 @@
4545
"type": "git",
4646
"url": "git+https://github.com/ColinTree/gitbook-plugin-ai2-blocks.git"
4747
},
48-
"version": "0.1.1"
48+
"version": "0.2.1"
4949
}

0 commit comments

Comments
 (0)