MCPcopy
hub / github.com/RaspberryPiFoundation/blockly / domToBlockInternal

Function domToBlockInternal

packages/blockly/core/xml.ts:632–688  ·  view source on GitHub ↗
(
  xmlBlock: Element,
  workspace: Workspace,
)

Source from the content-addressed store, hash-verified

630 * @internal
631 */
632export function domToBlockInternal(
633 xmlBlock: Element,
634 workspace: Workspace,
635): Block {
636 // Create top-level block.
637 eventUtils.disable();
638 const variablesBeforeCreation = workspace.getVariableMap().getAllVariables();
639 let topBlock;
640 try {
641 topBlock = domToBlockHeadless(xmlBlock, workspace);
642 // Generate list of all blocks.
643 if (workspace.rendered) {
644 const topBlockSvg = topBlock as BlockSvg;
645 const blocks = topBlock.getDescendants(false);
646 topBlockSvg.setConnectionTracking(false);
647 // Render each block.
648 for (let i = blocks.length - 1; i >= 0; i--) {
649 (blocks[i] as BlockSvg).initSvg();
650 }
651 for (let i = blocks.length - 1; i >= 0; i--) {
652 (blocks[i] as BlockSvg).queueRender();
653 }
654 // Populating the connection database may be deferred until after the
655 // blocks have rendered.
656 setTimeout(function () {
657 if (!topBlockSvg.disposed) {
658 topBlockSvg.setConnectionTracking(true);
659 }
660 }, 1);
661 // Allow the scrollbars to resize and move based on the new contents.
662 // TODO(@picklesrus): #387. Remove when domToBlock avoids resizing.
663 (workspace as WorkspaceSvg).resizeContents();
664 } else {
665 const blocks = topBlock.getDescendants(false);
666 for (let i = blocks.length - 1; i >= 0; i--) {
667 blocks[i].initModel();
668 }
669 }
670 } finally {
671 eventUtils.enable();
672 }
673 if (eventUtils.isEnabled()) {
674 const newVariables = Variables.getAddedVariables(
675 workspace,
676 variablesBeforeCreation,
677 );
678 // Fire a VarCreate event for each (if any) new variable created.
679 for (let i = 0; i < newVariables.length; i++) {
680 const thisVariable = newVariables[i];
681 eventUtils.fire(new (eventUtils.get(EventType.VAR_CREATE))(thisVariable));
682 }
683 // Block events come after var events, in case they refer to newly created
684 // variables.
685 eventUtils.fire(new (eventUtils.get(EventType.BLOCK_CREATE))(topBlock));
686 }
687 return topBlock;
688}
689

Callers 2

domToWorkspaceFunction · 0.85
domToBlockFunction · 0.85

Calls 11

domToBlockHeadlessFunction · 0.85
getVariableMapMethod · 0.80
getDescendantsMethod · 0.80
setConnectionTrackingMethod · 0.80
initSvgMethod · 0.80
queueRenderMethod · 0.80
resizeContentsMethod · 0.80
getMethod · 0.80
getAllVariablesMethod · 0.65
initModelMethod · 0.45
isEnabledMethod · 0.45

Tested by

no test coverage detected