Skip to content

Commit

Permalink
Image mode now mostly works
Browse files Browse the repository at this point in the history
  • Loading branch information
acbart committed Jun 3, 2020
1 parent c20c963 commit e7378cc
Show file tree
Hide file tree
Showing 8 changed files with 184 additions and 75 deletions.
32 changes: 17 additions & 15 deletions .idea/workspace.xml

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

5 changes: 4 additions & 1 deletion src/ast/ast_Call.js
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,7 @@ BlockMirrorTextToBlocks.prototype['ast_Call'] = function (node, parent) {
if (name in this.FUNCTION_SIGNATURES) {
signature = this.FUNCTION_SIGNATURES[Sk.ffi.remapToJs(func.id)];
}
console.log(signature);
} else if (func._astname === 'Attribute') {
isMethod = true;
caller = func.value;
Expand Down Expand Up @@ -516,8 +517,9 @@ BlockMirrorTextToBlocks.prototype['ast_Call'] = function (node, parent) {
if (signature !== null && signature !== undefined) {
if (signature.custom) {
try {
return signature.custom(node, parent)
return signature.custom(node, parent, this)
} catch (e) {
console.error(e);
// We tried to be fancy and failed, better fall back to default behavior!
}
}
Expand Down Expand Up @@ -590,3 +592,4 @@ BlockMirrorTextToBlocks.prototype['ast_Call'] = function (node, parent) {
return [newBlock];
}
};

12 changes: 8 additions & 4 deletions src/ast/ast_Import.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ Blockly.Python['ast_Import'] = function (block) {
// Optional from part
let from = "";
if (this.from_) {
from = "from "+block.getFieldValue('MODULE') + " ";
let moduleName = block.getFieldValue('MODULE');
from = "from "+moduleName + " ";
Blockly.Python.imported_["import_" + moduleName] = moduleName;
}
// Create a list with any number of elements of any type.
let elements = new Array(block.nameCount_);
Expand All @@ -106,7 +108,9 @@ Blockly.Python['ast_Import'] = function (block) {
name = Blockly.Python.variableDB_.getName(block.getFieldValue('ASNAME' + i), Blockly.Variables.NAME_TYPE);
elements[i] += " as " + name;
}
Blockly.Python.imported_["import_"+name] = name;
if (!from) {
Blockly.Python.imported_["import_" + name] = name;
}
}
return from + 'import ' + elements.join(', ') + "\n";
};
Expand Down Expand Up @@ -145,8 +149,8 @@ BlockMirrorTextToBlocks.prototype['ast_Import'] = function (node, parent) {
}

return BlockMirrorTextToBlocks.create_block("ast_Import", node.lineno, fields,
{}, {}, mutations);
{}, {"inline": true}, mutations);
};

// Alias ImportFrom because of big overlap
BlockMirrorTextToBlocks.prototype['ast_ImportFrom'] = BlockMirrorTextToBlocks.prototype['ast_Import'];
BlockMirrorTextToBlocks.prototype['ast_ImportFrom'] = BlockMirrorTextToBlocks.prototype['ast_Import'];
17 changes: 9 additions & 8 deletions src/ast/ast_Str.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ BlockMirrorTextToBlocks.BLOCKS.push({
});


