A Blockly plugin that adds Backpack support.
npm install @blockly/workspace-backpackA Blockly plugin that adds a Backpack to the workspace.
```
yarn add @blockly/workspace-backpack
``
npm install @blockly/workspace-backpack --save
The following context menu options are available for the backpack:
- "Copy all Blocks" to Backpack in main Workspace
- "Paste all Blocks" from Backpack in main Workspace
- "Remove from Backpack" on Block stack in Backpack Flyout
- "Copy to Backpack" on a Block stack in main Workspace
- "Empty" option when right clicking the Backpack that is disabled if the Backpack is empty
!An animated picture of all the context menu options in use
`js
import * as Blockly from 'blockly';
import {Backpack} from '@blockly/workspace-backpack';
// Inject Blockly.
const workspace = Blockly.inject('blocklyDiv', {
toolbox: toolboxCategories,
});
// Initialize plugin.
const backpack = new Backpack(workspace);
backpack.init();
`
This plugin takes an optional configuration object.
``
{
allowEmptyBackpackOpen: (boolean|undefined),
useFilledBackpackImage: (boolean|undefined),
skipSerializerRegistration: (boolean|undefined),
contextMenu: {
emptyBackpack: (boolean|undefined),
removeFromBackpack: (boolean|undefined),
copyToBackpack: (boolean|undefined),
copyAllToBackpack: (boolean|undefined),
pasteAllToBackpack: (boolean|undefined),
disablePreconditionChecks: (boolean|undefined),
},
}
The configuration options are passed in to the constructor. In this
configuration object, you can currently configure which context menu options are
registered at init.
`js`
const backpackOptions = {
allowEmptyBackpackOpen: true,
useFilledBackpackImage: true,
contextMenu: {
emptyBackpack: true,
removeFromBackpack: true,
copyToBackpack: false,
},
};
const backpack = new Backpack(workspace, backpackOptions);
The following options are the default values used for any property in the
passed in options that is undefined:
`js`
const defaultOptions = {
allowEmptyBackpackOpen: true,
useFilledBackpackImage: false,
skipSerializerRegistration: false,
contextMenu: {
emptyBackpack: true,
removeFromBackpack: true,
copyToBackpack: true,
copyAllToBackpack: false,
pasteAllToBackpack: false,
disablePreconditionChecks: false,
},
};
The allowEmptyBackpackOpen property, if set to false, will prevent the backpack flyout from
being opened if the backpack is empty.
The useFilledBackpackImage property, if set to true, will change the
backpack image when the backpack has something in it.
The skipSerializerRegistration property, if set to true, will not register the backpack serializer with the workspace serializer. Set this to true if you don't want the backpack to be serialized alongside the workspace.
The disablePreconditionChecks property will prevent the "Copy to Backpack"true
context menu option from disabling the context menu option if the block is
already in the Backpack. Setting this flag to to disable the check can be
beneficial for performance if you expect blocks stacks to be very large.
!An animated picture of the "Copy to Backpack" context menu
Note: Currently the empty Backpack context menu is registered globally, while
the others are registered per workspace.
We do not currently support translating the text in this plugin to different
languages. However, if you would like to support multiple languages the messages
can be translated by assigning the following properties of Blockly.Msg
- EMPTY_BACKPACK (Default: "Empty") context menu - Empty the backpack.REMOVE_FROM_BACKPACK
- (Default: "Remove from Backpack") context menu - RemoveCOPY_TO_BACKPACK
the selected Block from the backpack.
- (Default: "Copy to Backpack") context menu - Copy theCOPY_ALL_TO_BACKPACK
selected Block to the backpack.
- (Default: "Copy All Blocks to Backpack") Context menu -PASTE_ALL_FROM_BACKPACK
copy all Blocks on the workspace to the backpack.
- (Default: "Paste All Blocks from Backpack") context
menu - Paste all Blocks from the backpack to the workspace.
`javascript`
Blockly.Msg['EMPTY_BACKPACK'] = 'Opróżnij plecak'; // Polish
// Inject workspace, etc...
- init: Initializes the backpack.dispose
- : Disposes of backpack.
- getCount: Returns the count of items in the backpack.getContents
- Returns backpack contents.empty
- : Empties the backpack's contents. If the contents-flyout is currentlyaddBlock
open it will be closed.
- : Adds Block to backpack.addBlocks
- : Adds Blocks to backpack.removeBlock
- : Removes Block to backpack.containsBlock
- : Returns whether the Block is in the backpack.addBackpackable
- : Adds Backpackable to backpack.addBackpackables
- : Adds Backpackable to backpack.removeBackpackable
- : Removes Backpackable to backpack.containsBackpackable
- : Returns whether the Backpackable is in the backpack.addItem
- : Adds item to backpack.removeItem
- : Removes item from the backpack.setContents
- : Sets backpack contents.
- isOpen: Returns whether the backpack is open.open
- : Opens the backpack flyout.close
- : Closes the backpack flyout.
- handleBlockDrop: Handles a block drop on this backpack.onDragEnter
- : Handle mouse over.onDragExit
- : Handle mouse exit.
- getBoundingRectangle: Returns the bounding rectangle of the UI element inposition
pixel units relative to the Blockly injection div.
- : Positions the backpack UI element.
This plugin also currently only supports one Backpack per Workspace.
This plugin registers a custom context menu by overriding
Blockly.configureContextMenu in init in order to support the context menuBlockly.configureContextMenu
for emptying the Backpack.
If you also override after initializing thisBlockly.configureContextMenu
plugin, you must also call the old function.
Example:
``
const prevConfigureContextMenu = workspace.configureContextMenu;
workspace.configureContextMenu = (menuOptions, e) => {
prevConfigureContextMenu &&
prevConfigureContextMenu.call(null, menuOptions, e);
...
}
The Backpack Flyout uses the registered Flyout for either
Blockly.registry.Type.FLYOUTS_HORIZONTAL_TOOLBOX orBlockly.registry.Type.FLYOUTS_VERTICAL_TOOLBOX, similar to the implementationBlockly.Trashcan
for . If a custom class is registered for either of these
types, then the Backpack Flyout may need to be tested for compatibility.
If you have a custom draggable object, you can make it possible to add it to
the backpack by implementing the [Backpackable][backpackable] interface.
This interface requires the draggable to have a method that converts it into
[FlyoutItemInfo`][flyout-info]. As of Blockly core v11 flyouts only support displaying blocks, buttons, and labels.
Apache 2.0
[flyout-info]: https://developers.google.com/blockly/reference/js/blockly.utils_namespace.toolbox_namespace.flyoutiteminfo_typealias.md
[backpackable]: https://github.com/RaspberryPiFoundation/blockly-samples/blob/master/plugins/workspace-backpack/src/backpack.ts