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.

100 lines 5.39 kB
import { AutoPreviewContainersSource } from "./AutoPreviewContainersSource"; import { ProductPreviewRenderer } from "./ProductPreviewRenderer"; import { TextsWorkaroundItemsComparer } from "./TextsWorkaroundItemsComparer"; export class ProductPreviewService { constructor(productContext, designAtomsApiClient, colorPreviewService, service, viewerConfigurationContext, colorParser) { this._onCurrentSurfaceChanged = () => { var _a; if (this._productPreviewEnabled) { (_a = this._productPreviewRenderer) === null || _a === void 0 ? void 0 : _a.dispose(); this._productPreviewRenderer = this._createPreviewRenderer(); } }; this._productContext = productContext; this._designAtomsApiClient = designAtomsApiClient; this._colorPreviewService = colorPreviewService; this._service = service; this._viewerConfigurationContext = viewerConfigurationContext; this._colorParser = colorParser; } get _productPreviewEnabled() { return this._productPreviewRenderer != null; } get _currentSurfaceIndex() { return this._productContext.product.surfaces.indexOf(this._productContext.currentSurface); } /** * @param surfaceIndex Index of the surface to preview * @param options Preview rendering options * @returns Blob containing the preview image of given surface */ async getSurfacePreview(surfaceIndex, options) { if (!this._productPreviewEnabled) { this._enableProductPreview(); } if (this._productPreviewRenderer == null) { throw new Error("ProductPreviewRenderer is not initialized"); } if (surfaceIndex < 0 || surfaceIndex >= this._productContext.product.surfaces.length) { throw new Error("surfaceIndex was out of bounds"); } const surface = this._productContext.product.surfaces.get(surfaceIndex); if ((options === null || options === void 0 ? void 0 : options.printAreaIndex) != null && ((options === null || options === void 0 ? void 0 : options.printAreaIndex) < 0 || (options === null || options === void 0 ? void 0 : options.printAreaIndex) >= surface.printAreas.length)) { throw new Error("printAreaIndex was out of bounds"); } const preview = document.createElement("canvas"); const ignoreContainers = this._getIgnoreContainers(surface, options); const { size, offset } = this._getPreviewSizeAndOffset(surface, options); await this._productPreviewRenderer.waitUpdateAndExecute(surfaceIndex, () => { this._productPreviewRenderer.render(preview, surfaceIndex, container => !(ignoreContainers === null || ignoreContainers === void 0 ? void 0 : ignoreContainers.includes(container.id)), size, offset); }); return await new Promise(resolve => { preview.toBlob(blob => resolve(blob)); }); } /** * @param options Preview rendering options * @returns Blob containing the preview image of the current surface */ getCurrentSurfacePreview(options) { return this.getSurfacePreview(this._currentSurfaceIndex, options); } dispose() { var _a, _b; (_a = this._productPreviewRenderer) === null || _a === void 0 ? void 0 : _a.dispose(); this._productPreviewRenderer = null; (_b = this._productContext.currentSurfaceChanged) === null || _b === void 0 ? void 0 : _b.remove(this._onCurrentSurfaceChanged); } _enableProductPreview() { this._productPreviewRenderer = this._createPreviewRenderer(); this._productContext.currentSurfaceChanged.add(this._onCurrentSurfaceChanged); } _createPreviewRenderer() { return new ProductPreviewRenderer(this._productContext.product, { create: sourceSurface => new AutoPreviewContainersSource(sourceSurface, new TextsWorkaroundItemsComparer()) }, this._designAtomsApiClient, this._colorPreviewService, this._service, this._viewerConfigurationContext, this._colorParser); } _getIgnoreContainers(surface, options) { var _a; const mockupContainers = (options === null || options === void 0 ? void 0 : options.includeMockup) ? [] : surface.mockup.allContainers.toArray(); const mockupContainersIds = mockupContainers.map(container => container.id); return mockupContainersIds.concat((_a = options === null || options === void 0 ? void 0 : options.ignoreContainers) !== null && _a !== void 0 ? _a : []); } _getPreviewSizeAndOffset(surface, options) { if ((options === null || options === void 0 ? void 0 : options.includeMockup) || (options === null || options === void 0 ? void 0 : options.printAreaIndex) == null) { return { size: null, offset: null }; } const printAreaBounds = surface.printAreas.get(options === null || options === void 0 ? void 0 : options.printAreaIndex).bounds; return { size: printAreaBounds, offset: { x: -printAreaBounds.left, y: -printAreaBounds.top } }; } } //# sourceMappingURL=ProductPreviewService.js.map