Skip to content

Commit

Permalink
Initial fixes for procedures to work with latest Blockly
Browse files Browse the repository at this point in the history
  • Loading branch information
mark-friedman committed Sep 9, 2021
1 parent 14fc4fc commit ee06205
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 14 deletions.
24 changes: 21 additions & 3 deletions block-lexical-variables/src/field_procedure.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,31 @@

'use strict';

import * as Blockly from 'blockly/core';
import './msg';

// TODO: Maybe make a single importable goog compatibility object
const goog = {
provide: (_) => {},
require: (_) => {},
inherits: Blockly.utils.object.inherits,
dom: Blockly.utils.dom,
userAgent: Blockly.utils.userAgent,
asserts: {
assertObject: (_) => {},
},
};

goog.provide('Blockly.FieldProcedure');
goog.provide('Blockly.AIProcedure');

Blockly.FieldProcedure = {};

Blockly.FieldProcedure.defaultValue = ["",""];
Blockly.FieldProcedure.onChange = function(procedureId) {
var workspace = this.block.getTopWorkspace();
if(!this.block.editable_){ // [lyn, 10/14/13] .editable is undefined on blocks. Changed to .editable_
var workspace = this.block.workspace.getTopWorkspace();
if(!this.block.isEditable()){ // [lyn, 10/14/13] .editable is undefined on blocks. Changed to .editable_
workspace = Blockly.Drawer.flyout_.workspace_;
return;
}
Expand All @@ -34,7 +50,7 @@ Blockly.FieldProcedure.onChange = function(procedureId) {
}
//return;
}
this.setValue(text);
this.doValueUpdate_(text);
if(def) {
// [lyn, 10/27/13] Lyn sez: this causes complications (e.g., might open up mutator on collapsed procedure
// declaration block) and is no longer necessary with changes to setProedureParameters.
Expand All @@ -46,6 +62,8 @@ Blockly.FieldProcedure.onChange = function(procedureId) {
}
};
Blockly.AIProcedure = {};
Blockly.AIProcedure.getProcedureNames = function(returnValue, opt_workspace) {
var workspace = opt_workspace || Blockly.mainWorkspace;
var topBlocks = workspace.getTopBlocks();
Expand Down
17 changes: 17 additions & 0 deletions block-lexical-variables/src/field_procedurename.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,27 @@
*/
'use strict';

import * as Blockly from 'blockly/core';
import './msg';

// TODO: Maybe make a single importable goog compatibility object
const goog = {
provide: (_) => {},
require: (_) => {},
inherits: Blockly.utils.object.inherits,
dom: Blockly.utils.dom,
userAgent: Blockly.utils.userAgent,
asserts: {
assertObject: (_) => {},
},
};

goog.provide('AI.Blockly.FieldProcedureName');

goog.require('Blockly.FieldTextInput');

export const AI = {Blockly: {}};

/**
* FieldProcedureName is a specialization of {@link Blockly.FieldTextInput} that handles renaming
* procedures in the {@link AI.Blockly.ProcedureDatabase} when the procedure's name is changed.
Expand Down
15 changes: 15 additions & 0 deletions block-lexical-variables/src/procedure_database.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,21 @@

'use strict';

import * as Blockly from 'blockly/core';
import './msg';

// TODO: Maybe make a single importable goog compatibility object
const goog = {
provide: (_) => {},
require: (_) => {},
inherits: Blockly.utils.object.inherits,
dom: Blockly.utils.dom,
userAgent: Blockly.utils.userAgent,
asserts: {
assertObject: (_) => {},
},
};

goog.provide('AI.Blockly.ProcedureDatabase');
goog.require('Blockly');
goog.require('goog.object');
Expand Down
38 changes: 27 additions & 11 deletions block-lexical-variables/src/procedures.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@

import * as Blockly from 'blockly/core';
import './msg';
import {AI} from './field_procedurename';

// TODO: Maybe make a single importable goog compatibility object
const goog = {
Expand All @@ -86,7 +87,9 @@ Blockly.Blocks['procedures_defnoreturn'] = {
helpUrl: Blockly.Msg.LANG_PROCEDURES_DEFNORETURN_HELPURL,
bodyInputName: 'STACK',
init: function() {
this.setColour(Blockly.PROCEDURE_CATEGORY_HUE);
// Let the theme determine the color.
// this.setColour(Blockly.PROCEDURE_CATEGORY_HUE);
this.setStyle('procedure_blocks');
var name = Blockly.Procedures.findLegalName(
Blockly.Msg.LANG_PROCEDURES_DEFNORETURN_PROCEDURE, this);
this.appendDummyInput('HEADER')
Expand Down Expand Up @@ -285,7 +288,9 @@ Blockly.Blocks['procedures_defnoreturn'] = {
// superclass directly. I.e., can't do this:
// mutatorarg.getTitle_("NAME").setValue(newParamName);
// so instead do this:
Blockly.Field.prototype.setText.call(mutatorarg.getField("NAME"), newParamName);
mutatorarg.getField("NAME").setValue(newParamName);
// mutatorarg.getField("NAME").doValueUpdate_(newParamName);
// Blockly.Field.prototype.setText.call(mutatorarg.getField("NAME"), newParamName);
}
}
// console.log("exit procedureParameterChangeHandler");
Expand Down Expand Up @@ -490,7 +495,9 @@ Blockly.Blocks['procedures_defreturn'] = {
helpUrl: Blockly.Msg.LANG_PROCEDURES_DEFRETURN_HELPURL,
bodyInputName: 'RETURN',
init: function() {
this.setColour(Blockly.PROCEDURE_CATEGORY_HUE);
// Let the theme determine the color.
// this.setColour(Blockly.PROCEDURE_CATEGORY_HUE);
this.setStyle('procedure_blocks');
var name = Blockly.Procedures.findLegalName(
Blockly.Msg.LANG_PROCEDURES_DEFRETURN_PROCEDURE, this);
this.appendDummyInput('HEADER')
Expand Down Expand Up @@ -533,7 +540,9 @@ Blockly.Blocks['procedures_defreturn'] = {
Blockly.Blocks['procedures_mutatorcontainer'] = {
// Procedure container (for mutator dialog).
init: function() {
this.setColour(Blockly.PROCEDURE_CATEGORY_HUE);
// Let the theme determine the color.
// this.setColour(Blockly.PROCEDURE_CATEGORY_HUE);
this.setStyle('procedure_blocks');
this.appendDummyInput()
.appendField(Blockly.Msg.LANG_PROCEDURES_MUTATORCONTAINER_TITLE);
this.appendStatementInput('STACK');
Expand Down Expand Up @@ -574,7 +583,9 @@ Blockly.Blocks['procedures_mutatorarg'] = {
// + " and proc argumnets = [" + procArguments.join(',') + "]");
// return Blockly.LexicalVariable.renameParam.call(this,newName);
// }
this.setColour(Blockly.PROCEDURE_CATEGORY_HUE);
// Let the theme determine the color.
// this.setColour(Blockly.PROCEDURE_CATEGORY_HUE);
this.setStyle('procedure_blocks');
var editor = new Blockly.FieldTextInput('x',Blockly.LexicalVariable.renameParam);
// 2017 Blockly's text input change breaks our renaming behavior.
// The following is a version we've defined.
Expand Down Expand Up @@ -673,8 +684,10 @@ Blockly.Blocks['procedures_callnoreturn'] = {
category: 'Procedures', // Procedures are handled specially.
helpUrl: Blockly.Msg.LANG_PROCEDURES_CALLNORETURN_HELPURL,
init: function() {
this.setColour(Blockly.PROCEDURE_CATEGORY_HUE);
var procDb = this.getTopWorkspace().getProcedureDatabase();
// Let the theme determine the color.
// this.setColour(Blockly.PROCEDURE_CATEGORY_HUE);
this.setStyle('procedure_blocks');
var procDb = this.workspace.getTopWorkspace().getProcedureDatabase();
this.procNamesFxn = function() {
var items = procDb.getMenuItems(false);
return items.length > 0 ? items : ['',''];
Expand All @@ -699,7 +712,8 @@ Blockly.Blocks['procedures_callnoreturn'] = {
return this.getFieldValue('PROCNAME');
},
renameProcedure: function(oldName, newName) {
if (Blockly.Names.equals(oldName, this.getFieldValue('PROCNAME'))) {
if (!oldName ||
Blockly.Names.equals(oldName, this.getFieldValue('PROCNAME'))) {
this.setFieldValue(newName, 'PROCNAME');
}
},
Expand Down Expand Up @@ -842,7 +856,7 @@ Blockly.Blocks['procedures_callnoreturn'] = {
for (var x = 0; x < this.arguments_.length; x++) {
if (Blockly.Names.equals(oldName, this.arguments_[x])) {
this.arguments_[x] = newName;
this.getInput('ARG' + x).fieldRow[0].setText(newName);
this.getInput('ARG' + x).fieldRow[0].setValue(newName);
}
}
},
Expand Down Expand Up @@ -881,8 +895,10 @@ Blockly.Blocks['procedures_callreturn'] = {
category: 'Procedures', // Procedures are handled specially.
helpUrl: Blockly.Msg.LANG_PROCEDURES_CALLRETURN_HELPURL,
init: function() {
this.setColour(Blockly.PROCEDURE_CATEGORY_HUE);
var procDb = this.getTopWorkspace().getProcedureDatabase();
// Let the theme determine the color.
// this.setColour(Blockly.PROCEDURE_CATEGORY_HUE);
this.setStyle('procedure_blocks');
var procDb = this.workspace.getTopWorkspace().getProcedureDatabase();
this.procNamesFxn = function() {
var items = procDb.getMenuItems(true);
return items.length > 0 ? items : ['',''];
Expand Down
11 changes: 11 additions & 0 deletions block-lexical-variables/src/workspace_svg.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@ Blockly.WorkspaceSvg.prototype.getFlydown = function() {
return this.flydown_;
};

/**
* Obtain the {@link Blockly.ProcedureDatabase} associated with the workspace.
* @returns {!Blockly.ProcedureDatabase}
*/
Blockly.WorkspaceSvg.prototype.getProcedureDatabase = function() {
if (!this.procedureDb_) {
this.procedureDb_ = new Blockly.ProcedureDatabase(this);
}
return this.procedureDb_;
};

/**
* Get the topmost workspace in the workspace hierarchy.
* @returns {Blockly.WorkspaceSvg}
Expand Down

0 comments on commit ee06205

Please sign in to comment.