@nmmty/lazycanvas
Version:
A simple way to interact with @napi-rs/canvas in an advanced way!
182 lines (181 loc) • 5.97 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.LayersManager = void 0;
const components_1 = require("../components");
const LazyUtil_1 = require("../../utils/LazyUtil");
/**
* Class representing a manager for handling layers and groups.
*/
class LayersManager {
/**
* A map storing layers or groups with their IDs as keys.
*/
map;
/**
* Whether debugging is enabled.
*/
debug;
/**
* Constructs a new LayersManager instance.
* @param opts {Object} - Optional settings for the LayersManager.
* @param opts.debug {boolean} - Whether debugging is enabled.
*/
constructor(opts) {
this.map = new Map();
this.debug = opts?.debug || false;
}
/**
* Adds layers or groups to the map.
* @param layers {Array<AnyLayer | Group>} - The layers or groups to add to the map.
* @returns {this} The current instance for chaining.
* @throws {LazyError} If a layer with the same ID already exists.
*/
add(...layers) {
if (this.debug)
LazyUtil_1.LazyLog.log('info', `Adding layers...\nlength: ${layers.length}`);
let layersArray = layers.flat();
layersArray = layersArray.filter(l => l !== undefined);
for (const layer of layersArray) {
if (this.debug)
LazyUtil_1.LazyLog.log('none', `Data:`, 'toJSON' in layer ? layer.toJSON() : layer);
if (this.map.has(layer.id))
throw new LazyUtil_1.LazyError("Layer already exists");
this.map.set(layer.id, layer);
}
this.sort();
return this;
}
/**
* Removes layers or groups from the map by their IDs.
* @param ids {string[]} - The IDs of the layers or groups to remove.
* @returns {this} The current instance for chaining.
*/
remove(...ids) {
for (const id of ids) {
this.map.delete(id);
}
return this;
}
/**
* Clears all layers and groups from the map.
* @returns {this} The current instance for chaining.
*/
clear() {
this.map.clear();
return this;
}
/**
* Retrieves a layer or group from the map by its ID.
* @param id {string} - The ID of the layer or group to retrieve.
* @param cross {boolean} - Whether to search within groups for the ID.
* @returns {AnyLayer | Group | undefined} The retrieved layer or group, or undefined if not found.
*/
get(id, cross = false) {
if (cross)
return this.crossSearch(id);
else
return this.map.get(id);
}
/**
* Checks if a layer or group exists in the map by its ID.
* @param id {string} - The ID of the layer or group to check.
* @returns {boolean} True if the layer or group exists, false otherwise.
*/
has(id) {
return this.map.has(id);
}
/**
* Retrieves the number of layers and groups in the map.
* @returns {number} The size of the map.
*/
size() {
return this.map.size;
}
/**
* Retrieves the values (layers and groups) from the map.
* @returns {IterableIterator<AnyLayer | Group>} An iterator for the map values.
*/
values() {
return this.map.values();
}
/**
* Retrieves the keys (IDs) from the map.
* @returns {IterableIterator<string>} An iterator for the map keys.
*/
keys() {
return this.map.keys();
}
/**
* Retrieves the entries (key-value pairs) from the map.
* @returns {IterableIterator<[string, AnyLayer | Group]>} An iterator for the map entries.
*/
entries() {
return this.map.entries();
}
/**
* Executes a callback function for each layer or group in the map.
* @param callbackfn {Function} - The callback function to execute.
* @returns {this} The current instance for chaining.
*/
forEach(callbackfn) {
this.map.forEach(callbackfn);
return this;
}
/**
* Converts the map to a JSON object.
* @returns {object} The JSON representation of the map.
*/
toJSON() {
return Object.fromEntries(this.map);
}
/**
* Populates the map from a JSON object.
* @param json {object} - The JSON object to populate the map from.
* @returns {this} The current instance for chaining.
*/
fromJSON(json) {
this.map = new Map(Object.entries(json));
return this;
}
/**
* Converts the map to an array of layers and groups.
* @returns {Array<AnyLayer | Group>} An array of layers and groups.
*/
toArray() {
return Array.from(this.map.values());
}
/**
* Populates the map from an array of layers and groups.
* @param array {Array<AnyLayer | Group>} - The array of layers and groups to populate the map from.
* @returns {this} The current instance for chaining.
*/
fromArray(array) {
this.map = new Map(array.map(l => [l.id, l]));
return this;
}
/**
* Sorts the layers and groups in the map by their zIndex property.
* @returns {void}
*/
sort() {
this.fromArray(this.toArray().sort((a, b) => a.zIndex - b.zIndex));
}
/**
* Searches for a layer or group by its ID, including within groups.
* @param id {string} - The ID of the layer or group to search for.
* @returns {AnyLayer | Group | undefined} The found layer or group, or undefined if not found.
*/
crossSearch(id) {
for (const layer of Array.from(this.map.values())) {
if (layer.id === id)
return layer;
if (layer instanceof components_1.Group) {
const result = layer.layers.find(l => l.id === id);
if (result)
return result;
}
}
return undefined;
}
}
exports.LayersManager = LayersManager;