@@ -168,6 +168,36 @@ class Blocks extends React.Component {
168168 Blocks . defaultOptions
169169 ) ;
170170 this . workspace = this . ScratchBlocks . inject ( this . blocks , workspaceConfig ) ;
171+
172+ if ( this . ScratchBlocks . Procedures &&
173+ this . ScratchBlocks . Procedures . setProcedureBlocksAcrossTargetsCallback ) {
174+ this . ScratchBlocks . Procedures . setProcedureBlocksAcrossTargetsCallback ( ( ) => {
175+ const runtime = this . props . vm && this . props . vm . runtime ;
176+ if ( ! runtime || ! runtime . targets ) {
177+ return [ ] ;
178+ }
179+
180+ const procedureBlocks = [ ] ;
181+ for ( let i = 0 ; i < runtime . targets . length ; i ++ ) {
182+ const target = runtime . targets [ i ] ;
183+ const blocks = target && target . blocks && target . blocks . _blocks ;
184+ if ( ! blocks ) continue ;
185+
186+ for ( const blockId in blocks ) {
187+ if ( ! Object . prototype . hasOwnProperty . call ( blocks , blockId ) ) continue ;
188+ const block = blocks [ blockId ] ;
189+ if ( ! block ) continue ;
190+ if (
191+ block . opcode === 'procedures_prototype' ||
192+ block . opcode === this . ScratchBlocks . PROCEDURES_CALL_BLOCK_TYPE
193+ ) {
194+ procedureBlocks . push ( block ) ;
195+ }
196+ }
197+ }
198+ return procedureBlocks ;
199+ } ) ;
200+ }
171201 AddonHooks . blocklyWorkspace = this . workspace ;
172202
173203 // Register buttons under new callback keys for creating variables,
@@ -298,6 +328,12 @@ class Blocks extends React.Component {
298328 componentWillUnmount ( ) {
299329 this . detachVM ( ) ;
300330 this . unmounted = true ;
331+
332+ if ( this . ScratchBlocks && this . ScratchBlocks . Procedures &&
333+ this . ScratchBlocks . Procedures . setProcedureBlocksAcrossTargetsCallback ) {
334+ this . ScratchBlocks . Procedures . setProcedureBlocksAcrossTargetsCallback ( ( ) => [ ] ) ;
335+ }
336+
301337 this . workspace . dispose ( ) ;
302338 clearTimeout ( this . toolboxUpdateTimeout ) ;
303339
@@ -515,6 +551,29 @@ class Blocks extends React.Component {
515551 }
516552 log . error ( error ) ;
517553 }
554+
555+ const allBlocks = this . workspace . getAllBlocks ( false ) ;
556+ for ( let i = 0 ; i < allBlocks . length ; i ++ ) {
557+ const block = allBlocks [ i ] ;
558+ if ( block . type === this . ScratchBlocks . PROCEDURES_CALL_BLOCK_TYPE && block . getProcCode ) {
559+ const procCode = block . getProcCode ( ) ;
560+ if ( procCode ) {
561+ // Get the latest global procedure mutation from the provider
562+ const globalMutations = this . ScratchBlocks . Procedures . allGlobalProcedureMutations ( this . workspace ) ;
563+ for ( let j = 0 ; j < globalMutations . length ; j ++ ) {
564+ const mutation = globalMutations [ j ] ;
565+ if ( this . ScratchBlocks . Names . equals ( mutation . getAttribute ( 'proccode' ) , procCode ) ) {
566+ // Found the matching procedure, update the call block with the latest mutation
567+ if ( block . domToMutation ) {
568+ block . domToMutation ( mutation ) ;
569+ }
570+ break ;
571+ }
572+ }
573+ }
574+ }
575+ }
576+
518577 this . workspace . addChangeListener ( this . props . vm . blockListener ) ;
519578
520579 if ( this . props . vm . editingTarget && this . props . workspaceMetrics . targets [ this . props . vm . editingTarget . id ] ) {
0 commit comments