Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix!: change paste to return the pasted thing to support keyboard nav #5996

Merged
merged 5 commits into from
Mar 15, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
19 changes: 13 additions & 6 deletions core/clipboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');


/**
Expand All @@ -38,13 +42,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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would probably be cleaner and more future-proof if you used ICopyable as the return type like in duplicate(). Probably want to do this in workspace_svg as well.

* 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 +59,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;
17 changes: 13 additions & 4 deletions core/workspace_svg.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -1534,26 +1536,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 +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.
Expand Down Expand Up @@ -1662,6 +1670,7 @@ class WorkspaceSvg extends Workspace {
goog.module.get('Blockly.WorkspaceComment').fireCreateEvent(comment);
}
comment.select();
return comment;
}

/**
Expand Down