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

feat: add option skip backpack serializer registration #1829

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
4 changes: 4 additions & 0 deletions plugins/workspace-backpack/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ This plugin takes an optional configuration object.
{
allowEmptyBackpackOpen: (boolean|undefined),
useFilledBackpackImage: (boolean|undefined),
skipSerializerRegistration: (boolean|undefined),
contextMenu: {
emptyBackpack: (boolean|undefined),
removeFromBackpack: (boolean|undefined),
Expand Down Expand Up @@ -80,6 +81,7 @@ passed in options that is undefined:
const defaultOptions = {
allowEmptyBackpackOpen: true,
useFilledBackpackImage: false,
skipSerializerRegistration: false,
contextMenu: {
emptyBackpack: true,
removeFromBackpack: true,
Expand All @@ -97,6 +99,8 @@ 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"
context menu option from disabling the context menu option if the block is
already in the Backpack. Setting this flag to `true` to disable the check can be
Expand Down
27 changes: 24 additions & 3 deletions plugins/workspace-backpack/src/backpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,29 @@ export class Backpack extends Blockly.DragTarget {
* @private
*/
this.SPRITE_SIZE_ = 80;

this.registerSerializer();
}

/**
* Registers serializer if options.skipSerializerRegistration is false
* and it hasn't been registered already.
* @private
*/
registerSerializer() {
if (this.options_.skipSerializerRegistration) {
return;
}

if (Blockly.registry.hasItem(
Blockly.registry.Type.SERIALIZER,
'backpack')) {
return;
}

Blockly.serialization.registry.register(
'backpack',
new BackpackSerializer());
}

/**
Expand Down Expand Up @@ -890,6 +913,4 @@ class BackpackSerializer {
}
}

Blockly.serialization.registry.register(
'backpack',
new BackpackSerializer());

1 change: 1 addition & 0 deletions plugins/workspace-backpack/src/options.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export interface BackpackContextMenuOptions {
export interface BackpackOptions {
allowEmptyBackpackOpen?: boolean;
useFilledBackpackImage?: boolean;
skipSerializerRegistration?: boolean;
contextMenu?: BackpackContextMenuOptions;
}
/**
Expand Down
5 changes: 5 additions & 0 deletions plugins/workspace-backpack/src/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export let BackpackContextMenuOptions;
* @typedef {{
* allowEmptyBackpackOpen: (boolean|undefined),
* useFilledBackpackImage: (boolean|undefined),
* skipSerializerRegistration: (boolean|undefined),
* contextMenu:(!BackpackContextMenuOptions|undefined)
* }}
*/
Expand All @@ -41,6 +42,7 @@ export function parseOptions(options) {
const defaults = {
allowEmptyBackpackOpen: true,
useFilledBackpackImage: false,
skipSerializerRegistration: false,
contextMenu: {
emptyBackpack: true,
removeFromBackpack: true,
Expand All @@ -60,6 +62,9 @@ export function parseOptions(options) {
options.allowEmptyBackpackOpen ?? defaults.allowEmptyBackpackOpen,
useFilledBackpackImage:
options.useFilledBackpackImage ?? defaults.useFilledBackpackImage,
skipSerializerRegistration:
options.skipSerializerRegistration ??
defaults.skipSerializerRegistration,
contextMenu: {
...defaults.contextMenu,
...options.contextMenu,
Expand Down
Loading