import { BuildableComponent } from '../component/buildable_component.js';
import { Reporter } from '../../reporter/reporter.js';
import { UUIDRegex, checkPropTypes } from '../../lib.js';
import { BlockCategory } from './block_category.js';
import { Block } from './block.js';
import { WrappedImage } from '../image/wrapped_image.js';
import { Project } from '../../project.js';
/**
* Block set
* Component is not auto-linked and uses the Package to formulate the set of blocks
* from block categories and block references. As "BlockSet" is loaded after
* "Block" in the Package, this isn't a problem. Otherwise, building depends
* on Package change events.
*/
class BlockSet extends BuildableComponent {
/**
* @param {Reporter} reporter
* @param {Object} settings
* @param {UUID} [settings.id]
* @param {string} settings.name
* @param {WrappedImage} [settings.thumbnail]
* @param {Array<BlockCategory>} [settings.categories]
* @param {Array<Block>} [settings.blocks]
*/
constructor(reporter, settings) {
// this component should not be auto linked in parent class
super(reporter, settings, { autoLinkDependencies: false });
checkPropTypes(
settings,
{
name: 'string',
},
{
categories: val => {
return Array.isArray(val) && val.every(entry => entry instanceof BlockCategory)
},
blocks: val => {
return Array.isArray(val) && val.every(entry => entry instanceof Block)
},
id: UUIDRegex,
}
);
// this.exportName = 'blockSets';
this._dependencies.main = {};
// block set should not have dependencies
// or the number of alternative loading paths
// will explode (as ComponentLoader will "see"
// this as alternatives)
// thinking further about this, it is obvious
// that building the set should not lead to
// building every block in it. It is almost
// always undesirable to build "every" block
for ( let block of settings.blocks || [] ) {
this.addExportDependency( `block-${block.id}`, block );
}
for ( let category of settings.categories || [] ) {
this.addExportDependency( `category-${category.name}`, category );
}
// no!
// copyProps({
// from: settings,
// into: this._dependencies.main,
// optional: [ 'categories', 'blocks' ]
// });
if ( settings.thumbnail ) {
this._dependencies.UI = [ settings.thumbnail ];
}
this.collectBlocks();
// immediately build the "first version" with only the
// directly referenced blocks (so, without the categories)
// this._build('main');
}
static _exportName = {
singular: 'blockSet',
plural: 'blockSets'
};
_onTreeSet() {
const component = this;
this._tree.on(
'change',
({ cls = [] }) => {
if ( cls.includes( Block, BlockCategory ) ) {
component.report({ msg: 'Blocks or block categories changed in package. Rebuilding.' });
component._rebuild();
}
},
this.id
);
this._rebuild();
}
_onTreeUnset() {
if (this._tree.hasListener('change', this.id)) {
this._tree.removeListener('change', this.id);
}
}
/**
* All blocks that are in this set. (Available after building)
* @type {Array<Block>}
*/
allBlocks = [];
/**
* Check whether a block is in the set
* @param {Block} block
* @returns {boolean}
*/
has(block) {
return this.allBlocks.includes(block);
}
/**
* Number of blocks in set
* @returns {number}
*/
size(quality = 'medium') {
return this.allBlocks.length;
}
collectBlocks() {
const blocksInSet = this._tree
? this._tree.blocks
.filter(mat =>
(mat.settings.categories || []).some(cat => (this.settings.categories || []).includes(cat))
||
this._settings.blocks.indexOf( mat ) > -1
)
: this._settings.blocks || []
;
this.allBlocks = blocksInSet;
this.report({ msg: `size ${blocksInSet.length} blocks` });
}
/**
* @protected
*/
async _build(part, quality) {
switch (part) {
case 'UI':
if (this._settings.thumbnail) {
this._setContent('UI', quality, this._settings.thumbnail.content.main[quality]);
}
else {
this._setContent('UI', quality, Project.defaultImages.missing.cloneNode(true));
}
break;
case 'main':
this.collectBlocks();
this._setContent('main', quality, this.allBlocks);
break;
default:
this._setContent(part, quality, null);
break;
}
return this;
}
}
export { BlockSet };