Skip to content

Commit

Permalink
feat: add option skip backpack serializer registration (#1829)
Browse files Browse the repository at this point in the history
* feat: add skipSerializerRegistration option to workspace-backpack

* feat: skip workspace-backpack serializer registration if skipSerializerRegistration option is true

* docs: update workspace-backpac README with skipSerializerRegistration option
  • Loading branch information
johnnyoshika authored Jul 27, 2023
1 parent aa6f27e commit c139845
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 3 deletions.
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

0 comments on commit c139845

Please sign in to comment.