@aurigma/design-atoms
Version:
Design Atoms is a part of Customer's Canvas SDK which allows for manipulating individual design elements through your code.
396 lines • 19.3 kB
JavaScript
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
var __read = (this && this.__read) || function (o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m) return o;
var i = m.call(o), r, ar = [], e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
}
catch (error) { e = { error: error }; }
finally {
try {
if (r && !r.done && (m = i["return"])) m.call(i);
}
finally { if (e) throw e.error; }
}
return ar;
};
var __spread = (this && this.__spread) || function () {
for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i]));
return ar;
};
import { EventObject } from "@aurigma/design-atoms-model/EventObject";
import { BaseRectangleItemHandler } from "./BaseRectangleItemHandler";
import { Path, PointF } from "@aurigma/design-atoms-model/Math";
import { Graphics } from "../Graphics";
import { ShapeHandlerData } from "./ShapeHandlerData";
import { ClipartItem, WrappingMode } from "@aurigma/design-atoms-model/Product/Items";
import { PathHandler } from "./PathHandler";
import { Deferred } from "../Utils/Deferred";
export { WrappingMode };
var ShapeItemHandler = /** @class */ (function (_super) {
__extends(ShapeItemHandler, _super);
function ShapeItemHandler(path, item, textWhizz, apiClient, colorPreviewService) {
if (textWhizz === void 0) { textWhizz = null; }
if (apiClient === void 0) { apiClient = null; }
if (colorPreviewService === void 0) { colorPreviewService = null; }
var _this = _super.call(this, null, null, null, null, item, textWhizz, colorPreviewService) || this;
_this._apiClient = apiClient;
_this._originalPathId = null;
_this._isLoadingPath = false;
_this._isLoadedPath = false;
_this.originalPath = path != null ? path : new Path("");
_this._addedOnCanvasEvent = new EventObject();
if (item != null && item.sourcePathId != null) {
_this.setOriginalPathId(item.sourcePathId);
}
_this._allowNegativeResize = false;
_this._updateControlPointsSync();
return _this;
}
Object.defineProperty(ShapeItemHandler.prototype, "item", {
get: function () {
return _super.prototype._getItem.call(this);
},
set: function (item) {
_super.prototype._setItem.call(this, item);
},
enumerable: true,
configurable: true
});
Object.defineProperty(ShapeItemHandler.prototype, "originalPath", {
get: function () {
return this.item.sourcePath;
},
set: function (path) {
this.item.sourcePath = path;
this._isLoadedPath = true;
},
enumerable: true,
configurable: true
});
ShapeItemHandler.prototype.getOriginalPathId = function () {
return this._originalPathId;
};
ShapeItemHandler.prototype.setOriginalPathId = function (id) {
return __awaiter(this, void 0, void 0, function () {
var result, path, _a, ex_1;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
this._handlerUpdated = new Deferred();
this._originalPathId = id;
if (this._originalPathId == null)
return [2 /*return*/];
this._isLoadingPath = true;
this._isLoadedPath = false;
return [4 /*yield*/, this._waitAddOnCanvas()];
case 1:
_b.sent();
if (this.canvas == null)
return [2 /*return*/];
_b.label = 2;
case 2:
_b.trys.push([2, 4, 5, 6]);
result = this._apiClient.getContentPath(this._originalPathId);
_a = Path.bind;
return [4 /*yield*/, result];
case 3:
path = new (_a.apply(Path, [void 0, _b.sent()]))();
this.item.sourcePathLoaded = true;
this.originalPath = path;
this._updateControlPoints();
this._isLoadedPath = true;
this._handlerUpdated.resolve();
return [3 /*break*/, 6];
case 4:
ex_1 = _b.sent();
console.error("Unable to load content path. Reason: " + ex_1);
this._isLoadedPath = false;
this._handlerUpdated.reject(ex_1);
return [3 /*break*/, 6];
case 5:
this._isLoadingPath = false;
this.item.sourcePathLoaded = false;
this.item.getPathLoadedEvent().notify();
return [7 /*endfinally*/];
case 6:
this.canvas.redraw();
return [2 /*return*/];
}
});
});
};
ShapeItemHandler.prototype.drawItemHandler = function (itemHandlerCtx) {
if (itemHandlerCtx == null) {
return;
}
if (!this._isReadyToDraw) {
this.canvas.drawWaitClock(itemHandlerCtx, this.rectangle.center);
return;
}
if (this._hasVectorMask()) {
itemHandlerCtx.save();
this._clip(itemHandlerCtx);
}
var _a = this._getItemColorPreviews(), fillColorPreview = _a.fillColorPreview, borderColorPreview = _a.borderColorPreview, altBorderColorPreview = _a.altBorderColorPreview;
Graphics.path(itemHandlerCtx, this.item.sourcePath, this.getControlCenter(), this.item.transform, fillColorPreview === null || fillColorPreview === void 0 ? void 0 : fillColorPreview.toString(), 0, null, this.item.opacity, null);
var dash = this._getActualDash();
Graphics.drawStroke(itemHandlerCtx, this.item.sourcePath, this.getControlCenter(), this.item.transform, this._getActualBorderWidth(), borderColorPreview === null || borderColorPreview === void 0 ? void 0 : borderColorPreview.toString(), altBorderColorPreview === null || altBorderColorPreview === void 0 ? void 0 : altBorderColorPreview.toString(), this.item.opacity, dash);
if (this._hasVectorMask())
itemHandlerCtx.restore();
};
ShapeItemHandler.prototype.onTextWhizzInit = function () {
_super.prototype.onTextWhizzInit.call(this);
this._updateControlPoints();
};
ShapeItemHandler.prototype.getTextWrappingPath = function () {
if (this.item.textWrappingMode === WrappingMode.Tight)
return this._getTransformedPath();
return _super.prototype.getTextWrappingPath.call(this);
};
ShapeItemHandler.prototype._getColors = function () {
return __spread([this.item.fillColor, this.item.borderColor, this.item.altBorderColor], _super.prototype._getColors.call(this));
};
ShapeItemHandler.prototype._updateImpl = function (beforeUpdate, afterUpdate) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
if (typeof beforeUpdate != "function") {
return [2 /*return*/];
}
if (this.isVisible()) {
_super.prototype._update.call(this, null, beforeUpdate, afterUpdate);
}
else {
beforeUpdate();
}
return [2 /*return*/];
});
});
};
Object.defineProperty(ShapeItemHandler.prototype, "_areColorPreviewsReady", {
get: function () {
var _a = this._getItemColorPreviews(), fillColorPreview = _a.fillColorPreview, borderColorPreview = _a.borderColorPreview, altBorderColorPreview = _a.altBorderColorPreview;
return (this.item.fillColor == null || fillColorPreview != null)
&& (this.item.borderColor == null || borderColorPreview != null)
&& (this.item.altBorderColor == null || altBorderColorPreview != null);
},
enumerable: true,
configurable: true
});
Object.defineProperty(ShapeItemHandler.prototype, "_isReadyToDraw", {
get: function () {
return !this._isLoadingPath
&& this._isLoadedPath
&& this._areColorPreviewsReady;
},
enumerable: true,
configurable: true
});
ShapeItemHandler.prototype._setDataItem = function (item, itemHandlerData) {
_super.prototype._setDataItem.call(this, item, itemHandlerData);
this.item.fillColor = item.fillColor;
this.item.borderColor = item.borderColor;
this.item.sourcePath = item.sourcePath;
this.item.altBorderColor = item.altBorderColor;
this.item.borderWidth = item.borderWidth;
this.item.dash = item.dash;
this.item.fixedBorderWidth = item.fixedBorderWidth;
};
ShapeItemHandler.prototype._onItemPropertyChanged = function (sender, propertyName) {
var canvas = this.canvas;
switch (propertyName) {
case "fillColor":
case "borderColor":
case "altBorderColor":
if (!(this.item.parentGroupItem instanceof ClipartItem)) {
this.update();
if (this.canvas != null)
this.canvas.redraw();
}
break;
case "fixedBorderWidth":
case "borderWidth":
if (canvas != null) {
canvas.updateSelection();
canvas.redraw();
}
break;
case "sourceRectangle":
this.originalPath = Path.rectangle(this.item.sourceRectangle.left, this.item.sourceRectangle.top, this.item.sourceRectangle.width, this.item.sourceRectangle.height);
this._controlPoints = [
new PointF(this.item.sourceRectangle.left, this.item.sourceRectangle.top),
new PointF(this.item.sourceRectangle.right, this.item.sourceRectangle.bottom)
];
break;
default:
}
_super.prototype._onItemPropertyChanged.call(this, sender, propertyName);
};
ShapeItemHandler.prototype._getActualBorderWidth = function () {
var borderWidth = this.item.borderWidth;
var canvas = this.canvas;
if (this.item.fixedBorderWidth && canvas != null)
borderWidth /= canvas.zoom;
return borderWidth;
};
ShapeItemHandler.prototype._getBoundsMargin = function () {
return this._getActualBorderWidth();
};
ShapeItemHandler.prototype._getTransformedPath = function () {
var center = this.getControlCenter();
var path = this.item.sourcePath.clone();
path.transform(this.item.transform, center);
return path;
};
ShapeItemHandler.prototype._createDataInstance = function (itemHandler) {
return new ShapeHandlerData(itemHandler);
};
ShapeItemHandler.prototype._getItemColorPreviews = function () {
var colors = [this.item.fillColor, this.item.borderColor, this.item.altBorderColor];
var previews = this._colorPreviewService.getPreviews(colors);
var fillColorPreview = null;
if (this.item.fillColor != null) {
fillColorPreview = previews[0];
if (this.item.fillColor != null && fillColorPreview == null) {
this._colorPreviewService.subscribeToPreviewLoaded(this.item.fillColor, this._onColorPreviewLoaded);
}
}
var borderColorPreview = null;
if (this.item.borderColor != null) {
borderColorPreview = previews[1];
if (this.item.borderColor != null && borderColorPreview == null) {
this._colorPreviewService.subscribeToPreviewLoaded(this.item.borderColor, this._onColorPreviewLoaded);
}
}
var altBorderColorPreview = null;
if (this.item.altBorderColor != null) {
altBorderColorPreview = previews[2];
if (this.item.altBorderColor != null && altBorderColorPreview == null) {
this._colorPreviewService.subscribeToPreviewLoaded(this.item.altBorderColor, this._onColorPreviewLoaded);
}
}
return {
fillColorPreview: fillColorPreview,
borderColorPreview: borderColorPreview,
altBorderColorPreview: altBorderColorPreview
};
};
ShapeItemHandler.prototype._onAddedOnCanvas = function (canvas, supressUpdate) {
_super.prototype._onAddedOnCanvas.call(this, canvas, supressUpdate);
this._apiClient = canvas.designAtomsApiClient;
this._addedOnCanvasEvent.notify();
this._updateControlPoints();
};
ShapeItemHandler.prototype._updateControlPoints = function () {
return __awaiter(this, void 0, void 0, function () {
var bounds;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (this.item.sourcePath == null)
return [2 /*return*/];
return [4 /*yield*/, PathHandler.getBounds(this.item.sourcePath)];
case 1:
bounds = _a.sent();
this.controlPoints = [new PointF(bounds.left, bounds.top), new PointF(bounds.right, bounds.bottom)];
return [2 /*return*/];
}
});
});
};
ShapeItemHandler.prototype._updateControlPointsSync = function () {
if (this.item.sourcePath == null)
return;
var bounds = PathHandler.getBoundsSync(this.item.sourcePath);
if (bounds == null)
return;
this.controlPoints = [new PointF(bounds.left, bounds.top), new PointF(bounds.right, bounds.bottom)];
};
ShapeItemHandler.prototype._waitAddOnCanvas = function () {
return __awaiter(this, void 0, void 0, function () {
var _this = this;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (this.canvas != null)
return [2 /*return*/];
return [4 /*yield*/, new Promise(function (resolve) {
var handler = function () {
resolve();
_this._addedOnCanvasEvent.remove(handler);
};
_this._addedOnCanvasEvent.add(handler);
})];
case 1:
_a.sent();
return [2 /*return*/];
}
});
});
};
ShapeItemHandler.prototype._getActualDash = function () {
var dash = this.item.dash;
if (dash == null)
return dash;
var canvas = this.canvas;
var actualDash = dash.slice(0);
if (actualDash.length > 0 && this.item.fixedBorderWidth && canvas != null) {
for (var i = 0; i < actualDash.length; i++)
actualDash[i] /= canvas.zoom;
}
return actualDash;
};
ShapeItemHandler.typeName = "ShapeItemHandler";
return ShapeItemHandler;
}(BaseRectangleItemHandler));
export { ShapeItemHandler };
//# sourceMappingURL=ShapeItemHandler.js.map