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: move backpack to use interface for backpackability #2314

Merged
merged 5 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
47 changes: 41 additions & 6 deletions plugins/workspace-backpack/src/backpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import {registerContextMenus} from './backpack_helpers';
import {BackpackOptions, parseOptions} from './options';
import {BackpackChange, BackpackOpen} from './ui_events';
import {Backpackable, isBackpackable} from './backpackable';

/**
* Class for backpack that can be used save blocks from the workspace for
Expand Down Expand Up @@ -453,8 +454,8 @@
* being dragged.
*/
onDrop(dragElement: Blockly.IDraggable) {
if (dragElement instanceof Blockly.BlockSvg) {
this.addBlock(dragElement);
if (isBackpackable(dragElement)) {
this.addBackpackable(dragElement);
}
}

Expand Down Expand Up @@ -525,7 +526,7 @@
* provided block.
*/
containsBlock(block: Blockly.Block): boolean {
return this.contents_.indexOf(this.blockToJsonString(block)) !== -1;
return isBackpackable(block) && this.containsBackpackable(block);
}

/**
Expand All @@ -534,7 +535,7 @@
* @param block The block to be added to the backpack.
*/
addBlock(block: Blockly.Block) {
BeksOmega marked this conversation as resolved.
Show resolved Hide resolved
this.addItem(this.blockToJsonString(block));
if (isBackpackable(block)) this.addBackpackable(block);
}

/**
Expand All @@ -544,7 +545,9 @@
* backpack.
*/
addBlocks(blocks: Blockly.Block[]) {
this.addItems(blocks.map(this.blockToJsonString));
for (const block of blocks) {
if (isBackpackable(block)) this.addBackpackable(block);
}
}

/**
Expand All @@ -553,7 +556,39 @@
* @param block The block to be removed from the backpack.
*/
removeBlock(block: Blockly.Block) {
this.removeItem(this.blockToJsonString(block));
if (isBackpackable(block)) this.removeBackpackable(block);
}

/**

Check warning on line 562 in plugins/workspace-backpack/src/backpack.ts

View workflow job for this annotation

GitHub Actions / lint

Missing JSDoc @param "backpackable" declaration
* Returns whether the backpack contains a duplicate of the provided
BeksOmega marked this conversation as resolved.
Show resolved Hide resolved
* backpackable.
*/
containsBackpackable(backpackable: Backpackable) {
BeksOmega marked this conversation as resolved.
Show resolved Hide resolved
return backpackable
.toFlyoutData()
.every((info) => this.contents_.indexOf(JSON.stringify(info)) !== -1);
}

/** Adds the given backpackable to the backpack. */

Check warning on line 572 in plugins/workspace-backpack/src/backpack.ts

View workflow job for this annotation

GitHub Actions / lint

Missing JSDoc @param "backpackable" declaration
addBackpackable(backpackable: Backpackable) {
this.addBackpackables([backpackable]);
}

/** Adds the given backpackables to the backpack. */

Check warning on line 577 in plugins/workspace-backpack/src/backpack.ts

View workflow job for this annotation

GitHub Actions / lint

Missing JSDoc @param "backpackables" declaration
addBackpackables(backpackables: Backpackable[]) {
this.addItems(
backpackables
.map((b) => b.toFlyoutData())
.reduce((acc, curr) => [...acc, ...curr])
.map((info) => JSON.stringify(info)),
);
}

/** Removes the given backpackable from the backpack, if it exists. */

Check warning on line 587 in plugins/workspace-backpack/src/backpack.ts

View workflow job for this annotation

GitHub Actions / lint

Missing JSDoc @param "backpackable" declaration
removeBackpackable(backpackable: Backpackable) {
for (const info of backpackable.toFlyoutData()) {
this.removeItem(JSON.stringify(info));
}
}

/**
Expand Down
24 changes: 24 additions & 0 deletions plugins/workspace-backpack/src/backpackable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* @license
* Copyright 2024 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/

import * as Blockly from 'blockly/core';

/** Defines if an object can be added to the backpack. */
export interface Backpackable {
/**
* Returns a representation of this object as a FlyoutItemInfo array, so that
* it can be displayed in the backpack.
*
* This method should remove any unique or useless state data (e.g. IDs or
* coordinates).
*/
toFlyoutData(): Blockly.utils.toolbox.FlyoutItemInfo[];
}

/** Checks whether the given object conforms to the Backpackable interface. */

Check warning on line 21 in plugins/workspace-backpack/src/backpackable.ts

View workflow job for this annotation

GitHub Actions / lint

Missing JSDoc @param "obj" declaration
export function isBackpackable(obj: any): obj is Backpackable {

Check warning on line 22 in plugins/workspace-backpack/src/backpackable.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
BeksOmega marked this conversation as resolved.
Show resolved Hide resolved
return obj.toFlyoutData !== undefined;
}
Loading