|
const adapter = require('./adapter'); |
|
const mutationAdapter = require('./mutation-adapter'); |
|
const xmlEscape = require('../util/xml-escape'); |
|
const MonitorRecord = require('./monitor-record'); |
|
const Clone = require('../util/clone'); |
|
const {Map} = require('immutable'); |
|
const BlocksExecuteCache = require('./blocks-execute-cache'); |
|
const BlocksRuntimeCache = require('./blocks-runtime-cache'); |
|
const log = require('../util/log'); |
|
const Variable = require('./variable'); |
|
const getMonitorIdForBlockWithArgs = require('../util/get-monitor-id'); |
|
const StringUtil = require('../util/string-util'); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Blocks { |
|
constructor (runtime, optNoGlow) { |
|
this.runtime = runtime; |
|
|
|
|
|
|
|
|
|
|
|
|
|
this._blocks = {}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
this._scripts = []; |
|
|
|
|
|
|
|
|
|
|
|
|
|
Object.defineProperty(this, '_cache', {writable: true, enumerable: false}); |
|
this._cache = { |
|
|
|
|
|
|
|
|
|
inputs: {}, |
|
|
|
|
|
|
|
|
|
procedureParamNames: {}, |
|
|
|
|
|
|
|
|
|
procedureDefinitions: {}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
_executeCached: {}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
_monitored: null, |
|
|
|
|
|
|
|
|
|
|
|
scripts: {}, |
|
|
|
|
|
|
|
|
|
|
|
compiledScripts: {}, |
|
|
|
|
|
|
|
|
|
|
|
compiledProcedures: {}, |
|
|
|
|
|
|
|
|
|
proceduresPopulated: false |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.forceNoGlow = optNoGlow || false; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
getCachedCompileResult (blockId) { |
|
if (this._cache.compiledScripts.hasOwnProperty(blockId)) { |
|
return this._cache.compiledScripts[blockId]; |
|
} |
|
return null; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
cacheCompileResult (blockId, value) { |
|
this._cache.compiledScripts[blockId] = { |
|
success: true, |
|
value: value |
|
}; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
cacheCompileError (blockId, error) { |
|
this._cache.compiledScripts[blockId] = { |
|
success: false, |
|
value: error |
|
}; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
static get BRANCH_INPUT_PREFIX () { |
|
return 'SUBSTACK'; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
getBlock (blockId) { |
|
return this._blocks[blockId]; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
getScripts () { |
|
return this._scripts; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
getNextBlock (id) { |
|
const block = this._blocks[id]; |
|
return (typeof block === 'undefined') ? null : block.next; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
getBranch (id, branchNum) { |
|
const block = this._blocks[id]; |
|
if (typeof block === 'undefined') return null; |
|
if (!branchNum) branchNum = 1; |
|
|
|
let inputName = Blocks.BRANCH_INPUT_PREFIX; |
|
if (branchNum > 1) { |
|
inputName += branchNum; |
|
} |
|
|
|
|
|
const input = block.inputs[inputName]; |
|
return (typeof input === 'undefined') ? null : input.block; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
getOpcode (block) { |
|
return (typeof block === 'undefined') ? null : block.opcode; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
getFields (block) { |
|
return (typeof block === 'undefined') ? null : block.fields; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
getInputs (block) { |
|
if (typeof block === 'undefined') return null; |
|
let inputs = this._cache.inputs[block.id]; |
|
if (typeof inputs !== 'undefined') { |
|
return inputs; |
|
} |
|
|
|
inputs = {}; |
|
for (const input in block.inputs) { |
|
|
|
if (input.substring(0, Blocks.BRANCH_INPUT_PREFIX.length) !== |
|
Blocks.BRANCH_INPUT_PREFIX) { |
|
inputs[input] = block.inputs[input]; |
|
} |
|
} |
|
|
|
this._cache.inputs[block.id] = inputs; |
|
return inputs; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
getMutation (block) { |
|
return (typeof block === 'undefined') ? null : block.mutation; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
getTopLevelScript (id) { |
|
let block = this._blocks[id]; |
|
if (typeof block === 'undefined') return null; |
|
while (block.parent !== null) { |
|
block = this._blocks[block.parent]; |
|
} |
|
return block.id; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
getProcedureDefinition (name) { |
|
const blockID = this._cache.procedureDefinitions[name]; |
|
if (typeof blockID !== 'undefined') { |
|
return blockID; |
|
} |
|
|
|
for (const id in this._blocks) { |
|
if (!this._blocks.hasOwnProperty(id)) continue; |
|
const block = this._blocks[id]; |
|
if (block.opcode === 'procedures_definition' || block.opcode === 'procedures_definition_return') { |
|
|
|
const internal = this._getCustomBlockInternal(block); |
|
if (internal && internal.mutation.proccode === name) { |
|
this._cache.procedureDefinitions[name] = id; |
|
return id; |
|
} |
|
} |
|
} |
|
|
|
this._cache.procedureDefinitions[name] = null; |
|
return null; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
getProcedureParamNamesAndIds (name) { |
|
return this.getProcedureParamNamesIdsAndDefaults(name).slice(0, 2); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
getProcedureParamNamesIdsAndDefaults (name) { |
|
const cachedNames = this._cache.procedureParamNames[name]; |
|
if (typeof cachedNames !== 'undefined') { |
|
return cachedNames; |
|
} |
|
|
|
for (const id in this._blocks) { |
|
if (!this._blocks.hasOwnProperty(id)) continue; |
|
const block = this._blocks[id]; |
|
if (block.opcode === 'procedures_prototype' && |
|
block.mutation.proccode === name) { |
|
|
|
const names = JSON.parse(block.mutation.argumentnames); |
|
const ids = JSON.parse(block.mutation.argumentids); |
|
const defaults = JSON.parse(block.mutation.argumentdefaults); |
|
|
|
this._cache.procedureParamNames[name] = [names, ids, defaults]; |
|
return this._cache.procedureParamNames[name]; |
|
} |
|
} |
|
|
|
const addonBlock = this.runtime.getAddonBlock(name); |
|
if (addonBlock) { |
|
this._cache.procedureParamNames[name] = addonBlock.namesIdsDefaults; |
|
return addonBlock.namesIdsDefaults; |
|
} |
|
|
|
this._cache.procedureParamNames[name] = null; |
|
return null; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
populateProcedureCache () { |
|
if (this._cache.proceduresPopulated) { |
|
return; |
|
} |
|
for (const id in this._blocks) { |
|
if (!this._blocks.hasOwnProperty(id)) continue; |
|
const block = this._blocks[id]; |
|
|
|
if (block.opcode === 'procedures_prototype') { |
|
const name = block.mutation.proccode; |
|
if (!this._cache.procedureParamNames[name]) { |
|
const names = JSON.parse(block.mutation.argumentnames); |
|
const ids = JSON.parse(block.mutation.argumentids); |
|
const defaults = JSON.parse(block.mutation.argumentdefaults); |
|
this._cache.procedureParamNames[name] = [names, ids, defaults]; |
|
} |
|
continue; |
|
} |
|
|
|
if (block.opcode === 'procedures_definition' || block.opcode === 'procedures_definition_return') { |
|
const internal = this._getCustomBlockInternal(block); |
|
if (internal) { |
|
const name = internal.mutation.proccode; |
|
if (!this._cache.procedureDefinitions[name]) { |
|
this._cache.procedureDefinitions[name] = id; |
|
} |
|
continue; |
|
} |
|
} |
|
} |
|
this._cache.proceduresPopulated = true; |
|
} |
|
|
|
duplicate () { |
|
const newBlocks = new Blocks(this.runtime, this.forceNoGlow); |
|
newBlocks._blocks = Clone.simple(this._blocks); |
|
newBlocks._scripts = Clone.simple(this._scripts); |
|
return newBlocks; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
blocklyListen (e) { |
|
|
|
if (typeof e !== 'object') return; |
|
if (typeof e.blockId !== 'string' && typeof e.varId !== 'string' && |
|
typeof e.commentId !== 'string') { |
|
return; |
|
} |
|
const stage = this.runtime.getTargetForStage(); |
|
const editingTarget = this.runtime.getEditingTarget(); |
|
|
|
|
|
if (e.element === 'stackclick') { |
|
this.runtime.toggleScript(e.blockId, {stackClick: true}); |
|
return; |
|
} |
|
|
|
|
|
switch (e.type) { |
|
case 'create': { |
|
const newBlocks = adapter(e); |
|
|
|
for (let i = 0; i < newBlocks.length; i++) { |
|
this.createBlock(newBlocks[i]); |
|
} |
|
break; |
|
} |
|
case 'change': |
|
this.changeBlock({ |
|
id: e.blockId, |
|
element: e.element, |
|
name: e.name, |
|
value: e.newValue |
|
}); |
|
break; |
|
case 'move': |
|
this.moveBlock({ |
|
id: e.blockId, |
|
oldParent: e.oldParentId, |
|
oldInput: e.oldInputName, |
|
newParent: e.newParentId, |
|
newInput: e.newInputName, |
|
newCoordinate: e.newCoordinate |
|
}); |
|
break; |
|
case 'dragOutside': |
|
this.runtime.emitBlockDragUpdate(e.isOutside); |
|
break; |
|
case 'endDrag': |
|
this.runtime.emitBlockDragUpdate(false ); |
|
|
|
|
|
if (e.isOutside) { |
|
const newBlocks = adapter(e); |
|
this.runtime.emitBlockEndDrag(newBlocks, e.blockId); |
|
} |
|
break; |
|
case 'delete': |
|
|
|
|
|
if (!this._blocks.hasOwnProperty(e.blockId) || |
|
this._blocks[e.blockId].shadow) { |
|
return; |
|
} |
|
|
|
if (this._blocks[e.blockId].topLevel) { |
|
this.runtime.quietGlow(e.blockId); |
|
} |
|
this.deleteBlock(e.blockId); |
|
break; |
|
case 'var_create': |
|
this.resetCache(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (e.isLocal && editingTarget && !editingTarget.isStage && !e.isCloud) { |
|
if (!editingTarget.lookupVariableById(e.varId)) { |
|
editingTarget.createVariable(e.varId, e.varName, e.varType); |
|
this.runtime.emit('variableCreate', e.varType, e.varId, e.varName, e.isCloud); |
|
this.emitProjectChanged(); |
|
} |
|
} else { |
|
if (stage.lookupVariableById(e.varId)) { |
|
|
|
return; |
|
} |
|
|
|
const allTargets = this.runtime.targets.filter(t => t.isOriginal); |
|
for (const target of allTargets) { |
|
if (target.lookupVariableByNameAndType(e.varName, e.varType, true)) { |
|
return; |
|
} |
|
} |
|
stage.createVariable(e.varId, e.varName, e.varType, e.isCloud); |
|
this.runtime.emit('variableCreate', e.varType, e.varId, e.varName, e.isCloud); |
|
this.emitProjectChanged(); |
|
} |
|
break; |
|
case 'var_rename': |
|
if (editingTarget && editingTarget.variables.hasOwnProperty(e.varId)) { |
|
|
|
editingTarget.renameVariable(e.varId, e.newName); |
|
|
|
|
|
editingTarget.blocks.updateBlocksAfterVarRename(e.varId, e.newName); |
|
} else { |
|
|
|
stage.renameVariable(e.varId, e.newName); |
|
|
|
const targets = this.runtime.targets; |
|
for (let i = 0; i < targets.length; i++) { |
|
const currTarget = targets[i]; |
|
currTarget.blocks.updateBlocksAfterVarRename(e.varId, e.newName); |
|
} |
|
} |
|
this.runtime.emit('variableChange', e.varType, e.varId, e.newName, e.oldName); |
|
this.emitProjectChanged(); |
|
break; |
|
case 'var_delete': { |
|
this.resetCache(); |
|
const target = (editingTarget && editingTarget.variables.hasOwnProperty(e.varId)) ? |
|
editingTarget : stage; |
|
this.runtime.emit('variableDelete', e.varType, e.varId); |
|
target.deleteVariable(e.varId); |
|
this.emitProjectChanged(); |
|
break; |
|
} |
|
case 'comment_create': |
|
this.resetCache(); |
|
if (this.runtime.getEditingTarget()) { |
|
const currTarget = this.runtime.getEditingTarget(); |
|
currTarget.createComment(e.commentId, e.blockId, e.text, |
|
e.xy.x, e.xy.y, e.width, e.height, e.minimized); |
|
|
|
if (currTarget.comments[e.commentId].x === null && |
|
currTarget.comments[e.commentId].y === null) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
currTarget.comments[e.commentId].x = e.xy.x; |
|
currTarget.comments[e.commentId].y = e.xy.y; |
|
} |
|
} |
|
this.emitProjectChanged(); |
|
break; |
|
case 'comment_change': |
|
this.resetCache(); |
|
if (this.runtime.getEditingTarget()) { |
|
const currTarget = this.runtime.getEditingTarget(); |
|
if (!currTarget.comments.hasOwnProperty(e.commentId)) { |
|
log.warn(`Cannot change comment with id ${e.commentId} because it does not exist.`); |
|
return; |
|
} |
|
const comment = currTarget.comments[e.commentId]; |
|
const change = e.newContents_; |
|
if (change.hasOwnProperty('minimized')) { |
|
comment.minimized = change.minimized; |
|
} |
|
if (change.hasOwnProperty('width') && change.hasOwnProperty('height')){ |
|
comment.width = change.width; |
|
comment.height = change.height; |
|
} |
|
if (change.hasOwnProperty('text')) { |
|
comment.text = change.text; |
|
} |
|
this.emitProjectChanged(); |
|
} |
|
break; |
|
case 'comment_move': |
|
if (this.runtime.getEditingTarget()) { |
|
const currTarget = this.runtime.getEditingTarget(); |
|
if (currTarget && !currTarget.comments.hasOwnProperty(e.commentId)) { |
|
log.warn(`Cannot change comment with id ${e.commentId} because it does not exist.`); |
|
return; |
|
} |
|
const comment = currTarget.comments[e.commentId]; |
|
const newCoord = e.newCoordinate_; |
|
comment.x = newCoord.x; |
|
comment.y = newCoord.y; |
|
|
|
this.emitProjectChanged(); |
|
} |
|
break; |
|
case 'comment_delete': |
|
this.resetCache(); |
|
if (this.runtime.getEditingTarget()) { |
|
const currTarget = this.runtime.getEditingTarget(); |
|
if (!currTarget.comments.hasOwnProperty(e.commentId)) { |
|
|
|
|
|
|
|
|
|
return; |
|
} |
|
delete currTarget.comments[e.commentId]; |
|
if (e.blockId) { |
|
const block = currTarget.blocks.getBlock(e.blockId); |
|
if (!block) { |
|
log.warn(`Could not find block referenced by comment with id: ${e.commentId}`); |
|
return; |
|
} |
|
delete block.comment; |
|
} |
|
|
|
this.emitProjectChanged(); |
|
} |
|
break; |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
resetCache () { |
|
this._cache.inputs = {}; |
|
this._cache.procedureParamNames = {}; |
|
this._cache.procedureDefinitions = {}; |
|
this._cache._executeCached = {}; |
|
this._cache._monitored = null; |
|
this._cache.scripts = {}; |
|
this._cache.compiledScripts = {}; |
|
this._cache.compiledProcedures = {}; |
|
this._cache.proceduresPopulated = false; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
emitProjectChanged () { |
|
if (!this.forceNoGlow) { |
|
this.runtime.emitProjectChanged(); |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
createBlock (block) { |
|
|
|
|
|
if (this._blocks.hasOwnProperty(block.id)) { |
|
return; |
|
} |
|
|
|
this._blocks[block.id] = block; |
|
|
|
|
|
|
|
if (block.topLevel) { |
|
this._addScript(block.id); |
|
} |
|
|
|
this.resetCache(); |
|
|
|
|
|
|
|
this.emitProjectChanged(); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
changeBlock (args) { |
|
|
|
if (['field', 'mutation', 'checkbox'].indexOf(args.element) === -1) return; |
|
let block = this._blocks[args.id]; |
|
if (typeof block === 'undefined') return; |
|
switch (args.element) { |
|
case 'field': |
|
|
|
if (!block.fields[args.name]) return; |
|
const field = block.fields[args.name]; |
|
if (typeof field.variableType === 'string') { |
|
|
|
const variable = this.runtime.getEditingTarget().lookupVariableById(args.value); |
|
if (variable) { |
|
block.fields[args.name].value = variable.name; |
|
block.fields[args.name].id = args.value; |
|
} |
|
} else { |
|
|
|
block.fields[args.name].value = args.value; |
|
|
|
|
|
|
|
|
|
if (block.opcode === 'sensing_of_object_menu') { |
|
if (block.fields.OBJECT.value === '_stage_') { |
|
this._blocks[block.parent].fields.PROPERTY.value = 'backdrop #'; |
|
} else { |
|
this._blocks[block.parent].fields.PROPERTY.value = 'x position'; |
|
} |
|
this.runtime.requestBlocksUpdate(); |
|
} |
|
|
|
const flyoutBlock = block.shadow && block.parent ? this._blocks[block.parent] : block; |
|
if (flyoutBlock.isMonitored) { |
|
this.runtime.requestUpdateMonitor(Map({ |
|
id: flyoutBlock.id, |
|
params: this._getBlockParams(flyoutBlock) |
|
})); |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
if (typeof ScratchBlocks === 'object') { |
|
|
|
const monitorState = this.runtime.getMonitorState(); |
|
const shouldCheck = ( |
|
monitorState.get(`${args.id}_${args.value}`) !== undefined || |
|
monitorState.get(`${args.id}_${args.value.toLowerCase()}`) !== undefined |
|
); |
|
|
|
const workspace = ScratchBlocks.mainWorkspace; |
|
const flyout = workspace.isFlyout ? workspace : workspace.getFlyout(); |
|
const checkbox = flyout.checkboxes_[args.id]; |
|
if (checkbox) { |
|
checkbox.clicked = shouldCheck; |
|
if (shouldCheck) { |
|
ScratchBlocks.utils.addClass(checkbox.svgRoot, 'checked'); |
|
} else { |
|
ScratchBlocks.utils.removeClass(checkbox.svgRoot, 'checked'); |
|
} |
|
} |
|
} |
|
break; |
|
case 'mutation': |
|
block.mutation = mutationAdapter(args.value); |
|
break; |
|
case 'checkbox': { |
|
|
|
|
|
|
|
if (block.fields && Object.keys(block.fields).length > 0 && |
|
block.opcode !== 'data_variable' && block.opcode !== 'data_listcontents') { |
|
|
|
|
|
|
|
const newId = getMonitorIdForBlockWithArgs(block.id, block.fields); |
|
|
|
|
|
|
|
|
|
let newBlock = this.runtime.monitorBlocks.getBlock(newId); |
|
if (!newBlock) { |
|
newBlock = JSON.parse(JSON.stringify(block)); |
|
newBlock.id = newId; |
|
this.runtime.monitorBlocks.createBlock(newBlock); |
|
} |
|
|
|
block = newBlock; |
|
} |
|
|
|
const wasMonitored = block.isMonitored; |
|
block.isMonitored = args.value; |
|
|
|
|
|
let isSpriteLocalVariable = false; |
|
if (block.opcode === 'data_variable') { |
|
isSpriteLocalVariable = !(this.runtime.getTargetForStage().variables[block.fields.VARIABLE.id]); |
|
} else if (block.opcode === 'data_listcontents') { |
|
isSpriteLocalVariable = !(this.runtime.getTargetForStage().variables[block.fields.LIST.id]); |
|
} else { |
|
isSpriteLocalVariable = Object.values(block.fields).some(field => { |
|
if (field.variableType === undefined) return false; |
|
else return ("id" in field) && !(this.runtime.getTargetForStage().variables[field.id]); |
|
}); |
|
} |
|
|
|
|
|
|
|
var extension_sprite_specific = ((info) => { |
|
if (info == undefined) return false; |
|
const block_info = info.blocks.find(_block => { |
|
return _block.info.opcode === StringUtil.splitFirst(block.opcode, "_")[1]; |
|
}); |
|
return block_info?.info?.isSpriteSpecific ?? false; |
|
})(vm.runtime._blockInfo.find(a => a.id === StringUtil.splitFirst(block.opcode, "_")[0])); |
|
|
|
|
|
const isSpriteSpecific = isSpriteLocalVariable || |
|
(this.runtime.monitorBlockInfo.hasOwnProperty(block.opcode) && |
|
this.runtime.monitorBlockInfo[block.opcode]?.isSpriteSpecific) || |
|
extension_sprite_specific; |
|
|
|
if (isSpriteSpecific) { |
|
|
|
|
|
|
|
block.targetId = block.targetId || this.runtime.getEditingTarget().id; |
|
} else { |
|
block.targetId = null; |
|
} |
|
|
|
if (wasMonitored && !block.isMonitored) { |
|
this.runtime.requestHideMonitor(block.id); |
|
} else if (!wasMonitored && block.isMonitored) { |
|
|
|
if (!this.runtime.requestShowMonitor(block.id)) { |
|
this.runtime.requestAddMonitor(MonitorRecord({ |
|
id: block.id, |
|
targetId: block.targetId, |
|
spriteName: block.targetId ? this.runtime.getTargetById(block.targetId).getName() : null, |
|
opcode: block.opcode, |
|
params: this._getBlockParams(block), |
|
|
|
value: '', |
|
mode: block.opcode === 'data_listcontents' ? 'list' : 'default', |
|
variableType: Object.values(block.fields)[0]?.variableType, |
|
variableId: Object.values(block.fields)[0]?.id |
|
})); |
|
} |
|
} |
|
break; |
|
} |
|
} |
|
|
|
this.emitProjectChanged(); |
|
|
|
this.resetCache(); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
moveBlock (e) { |
|
if (!this._blocks.hasOwnProperty(e.id)) { |
|
return; |
|
} |
|
|
|
const block = this._blocks[e.id]; |
|
|
|
|
|
|
|
let didChange = false; |
|
|
|
|
|
if (e.newCoordinate) { |
|
|
|
didChange = (block.x !== e.newCoordinate.x) || (block.y !== e.newCoordinate.y); |
|
|
|
block.x = e.newCoordinate.x; |
|
block.y = e.newCoordinate.y; |
|
} |
|
|
|
|
|
if (typeof e.oldParent !== 'undefined') { |
|
const oldParent = this._blocks[e.oldParent]; |
|
if (typeof e.oldInput !== 'undefined' && |
|
oldParent.inputs[e.oldInput].block === e.id) { |
|
|
|
oldParent.inputs[e.oldInput].block = null; |
|
} else if (oldParent.next === e.id) { |
|
|
|
oldParent.next = null; |
|
} |
|
this._blocks[e.id].parent = null; |
|
didChange = true; |
|
} |
|
|
|
|
|
if (typeof e.newParent === 'undefined') { |
|
this._addScript(e.id); |
|
} else { |
|
|
|
this._deleteScript(e.id); |
|
|
|
if (typeof e.newInput === 'undefined') { |
|
|
|
this._blocks[e.newParent].next = e.id; |
|
} else { |
|
|
|
|
|
let oldShadow = null; |
|
if (this._blocks[e.newParent].inputs.hasOwnProperty(e.newInput)) { |
|
oldShadow = this._blocks[e.newParent].inputs[e.newInput].shadow; |
|
} |
|
|
|
|
|
|
|
|
|
if (this._blocks[e.id].shadow) oldShadow = e.id; |
|
|
|
this._blocks[e.newParent].inputs[e.newInput] = { |
|
name: e.newInput, |
|
block: e.id, |
|
shadow: oldShadow |
|
}; |
|
} |
|
this._blocks[e.id].parent = e.newParent; |
|
didChange = true; |
|
} |
|
this.resetCache(); |
|
|
|
if (didChange) this.emitProjectChanged(); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
runAllMonitored (runtime) { |
|
if (this._cache._monitored === null) { |
|
this._cache._monitored = Object.keys(this._blocks) |
|
.filter(blockId => this.getBlock(blockId).isMonitored) |
|
.map(blockId => { |
|
const targetId = this.getBlock(blockId).targetId; |
|
return { |
|
blockId, |
|
target: targetId ? runtime.getTargetById(targetId) : null |
|
}; |
|
}); |
|
} |
|
|
|
const monitored = this._cache._monitored; |
|
for (let i = 0; i < monitored.length; i++) { |
|
const {blockId, target} = monitored[i]; |
|
runtime.addMonitorScript(blockId, target); |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
deleteBlock (blockId, preserveStack) { |
|
|
|
|
|
|
|
const block = this._blocks[blockId]; |
|
if (!block) { |
|
|
|
return; |
|
} |
|
|
|
|
|
if (block.next !== null && !preserveStack) { |
|
this.deleteBlock(block.next); |
|
} |
|
|
|
if (preserveStack) { |
|
const parent = this._blocks[block.parent]; |
|
const next = this._blocks[block.next]; |
|
const input = parent?.inputs |
|
? [...Object.entries(parent.inputs)] |
|
.find(ent => ent[1].block === blockId)?.[1] |
|
: null; |
|
if (parent && !input) parent.next = block.next; |
|
if (next) next.parent = block.parent; |
|
if (next && input) input.block = block.next; |
|
} |
|
|
|
|
|
for (const input in block.inputs) { |
|
|
|
if (block.inputs[input].block !== null) { |
|
this.deleteBlock(block.inputs[input].block); |
|
} |
|
|
|
if (block.inputs[input].shadow !== null && |
|
block.inputs[input].shadow !== block.inputs[input].block) { |
|
this.deleteBlock(block.inputs[input].shadow); |
|
} |
|
} |
|
|
|
if (!preserveStack) { |
|
|
|
this._deleteScript(blockId); |
|
} |
|
const i = this._scripts.indexOf(blockId); |
|
if (preserveStack && i > -1) { |
|
const next = this._blocks[block.next]; |
|
if (next) { |
|
this._scripts.push(next.id); |
|
next.topLevel = true; |
|
next.x = block.x; |
|
next.y = block.y; |
|
} |
|
this._scripts.splice(i, 1); |
|
} |
|
|
|
|
|
delete this._blocks[blockId]; |
|
|
|
this.resetCache(); |
|
this.emitProjectChanged(); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
getAllVariableAndListReferences (optBlocks, optIncludeBroadcast) { |
|
const blocks = optBlocks ? optBlocks : this._blocks; |
|
const allReferences = Object.create(null); |
|
for (const blockId in blocks) { |
|
let varOrListField = null; |
|
let varType = null; |
|
if (blocks[blockId].fields.VARIABLE) { |
|
varOrListField = blocks[blockId].fields.VARIABLE; |
|
varType = Variable.SCALAR_TYPE; |
|
} else if (blocks[blockId].fields.LIST) { |
|
varOrListField = blocks[blockId].fields.LIST; |
|
varType = Variable.LIST_TYPE; |
|
} else if (optIncludeBroadcast && blocks[blockId].fields.BROADCAST_OPTION) { |
|
varOrListField = blocks[blockId].fields.BROADCAST_OPTION; |
|
varType = Variable.BROADCAST_MESSAGE_TYPE; |
|
} |
|
if (varOrListField) { |
|
const currVarId = varOrListField.id; |
|
if (allReferences[currVarId]) { |
|
allReferences[currVarId].push({ |
|
referencingField: varOrListField, |
|
type: varType |
|
}); |
|
} else { |
|
allReferences[currVarId] = [{ |
|
referencingField: varOrListField, |
|
type: varType |
|
}]; |
|
} |
|
} |
|
} |
|
return allReferences; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
updateBlocksAfterVarRename (varId, newName) { |
|
const blocks = this._blocks; |
|
for (const blockId in blocks) { |
|
let varOrListField = null; |
|
if (blocks[blockId].fields.VARIABLE) { |
|
varOrListField = blocks[blockId].fields.VARIABLE; |
|
} else if (blocks[blockId].fields.LIST) { |
|
varOrListField = blocks[blockId].fields.LIST; |
|
} |
|
if (varOrListField) { |
|
const currFieldId = varOrListField.id; |
|
if (varId === currFieldId) { |
|
varOrListField.value = newName; |
|
} |
|
} |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
updateTargetSpecificBlocks (isStage) { |
|
const blocks = this._blocks; |
|
for (const blockId in blocks) { |
|
if (isStage && blocks[blockId].opcode === 'event_whenthisspriteclicked') { |
|
blocks[blockId].opcode = 'event_whenstageclicked'; |
|
} else if (!isStage && blocks[blockId].opcode === 'event_whenstageclicked') { |
|
blocks[blockId].opcode = 'event_whenthisspriteclicked'; |
|
} |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
updateAssetName (oldName, newName, assetType) { |
|
let getAssetField; |
|
if (assetType === 'costume') { |
|
getAssetField = this._getCostumeField.bind(this); |
|
} else if (assetType === 'sound') { |
|
getAssetField = this._getSoundField.bind(this); |
|
} else if (assetType === 'backdrop') { |
|
getAssetField = this._getBackdropField.bind(this); |
|
} else if (assetType === 'sprite') { |
|
getAssetField = this._getSpriteField.bind(this); |
|
} else { |
|
return; |
|
} |
|
const blocks = this._blocks; |
|
for (const blockId in blocks) { |
|
const assetField = getAssetField(blockId); |
|
if (assetField && assetField.value === oldName) { |
|
assetField.value = newName; |
|
} |
|
} |
|
this.resetCache(); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
updateSensingOfReference (oldName, newName, targetName) { |
|
const blocks = this._blocks; |
|
let blockUpdated = false; |
|
for (const blockId in blocks) { |
|
const block = blocks[blockId]; |
|
if (block.opcode === 'sensing_of' && |
|
block.fields.PROPERTY.value === oldName && |
|
|
|
block.inputs.OBJECT.block === block.inputs.OBJECT.shadow) { |
|
const inputBlock = this.getBlock(block.inputs.OBJECT.block); |
|
if (inputBlock.fields.OBJECT.value === targetName) { |
|
block.fields.PROPERTY.value = newName; |
|
blockUpdated = true; |
|
} |
|
} |
|
} |
|
if (blockUpdated) this.resetCache(); |
|
return blockUpdated; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_getCostumeField (blockId) { |
|
const block = this.getBlock(blockId); |
|
if (block && block.fields.hasOwnProperty('COSTUME')) { |
|
return block.fields.COSTUME; |
|
} |
|
return null; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_getSoundField (blockId) { |
|
const block = this.getBlock(blockId); |
|
if (block && block.fields.hasOwnProperty('SOUND_MENU')) { |
|
return block.fields.SOUND_MENU; |
|
} |
|
return null; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_getBackdropField (blockId) { |
|
const block = this.getBlock(blockId); |
|
if (block && block.fields.hasOwnProperty('BACKDROP')) { |
|
return block.fields.BACKDROP; |
|
} |
|
return null; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_getSpriteField (blockId) { |
|
const block = this.getBlock(blockId); |
|
if (!block) { |
|
return null; |
|
} |
|
const spriteMenuNames = ['TOWARDS', 'TO', 'OBJECT', 'VIDEOONMENU2', |
|
'DISTANCETOMENU', 'TOUCHINGOBJECTMENU', 'CLONE_OPTION']; |
|
for (let i = 0; i < spriteMenuNames.length; i++) { |
|
const menuName = spriteMenuNames[i]; |
|
if (block.fields.hasOwnProperty(menuName)) { |
|
return block.fields[menuName]; |
|
} |
|
} |
|
return null; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
toXML (comments) { |
|
return this._scripts.map(script => this.blockToXML(script, comments)).join(); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
blockToXML (blockId, comments) { |
|
const block = this._blocks[blockId]; |
|
|
|
|
|
|
|
if (!block) return; |
|
|
|
const tagName = (block.shadow) ? 'shadow' : 'block'; |
|
let xmlString = |
|
`<${tagName} |
|
id="${xmlEscape(block.id)}" |
|
type="${xmlEscape(block.opcode)}" |
|
${block.topLevel ? `x="${block.x}" y="${block.y}"` : ''} |
|
>`; |
|
const commentId = block.comment; |
|
if (commentId) { |
|
if (comments) { |
|
if (comments.hasOwnProperty(commentId)) { |
|
xmlString += comments[commentId].toXML(); |
|
} else { |
|
log.warn(`Could not find comment with id: ${commentId} in provided comment descriptions.`); |
|
} |
|
} else { |
|
log.warn(`Cannot serialize comment with id: ${commentId}; no comment descriptions provided.`); |
|
} |
|
} |
|
|
|
if (block.mutation) { |
|
xmlString += this.mutationToXML(block.mutation); |
|
} |
|
|
|
for (const input in block.inputs) { |
|
if (!block.inputs.hasOwnProperty(input)) continue; |
|
const blockInput = block.inputs[input]; |
|
|
|
if (blockInput.block || blockInput.shadow) { |
|
xmlString += `<value name="${xmlEscape(blockInput.name)}">`; |
|
if (blockInput.block) { |
|
xmlString += this.blockToXML(blockInput.block, comments); |
|
} |
|
if (blockInput.shadow && blockInput.shadow !== blockInput.block) { |
|
|
|
xmlString += this.blockToXML(blockInput.shadow, comments); |
|
} |
|
xmlString += '</value>'; |
|
} |
|
} |
|
|
|
for (const field in block.fields) { |
|
if (!block.fields.hasOwnProperty(field)) continue; |
|
const blockField = block.fields[field]; |
|
xmlString += `<field name="${xmlEscape(blockField.name)}"`; |
|
const fieldId = blockField.id; |
|
if (fieldId) { |
|
xmlString += ` id="${xmlEscape(fieldId)}"`; |
|
} |
|
const varType = blockField.variableType; |
|
if (typeof varType === 'string') { |
|
xmlString += ` variabletype="${xmlEscape(varType)}"`; |
|
} |
|
let value = blockField.value; |
|
if (typeof value === 'string') { |
|
value = xmlEscape(blockField.value); |
|
} |
|
xmlString += `>${value}</field>`; |
|
} |
|
|
|
if (block.next) { |
|
xmlString += `<next>${this.blockToXML(block.next, comments)}</next>`; |
|
} |
|
xmlString += `</${tagName}>`; |
|
return xmlString; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
mutationToXML (mutation) { |
|
if (typeof mutation === 'string') return xmlEscape(mutation) |
|
let mutationString = `<${mutation.tagName}`; |
|
for (const prop in mutation) { |
|
if (prop === 'children' || prop === 'tagName') continue; |
|
let mutationValue = (typeof mutation[prop] === 'string') ? |
|
xmlEscape(mutation[prop]) : mutation[prop]; |
|
|
|
|
|
if (prop === 'blockInfo') { |
|
mutationValue = xmlEscape(JSON.stringify(mutation[prop])); |
|
} |
|
|
|
mutationString += ` ${prop}="${mutationValue}"`; |
|
} |
|
mutationString += '>'; |
|
for (let i = 0; i < mutation.children.length; i++) { |
|
mutationString += this.mutationToXML(mutation.children[i]); |
|
} |
|
mutationString += `</${mutation.tagName}>`; |
|
return mutationString; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_getBlockParams (block) { |
|
const params = {}; |
|
for (const key in block.fields) { |
|
params[key] = block.fields[key].value; |
|
} |
|
for (const inputKey in block.inputs) { |
|
const inputBlock = this._blocks[block.inputs[inputKey].block]; |
|
for (const key in inputBlock.fields) { |
|
params[key] = inputBlock.fields[key].value; |
|
} |
|
} |
|
return params; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
_getCustomBlockInternal (defineBlock) { |
|
if (defineBlock.inputs && defineBlock.inputs.custom_block) { |
|
return this._blocks[defineBlock.inputs.custom_block.block]; |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
_addScript (topBlockId) { |
|
const i = this._scripts.indexOf(topBlockId); |
|
if (i > -1) return; |
|
this._scripts.push(topBlockId); |
|
|
|
this._blocks[topBlockId].topLevel = true; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
_deleteScript (topBlockId) { |
|
const i = this._scripts.indexOf(topBlockId); |
|
if (i > -1) this._scripts.splice(i, 1); |
|
|
|
if (this._blocks[topBlockId]) this._blocks[topBlockId].topLevel = false; |
|
} |
|
XMLToBlock = adapter; |
|
XMLToMutation = mutationAdapter; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
BlocksExecuteCache.getCached = function (blocks, blockId, CacheType) { |
|
let cached = blocks._cache._executeCached[blockId]; |
|
if (typeof cached !== 'undefined') { |
|
return cached; |
|
} |
|
|
|
const block = blocks.getBlock(blockId); |
|
if (typeof block === 'undefined') return null; |
|
|
|
if (typeof CacheType === 'undefined') { |
|
cached = { |
|
id: blockId, |
|
opcode: blocks.getOpcode(block), |
|
fields: blocks.getFields(block), |
|
inputs: blocks.getInputs(block), |
|
mutation: blocks.getMutation(block) |
|
}; |
|
} else { |
|
cached = new CacheType(blocks, { |
|
id: blockId, |
|
opcode: blocks.getOpcode(block), |
|
fields: blocks.getFields(block), |
|
inputs: blocks.getInputs(block), |
|
mutation: blocks.getMutation(block) |
|
}); |
|
} |
|
|
|
blocks._cache._executeCached[blockId] = cached; |
|
return cached; |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
const RuntimeScriptCache = BlocksRuntimeCache._RuntimeScriptCache; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
BlocksRuntimeCache.getScripts = function (blocks, opcode) { |
|
let scripts = blocks._cache.scripts[opcode]; |
|
if (!scripts) { |
|
scripts = blocks._cache.scripts[opcode] = []; |
|
|
|
const allScripts = blocks._scripts; |
|
for (let i = 0; i < allScripts.length; i++) { |
|
const topBlockId = allScripts[i]; |
|
const block = blocks.getBlock(topBlockId); |
|
if (block.opcode === opcode) { |
|
scripts.push(new RuntimeScriptCache(blocks, topBlockId)); |
|
} |
|
} |
|
} |
|
return scripts; |
|
}; |
|
|
|
module.exports = Blocks; |
|
|