@aurigma/design-atoms
Version:
Design Atoms is a part of Customer's Canvas SDK which allows for manipulating individual design elements through your code.
321 lines • 17.7 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, RectangleItem, ImageItem, PlaceholderItem, BarcodeItem, BoundedTextItem, CurvedTextItem, PathBoundedTextItem, ShapeItem, LineItem, PolylineItem, PlainTextItem, DashedLineItem, AutoScaledTextItem, GridItem, EllipseItem, GroupItem, ClipartItem, ArchedTextItem, LayoutItem } from "@aurigma/design-atoms-model/Product/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";
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;
var apiClient = (_a = this._canvas) === null || _a === void 0 ? void 0 : _a.designAtomsApiClient;
if (isRectangleItem(item))
return new RectangleItemHandler(item, (_b = this._canvas) === null || _b === void 0 ? void 0 : _b.textWhizz, apiClient, this._colorPreviewService);
if (isImageItem(item))
return new ImageItemHandler(item, (_c = this._canvas) === null || _c === void 0 ? void 0 : _c.textWhizz, apiClient, this._colorPreviewService);
if (isBarcodeItem(item))
return new BarcodeItemHandler(item, (_d = this._canvas) === null || _d === void 0 ? void 0 : _d.textWhizz, apiClient, this._colorPreviewService);
if (isPlaceholderItem(item)) {
var placeholderHandler = new PlaceholderItemHandler(item, (_e = this._canvas) === null || _e === void 0 ? void 0 : _e.textWhizz, apiClient, this._colorPreviewService);
//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, !((_f = this._canvas) === null || _f === void 0 ? void 0 : _f.frontEndTextRenderingEnabled))) {
if (isBoundedTextItem(item))
return new BoundedTextItemHandler(item, (_g = this._canvas) === null || _g === void 0 ? void 0 : _g.textWhizz, apiClient, this._colorPreviewService);
if (isPathBoundedTextItem(item))
return new PathBoundedTextItemHandler(item, (_h = this._canvas) === null || _h === void 0 ? void 0 : _h.textWhizz, apiClient, this._colorPreviewService);
if (isCurvedTextItem(item))
return new CurvedTextItemHandler(item, (_j = this._canvas) === null || _j === void 0 ? void 0 : _j.textWhizz, apiClient, this._colorPreviewService);
if (isPlainTextItem(item))
return new PlainTextItemHandler(item, (_k = this._canvas) === null || _k === void 0 ? void 0 : _k.textWhizz, apiClient, this._colorPreviewService);
if (isAutoScaledTextItem(item))
return new AutoScaledTextItemHandler(item, (_l = this._canvas) === null || _l === void 0 ? void 0 : _l.textWhizz, apiClient, this._colorPreviewService);
if (isArchedTextItem(item))
return new ArchedTextItemHandler(item, (_m = this._canvas) === null || _m === void 0 ? void 0 : _m.textWhizz, apiClient, this._colorPreviewService);
}
else {
if (isBoundedTextItem(item) || isPathBoundedTextItem(item))
return new NewBoundedTextItemHandler(this._fontRegistry, this._textEditorControllerFactory, item, (_o = this._canvas) === null || _o === void 0 ? void 0 : _o.textWhizz, apiClient, this._colorPreviewService, this._colorParser);
if (isCurvedTextItem(item))
return new NewCurvedTextItemHandler(this._fontRegistry, this._textEditorControllerFactory, item, (_p = this._canvas) === null || _p === void 0 ? void 0 : _p.textWhizz, apiClient, this._colorPreviewService, this._colorParser);
if (isPlainTextItem(item))
return new NewPlainTextItemHandler(this._fontRegistry, this._textEditorControllerFactory, item, (_q = this._canvas) === null || _q === void 0 ? void 0 : _q.textWhizz, apiClient, this._colorPreviewService, this._colorParser);
if (isArchedTextItem(item))
return new NewArchedTextItemHandler(this._fontRegistry, this._textEditorControllerFactory, item, (_r = this._canvas) === null || _r === void 0 ? void 0 : _r.textWhizz, apiClient, this._colorPreviewService, this._colorParser);
}
}
if (isLineItem(item))
return new LineItemHandler(item, (_s = this._canvas) === null || _s === void 0 ? void 0 : _s.textWhizz, this._colorPreviewService);
if (isDashedLineItem(item))
return new DashedLineItemHandler(item, (_t = this._canvas) === null || _t === void 0 ? void 0 : _t.textWhizz, this._colorPreviewService);
if (isShapeItem(item))
return new ShapeItemHandler(item.sourcePath, item, (_u = this._canvas) === null || _u === void 0 ? void 0 : _u.textWhizz, apiClient, this._colorPreviewService);
if (isPolylineItem(item))
return new PolylineItemHandler(item, (_v = this._canvas) === null || _v === void 0 ? void 0 : _v.textWhizz, this._colorPreviewService);
if (isGridItem(item))
return new GridItemHandler(item, (_w = this._canvas) === null || _w === void 0 ? void 0 : _w.textWhizz, this._colorPreviewService);
if (isEllipseItem(item))
return new EllipseItemHandler(item, (_x = this._canvas) === null || _x === void 0 ? void 0 : _x.textWhizz, this._colorPreviewService);
if (isGroupItem(item))
return new GroupItemHandler(item, item.items.toArray().map(function (item) { return _this.get(item); }), (_y = this._canvas) === null || _y === void 0 ? void 0 : _y.textWhizz);
if (isClipartItem(item))
return new ClipartItemHandler(item, item.items.toArray().map(function (item) { return _this.get(item); }), (_z = this._canvas) === null || _z === void 0 ? void 0 : _z.textWhizz);
if (isLayoutItem(item))
return new LayoutItemHandler(item, null, (_0 = this._canvas) === null || _0 === void 0 ? void 0 : _0.history, (_1 = this._canvas) === null || _1 === void 0 ? void 0 : _1.textWhizz);
else
throw new NotImplementedException("Item of type " + item.type + " not supported");
function isRectangleItem(i) {
return i.type === RectangleItem.type;
}
function isImageItem(i) {
return (i === null || i === void 0 ? void 0 : i.type) === ImageItem.type;
}
function isPlaceholderItem(i) {
return i.type === PlaceholderItem.type;
}
function isBarcodeItem(i) {
return i.type === BarcodeItem.type;
}
function isBoundedTextItem(i) {
return i.type === BoundedTextItem.type;
}
function isCurvedTextItem(i) {
return i.type === CurvedTextItem.type;
}
function isAutoScaledTextItem(i) {
return i.type === AutoScaledTextItem.type;
}
function isPlainTextItem(i) {
return i.type === PlainTextItem.type;
}
function isPathBoundedTextItem(i) {
return i.type === PathBoundedTextItem.type;
}
function isArchedTextItem(i) {
return i.type === ArchedTextItem.type;
}
function isLineItem(i) {
return i.type === LineItem.type;
}
function isDashedLineItem(i) {
return i.type === DashedLineItem.type;
}
function isPolylineItem(i) {
return i.type === PolylineItem.type;
}
function isShapeItem(i) {
return i.type === ShapeItem.type;
}
function isGridItem(i) {
return i.type === GridItem.type;
}
function isEllipseItem(i) {
return i.type === EllipseItem.type;
}
function isGroupItem(i) {
return i.type === GroupItem.type;
}
function isClipartItem(i) {
return i.type === ClipartItem.type;
}
function isLayoutItem(i) {
return i.type === LayoutItem.type;
}
};
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