(xml: Element, workspace: Workspace)
| 445 | * @returns An array containing new block IDs. |
| 446 | */ |
| 447 | export function domToWorkspace(xml: Element, workspace: Workspace): string[] { |
| 448 | let width = 0; // Not used in LTR. |
| 449 | if (workspace.RTL) { |
| 450 | width = workspace.getWidth(); |
| 451 | } |
| 452 | const newBlockIds = []; // A list of block IDs added by this call. |
| 453 | dom.startTextWidthCache(); |
| 454 | const existingGroup = eventUtils.getGroup(); |
| 455 | if (!existingGroup) { |
| 456 | eventUtils.setGroup(true); |
| 457 | } |
| 458 | |
| 459 | // Disable workspace resizes as an optimization. |
| 460 | // Assume it is rendered so we can check. |
| 461 | if ((workspace as WorkspaceSvg).setResizesEnabled) { |
| 462 | (workspace as WorkspaceSvg).setResizesEnabled(false); |
| 463 | } |
| 464 | let variablesFirst = true; |
| 465 | try { |
| 466 | for (let i = 0, xmlChild; (xmlChild = xml.childNodes[i]); i++) { |
| 467 | const name = xmlChild.nodeName.toLowerCase(); |
| 468 | const xmlChildElement = xmlChild as Element; |
| 469 | if ( |
| 470 | name === 'block' || |
| 471 | (name === 'shadow' && !eventUtils.getRecordUndo()) |
| 472 | ) { |
| 473 | // Allow top-level shadow blocks if recordUndo is disabled since |
| 474 | // that means an undo is in progress. Such a block is expected |
| 475 | // to be moved to a nested destination in the next operation. |
| 476 | const block = domToBlockInternal(xmlChildElement, workspace); |
| 477 | newBlockIds.push(block.id); |
| 478 | const blockX = parseInt(xmlChildElement.getAttribute('x') ?? '10', 10); |
| 479 | const blockY = parseInt(xmlChildElement.getAttribute('y') ?? '10', 10); |
| 480 | if (!isNaN(blockX) && !isNaN(blockY)) { |
| 481 | block.moveBy(workspace.RTL ? width - blockX : blockX, blockY, [ |
| 482 | 'create', |
| 483 | ]); |
| 484 | } |
| 485 | variablesFirst = false; |
| 486 | } else if (name === 'shadow') { |
| 487 | throw TypeError('Shadow block cannot be a top-level block.'); |
| 488 | } else if (name === 'comment') { |
| 489 | loadWorkspaceComment(xmlChildElement, workspace); |
| 490 | } else if (name === 'variables') { |
| 491 | if (variablesFirst) { |
| 492 | domToVariables(xmlChildElement, workspace); |
| 493 | } else { |
| 494 | throw Error( |
| 495 | "'variables' tag must exist once before block and " + |
| 496 | 'shadow tag elements in the workspace XML, but it was found in ' + |
| 497 | 'another location.', |
| 498 | ); |
| 499 | } |
| 500 | variablesFirst = false; |
| 501 | } |
| 502 | } |
| 503 | } finally { |
| 504 | eventUtils.setGroup(existingGroup); |
no test coverage detected