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

Function appendDomToWorkspace

packages/blockly/core/xml.ts:555–604  ·  view source on GitHub ↗
(
  xml: Element,
  workspace: WorkspaceSvg,
)

Source from the content-addressed store, hash-verified

553 * @returns An array containing new block IDs.
554 */
555export function appendDomToWorkspace(
556 xml: Element,
557 workspace: WorkspaceSvg,
558): string[] {
559 // First check if we have a WorkspaceSvg, otherwise the blocks have no shape
560 // and the position does not matter.
561 // Assume it is rendered so we can check.
562 if (!(workspace as WorkspaceSvg).getBlocksBoundingBox) {
563 return domToWorkspace(xml, workspace);
564 }
565
566 const bbox = (workspace as WorkspaceSvg).getBlocksBoundingBox();
567 // Load the new blocks into the workspace and get the IDs of the new blocks.
568 const newBlockIds = domToWorkspace(xml, workspace);
569 if (bbox && bbox.top !== bbox.bottom) {
570 // Check if any previous block.
571 let offsetY = 0; // Offset to add to y of the new block.
572 let offsetX = 0;
573 const farY = bbox.bottom; // Bottom position.
574 const topX = workspace.RTL ? bbox.right : bbox.left; // X of bounding box.
575 // Check position of the new blocks.
576 let newLeftX = Infinity; // X of top left corner.
577 let newRightX = -Infinity; // X of top right corner.
578 let newY = Infinity; // Y of top corner.
579 const ySeparation = 10;
580 for (let i = 0; i < newBlockIds.length; i++) {
581 const blockXY = workspace
582 .getBlockById(newBlockIds[i])!
583 .getRelativeToSurfaceXY();
584 if (blockXY.y < newY) {
585 newY = blockXY.y;
586 }
587 if (blockXY.x < newLeftX) {
588 // if we left align also on x
589 newLeftX = blockXY.x;
590 }
591 if (blockXY.x > newRightX) {
592 // if we right align also on x
593 newRightX = blockXY.x;
594 }
595 }
596 offsetY = farY - newY + ySeparation;
597 offsetX = workspace.RTL ? topX - newRightX : topX - newLeftX;
598 for (let i = 0; i < newBlockIds.length; i++) {
599 const block = workspace.getBlockById(newBlockIds[i]);
600 block!.moveBy(offsetX, offsetY, ['create']);
601 }
602 }
603 return newBlockIds;
604}
605
606/**
607 * Decode an XML block tag and create a block (and possibly sub blocks) on the

Callers

nothing calls this directly

Calls 5

domToWorkspaceFunction · 0.85
getBlocksBoundingBoxMethod · 0.80
moveByMethod · 0.65
getBlockByIdMethod · 0.45

Tested by

no test coverage detected