Skip to content

Commit

Permalink
feat: make the filled backpack image optional (#1146)
Browse files Browse the repository at this point in the history
  • Loading branch information
BeksOmega authored May 26, 2022
1 parent c74031e commit 549be3d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 13 deletions.
6 changes: 6 additions & 0 deletions plugins/workspace-backpack/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ This plugin takes an optional configuration object.
```
{
allowEmptyBackpackOpen: (boolean|undefined),
useFilledBackpackImage: (boolean|undefined),
contextMenu: {
emptyBackpack: (boolean|undefined),
removeFromBackpack: (boolean|undefined),
Expand All @@ -63,6 +64,7 @@ registered at `init`.
```js
const backpackOptions = {
allowEmptyBackpackOpen: true,
useFilledBackpackImage: true,
contextMenu: {
emptyBackpack: true,
removeFromBackpack: true,
Expand All @@ -77,6 +79,7 @@ passed in options that is undefined:
```js
const defaultOptions = {
allowEmptyBackpackOpen: true,
useFilledBackpackImage: false,
contextMenu: {
emptyBackpack: true,
removeFromBackpack: true,
Expand All @@ -91,6 +94,9 @@ const defaultOptions = {
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 `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
9 changes: 4 additions & 5 deletions plugins/workspace-backpack/src/backpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
import * as Blockly from 'blockly/core';
import {cleanBlockXML, registerContextMenus} from './backpack_helpers';
import {BackpackChange, BackpackOpen} from './ui_events';
import {
BackpackContextMenuOptions, BackpackOptions, parseOptions,
} from './options';
import {BackpackContextMenuOptions, BackpackOptions, parseOptions} from './options';

/**
* Class for backpack that can be used save blocks from the workspace for
Expand Down Expand Up @@ -556,15 +554,16 @@ export class Backpack extends Blockly.DragTarget {
*/
onContentChange_() {
this.maybeRefreshFlyoutContents_();
Blockly.Events.fire(new BackpackChange(this.workspace_.id));

if (!this.options_.useFilledBackpackImage) return;
if (this.contents_.length > 0) {
this.svgImg_.setAttributeNS(Blockly.utils.dom.XLINK_NS, 'xlink:href',
BACKPACK_FILLED_SVG_DATAURI);
} else {
this.svgImg_.setAttributeNS(Blockly.utils.dom.XLINK_NS, 'xlink:href',
BACKPACK_SVG_DATAURI);
}

Blockly.Events.fire(new BackpackChange(this.workspace_.id));
}

/**
Expand Down
24 changes: 16 additions & 8 deletions plugins/workspace-backpack/src/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export let BackpackContextMenuOptions;
/**
* @typedef {{
* allowEmptyBackpackOpen: (boolean|undefined),
* useFilledBackpackImage: (boolean|undefined),
* contextMenu:(!BackpackContextMenuOptions|undefined)
* }}
*/
Expand All @@ -37,8 +38,9 @@ export let BackpackOptions;
* @return {!BackpackOptions} The created options object.
*/
export function parseOptions(options) {
const defaultOptions = {
const defaults = {
allowEmptyBackpackOpen: true,
useFilledBackpackImage: false,
contextMenu: {
emptyBackpack: true,
removeFromBackpack: true,
Expand All @@ -48,13 +50,19 @@ export function parseOptions(options) {
disablePreconditionChecks: false,
},
};

if (!options) {
return defaultOptions;
return defaults;
}
const mergedOptions = {};
mergedOptions.contextMenu = {
...defaultOptions.contextMenu, ...options.contextMenu};
mergedOptions.allowEmptyBackpackOpen = options.allowEmptyBackpackOpen ??
defaultOptions.allowEmptyBackpackOpen;
return mergedOptions;

return {
allowEmptyBackpackOpen:
options.allowEmptyBackpackOpen ?? defaults.allowEmptyBackpackOpen,
useFilledBackpackImage:
options.useFilledBackpackImage ?? defaults.useFilledBackpackImage,
contextMenu: {
...defaults.contextMenu,
...options.contextMenu,
},
};
}

0 comments on commit 549be3d

Please sign in to comment.