@aurigma/design-atoms
Version:
Design Atoms is a part of Customer's Canvas SDK which allows for manipulating individual design elements through your code.
269 lines • 16.6 kB
JavaScript
var __values = (this && this.__values) || function(o) {
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
if (m) return m.call(o);
if (o && typeof o.length === "number") return {
next: function () {
if (o && i >= o.length) o = void 0;
return { value: o && o[i++], done: !o };
}
};
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
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";
var HandlerFactoryByItem = /** @class */ (function () {
function HandlerFactoryByItem(canvas, textEditorControllerFactory, colorPreviewService, colorParser) {
var _this = this;
this._handlers = new Map();
this._beforeReplaceItemHandlerEvent = new EventObject();
this._afterReplaceItemHandlerEvent = new EventObject();
this.get = function (item) {
var _a;
if (!_this._handlers.has(item)) {
var handler_1 = _this._createItemHandler(item);
_this._handlers.set(item, handler_1);
_this._initItemHandler(handler_1);
}
var 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;
}
Object.defineProperty(HandlerFactoryByItem.prototype, "beforeReplaceItemHandlerEvent", {
get: function () {
return this._beforeReplaceItemHandlerEvent;
},
enumerable: true,
configurable: true
});
Object.defineProperty(HandlerFactoryByItem.prototype, "afterReplaceItemHandlerEvent", {
get: function () {
return this._afterReplaceItemHandlerEvent;
},
enumerable: true,
configurable: true
});
HandlerFactoryByItem.prototype.delete = function (handler) {
var _this = this;
if (handler instanceof GroupItemHandler)
handler.itemHandlers.forEach(function (childHandler) { return _this.delete(childHandler); });
if (handler instanceof PlaceholderItemHandler) {
if (handler.content)
this.delete(handler.content);
if (handler.topFrames)
handler.topFrames.forEach(function (child) { return _this.delete(child); });
if (handler.bottomFrames)
handler.bottomFrames.forEach(function (child) { return _this.delete(child); });
}
if (!this._handlers.has(handler.item)) {
return;
}
this._handlers.delete(handler.item);
handler.dispose();
};
HandlerFactoryByItem.prototype.clear = function () {
var e_1, _a;
try {
for (var _b = __values(this._handlers.values()), _c = _b.next(); !_c.done; _c = _b.next()) {
var handler = _c.value;
this.delete(handler);
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
}
finally { if (e_1) throw e_1.error; }
}
};
HandlerFactoryByItem.prototype.replaceItemHandler = function (item, newItem) {
this._canvas.pauseUpdateTexts();
var handler = this.get(item);
var selected = this._canvas.isItemHandlerSelected(handler);
this._beforeReplaceItemHandlerEvent.notify(handler);
this._handlers.delete(item);
var newHandler = this.get(newItem !== null && newItem !== void 0 ? newItem : item);
var parent = handler.parentGroupItemHandler;
if (parent != null) {
parent.replaceItemHandler(handler, newHandler);
}
else {
var itemHandlers = handler.layer.itemHandlers;
var 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);
};
HandlerFactoryByItem.prototype.replaceTextItemHandler = function (item) {
if (!(item instanceof BaseTextItem))
return;
var useOldHandler = this._useOldItemHandler(item, false);
var handler = this.get(item);
if (useOldHandler && handler instanceof NewBaseTextItemHandler || !useOldHandler && handler instanceof BaseTextItemHandler) {
this.replaceItemHandler(item);
}
};
HandlerFactoryByItem.prototype.setFontRegistry = function (fontRegistry) {
this._fontRegistry = fontRegistry;
};
HandlerFactoryByItem.prototype._initItemHandler = function (itemHandler) {
var _this = this;
if (itemHandler instanceof LayoutItemHandler) {
this._canvas.pauseUpdateTexts();
var childHandlers = itemHandler.item.items.select(function (item) { return _this.get(item); });
itemHandler.setItemHandlers(childHandlers.toArray());
this._canvas.resumeUpdateTexts();
}
};
HandlerFactoryByItem.prototype._createItemHandler = function (item) {
var _this = this;
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;
var apiClient = (_a = this._canvas) === null || _a === void 0 ? void 0 : _a.designAtomsApiClient;
var 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;
var 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: fitModeType, isCropMode: 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)) {
var placeholderHandler = new PlaceholderItemHandler(item, (_l = this._canvas) === null || _l === void 0 ? void 0 : _l.textWhizz, apiClient, this._colorPreviewService, { fitModeType: fitModeType, isCropMode: isCropMode });
//TODO track the change
if (item.content != null)
placeholderHandler.content = this.get(item.content);
placeholderHandler.bottomFrames = item.bottomFrames.map(function (fi) { return _this.get(fi); });
placeholderHandler.topFrames = item.topFrames.map(function (fi) { return _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(function (item) { return _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(function (item) { return _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");
};
Object.defineProperty(HandlerFactoryByItem.prototype, "_debugWarnEnabled", {
get: function () {
if (window["$DesignAtoms$WarnOnLegacyItemHandlers"] === undefined)
window["$DesignAtoms$WarnOnLegacyItemHandlers"] = false;
return window["$DesignAtoms$WarnOnLegacyItemHandlers"];
},
enumerable: true,
configurable: true
});
HandlerFactoryByItem.prototype._logUseOldItemHandler = function (item, reason) {
if (!this._debugWarnEnabled)
return;
console.warn("Legacy text item handler created for item [" + item.type + "#" + item.name + "] reason: " + reason + ".");
};
HandlerFactoryByItem.getUseLegacyTextReasons = function (item) {
var 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;
};
HandlerFactoryByItem.prototype._useOldItemHandler = function (item, force) {
if (force === true)
return true;
var reasons = HandlerFactoryByItem.getUseLegacyTextReasons(item);
if (reasons.length > 0) {
this._logUseOldItemHandler(item, reasons.join(", "));
return true;
}
return false;
};
return HandlerFactoryByItem;
}());
export { HandlerFactoryByItem };
//# sourceMappingURL=HandlerFactoryByItem.js.map