UNPKG

@nmmty/lazycanvas

Version:

A simple way to interact with @napi-rs/canvas in an advanced way!

144 lines (143 loc) 3.76 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.LayersManager = void 0; const Group_1 = require("../components/Group"); const LazyUtil_1 = require("../../utils/LazyUtil"); class LayersManager { map; debug; constructor(debug = false) { this.map = new Map(); this.debug = debug; } /** * Add a layer to the map * @param layers {AnyLayer[]} - The `layer` or `group` to add to the map */ 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:`, layer.toJSON()); if (this.map.has(layer.id)) throw new LazyUtil_1.LazyError("Layer already exists"); this.map.set(layer.id, layer); } this.sort(); return this; } /** * Remove a layer from the map * @param ids {string[]} - The `id` of the layer or group to remove */ remove(...ids) { for (const id of ids) { this.map.delete(id); } return this; } /** * ClearLayer all layers from the map */ clear() { this.map.clear(); return this; } /** * Get a layer from the map * @param id {string} - The `id` of the layer or group to get * @param cross {boolean} - Whether to search in groups or not */ get(id, cross = false) { if (cross) return this.crossSearch(id); else return this.map.get(id); } /** * Check if a layer exists in the map * @param id {string} - The `id` of the layer or group to check */ has(id) { return this.map.has(id); } /** * Get the size of the map */ size() { return this.map.size; } /** * Get the values of the map */ values() { return this.map.values(); } /** * Get the keys of the map */ keys() { return this.map.keys(); } /** * Get the entries of the map */ entries() { return this.map.entries(); } /** * For each layer in the map * @param callbackfn {Function} - The `callback` function to execute */ forEach(callbackfn) { this.map.forEach(callbackfn); return this; } /** * Convert the map to a JSON object */ toJSON() { return Object.fromEntries(this.map); } /** * Convert a JSON object to the map * @param json {object} - The `json` object to convert */ fromJSON(json) { this.map = new Map(Object.entries(json)); return this; } /** * Convert the map to an array */ toArray() { return Array.from(this.map.values()); } /** * Convert an array to the map * @param array {Array<AnyLayer>} - The `array` to convert */ fromArray(array) { this.map = new Map(array.map(l => [l.id, l])); return this; } sort() { this.fromArray(this.toArray().sort((a, b) => a.zIndex - b.zIndex)); } crossSearch(id) { for (const layer of Array.from(this.map.values())) { if (layer.id === id) return layer; if (layer instanceof Group_1.Group) { const result = layer.components.find(l => l.id === id); if (result) return result; } } return undefined; } } exports.LayersManager = LayersManager;