From ac090cac09d81e712f40fed7c7bec0b4659a1e3b Mon Sep 17 00:00:00 2001 From: "Evan W. Patton" Date: Tue, 8 Nov 2016 12:29:59 -0500 Subject: [PATCH] [WIP] Fix headless loading of blocks --- core/block_render_svg.js | 53 ++++--------------------------------- core/block_svg.js | 12 ++++++--- core/connection.js | 3 --- core/inject.js | 4 +-- core/rendered_connection.js | 3 +++ core/xml.js | 2 ++ 6 files changed, 20 insertions(+), 57 deletions(-) diff --git a/core/block_render_svg.js b/core/block_render_svg.js index 5a10a3292..3479585a8 100644 --- a/core/block_render_svg.js +++ b/core/block_render_svg.js @@ -263,49 +263,6 @@ Blockly.BlockSvg.prototype.render = function(opt_bubble) { Blockly.Field.startCache(); this.rendered = true; - this.renderHere(opt_bubble); - - // Render all blocks above this one (propagate a reflow). - var parentBlock = this.getParent(); - if (parentBlock) { - parentBlock.render(); - } - - Blockly.Field.stopCache(); -}; - -/** - * [lyn, 04/01/14] Render a tree of blocks. - * In general, this is more efficient than calling render() on all the leaves of the tree, - * because that will: - * (1) repeat the rendering of all internal nodes; and - * (2) will unnecessarily call Blockly.fireUiEvent(window, 'resize') in the - * case where the parentPointer hasn't been set yet (particularly for - * value, statement, and next connections in Xml.domToBlock). - * These two factors account for much of the slow project loading times in Blockly - * and previous versions of AI2. - */ -Blockly.BlockSvg.prototype.renderDown = function(opt_bubble) { - this.rendered = true; - - // Recursively renderDown all my children (as long as I'm not collapsed) - if (! (Blockly.Instrument.avoidRenderDownOnCollapsedSubblocks && this.isCollapsed())) { - var childBlocks = this.childBlocks_; - for (var c = 0, childBlock; childBlock = childBlocks[c]; c++) { - childBlock.renderDown(opt_bubble); - } - } - - // Render me after all my children have been rendered. - this.renderHere(opt_bubble); -}; - -/** - * Render this block. Assumes descendants have already been rendered. - */ -Blockly.BlockSvg.prototype.renderHere = function(opt_bubble) { - var start = new Date().getTime(); - // Now render me (even if I am collapsed, since still need to show collapsed block) var cursorX = Blockly.BlockSvg.SEP_SPACE_X; if (this.RTL) { cursorX = -cursorX; @@ -325,16 +282,16 @@ Blockly.BlockSvg.prototype.renderHere = function(opt_bubble) { this.renderMoveConnections_(); if (opt_bubble !== false) { + // Render all blocks above this one (propagate a reflow). var parentBlock = this.getParent(); - if (!parentBlock) { + if (parentBlock) { + parentBlock.render(true); + } else { // Top-most block. Fire an event to allow scrollbars to resize. this.workspace.resizeContents(); } } - var stop = new Date().getTime(); - var timeDiff = stop - start; - Blockly.Instrument.stats.renderHereCalls++; - Blockly.Instrument.stats.renderHereTime += timeDiff; + Blockly.Field.stopCache(); }; /** diff --git a/core/block_svg.js b/core/block_svg.js index f17dc5c5f..a4f53ae0b 100644 --- a/core/block_svg.js +++ b/core/block_svg.js @@ -615,10 +615,14 @@ Blockly.BlockSvg.prototype.onMouseUp_ = function(e) { } else if (!this.getParent() && Blockly.selected.isDeletable() && this.workspace.isDeleteArea(e)) { var trashcan = this.workspace.trashcan; - if (trashcan) { - goog.Timer.callOnce(trashcan.close, 100, trashcan); - } - Blockly.selected.dispose(false, true); + Blockly.confirmDeletion(function(confirmedDelete) { + if (trashcan) { + goog.Timer.callOnce(trashcan.close, 100, trashcan); + } + if (confirmedDelete) { + Blockly.selected.dispose(false, true); + } + }); } if (Blockly.highlightedConnection_) { Blockly.highlightedConnection_.unhighlight(); diff --git a/core/connection.js b/core/connection.js index f98a20069..3200defa8 100644 --- a/core/connection.js +++ b/core/connection.js @@ -218,9 +218,6 @@ Blockly.Connection.prototype.connect_ = function(childConnection) { } }, Blockly.BUMP_DELAY); } - if (block.errorIcon) { - block.errorIcon.setVisible(false); - } } // Restore the shadow DOM. parentConnection.setShadowDom(shadowDom); diff --git a/core/inject.js b/core/inject.js index 57e3e51f4..f8b48c710 100644 --- a/core/inject.js +++ b/core/inject.js @@ -50,7 +50,7 @@ Blockly.inject = function(container, opt_options) { if (!goog.dom.contains(document, container)) { throw 'Error: container is not in current document.'; } - var options = new Blockly.Options(opt_options || {}); + var options = (Blockly.mainWorkspace && Blockly.mainWorkspace.options) || new Blockly.Options(opt_options || {}); var subContainer = goog.dom.createDom('div', 'injectionDiv'); container.appendChild(subContainer); var svg = Blockly.createDom_(subContainer, options); @@ -185,7 +185,7 @@ Blockly.createDom_ = function(container, options) { */ Blockly.createMainWorkspace_ = function(svg, options) { options.parentWorkspace = null; - var mainWorkspace = new Blockly.WorkspaceSvg(options); + var mainWorkspace = Blockly.mainWorkspace || new Blockly.WorkspaceSvg(options); mainWorkspace.scale = options.zoomOptions.startScale; svg.appendChild(mainWorkspace.createDom('blocklyMainBackground')); // A null translation will also apply the correct initial scale. diff --git a/core/rendered_connection.js b/core/rendered_connection.js index dcb90f1b0..6eb277fc7 100644 --- a/core/rendered_connection.js +++ b/core/rendered_connection.js @@ -287,6 +287,9 @@ Blockly.RenderedConnection.prototype.hideAll = function() { for (var j = 0; j < icons.length; j++) { icons[j].setVisible(false); } + if (block.errorIcon) { + block.errorIcon.setVisible(false); + } } } }; diff --git a/core/xml.js b/core/xml.js index 2567560cd..9f4ec2d31 100644 --- a/core/xml.js +++ b/core/xml.js @@ -417,10 +417,12 @@ Blockly.Xml.domToBlockHeadless_ = function(xmlBlock, workspace) { // Custom data for an advanced block. if (block.domToMutation) { block.domToMutation(xmlChild); + /* disabling due to this being _headless_ if (block.initSvg) { // Mutation may have added some elements that need initalizing. block.initSvg(); } + */ } break; case 'comment':