Blockly.Blocks['ast_StrImage'] = {
Blockly.Blocks['ast_Image'] = {
init: function () {
this.setColour(BlockMirrorTextToBlocks.COLOR.TEXT);
this.src_ = "loading.png";
Expand All @@ -66,7 +66,7 @@ Blockly.Blocks['ast_StrImage'] = {
let image = this.getInput('IMAGE');
if (!image) {
image = this.appendDummyInput("IMAGE");
image.appendField(new Blockly.FieldImage(this.src_, 20, 20, { alt: this.src_, flipRtl: "FALSE" }));
image.appendField(new Blockly.FieldImage(this.src_, 40, 40, { alt: this.src_, flipRtl: "FALSE" }));
}
let imageField = image.fieldRow[0];
imageField.setValue(this.src_);
Expand Down Expand Up @@ -103,10 +103,11 @@ Blockly.Python['ast_StrChar'] = function (block) {
}
};

Blockly.Python['ast_StrImage'] = function (block) {
Blockly.Python['ast_Image'] = function (block) {
// Text value
let code = Blockly.Python.quote_(block.src_);
return [code, Blockly.Python.ORDER_ATOMIC];
Blockly.Python.definitions_["import_image"] = "from image import Image";
let code = "Image("+Blockly.Python.quote_(block.src_)+")";
return [code, Blockly.Python.ORDER_FUNCTION_CALL];
};

Blockly.Python['ast_StrMultiline'] = function (block) {
Expand Down Expand Up @@ -177,10 +178,10 @@ BlockMirrorTextToBlocks.prototype.dedent = function (text, levels, isDocString)
BlockMirrorTextToBlocks.prototype['ast_Str'] = function (node, parent) {
let s = node.s;
let text = Sk.ffi.remapToJs(s);
if (text.startsWith("http") && text.endsWith(".png")) {
return BlockMirrorTextToBlocks.create_block("ast_StrImage", node.lineno, {}, {}, {},
/*if (text.startsWith("http") && text.endsWith(".png")) {
return BlockMirrorTextToBlocks.create_block("ast_Image", node.lineno, {}, {}, {},
{"@src": text});
} else if (this.isSingleChar(text)) {
} else*/ if (this.isSingleChar(text)) {
return BlockMirrorTextToBlocks.create_block("ast_StrChar", node.lineno, {"TEXT": text});
} else if (this.isDocString(node, parent)) {
let dedented = this.dedent(text, this.levelIndex - 1, true);
Expand Down
16 changes: 16 additions & 0 deletions src/ast/ast_functions.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
BlockMirrorTextToBlocks['ast_Image'] = function (node, parent, bmttb) {
if (!bmttb.blockMirror.configuration.imageMode) {
throw "Not using image constructor";
}
if (node.args.length !== 1) {
throw "More than one argument to Image constructor";
}
if (node.args[0]._astname !== "Str") {
throw "First argument for Image constructor must be string literal";
}
return BlockMirrorTextToBlocks.create_block("ast_Image", node.lineno, {}, {}, {},
{"@src": Sk.ffi.remapToJs(node.args[0].s)});
};


BlockMirrorTextToBlocks.prototype.FUNCTION_SIGNATURES = {
'abs': {
'returns': true,
Expand Down Expand Up @@ -36,6 +51,7 @@ BlockMirrorTextToBlocks.prototype.FUNCTION_SIGNATURES = {
'help': {returns: true, colour: BlockMirrorTextToBlocks.COLOR.PYTHON},
'hex': {returns: true, colour: BlockMirrorTextToBlocks.COLOR.MATH},
'id': {returns: true, colour: BlockMirrorTextToBlocks.COLOR.PYTHON},
'Image': {custom: BlockMirrorTextToBlocks.ast_Image},
'input': {returns: true, colour: BlockMirrorTextToBlocks.COLOR.FILE,
simple: ['prompt']},
'int': {returns: true, colour: BlockMirrorTextToBlocks.COLOR.MATH,
Expand Down
15 changes: 14 additions & 1 deletion src/block_mirror.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,10 @@ BlockMirror.prototype.validateConfiguration = function (configuration) {
this.isParsons = function() { return false; };

// Convert image URLs?
this.configuration.imageUrls = configuration.imageUrls || true;
this.configuration.imageUploadHook = configuration.imageUploadHook || (old => Promise.resolve(old));
this.configuration.imageDownloadHook = configuration.imageDownloadHook || (old => old);
this.configuration.imageLiteralHook = configuration.imageLiteralHook || (old => old);
this.configuration.imageMode = configuration.imageMode || false;
};

BlockMirror.prototype.initializeVariables = function () {
Expand Down Expand Up @@ -198,6 +201,16 @@ BlockMirror.prototype.setMode = function (mode) {
this.textEditor.setMode(mode);
};

BlockMirror.prototype.setImageMode = function (imageMode) {
this.configuration.imageMode = imageMode;
if (imageMode) {
this.textEditor.enableImages();
} else {
this.textEditor.disableImages();
}
console.log(imageMode);
};

BlockMirror.prototype.setReadOnly = function (isReadOnly) {
this.textEditor.setReadOnly(isReadOnly);
this.blockEditor.setReadOnly(isReadOnly);
Expand Down
Loading

0 comments on commit e7378cc

Please sign in to comment.