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.

229 lines 14.4 kB
import { BaseTextItem, BoundedTextItem, AutoScaledTextItem } from "@aurigma/design-atoms-model/Product/Items"; import { isArchedTextItem, isAutoScaledTextItem, isBarcodeItem, isBoundedTextItem, isClipartItem, isCurvedTextItem, isDashedLineItem, isEllipseItem, isGridItem, isGroupItem, isImageItem, isLayoutItem, isLineItem, isPathBoundedTextItem, isPlaceholderItem, isPlainTextItem, isPolylineItem, isRectangleItem, isShapeItem } from "@aurigma/design-atoms-model/Utils/Items"; import { RectangleItemHandler } from "./RectangleItemHandler"; import { ImageItemHandler } from "./ImageItemHandler"; import { PlaceholderItemHandler } from "./PlaceholderItemHandler"; import { NotImplementedException } from "@aurigma/design-atoms-model/Exception"; import { BarcodeItemHandler } from "./BarcodeItemHandler"; import { BoundedTextItemHandler } from "./BoundedTextItemHandler"; import { CurvedTextItemHandler } from "./CurvedTextItemHandler"; import { PathBoundedTextItemHandler } from "./PathBoundedTextItemHandler"; import { ShapeItemHandler } from "./ShapeItemHandler"; import { LineItemHandler } from "./LineItemHandler"; import { PolylineItemHandler } from "./PolylineItemHandler"; import { PlainTextItemHandler } from "./PlainTextItemHandler"; import { DashedLineItemHandler } from "./DashedLineItemHandler"; import { AutoScaledTextItemHandler } from "./AutoScaledTextItemHandler"; import { GridItemHandler } from "./GridItemHandler"; import { EllipseItemHandler } from "./EllipseItemHandler"; import { GroupItemHandler } from "./GroupItemHandler"; import { NewBoundedTextItemHandler } from "./NewBoundedTextItemHandler"; import { NewCurvedTextItemHandler } from "./NewCurvedTextItemHandler"; import { ClipartItemHandler, NewPlainTextItemHandler, NewBaseTextItemHandler, BaseTextItemHandler } from "."; import { EventObject } from "@aurigma/design-atoms-model/EventObject"; import { ArchedTextItemHandler } from "./ArchedTextItemHandler"; import { LayoutItemHandler } from "./LayoutItemHandler"; import { StringUtils } from "../Utils/StringUtils"; import { NewArchedTextItemHandler } from "./NewArchedTextItemHandler"; import { PlaceholderFitModeType } from "../Viewer/Interfaces"; export class HandlerFactoryByItem { constructor(canvas, textEditorControllerFactory, colorPreviewService, colorParser) { this._handlers = new Map(); this._beforeReplaceItemHandlerEvent = new EventObject(); this._afterReplaceItemHandlerEvent = new EventObject(); this.get = (item) => { var _a; if (!this._handlers.has(item)) { const handler = this._createItemHandler(item); this._handlers.set(item, handler); this._initItemHandler(handler); } const handler = this._handlers.get(item); if (((_a = this._canvas) === null || _a === void 0 ? void 0 : _a.doesContainItem(item)) && handler.canvas != this._canvas) handler.canvas = this._canvas; return handler; }; this._canvas = canvas; this._fontRegistry = canvas.fontRegistry; this._textEditorControllerFactory = textEditorControllerFactory; this._colorPreviewService = colorPreviewService; this._colorParser = colorParser; } get beforeReplaceItemHandlerEvent() { return this._beforeReplaceItemHandlerEvent; } get afterReplaceItemHandlerEvent() { return this._afterReplaceItemHandlerEvent; } delete(handler) { if (handler instanceof GroupItemHandler) handler.itemHandlers.forEach(childHandler => this.delete(childHandler)); if (handler instanceof PlaceholderItemHandler) { if (handler.content) this.delete(handler.content); if (handler.topFrames) handler.topFrames.forEach(child => this.delete(child)); if (handler.bottomFrames) handler.bottomFrames.forEach(child => this.delete(child)); } if (!this._handlers.has(handler.item)) { return; } this._handlers.delete(handler.item); handler.dispose(); } clear() { for (const handler of this._handlers.values()) { this.delete(handler); } } replaceItemHandler(item, newItem) { this._canvas.pauseUpdateTexts(); const handler = this.get(item); const selected = this._canvas.isItemHandlerSelected(handler); this._beforeReplaceItemHandlerEvent.notify(handler); this._handlers.delete(item); const newHandler = this.get(newItem !== null && newItem !== void 0 ? newItem : item); const parent = handler.parentGroupItemHandler; if (parent != null) { parent.replaceItemHandler(handler, newHandler); } else { const itemHandlers = handler.layer.itemHandlers; const index = itemHandlers.indexOf(handler); itemHandlers.remove(handler); itemHandlers.insertAt(index, newHandler); } handler.dispose(); handler.item = null; this._canvas.resumeUpdateTexts(); this._afterReplaceItemHandlerEvent.notify(newHandler); if (selected) this._canvas.addSelectedItemHandler(newHandler); } replaceTextItemHandler(item) { if (!(item instanceof BaseTextItem)) return; const useOldHandler = this._useOldItemHandler(item, false); const handler = this.get(item); if (useOldHandler && handler instanceof NewBaseTextItemHandler || !useOldHandler && handler instanceof BaseTextItemHandler) { this.replaceItemHandler(item); } } setFontRegistry(fontRegistry) { this._fontRegistry = fontRegistry; } _initItemHandler(itemHandler) { if (itemHandler instanceof LayoutItemHandler) { this._canvas.pauseUpdateTexts(); const childHandlers = itemHandler.item.items.select(item => this.get(item)); itemHandler.setItemHandlers(childHandlers.toArray()); this._canvas.resumeUpdateTexts(); } } _createItemHandler(item) { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0, _1, _2, _3, _4, _5, _6, _7; const apiClient = (_a = this._canvas) === null || _a === void 0 ? void 0 : _a.designAtomsApiClient; const isCropMode = (_d = (_c = (_b = this._canvas.viewerConfiguration) === null || _b === void 0 ? void 0 : _b.handlers) === null || _c === void 0 ? void 0 : _c.placeholderCropMode) !== null && _d !== void 0 ? _d : true; const fitModeType = (_g = (_f = (_e = this._canvas.viewerConfiguration) === null || _e === void 0 ? void 0 : _e.handlers) === null || _f === void 0 ? void 0 : _f.placeholderFitMode) !== null && _g !== void 0 ? _g : PlaceholderFitModeType.Always; if (isRectangleItem(item)) return new RectangleItemHandler(item, (_h = this._canvas) === null || _h === void 0 ? void 0 : _h.textWhizz, apiClient, this._colorPreviewService); if (isImageItem(item)) { return new ImageItemHandler(item, (_j = this._canvas) === null || _j === void 0 ? void 0 : _j.textWhizz, apiClient, this._colorPreviewService, { fitModeType, isCropMode }); } if (isBarcodeItem(item)) return new BarcodeItemHandler(item, (_k = this._canvas) === null || _k === void 0 ? void 0 : _k.textWhizz, apiClient, this._colorPreviewService); if (isPlaceholderItem(item)) { const placeholderHandler = new PlaceholderItemHandler(item, (_l = this._canvas) === null || _l === void 0 ? void 0 : _l.textWhizz, apiClient, this._colorPreviewService, { fitModeType, isCropMode }); //TODO track the change if (item.content != null) placeholderHandler.content = this.get(item.content); placeholderHandler.bottomFrames = item.bottomFrames.map(fi => this.get(fi)); placeholderHandler.topFrames = item.topFrames.map(fi => this.get(fi)); return placeholderHandler; } if (item instanceof BaseTextItem) { if (this._useOldItemHandler(item, !((_m = this._canvas) === null || _m === void 0 ? void 0 : _m.frontEndTextRenderingEnabled))) { if (isBoundedTextItem(item)) return new BoundedTextItemHandler(item, (_o = this._canvas) === null || _o === void 0 ? void 0 : _o.textWhizz, apiClient, this._colorPreviewService); if (isPathBoundedTextItem(item)) return new PathBoundedTextItemHandler(item, (_p = this._canvas) === null || _p === void 0 ? void 0 : _p.textWhizz, apiClient, this._colorPreviewService); if (isCurvedTextItem(item)) return new CurvedTextItemHandler(item, (_q = this._canvas) === null || _q === void 0 ? void 0 : _q.textWhizz, apiClient, this._colorPreviewService); if (isPlainTextItem(item)) return new PlainTextItemHandler(item, (_r = this._canvas) === null || _r === void 0 ? void 0 : _r.textWhizz, apiClient, this._colorPreviewService); if (isAutoScaledTextItem(item)) return new AutoScaledTextItemHandler(item, (_s = this._canvas) === null || _s === void 0 ? void 0 : _s.textWhizz, apiClient, this._colorPreviewService); if (isArchedTextItem(item)) return new ArchedTextItemHandler(item, (_t = this._canvas) === null || _t === void 0 ? void 0 : _t.textWhizz, apiClient, this._colorPreviewService); } else { if (isBoundedTextItem(item) || isPathBoundedTextItem(item)) return new NewBoundedTextItemHandler(this._fontRegistry, this._textEditorControllerFactory, item, (_u = this._canvas) === null || _u === void 0 ? void 0 : _u.textWhizz, apiClient, this._colorPreviewService, this._colorParser); if (isCurvedTextItem(item)) return new NewCurvedTextItemHandler(this._fontRegistry, this._textEditorControllerFactory, item, (_v = this._canvas) === null || _v === void 0 ? void 0 : _v.textWhizz, apiClient, this._colorPreviewService, this._colorParser); if (isPlainTextItem(item)) return new NewPlainTextItemHandler(this._fontRegistry, this._textEditorControllerFactory, item, (_w = this._canvas) === null || _w === void 0 ? void 0 : _w.textWhizz, apiClient, this._colorPreviewService, this._colorParser); if (isArchedTextItem(item)) return new NewArchedTextItemHandler(this._fontRegistry, this._textEditorControllerFactory, item, (_x = this._canvas) === null || _x === void 0 ? void 0 : _x.textWhizz, apiClient, this._colorPreviewService, this._colorParser); } } if (isLineItem(item)) return new LineItemHandler(item, (_y = this._canvas) === null || _y === void 0 ? void 0 : _y.textWhizz, this._colorPreviewService); if (isDashedLineItem(item)) return new DashedLineItemHandler(item, (_z = this._canvas) === null || _z === void 0 ? void 0 : _z.textWhizz, this._colorPreviewService); if (isShapeItem(item)) return new ShapeItemHandler(item.sourcePath, item, (_0 = this._canvas) === null || _0 === void 0 ? void 0 : _0.textWhizz, apiClient, this._colorPreviewService); if (isPolylineItem(item)) return new PolylineItemHandler(item, (_1 = this._canvas) === null || _1 === void 0 ? void 0 : _1.textWhizz, this._colorPreviewService); if (isGridItem(item)) return new GridItemHandler(item, (_2 = this._canvas) === null || _2 === void 0 ? void 0 : _2.textWhizz, this._colorPreviewService); if (isEllipseItem(item)) return new EllipseItemHandler(item, (_3 = this._canvas) === null || _3 === void 0 ? void 0 : _3.textWhizz, this._colorPreviewService); if (isGroupItem(item)) return new GroupItemHandler(item, item.items.toArray().map(item => this.get(item)), (_4 = this._canvas) === null || _4 === void 0 ? void 0 : _4.textWhizz); if (isClipartItem(item)) return new ClipartItemHandler(item, item.items.toArray().map(item => this.get(item)), (_5 = this._canvas) === null || _5 === void 0 ? void 0 : _5.textWhizz); if (isLayoutItem(item)) return new LayoutItemHandler(item, null, (_6 = this._canvas) === null || _6 === void 0 ? void 0 : _6.history, (_7 = this._canvas) === null || _7 === void 0 ? void 0 : _7.textWhizz); else throw new NotImplementedException(`Item of type ${item.type} not supported`); } get _debugWarnEnabled() { if (window["$DesignAtoms$WarnOnLegacyItemHandlers"] === undefined) window["$DesignAtoms$WarnOnLegacyItemHandlers"] = false; return window["$DesignAtoms$WarnOnLegacyItemHandlers"]; } _logUseOldItemHandler(item, reason) { if (!this._debugWarnEnabled) return; console.warn(`Legacy text item handler created for item [${item.type}#${item.name}] reason: ${reason}.`); } static getUseLegacyTextReasons(item) { const reasons = []; if (StringUtils.containsRtlChars(item.text)) reasons.push("RTL text"); if (item instanceof AutoScaledTextItem) reasons.push("auto-scaled text"); if (item.shadow != null) reasons.push("shadow"); if (item.overlapLinesEnabled) reasons.push("overlap lines enabled"); if (item instanceof BoundedTextItem) { if (item.scheduledFitMode != null) reasons.push(`scheduledFitMode = ${item.scheduledFitMode}`); } return reasons; } _useOldItemHandler(item, force) { if (force === true) return true; const reasons = HandlerFactoryByItem.getUseLegacyTextReasons(item); if (reasons.length > 0) { this._logUseOldItemHandler(item, reasons.join(", ")); return true; } return false; } } //# sourceMappingURL=HandlerFactoryByItem.js.map