Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
76 changes: 8 additions & 68 deletions core/blockly.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
goog.provide('Blockly');

goog.require('Blockly.browserEvents');
goog.require('Blockly.clipboard');
goog.require('Blockly.ComponentManager');
goog.require('Blockly.connectionTypes');
goog.require('Blockly.constants');
Expand Down Expand Up @@ -91,27 +92,6 @@ Blockly.selected = null;
*/
Blockly.draggingConnections = [];

/**
* Contents of the local clipboard.
* @type {Element}
* @private
*/
Blockly.clipboardXml_ = null;

/**
* Source of the local clipboard.
* @type {Blockly.WorkspaceSvg}
* @private
*/
Blockly.clipboardSource_ = null;

/**
* Map of types to type counts for the clipboard object and descendants.
* @type {Object}
* @private
*/
Blockly.clipboardTypeCounts_ = null;

/**
* Cached value for whether 3D is supported.
* @type {?boolean}
Expand All @@ -136,9 +116,7 @@ Blockly.svgSize = function(svg) {
// When removing this function, remove svg.cachedWidth_ and svg.cachedHeight_
// from setCachedParentSvgSize.
Blockly.utils.deprecation.warn(
'Blockly.svgSize',
'March 2021',
'March 2022',
'Blockly.svgSize', 'March 2021', 'March 2022',
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

clang-format did this.

'workspace.getCachedParentSvgSize');
svg = /** @type {?} */ (svg);
return new Blockly.utils.Size(svg.cachedWidth_, svg.cachedHeight_);
Expand Down Expand Up @@ -220,7 +198,8 @@ Blockly.deleteBlock = function(selected) {
Blockly.Events.setGroup(true);
Blockly.hideChaff();
if (selected.outputConnection) {
// Do not attempt to heal rows (https://github.com/google/blockly/issues/4832)
// Do not attempt to heal rows
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

clang-format did this.

// (https://github.com/google/blockly/issues/4832)
selected.dispose(false, true);
} else {
selected.dispose(/* heal */ true, true);
Expand All @@ -234,59 +213,22 @@ Blockly.deleteBlock = function(selected) {
* @param {!Blockly.ICopyable} toCopy Block or Workspace Comment to be copied.
* @package
*/
Blockly.copy = function(toCopy) {
var data = toCopy.toCopyData();
if (data) {
Blockly.clipboardXml_ = data.xml;
Blockly.clipboardSource_ = data.source;
Blockly.clipboardTypeCounts_ = data.typeCounts;
}
};
Blockly.copy = Blockly.clipboard.copy;

/**
* Paste a block or workspace comment on to the main workspace.
* @return {boolean} True if the paste was successful, false otherwise.
* @package
*/
Blockly.paste = function() {
if (!Blockly.clipboardXml_) {
return false;
}
// Pasting always pastes to the main workspace, even if the copy
// started in a flyout workspace.
var workspace = Blockly.clipboardSource_;
if (workspace.isFlyout) {
workspace = workspace.targetWorkspace;
}
if (Blockly.clipboardTypeCounts_ &&
workspace.isCapacityAvailable(Blockly.clipboardTypeCounts_)) {
Blockly.Events.setGroup(true);
workspace.paste(Blockly.clipboardXml_);
Blockly.Events.setGroup(false);
return true;
}
return false;
};
Blockly.paste = Blockly.clipboard.paste;

/**
* Duplicate this block and its children, or a workspace comment.
* @param {!Blockly.ICopyable} toDuplicate Block or Workspace Comment to be
* copied.
* @package
*/
Blockly.duplicate = function(toDuplicate) {
// Save the clipboard.
var clipboardXml = Blockly.clipboardXml_;
var clipboardSource = Blockly.clipboardSource_;

// Create a duplicate via a copy/paste operation.
Blockly.copy(toDuplicate);
toDuplicate.workspace.paste(Blockly.clipboardXml_);

// Restore the clipboard.
Blockly.clipboardXml_ = clipboardXml;
Blockly.clipboardSource_ = clipboardSource;
};
Blockly.duplicate = Blockly.clipboard.duplicate;

/**
* Cancel the native context menu, unless the focus is on an HTML input widget.
Expand Down Expand Up @@ -402,9 +344,7 @@ Blockly.defineBlocksWithJsonArray = function(jsonArray) {
'Block definition #' + i + ' in JSON array' +
' overwrites prior definition of "' + typename + '".');
}
Blockly.Blocks[typename] = {
init: Blockly.jsonInitFactory_(elem)
};
Blockly.Blocks[typename] = {init: Blockly.jsonInitFactory_(elem)};
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Clang-format did this.

}
}
}
Expand Down
80 changes: 80 additions & 0 deletions core/clipboard.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/**
* @license
* Copyright 2021 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/

/**
* @fileoverview Blockly's internal clipboard for managing copy-paste.
* @author fenichel@google.com (Rachel Fenichel)
*/
'use strict';

goog.module('Blockly.clipboard');
goog.module.declareLegacyNamespace();

const Events = goog.require('Blockly.Events');
/* eslint-disable-next-line no-unused-vars */
const ICopyable = goog.requireType('Blockly.ICopyable');

/**
* Metadata about the object that is currently on the clipboard.
* @type {?ICopyable.CopyData}
*/
let copyData = null;

/**
* Copy a block or workspace comment onto the local clipboard.
* @param {!ICopyable} toCopy Block or Workspace Comment to be copied.
*/
const copy = function(toCopy) {
copyData = toCopy.toCopyData();
};
/** @package */
exports.copy = copy;
Copy link
Contributor

Choose a reason for hiding this comment

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

I think @Package is suppose to go above this line now.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done.


/**
* Paste a block or workspace comment on to the main workspace.
* @return {boolean} True if the paste was successful, false otherwise.
*/
const paste = function() {
if (!copyData.xml) {
return false;
}
// Pasting always pastes to the main workspace, even if the copy
// started in a flyout workspace.
var workspace = copyData.source;
if (workspace.isFlyout) {
workspace = workspace.targetWorkspace;
}
if (copyData.typeCounts &&
workspace.isCapacityAvailable(copyData.typeCounts)) {
Events.setGroup(true);
workspace.paste(copyData.xml);
Events.setGroup(false);
return true;
}
return false;
};
/** @package */
exports.paste = paste;

/**
* Duplicate this block and its children, or a workspace comment.
* @param {!ICopyable} toDuplicate Block or Workspace Comment to be
* duplicated.
*/
const duplicate = function(toDuplicate) {
// Save the clipboard.
const oldCopyData = copyData;

// Create a duplicate via a copy/paste operation.
copy(toDuplicate);
// copy() replaced the value of copyData.
toDuplicate.workspace.paste(copyData.xml);

// Restore the clipboard.
copyData = oldCopyData;
};
/** @package */
exports.duplicate = duplicate;
4 changes: 2 additions & 2 deletions core/contextmenu_items.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* @namespace
*/
goog.provide('Blockly.ContextMenuItems');

goog.require('Blockly.clipboard');
/** @suppress {extraRequire} */
goog.require('Blockly.constants');
goog.require('Blockly.ContextMenuRegistry');
Expand Down Expand Up @@ -316,7 +316,7 @@ Blockly.ContextMenuItems.registerDuplicate = function() {
},
callback: function(/** @type {!Blockly.ContextMenuRegistry.Scope} */ scope) {
if (scope.block) {
Blockly.duplicate(scope.block);
Blockly.clipboard.duplicate(scope.block);
}
},
scopeType: Blockly.ContextMenuRegistry.ScopeType.BLOCK,
Expand Down
7 changes: 4 additions & 3 deletions core/shortcut_items.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
goog.provide('Blockly.ShortcutItems');

goog.require('Blockly.clipboard');
goog.require('Blockly.Gesture');
goog.require('Blockly.ShortcutRegistry');
goog.require('Blockly.utils.KeyCodes');
Expand Down Expand Up @@ -104,7 +105,7 @@ Blockly.ShortcutItems.registerCopy = function() {
// an error due to the lack of a selection.
e.preventDefault();
Blockly.hideChaff();
Blockly.copy(/** @type {!Blockly.ICopyable} */ (Blockly.selected));
Blockly.clipboard.copy(/** @type {!Blockly.ICopyable} */ (Blockly.selected));
return true;
}
};
Expand Down Expand Up @@ -137,7 +138,7 @@ Blockly.ShortcutItems.registerCut = function() {
!Blockly.selected.workspace.isFlyout;
},
callback: function() {
Blockly.copy(/** @type {!Blockly.ICopyable} */ (Blockly.selected));
Blockly.clipboard.copy(/** @type {!Blockly.ICopyable} */ (Blockly.selected));
Blockly.deleteBlock(/** @type {!Blockly.BlockSvg} */ (Blockly.selected));
return true;
}
Expand Down Expand Up @@ -167,7 +168,7 @@ Blockly.ShortcutItems.registerPaste = function() {
return !workspace.options.readOnly && !Blockly.Gesture.inProgress();
},
callback: function() {
return Blockly.paste();
return Blockly.clipboard.paste();
}
};

Expand Down
7 changes: 4 additions & 3 deletions tests/deps.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions tests/mocha/contextmenu_items_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ suite('Context Menu Items', function() {
Blockly.ContextMenuItems.registerDefaultOptions();
this.registry = Blockly.ContextMenuRegistry.registry;
});

teardown(function() {
sharedTestTeardown.call(this);
});

suite('Workspace Items', function() {
setup(function() {
this.scope = {workspace: this.workspace};
Expand Down Expand Up @@ -328,12 +328,12 @@ suite('Context Menu Items', function() {
});

test('Calls duplicate', function() {
var stub = sinon.stub(Blockly, 'duplicate');
var spy = sinon.spy(Blockly.clipboard, 'duplicate');

this.duplicateOption.callback(this.scope);

sinon.assert.calledOnce(stub);
sinon.assert.calledWith(stub, this.block);
sinon.assert.calledOnce(spy);
sinon.assert.calledWith(spy, this.block);
});

test('Has correct label', function() {
Expand Down
2 changes: 1 addition & 1 deletion tests/mocha/keydown_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ suite('Key Down', function() {
suite('Copy', function() {
setup(function() {
setSelectedBlock(this.workspace);
this.copySpy = sinon.spy(Blockly, 'copy');
this.copySpy = sinon.spy(Blockly.clipboard, 'copy');
this.hideChaffSpy = sinon.spy(Blockly, 'hideChaff');
});
var testCases = [
Expand Down