UNPKG

@aurigma/design-atoms

Version:

Design Atoms is a part of Customer's Canvas SDK which allows for manipulating individual design elements through your code.

63 lines 2.93 kB
import { Surface, PrintArea, SurfaceMockup, BaseTextItem, LayoutItem, RectangleF } from "@aurigma/design-atoms-model"; import { NewBaseTextItemHandler } from "../ItemHandlers/NewBaseTextItemHandler"; export class SurfaceHandler { constructor(_surface) { this._surface = _surface; } getBounds(type) { const printAreasBounds = this.getPrintAreasBounds(type); return RectangleF.union(printAreasBounds, new RectangleF(0, 0, this._surface.width, this._surface.height)); } getPrintAreasBounds(type) { return RectangleF.getOverallBounds(this._surface.printAreas.toArray().map(p => p.getBounds(type))); } static getItemSurface(item) { if (item.parentGroupItem) return SurfaceHandler.getItemSurface(item.parentGroupItem); let container = item.parentContainer; if (!container || !container.parentComponent) return null; if (container.parentComponent instanceof Surface) return container.parentComponent; else if (container.parentComponent instanceof PrintArea) return container.parentComponent.parentSurface; else if (container.parentComponent instanceof SurfaceMockup) return container.parentComponent.parentSurface; return null; } static async applyLayout(handlerFactory, canvas, surface) { let layoutItemsHandlers = surface.getAllItems({ flatGroupItems: true, ignoreMockups: true }) .where(x => x.type == LayoutItem.type) .select(x => handlerFactory.get(x)); if (layoutItemsHandlers.any()) { let textItemHandlers = surface.getAllItems({ flatGroupItems: true, ignoreMockups: true }) .ofType(BaseTextItem) .select(x => handlerFactory.get(x)) .where(x => x instanceof NewBaseTextItemHandler).toArray(); await SurfaceHandler._updateTextItemHandlers(textItemHandlers, canvas); } for (let handler of layoutItemsHandlers) handler.applyLayout(); } // this method update TextItemHandlers (new) that are not on the current canvas to allow applyLayout work correctly // TODO: need to make solution to update text items handlers that are not on the canvas static async _updateTextItemHandlers(textItemHandlers, canvas) { canvas.pauseUpdateTexts(); let promises = textItemHandlers.map(async (x) => { if (x.canvas) return; // hack to update text itemHandler try { x.canvas = canvas; x.update(); await x.waitUpdate(); } finally { x.canvas = null; } }); canvas.resumeUpdateTexts(); await Promise.all(promises); } } //# sourceMappingURL=SurfaceHandler.js.map