@aurigma/design-atoms
Version:
Design Atoms is a part of Customer's Canvas SDK which allows for manipulating individual design elements through your code.
378 lines • 20.2 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 __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 { EqualsOfFloatNumbers, Path, RotatedRectangleF, PointF } from "@aurigma/design-atoms-model/Math";
import { NewBaseTextItemHandler } from "./NewBaseTextItemHandler";
import { Graphics } from "../Graphics";
import { BoundedTextItem, FirstBaselineOffset, OverflowStrategy, PathBoundedTextItem, WrappingMode } from "@aurigma/design-atoms-model/Product/Items";
import { TextFrameType } from "@aurigma/design-atoms-text/TextEditor/Enums/TextFrameType";
import { ResizeHelper } from "../Services/Selection/SelectionHandler";
import { fromTextWhizzPath, toTextWhizzPath } from "@aurigma/design-atoms-text/Utils/PathUtils";
var NewBoundedTextItemHandler = /** @class */ (function (_super) {
__extends(NewBoundedTextItemHandler, _super);
function NewBoundedTextItemHandler(fontRegistry, textEditorControllerFactory, item, textWhizz, apiClient, colorPreviewService, colorParser) {
if (textWhizz === void 0) { textWhizz = null; }
var _this = _super.call(this, fontRegistry, textEditorControllerFactory, item, textWhizz, apiClient, colorPreviewService, colorParser) || this;
_this.lastRectangle = null;
return _this;
}
Object.defineProperty(NewBoundedTextItemHandler.prototype, "item", {
get: function () {
return this._getItem();
},
set: function (item) {
_super.prototype._setItem.call(this, item);
},
enumerable: true,
configurable: true
});
Object.defineProperty(NewBoundedTextItemHandler.prototype, "drawingRectangle", {
get: function () {
var isResizing = this.canvas.isItemHandlerSelected(this) && this.canvas.isSelectionResizing;
if (this._staticTextRect == null)
return isResizing && this.startRectangle != null ? this.startRectangle : this.rectangle;
var rectangle = RotatedRectangleF.fromRectangleF(this._staticTextRect);
var transfrom = (isResizing && this.startRectangle != null) ?
this.startRectangle.getTransform(RotatedRectangleF.fromRectangleF(this.getControlBounds())) :
this.item.transform.clone();
rectangle.transform(transfrom, this.getControlCenter());
return rectangle;
},
enumerable: true,
configurable: true
});
NewBoundedTextItemHandler.prototype.setTextWhizzWrappingPath = function (handler) {
var item = this.item;
var wrappingPath = null;
if (item.wrappingPath != null) {
if (!EqualsOfFloatNumbers(0, this.item.transform.angle)) {
var rotatedPath = item.wrappingPath.clone();
rotatedPath.rotateAt(-this.item.transform.angle, this.getControlCenter());
wrappingPath = toTextWhizzPath(this._textWhizz, rotatedPath);
}
else {
wrappingPath = toTextWhizzPath(this._textWhizz, item.wrappingPath);
}
wrappingPath.offsetContour(item.wrappingMargin);
}
else
wrappingPath = new this._textWhizz.Path();
handler.setWrappingPath(wrappingPath);
};
NewBoundedTextItemHandler.prototype.getWrappingPathData = function () {
if (this.item.wrappingPath != null) {
var path = null;
if (!EqualsOfFloatNumbers(0, this.item.transform.angle)) {
var rotatedPath = this.item.wrappingPath.clone();
rotatedPath.rotateAt(-this.item.transform.angle, this.getControlCenter());
path = rotatedPath;
}
else {
path = this.item.wrappingPath;
}
return { path: path, wrappingMargin: this.item.wrappingMargin };
}
else
return { isEmptyPath: true };
};
NewBoundedTextItemHandler.prototype.getFramesData = function () {
return {
type: TextFrameType.shapeTextFrame,
frames: this._getFrames()
};
};
NewBoundedTextItemHandler.prototype.getTextWrappingPath = function () {
var _this = this;
if (this.item instanceof PathBoundedTextItem && this.item.textWrappingMode === WrappingMode.Tight) {
var frames_1 = this._getFrames();
var getTransformedFrame = function (frame) {
var transformedFrame = frame.clone();
transformedFrame.transform(_this.item.transform, _this.getControlCenter());
return transformedFrame;
};
if (frames_1.length == 1)
return getTransformedFrame(frames_1[0]);
var modelPath = getTransformedFrame(frames_1[0]);
var union = toTextWhizzPath(this._textWhizz, modelPath);
for (var i = 1; i < frames_1.length; i++) {
var modelPath_1 = getTransformedFrame(frames_1[i]);
var twPath = toTextWhizzPath(this._textWhizz, modelPath_1);
union = this._textWhizz.Path.union(union, twPath);
}
return fromTextWhizzPath(union);
}
return _super.prototype.getTextWrappingPath.call(this);
};
NewBoundedTextItemHandler.prototype._endTransform = function (changed, resized) {
_super.prototype._endTransform.call(this, changed, resized);
this.setRectangleValidated();
};
NewBoundedTextItemHandler.prototype.setRectangleValidated = function (rectangle) {
if (rectangle === void 0) { rectangle = null; }
if (rectangle == null)
rectangle = this.rectangle;
this._validateRectAndSet(rectangle, true);
};
NewBoundedTextItemHandler.prototype.getTextRectangle = function () {
var center = this.getControlCenter();
var rectangle;
if (this._textEditorController.ready)
rectangle = RotatedRectangleF.fromRectangleF(this._textEditorController.measureText());
else
rectangle = RotatedRectangleF.fromRectangleF(this.item.sourceRectangle);
rectangle.transform(this.item.transform.clone(), center);
return rectangle;
};
NewBoundedTextItemHandler.prototype._onFrontEndRenderingInitialized = function () {
_super.prototype._onFrontEndRenderingInitialized.call(this);
this.setRectangleValidated();
};
NewBoundedTextItemHandler.prototype._onTransformRectangle = function (startRectangle, endRectangle, highlightOnly) {
var _a;
if (startRectangle == null || endRectangle == null || this._startRectangle == null)
return;
var newRectangle = this._calculateTransformedRectangle(startRectangle, endRectangle);
this.setRectangle(newRectangle, true);
(_a = this.canvas) === null || _a === void 0 ? void 0 : _a.redraw();
};
NewBoundedTextItemHandler.prototype._onItemPropertyChanged = function (sender, propertyName) {
var _a;
return __awaiter(this, void 0, void 0, function () {
var _b;
return __generator(this, function (_c) {
switch (_c.label) {
case 0:
_b = propertyName;
switch (_b) {
case "textRectangle": return [3 /*break*/, 1];
case "boundingPaths": return [3 /*break*/, 1];
case "firstBaselineOffset": return [3 /*break*/, 1];
case "firstBaselineMinOffset": return [3 /*break*/, 1];
case "wrappingPath": return [3 /*break*/, 2];
case "wrappingMargin": return [3 /*break*/, 2];
case "verticalAlignment": return [3 /*break*/, 3];
case "isVertical": return [3 /*break*/, 3];
case "shrinkMode": return [3 /*break*/, 3];
case "scheduledFitMode": return [3 /*break*/, 3];
case "paragraphSettings": return [3 /*break*/, 3];
case "characterLimit": return [3 /*break*/, 3];
case "text": return [3 /*break*/, 4];
case "overflowStrategy": return [3 /*break*/, 4];
}
return [3 /*break*/, 6];
case 1:
this.update(null, null, "frame");
return [3 /*break*/, 6];
case 2:
this.update(null, null, this.item.transform.isEmpty ? "wrappingPath" : null);
return [3 /*break*/, 6];
case 3:
this.update();
return [3 /*break*/, 6];
case 4: return [4 /*yield*/, ((_a = this.textEditorController) === null || _a === void 0 ? void 0 : _a.initialize())];
case 5:
_c.sent();
if (this.item instanceof BoundedTextItem && this.item.overflowStrategy != OverflowStrategy.Clip)
this.setRectangleValidated();
return [3 /*break*/, 6];
case 6:
_super.prototype._onItemPropertyChanged.call(this, sender, propertyName);
return [2 /*return*/];
}
});
});
};
NewBoundedTextItemHandler.prototype._transformText = function (transform) {
if (this.canvas.isSelectionResizing || this.item.wrappingPath != null)
this.update(null, null, "frame");
else
_super.prototype._transformText.call(this, transform);
};
NewBoundedTextItemHandler.prototype.renderFill = function (itemHandlerCtx) {
var item = this.item;
var center = this.getControlCenter();
var transform = this.item.transform;
var fillColorPreview = this._getItemColorPreviews().fillColorPreview;
this._getFrames().forEach(function (path) {
Graphics.fillPath(itemHandlerCtx, path, center, transform, fillColorPreview === null || fillColorPreview === void 0 ? void 0 : fillColorPreview.toString(), item.opacity);
});
};
NewBoundedTextItemHandler.prototype.renderStroke = function (itemHandlerCtx) {
var _this = this;
var borderWidth = this._getActualBorderWidth();
if (borderWidth <= 0)
return;
var item = this.item;
var center = this.getControlCenter();
var transform = this.item.transform;
var _a = this._getItemColorPreviews(), borderColorPreview = _a.borderColorPreview, altBorderColorPreview = _a.altBorderColorPreview;
this._getFrames().forEach(function (path) {
var twPath = toTextWhizzPath(_this._textWhizz, path);
twPath.offsetContour(borderWidth / 2);
Graphics.drawStroke(itemHandlerCtx, twPath, center, transform, borderWidth, borderColorPreview === null || borderColorPreview === void 0 ? void 0 : borderColorPreview.toString(), altBorderColorPreview === null || altBorderColorPreview === void 0 ? void 0 : altBorderColorPreview.toString(), item.opacity, item.dash);
});
};
NewBoundedTextItemHandler.prototype._onTextManagerExitedEditMode = function () {
_super.prototype._onTextManagerExitedEditMode.call(this);
this.setRectangleValidated();
};
NewBoundedTextItemHandler.prototype._applyTransform = function (item, transform, center) {
var e_1, _a;
if (item instanceof BoundedTextItem) {
var newRect = RotatedRectangleF.fromRectangleF(item.textRectangle);
newRect.setTransform(transform);
var rectangle = newRect.toRectangleF();
item.textRectangle = rectangle;
this.controlPoints = [new PointF(rectangle.left, rectangle.top), new PointF(rectangle.right, rectangle.bottom)];
}
if (item instanceof PathBoundedTextItem) {
try {
for (var _b = __values(item.boundingPaths), _c = _b.next(); !_c.done; _c = _b.next()) {
var path = _c.value;
path.transform(transform, center);
}
}
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; }
}
}
};
NewBoundedTextItemHandler.prototype._validateRect = function (newRectangle) {
var _a;
return __awaiter(this, void 0, void 0, function () {
var measuredRect, delta, incRect;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
if (this._textEditorController === null)
return [2 /*return*/, null];
if (!(this.item instanceof BoundedTextItem && this.item.overflowStrategy == OverflowStrategy.ExpandBox)) return [3 /*break*/, 2];
return [4 /*yield*/, this._textEditorController.measureTextFrame()];
case 1:
measuredRect = _b.sent();
if (measuredRect.height > newRectangle.height) {
delta = measuredRect.height - newRectangle.height;
if (EqualsOfFloatNumbers(delta, 0))
return [2 /*return*/, null];
incRect = newRectangle.clone();
if (((_a = this._transformOperation) === null || _a === void 0 ? void 0 : _a.resize) && ResizeHelper.isTopResizeGrip(this._transformOperation.resizeIndex))
delta *= -1;
incRect.centerY += delta / 2;
incRect.height = measuredRect.height;
return [2 /*return*/, incRect];
}
_b.label = 2;
case 2: return [2 /*return*/, null];
}
});
});
};
NewBoundedTextItemHandler.prototype._validateRectAndSet = function (rectangle, suppressOnChanged) {
var _this = this;
this._validateRect(rectangle)
.then(function (validRect) {
if (validRect != null) {
_super.prototype.setRectangle.call(_this, validRect, suppressOnChanged);
_this.update();
}
else {
_super.prototype.setRectangle.call(_this, rectangle, suppressOnChanged);
}
});
};
NewBoundedTextItemHandler.prototype._getFrames = function () {
var _a;
if (this.item instanceof BoundedTextItem) {
var rect = null;
if ((_a = this._textEditorController) === null || _a === void 0 ? void 0 : _a.isInEdit)
rect = this.rectangle.toRectangleF();
else
rect = this.item.textRectangle;
return [Path.rectangle(rect.left, rect.top, rect.width, rect.height)];
}
else {
return this.item.boundingPaths;
}
};
NewBoundedTextItemHandler.prototype._firstBaselineOffsetToTextWhizz = function (offset) {
switch (offset) {
case FirstBaselineOffset.Ascent:
return this._textWhizz.FirstBaselineOffset.ascent;
case FirstBaselineOffset.CapHeight:
return this._textWhizz.FirstBaselineOffset.capHeight;
case FirstBaselineOffset.Leading:
return this._textWhizz.FirstBaselineOffset.leading;
case FirstBaselineOffset.XHeight:
return this._textWhizz.FirstBaselineOffset.xHeight;
case FirstBaselineOffset.Fixed:
return this._textWhizz.FirstBaselineOffset.fixed;
case FirstBaselineOffset.EmboxHeight:
return this._textWhizz.FirstBaselineOffset.emBox;
}
};
NewBoundedTextItemHandler.typeName = "NewBoundedTextItemHandler";
return NewBoundedTextItemHandler;
}(NewBaseTextItemHandler));
export { NewBoundedTextItemHandler };
//# sourceMappingURL=NewBoundedTextItemHandler.js.map