src/virtual-machine.js
/**
     * Emit an Blockly/scratch-blocks compatible XML representation
     * of the current editing target's blocks.
     */
Emit an Blockly/scratch-blocks compatible XML representation
of the current editing target's blocks.
Emit an Blockly / scratch - blocks compatible XML representation of the current editing target ' s blocks .
emitWorkspaceUpdate () {
        const stageVariables = this.runtime.getTargetForStage().variables;
        let messageIds = [];
        for (const varId in stageVariables) {
            if (stageVariables[varId].type === Variable.BROADCAST_MESSAGE_TYPE) {
                messageIds.push(varId);
            }
        }
        for (let i = 0; i < this.runtime.targets.length; i++) {
            const currTarget = this.runtime.targets[i];
            const currBlocks = currTarget.blocks._blocks;
            for (const blockId in currBlocks) {
                if (currBlocks[blockId].fields.BROADCAST_OPTION) {
                    const id = currBlocks[blockId].fields.BROADCAST_OPTION.id;
                    const index = messageIds.indexOf(id);
                    if (index !== -1) {
                        messageIds = messageIds.slice(0, index)
                            .concat(messageIds.slice(index + 1));
                    }
                }
            }
        }
        for (let i = 0; i < messageIds.length; i++) {
            const id = messageIds[i];
            delete this.runtime.getTargetForStage().variables[id];
        }
        const variableMap = Object.assign({},
            this.runtime.getTargetForStage().variables,
            this.editingTarget.variables
        );
        const variables = Object.keys(variableMap).map(k => variableMap[k]);
        const xmlString = `<xml xmlns="http://www.w3.org/1999/xhtml">
                            <variables>
                                ${variables.map(v => v.toXML()).join()}
                            </variables>
                            ${this.editingTarget.blocks.toXML()}
                        </xml>`;
        this.emit('workspaceUpdate', {xml: xmlString});
    }
emitWorkspaceUpdate ( ) { const stageVariables = this . runtime . getTargetForStage ( ) . variables ; let messageIds = [ ] ; for ( const varId in stageVariables ) { if ( stageVariables [ varId ] . type === Variable . BROADCAST_MESSAGE_TYPE ) { messageIds . push ( varId ) ; } } for ( let i = 0 ; i < this . runtime . targets . length ; i ++ ) { const currTarget = this . runtime . targets [ i ] ; const currBlocks = currTarget . blocks . _blocks ; for ( const blockId in currBlocks ) { if ( currBlocks [ blockId ] . fields . BROADCAST_OPTION ) { const id = currBlocks [ blockId ] . fields . BROADCAST_OPTION . id ; const index = messageIds . indexOf ( id ) ; if ( index !== - 1 ) { messageIds = messageIds . slice ( 0 , index ) . concat ( messageIds . slice ( index + 1 ) ) ; } } } } for ( let i = 0 ; i < messageIds . length ; i ++ ) { const id = messageIds [ i ] ; delete this . runtime . getTargetForStage ( ) . variables [ id ] ; } const variableMap = Object . assign ( { } , this . runtime . getTargetForStage ( ) . variables , this . editingTarget . variables ) ; const variables = Object . keys ( variableMap ) . map ( k => variableMap [ k ] ) ; const xmlString = ` ${ variables . map ( v => v . toXML ( ) ) . join ( ) } ${ this . editingTarget . blocks . toXML ( ) } ` ; this . emit ( 'workspaceUpdate' , { xml : xmlString } ) ; }
Emit an Blockly/scratch-blocks compatible XML representation
 of the current editing target's blocks.
Emit an Blockly / scratch - blocks compatible XML representation of the current editing target ' s blocks .
// Create a list of broadcast message Ids according to the stage variables // Go through all blocks on all targets, removing referenced // broadcast ids from the list. // Anything left in messageIds is not referenced by a block, so delete it.