Skip to content

Commit

Permalink
Configurable image urls, and the new CT2 menu
Browse files Browse the repository at this point in the history
  • Loading branch information
acbart committed Jun 2, 2020
1 parent 58bb500 commit 8ef3dbe
Show file tree
Hide file tree
Showing 10 changed files with 233 additions and 101 deletions.
33 changes: 21 additions & 12 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions dist/block_mirror.css
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,11 @@
font-style: normal;
fill: black;
}

.blockmirror-toolbox-variable>.blocklyFlyoutLabelBackground {
font-style: normal;
fill: white;
opacity: 1;
stroke: darkgray;
transform: translate(-1px, -5px) scale(1.1,1.5);
}
150 changes: 98 additions & 52 deletions dist/block_mirror.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,14 @@ Blockly.Variables.flyoutCategoryBlocks = function (workspace) {
// New variables are added to the end of the variableModelList.
var mostRecentVariableFieldXmlString = variableModelList[variableModelList.length - 1];

if (Blockly.Blocks['ast_Assign']) {
if (!Blockly.Variables._HIDE_GETTERS_SETTERS && Blockly.Blocks['ast_Assign']) {
var gap = Blockly.Blocks['ast_AugAssign'] ? 8 : 24;
var blockText = '<xml>' + '<block type="ast_Assign" gap="' + gap + '">' + mostRecentVariableFieldXmlString + '</block>' + '</xml>';
var block = Blockly.Xml.textToDom(blockText).firstChild;
xmlList.push(block);
}

if (Blockly.Blocks['ast_AugAssign']) {
if (!Blockly.Variables._HIDE_GETTERS_SETTERS && Blockly.Blocks['ast_AugAssign']) {
var gap = Blockly.Blocks['ast_Name'] ? 20 : 8;
var blockText = '<xml>' + '<block type="ast_AugAssign" gap="' + gap + '">' + mostRecentVariableFieldXmlString + '<value name="VALUE">' + '<shadow type="ast_Num">' + '<field name="NUM">1</field>' + '</shadow>' + '</value>' + '<mutation options="false" simple="true"></mutation>' + '</block>' + '</xml>';
var block = Blockly.Xml.textToDom(blockText).firstChild;
Expand All @@ -127,21 +127,23 @@ Blockly.Variables.flyoutCategoryBlocks = function (workspace) {
variableModelList.sort(Blockly.VariableModel.compareByName);

for (var i = 0, variable; variable = variableModelList[i]; i++) {
var _block = Blockly.utils.xml.createElement('block');
if (!Blockly.Variables._HIDE_GETTERS_SETTERS) {
var _block = Blockly.utils.xml.createElement('block');

_block.setAttribute('type', 'ast_Name');
_block.setAttribute('type', 'ast_Name');

_block.setAttribute('gap', 8);
_block.setAttribute('gap', 8);

_block.appendChild(Blockly.Variables.generateVariableFieldDom(variable));
_block.appendChild(Blockly.Variables.generateVariableFieldDom(variable));

xmlList.push(_block);
/*block = Blockly.utils.xml.createElement('label');
console.log(variable);
block.setAttribute('text', variable.name);
block.setAttribute('web-class', 'blockmirror-toolbox-variable');
//block.setAttribute('gap', 8);
xmlList.push(block);*/
xmlList.push(_block);
} else {
block = Blockly.utils.xml.createElement('label');
block.setAttribute('text', variable.name);
block.setAttribute('web-class', 'blockmirror-toolbox-variable'); //block.setAttribute('gap', 8);

xmlList.push(block);
}
}
}
}
Expand Down Expand Up @@ -235,7 +237,10 @@ BlockMirror.prototype.validateConfiguration = function (configuration) {

this.isParsons = function () {
return false;
};
}; // Convert image URLs?


this.configuration.imageUrls = configuration.imageUrls || true;
};

BlockMirror.prototype.initializeVariables = function () {
Expand Down Expand Up @@ -444,51 +449,55 @@ function BlockMirrorTextEditor(blockMirror) {
//https://i.imgur.com/ITZKRiq.png

this.codeMirror.on("beforeChange", function (cm, change) {
if (change.origin === "paste") {
var newText = change.text[0];
if (_this.blockMirror.configuration.imageUrls) {
if (change.origin === "paste") {
var newText = change.text[0];

if (_this.isImageUrl(newText)) {
change.update(null, null, ["\"".concat(newText, "\"")]);
if (_this.isImageUrl(newText)) {
change.update(null, null, ["\"".concat(newText, "\"")]);
}
}
}
});
this.codeMirror.on("change", function (cm, change) {
console.log(change);
var lastLine;

if (change.origin === "paste" || change.origin === "setValue") {
//"https://game-icons.net/icons/ffffff/000000/1x1/delapouite/labrador-head.png"
lastLine = change.from.line + change.text.length;
} else {
lastLine = Math.max(1 + change.to.line, change.text.length);
}

cm.doc.eachLine(change.from.line, lastLine, function (line) {
var match;

var _loop = function _loop() {
var imageWidget = _this.makeImageWidget(match[2]);
if (_this.blockMirror.configuration.imageUrls) {
console.log(change);
var lastLine;

var imageMarker = cm.markText({
line: cm.doc.getLineNumber(line),
ch: match.index
}, {
line: cm.doc.getLineNumber(line),
ch: match.index + match[0].length
}, {
atomic: true,
replacedWith: imageWidget
});
if (change.origin === "paste" || change.origin === "setValue") {
//"https://game-icons.net/icons/ffffff/000000/1x1/delapouite/labrador-head.png"
lastLine = change.from.line + change.text.length;
} else {
lastLine = Math.max(1 + change.to.line, change.text.length);
}

imageWidget.onclick = function (x) {
return imageMarker.clear();
cm.doc.eachLine(change.from.line, lastLine, function (line) {
var match;

var _loop = function _loop() {
var imageWidget = _this.makeImageWidget(match[2]);

var imageMarker = cm.markText({
line: cm.doc.getLineNumber(line),
ch: match.index
}, {
line: cm.doc.getLineNumber(line),
ch: match.index + match[0].length
}, {
atomic: true,
replacedWith: imageWidget
});

imageWidget.onclick = function (x) {
return imageMarker.clear();
};
};
};

while ((match = HAS_IMAGE_URL.exec(line.text)) !== null) {
_loop();
}
});
while ((match = HAS_IMAGE_URL.exec(line.text)) !== null) {
_loop();
}
});
}
});
} //BlockMirrorTextEditor.prototype.enableImages = function (url) {
//BlockMirrorTextEditor.prototype.disableImage = function (url) {
Expand Down Expand Up @@ -835,6 +844,7 @@ BlockMirrorBlockEditor.prototype.resized = function (e) {
BlockMirrorBlockEditor.prototype.toolboxPythonToBlocks = function (toolboxPython) {
var _this5 = this;

Blockly.Variables._HIDE_GETTERS_SETTERS = false;
return toolboxPython.map(function (category) {
if (typeof category === "string") {
return category;
Expand All @@ -855,6 +865,11 @@ BlockMirrorBlockEditor.prototype.toolboxPythonToBlocks = function (toolboxPython
return result.rawXml.innerHTML.toString();
}).join("\n");
var footer = "</category>";

if (category['hideGettersSetters']) {
Blockly.Variables._HIDE_GETTERS_SETTERS = true;
}

return [header, body, footer].join("\n");
}).join("\n");
};
Expand Down Expand Up @@ -2554,7 +2569,7 @@ TOOLBOX_CATEGORY.TURTLES = {
TOOLBOX_CATEGORY.INPUT = {
name: "Input",
colour: "TEXT",
blocks: ["input(___)"]
blocks: ["input('')"]
};
TOOLBOX_CATEGORY.VALUES = {
name: "Values",
Expand Down Expand Up @@ -2667,7 +2682,38 @@ BlockMirrorBlockEditor.prototype.TOOLBOXES = {
"delete ___",
"global ___"
]}*/
]
],
//******************************************************
'ct2': [{
name: 'Memory',
colour: 'VARIABLES',
custom: 'VARIABLE',
hideGettersSetters: true
}, TOOLBOX_CATEGORY.SEP, '<category name="Expressions" expanded="true">', {
name: "Constants",
colour: "TEXT",
blocks: ['""', "0", "True", "[0, 0, 0]", "[___, ___, ___]", "[]"]
}, {
name: "Variables",
colour: "VARIABLES",
blocks: ["VARIABLE"]
}, TOOLBOX_CATEGORY.CALCULATIONS, TOOLBOX_CATEGORY.CONVERSIONS, {
name: "Conditions",
colour: "LOGIC",
blocks: ['___ == ___', '___ < ___', '___ and ___', 'not ___']
}, TOOLBOX_CATEGORY.INPUT, '</category>', TOOLBOX_CATEGORY.SEP, '<category name="Operations" expanded="true">', {
name: "Assignment",
colour: "VARIABLES",
blocks: ["VARIABLE = ___", "___.append(___)"]
}, TOOLBOX_CATEGORY.OUTPUT_WITH_PLOTTING, '</category>', TOOLBOX_CATEGORY.SEP, '<category name="Control" expanded="true">', {
name: "Decision",
colour: "CONTROL",
blocks: ['if ___: pass', 'if ___: pass\nelse: pass']
}, {
name: "Iteration",
colour: "CONTROL",
blocks: ['for ___ in ___: pass']
}, '</category>']
};
BlockMirrorTextToBlocks.BLOCKS.push({
"type": "ast_For",
Expand Down
6 changes: 5 additions & 1 deletion src/block_editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ BlockMirrorBlockEditor.prototype.resized = function (e) {
};

BlockMirrorBlockEditor.prototype.toolboxPythonToBlocks = function (toolboxPython) {
Blockly.Variables._HIDE_GETTERS_SETTERS = false;
return toolboxPython.map((category) => {
if (typeof category === "string") {
return category;
Expand All @@ -129,6 +130,9 @@ BlockMirrorBlockEditor.prototype.toolboxPythonToBlocks = function (toolboxPython
return result.rawXml.innerHTML.toString();
}).join("\n");
let footer = "</category>";
if (category['hideGettersSetters']) {
Blockly.Variables._HIDE_GETTERS_SETTERS = true;
}
return [header, body, footer].join("\n");
}).join("\n");
};
Expand Down Expand Up @@ -374,4 +378,4 @@ BlockMirrorBlockEditor.prototype.highlightLines = function(lines, style) {
/*this.workspace.getAllBlocks().map((block) => {
block
});*/
};
};
8 changes: 8 additions & 0 deletions src/block_mirror.css
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,11 @@
font-style: normal;
fill: black;
}

.blockmirror-toolbox-variable>.blocklyFlyoutLabelBackground {
font-style: normal;
fill: white;
opacity: 1;
stroke: darkgray;
transform: translate(-1px, -5px) scale(1.1,1.5);
}
3 changes: 3 additions & 0 deletions src/block_mirror.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ BlockMirror.prototype.validateConfiguration = function (configuration) {

// IsParsons?
this.isParsons = function() { return false; };

// Convert image URLs?
this.configuration.imageUrls = configuration.imageUrls || true;
};

BlockMirror.prototype.initializeVariables = function () {
Expand Down
Loading

0 comments on commit 8ef3dbe

Please sign in to comment.