From cf50ba1317c311873f05c6de2c51335ed22055ca Mon Sep 17 00:00:00 2001 From: Beka Westberg Date: Mon, 14 Mar 2022 21:18:14 +0000 Subject: [PATCH 1/5] fix: change paste to return the pasted thing --- core/clipboard.js | 10 +++++----- core/workspace_svg.js | 17 +++++++++++++---- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/core/clipboard.js b/core/clipboard.js index fc0377b7bb8..84e3615035c 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 {!BlockSvg|!WorkspaceCommentSvg|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; diff --git a/core/workspace_svg.js b/core/workspace_svg.js index c38a143e232..df37806fe82 100644 --- a/core/workspace_svg.js +++ b/core/workspace_svg.js @@ -1520,10 +1520,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 {!BlockSvg|!WorkspaceCommentSvg|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 +1536,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 +1559,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 +1632,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 +1670,7 @@ class WorkspaceSvg extends Workspace { goog.module.get('Blockly.WorkspaceComment').fireCreateEvent(comment); } comment.select(); + return comment; } /** From 72970d2391d15ba06a1c33eba14ba22a34f7b348 Mon Sep 17 00:00:00 2001 From: Beka Westberg Date: Mon, 14 Mar 2022 21:34:29 +0000 Subject: [PATCH 2/5] fix: format --- core/workspace_svg.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/workspace_svg.js b/core/workspace_svg.js index df37806fe82..c7d46efd547 100644 --- a/core/workspace_svg.js +++ b/core/workspace_svg.js @@ -1539,8 +1539,8 @@ class WorkspaceSvg extends Workspace { let pastedThing; // Checks if this is JSON. JSON has a type property, while elements don't. if (state['type']) { - pastedThing = 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') { From 467dc9756747e261bafc0286d2b581abc53e73f6 Mon Sep 17 00:00:00 2001 From: Beka Westberg Date: Mon, 14 Mar 2022 21:48:55 +0000 Subject: [PATCH 3/5] fix: build --- core/blockly.js | 2 +- core/clipboard.js | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) 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 84e3615035c..b2491ecaced 100644 --- a/core/clipboard.js +++ b/core/clipboard.js @@ -15,8 +15,12 @@ */ goog.module('Blockly.clipboard'); +/* eslint-disable-next-line no-unused-vars */ +const {BlockSvg} = goog.requireType('Blockly.BlockSvg'); /* eslint-disable-next-line no-unused-vars */ const {ICopyable} = goog.requireType('Blockly.ICopyable'); +/* eslint-disable-next-line no-unused-vars */ +const {WorkspaceCommentSvg} = goog.requireType('Blockly.WorkspaceCommentSvg'); /** From b96d3f48671e7420a0d99b00071e2377f2a8dc51 Mon Sep 17 00:00:00 2001 From: Beka Westberg Date: Tue, 15 Mar 2022 14:37:32 +0000 Subject: [PATCH 4/5] fix: update the API for duplicate as well --- core/clipboard.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/core/clipboard.js b/core/clipboard.js index b2491ecaced..b98d6f04807 100644 --- a/core/clipboard.js +++ b/core/clipboard.js @@ -69,13 +69,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; From a84e58efdcf5997640360e43ec9341164f91e8c9 Mon Sep 17 00:00:00 2001 From: Beka Westberg Date: Tue, 15 Mar 2022 18:54:38 +0000 Subject: [PATCH 5/5] fix: change types to ICopyable --- core/clipboard.js | 6 +----- core/workspace_svg.js | 4 +++- scripts/gulpfiles/chunks.json | 6 +++--- 3 files changed, 7 insertions(+), 9 deletions(-) diff --git a/core/clipboard.js b/core/clipboard.js index b98d6f04807..5d6d9526abe 100644 --- a/core/clipboard.js +++ b/core/clipboard.js @@ -15,12 +15,8 @@ */ goog.module('Blockly.clipboard'); -/* eslint-disable-next-line no-unused-vars */ -const {BlockSvg} = goog.requireType('Blockly.BlockSvg'); /* eslint-disable-next-line no-unused-vars */ const {ICopyable} = goog.requireType('Blockly.ICopyable'); -/* eslint-disable-next-line no-unused-vars */ -const {WorkspaceCommentSvg} = goog.requireType('Blockly.WorkspaceCommentSvg'); /** @@ -42,7 +38,7 @@ exports.copy = copy; /** * Paste a block or workspace comment on to the main workspace. - * @return {!BlockSvg|!WorkspaceCommentSvg|null} The pasted thing if the paste + * @return {!ICopyable|null} The pasted thing if the paste * was successful, null otherwise. * @alias Blockly.clipboard.paste * @package diff --git a/core/workspace_svg.js b/core/workspace_svg.js index c7d46efd547..d2a36ddc449 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,7 +1522,7 @@ class WorkspaceSvg extends Workspace { * should be done before calling this method. * @param {!Object|!Element|!DocumentFragment} state The representation of the * thing to paste. - * @return {!BlockSvg|!WorkspaceCommentSvg|null} The pasted thing, or null if + * @return {!ICopyable|null} The pasted thing, or null if * the paste was not successful. */ paste(state) { 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",