mirror of
https://github.com/google/blockly.git
synced 2026-01-09 01:50:11 +01:00
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
This commit is contained in:
@@ -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;
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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",
|
||||
|
||||
Reference in New Issue
Block a user