Skip to content

Commit

Permalink
fix!: change paste to return the pasted thing to support keyboard nav (
Browse files Browse the repository at this point in the history
…#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
  • Loading branch information
BeksOmega authored Mar 15, 2022
1 parent fec44d9 commit 20f1475
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 14 deletions.
2 changes: 1 addition & 1 deletion core/blockly.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
15 changes: 9 additions & 6 deletions core/clipboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -54,24 +55,26 @@ 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;

/**
* 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;
19 changes: 15 additions & 4 deletions core/workspace_svg.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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.
Expand All @@ -1534,26 +1538,30 @@ 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;
}

/**
* Paste the provided block onto the workspace.
* @param {?Element} xmlBlock XML block element.
* @param {?blocks.State} jsonBlock JSON block
* representation.
* @return {!BlockSvg} The pasted block.
* @private
*/
pasteBlock_(xmlBlock, jsonBlock) {
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -1662,6 +1672,7 @@ class WorkspaceSvg extends Workspace {
goog.module.get('Blockly.WorkspaceComment').fireCreateEvent(comment);
}
comment.select();
return comment;
}

/**
Expand Down
6 changes: 3 additions & 3 deletions scripts/gulpfiles/chunks.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down

0 comments on commit 20f1475

Please sign in to comment.