chore: Use JSON objects for context menu callbackFactory (#7382)

* chore: use json object for context callbackFactory

* Add test file for context menu callback function.
This commit is contained in:
John Nesky
2023-09-08 16:16:59 -07:00
committed by GitHub
parent 0d2879be7d
commit 1b2e91246e
7 changed files with 119 additions and 52 deletions

View File

@@ -15,8 +15,6 @@ import type {
} from '../core/contextmenu_registry.js';
import * as Events from '../core/events/events.js';
import * as Extensions from '../core/extensions.js';
import * as Variables from '../core/variables.js';
import * as xmlUtils from '../core/utils/xml.js';
import {Msg} from '../core/msg.js';
import {
createBlockDefinitionsFromJsonArray,
@@ -272,15 +270,15 @@ const CUSTOM_CONTEXT_MENU_CREATE_VARIABLES_GET_MIXIN = {
const variable = varField.getVariable()!;
const varName = variable.name;
if (!this.isCollapsed() && varName !== null) {
const xmlField = Variables.generateVariableFieldDom(variable);
const xmlBlock = xmlUtils.createElement('block');
xmlBlock.setAttribute('type', 'variables_get');
xmlBlock.appendChild(xmlField);
const getVarBlockState = {
type: 'variables_get',
fields: {VAR: varField.saveState(true)},
};
options.push({
enabled: true,
text: Msg['VARIABLES_SET_CREATE_GET'].replace('%1', varName),
callback: ContextMenu.callbackFactory(this, xmlBlock),
callback: ContextMenu.callbackFactory(this, getVarBlockState),
});
}
},

View File

@@ -454,34 +454,30 @@ const PROCEDURE_DEF_COMMON = {
}
// Add option to create caller.
const name = this.getFieldValue('NAME');
const xmlMutation = xmlUtils.createElement('mutation');
xmlMutation.setAttribute('name', name);
for (let i = 0; i < this.arguments_.length; i++) {
const xmlArg = xmlUtils.createElement('arg');
xmlArg.setAttribute('name', this.arguments_[i]);
xmlMutation.appendChild(xmlArg);
}
const xmlBlock = xmlUtils.createElement('block');
xmlBlock.setAttribute('type', (this as AnyDuringMigration).callType_);
xmlBlock.appendChild(xmlMutation);
const callProcedureBlockState = {
type: (this as AnyDuringMigration).callType_,
extraState: {name: name, params: this.arguments_},
};
options.push({
enabled: true,
text: Msg['PROCEDURES_CREATE_DO'].replace('%1', name),
callback: ContextMenu.callbackFactory(this, xmlBlock),
callback: ContextMenu.callbackFactory(this, callProcedureBlockState),
});
// Add options to create getters for each parameter.
if (!this.isCollapsed()) {
for (let i = 0; i < this.argumentVarModels_.length; i++) {
const argVar = this.argumentVarModels_[i];
const argXmlField = Variables.generateVariableFieldDom(argVar);
const argXmlBlock = xmlUtils.createElement('block');
argXmlBlock.setAttribute('type', 'variables_get');
argXmlBlock.appendChild(argXmlField);
const getVarBlockState = {
type: 'variables_get',
fields: {
VAR: {name: argVar.name, id: argVar.getId(), type: argVar.type},
},
};
options.push({
enabled: true,
text: Msg['VARIABLES_SET_CREATE_GET'].replace('%1', argVar.name),
callback: ContextMenu.callbackFactory(this, argXmlBlock),
callback: ContextMenu.callbackFactory(this, getVarBlockState),
});
}
}

View File

@@ -9,7 +9,6 @@
import * as ContextMenu from '../core/contextmenu.js';
import * as Extensions from '../core/extensions.js';
import * as Variables from '../core/variables.js';
import * as xmlUtils from '../core/utils/xml.js';
import type {Block} from '../core/block.js';
import type {
ContextMenuOption,
@@ -102,18 +101,16 @@ const CUSTOM_CONTEXT_MENU_VARIABLE_GETTER_SETTER_MIXIN = {
contextMenuMsg = Msg['VARIABLES_SET_CREATE_GET'];
}
const name = this.getField('VAR')!.getText();
const xmlField = xmlUtils.createElement('field');
xmlField.setAttribute('name', 'VAR');
xmlField.appendChild(xmlUtils.createTextNode(name));
const xmlBlock = xmlUtils.createElement('block');
xmlBlock.setAttribute('type', oppositeType);
xmlBlock.appendChild(xmlField);
const varField = this.getField('VAR')!;
const newVarBlockState = {
type: oppositeType,
fields: {VAR: varField.saveState(true)},
};
options.push({
enabled: this.workspace.remainingCapacity() > 0,
text: contextMenuMsg.replace('%1', name),
callback: ContextMenu.callbackFactory(this, xmlBlock),
text: contextMenuMsg.replace('%1', varField.getText()),
callback: ContextMenu.callbackFactory(this, newVarBlockState),
});
// Getter blocks have the option to rename or delete that variable.
} else {

View File

@@ -9,7 +9,6 @@
import * as ContextMenu from '../core/contextmenu.js';
import * as Extensions from '../core/extensions.js';
import * as Variables from '../core/variables.js';
import * as xml from '../core/utils/xml.js';
import {Abstract as AbstractEvent} from '../core/events/events_abstract.js';
import type {Block} from '../core/block.js';
import type {
@@ -95,9 +94,6 @@ const CUSTOM_CONTEXT_MENU_VARIABLE_GETTER_SETTER_MIXIN = {
if (!this.isInFlyout) {
let oppositeType;
let contextMenuMsg;
const id = this.getFieldValue('VAR');
const variableModel = this.workspace.getVariableById(id);
const varType = variableModel!.type;
if (this.type === 'variables_get_dynamic') {
oppositeType = 'variables_set_dynamic';
contextMenuMsg = Msg['VARIABLES_GET_CREATE_SET'];
@@ -106,19 +102,16 @@ const CUSTOM_CONTEXT_MENU_VARIABLE_GETTER_SETTER_MIXIN = {
contextMenuMsg = Msg['VARIABLES_SET_CREATE_GET'];
}
const name = this.getField('VAR')!.getText();
const xmlField = xml.createElement('field');
xmlField.setAttribute('name', 'VAR');
xmlField.setAttribute('variabletype', varType);
xmlField.appendChild(xml.createTextNode(name));
const xmlBlock = xml.createElement('block');
xmlBlock.setAttribute('type', oppositeType);
xmlBlock.appendChild(xmlField);
const varField = this.getField('VAR')!;
const newVarBlockState = {
type: oppositeType,
fields: {VAR: varField.saveState(true)},
};
options.push({
enabled: this.workspace.remainingCapacity() > 0,
text: contextMenuMsg.replace('%1', name),
callback: ContextMenu.callbackFactory(this, xmlBlock),
text: contextMenuMsg.replace('%1', varField.getText()),
callback: ContextMenu.callbackFactory(this, newVarBlockState),
});
} else {
if (