From 20f1475afc1abf4b5e600219c2981150fc621ba5 Mon Sep 17 00:00:00 2001 From: Beka Westberg Date: Tue, 15 Mar 2022 11:58:14 -0700 Subject: [PATCH] fix!: change paste to return the pasted thing to support keyboard nav (#5996) * fix: change paste to return the pasted thing * fix: format * fix: build * fix: update the API for duplicate as well * fix: change types to ICopyable --- core/blockly.js | 2 +- core/clipboard.js | 15 +++++++++------ core/workspace_svg.js | 19 +++++++++++++++---- scripts/gulpfiles/chunks.json | 6 +++--- 4 files changed, 28 insertions(+), 14 deletions(-) diff --git a/core/blockly.js b/core/blockly.js index da363625790..4aca392f980 100644 --- a/core/blockly.js +++ b/core/blockly.js @@ -529,7 +529,7 @@ const paste = function() { deprecation.warn( 'Blockly.paste', 'December 2021', 'December 2022', 'Blockly.clipboard.paste'); - return clipboard.paste(); + return !!clipboard.paste(); }; exports.paste = paste; diff --git a/core/clipboard.js b/core/clipboard.js index fc0377b7bb8..5d6d9526abe 100644 --- a/core/clipboard.js +++ b/core/clipboard.js @@ -38,13 +38,14 @@ exports.copy = copy; /** * Paste a block or workspace comment on to the main workspace. - * @return {boolean} True if the paste was successful, false otherwise. + * @return {!ICopyable|null} The pasted thing if the paste + * was successful, null otherwise. * @alias Blockly.clipboard.paste * @package */ const paste = function() { if (!copyData) { - return false; + return null; } // Pasting always pastes to the main workspace, even if the copy // started in a flyout workspace. @@ -54,10 +55,9 @@ const paste = function() { } if (copyData.typeCounts && workspace.isCapacityAvailable(copyData.typeCounts)) { - workspace.paste(copyData.saveInfo); - return true; + return workspace.paste(copyData.saveInfo); } - return false; + return null; }; exports.paste = paste; @@ -65,13 +65,16 @@ exports.paste = paste; * Duplicate this block and its children, or a workspace comment. * @param {!ICopyable} toDuplicate Block or Workspace Comment to be * duplicated. + * @return {!ICopyable|null} The block or workspace comment that was duplicated, + * or null if the duplication failed. * @alias Blockly.clipboard.duplicate * @package */ const duplicate = function(toDuplicate) { const oldCopyData = copyData; copy(toDuplicate); - toDuplicate.workspace.paste(copyData.saveInfo); + const pastedThing = toDuplicate.workspace.paste(copyData.saveInfo); copyData = oldCopyData; + return pastedThing; }; exports.duplicate = duplicate; diff --git a/core/workspace_svg.js b/core/workspace_svg.js index c6226068af8..33ec2efb466 100644 --- a/core/workspace_svg.js +++ b/core/workspace_svg.js @@ -62,6 +62,8 @@ const {IASTNodeLocationSvg} = goog.require('Blockly.IASTNodeLocationSvg'); /* eslint-disable-next-line no-unused-vars */ const {IBoundedElement} = goog.requireType('Blockly.IBoundedElement'); /* eslint-disable-next-line no-unused-vars */ +const {ICopyable} = goog.requireType('Blockly.ICopyable'); +/* eslint-disable-next-line no-unused-vars */ const {IDragTarget} = goog.requireType('Blockly.IDragTarget'); /* eslint-disable-next-line no-unused-vars */ const {IFlyout} = goog.requireType('Blockly.IFlyout'); @@ -1520,10 +1522,12 @@ class WorkspaceSvg extends Workspace { * should be done before calling this method. * @param {!Object|!Element|!DocumentFragment} state The representation of the * thing to paste. + * @return {!ICopyable|null} The pasted thing, or null if + * the paste was not successful. */ paste(state) { if (!this.rendered || !state['type'] && !state.tagName) { - return; + return null; } if (this.currentGesture_) { this.currentGesture_.cancel(); // Dragging while pasting? No. @@ -1534,19 +1538,22 @@ class WorkspaceSvg extends Workspace { eventUtils.setGroup(true); } + let pastedThing; // Checks if this is JSON. JSON has a type property, while elements don't. if (state['type']) { - this.pasteBlock_(null, /** @type {!blocks.State} */ (state)); + pastedThing = + this.pasteBlock_(null, /** @type {!blocks.State} */ (state)); } else { const xmlBlock = /** @type {!Element} */ (state); if (xmlBlock.tagName.toLowerCase() === 'comment') { - this.pasteWorkspaceComment_(xmlBlock); + pastedThing = this.pasteWorkspaceComment_(xmlBlock); } else { - this.pasteBlock_(xmlBlock, null); + pastedThing = this.pasteBlock_(xmlBlock, null); } } eventUtils.setGroup(existingGroup); + return pastedThing; } /** @@ -1554,6 +1561,7 @@ class WorkspaceSvg extends Workspace { * @param {?Element} xmlBlock XML block element. * @param {?blocks.State} jsonBlock JSON block * representation. + * @return {!BlockSvg} The pasted block. * @private */ pasteBlock_(xmlBlock, jsonBlock) { @@ -1626,11 +1634,13 @@ class WorkspaceSvg extends Workspace { eventUtils.fire(new (eventUtils.get(eventUtils.BLOCK_CREATE))(block)); } block.select(); + return block; } /** * Paste the provided comment onto the workspace. * @param {!Element} xmlComment XML workspace comment element. + * @return {!WorkspaceCommentSvg} The pasted workspace comment. * @private * @suppress {checkTypes} Suppress checks while workspace comments are not * bundled in. @@ -1662,6 +1672,7 @@ class WorkspaceSvg extends Workspace { goog.module.get('Blockly.WorkspaceComment').fireCreateEvent(comment); } comment.select(); + return comment; } /** diff --git a/scripts/gulpfiles/chunks.json b/scripts/gulpfiles/chunks.json index 98f71962ce7..fce78801fd7 100644 --- a/scripts/gulpfiles/chunks.json +++ b/scripts/gulpfiles/chunks.json @@ -97,9 +97,6 @@ "./core/zoom_controls.js", "./core/workspace_drag_surface_svg.js", "./core/events/events_selected.js", - "./core/interfaces/i_movable.js", - "./core/interfaces/i_selectable.js", - "./core/interfaces/i_copyable.js", "./core/events/events_comment_delete.js", "./core/events/events_comment_change.js", "./core/workspace_comment.js", @@ -119,6 +116,9 @@ "./core/theme_manager.js", "./core/scrollbar_pair.js", "./core/options.js", + "./core/interfaces/i_movable.js", + "./core/interfaces/i_selectable.js", + "./core/interfaces/i_copyable.js", "./core/interfaces/i_bounded_element.js", "./core/grid.js", "./core/css.js",