@aurigma/design-atoms
Version:
Design Atoms is a part of Customer's Canvas SDK which allows for manipulating individual design elements through your code.
260 lines • 11.9 kB
JavaScript
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 { fromTextWhizzPath, toTextWhizzPath } from "@aurigma/design-atoms-text/Utils/PathUtils";
export class NewBoundedTextItemHandler extends NewBaseTextItemHandler {
constructor(fontRegistry, textEditorControllerFactory, item, textWhizz = null, apiClient, colorPreviewService, colorParser) {
super(fontRegistry, textEditorControllerFactory, item, textWhizz, apiClient, colorPreviewService, colorParser);
this.lastRectangle = null;
}
get item() {
return this._getItem();
}
set item(item) {
super._setItem(item);
}
get drawingRectangle() {
const isResizing = this.canvas.isItemHandlerSelected(this) && this.canvas.isSelectionResizing;
if (this._staticTextRect == null)
return isResizing && this.startRectangle != null ? this.startRectangle : this.rectangle;
const rectangle = RotatedRectangleF.fromRectangleF(this._staticTextRect);
const transfrom = (isResizing && this.startRectangle != null) ?
this.startRectangle.getTransform(RotatedRectangleF.fromRectangleF(this.getControlBounds())) :
this.item.transform.clone();
rectangle.transform(transfrom, this.getControlCenter());
return rectangle;
}
setTextWhizzWrappingPath(handler) {
const item = this.item;
let wrappingPath = null;
if (item.wrappingPath != null) {
if (!EqualsOfFloatNumbers(0, this.item.transform.angle)) {
const 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);
}
getWrappingPathData() {
if (this.item.wrappingPath != null) {
let path = null;
if (!EqualsOfFloatNumbers(0, this.item.transform.angle)) {
const 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 };
}
getFramesData() {
return {
type: TextFrameType.shapeTextFrame,
frames: this._getFrames()
};
}
getTextWrappingPath() {
if (this.item instanceof PathBoundedTextItem && this.item.textWrappingMode === WrappingMode.Tight) {
const frames = this._getFrames();
const getTransformedFrame = (frame) => {
const transformedFrame = frame.clone();
transformedFrame.transform(this.item.transform, this.getControlCenter());
return transformedFrame;
};
if (frames.length == 1)
return getTransformedFrame(frames[0]);
let modelPath = getTransformedFrame(frames[0]);
let union = toTextWhizzPath(this._textWhizz, modelPath);
for (let i = 1; i < frames.length; i++) {
let modelPath = getTransformedFrame(frames[i]);
let twPath = toTextWhizzPath(this._textWhizz, modelPath);
union = this._textWhizz.Path.union(union, twPath);
}
return fromTextWhizzPath(union);
}
return super.getTextWrappingPath();
}
_endTransform(changed, resized) {
super._endTransform(changed, resized);
this.setRectangleValidated();
}
setRectangleValidated(rectangle = null) {
if (rectangle == null)
rectangle = this.rectangle;
this._validateRectAndSet(rectangle, true);
}
getTextRectangle() {
const center = this.getControlCenter();
let 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;
}
_onFrontEndRenderingInitialized() {
super._onFrontEndRenderingInitialized();
this.setRectangleValidated();
}
_onTransformRectangle(startRectangle, endRectangle, highlightOnly) {
var _a;
if (startRectangle == null || endRectangle == null || this._startRectangle == null)
return;
const newRectangle = this._calculateTransformedRectangle(startRectangle, endRectangle);
this.setRectangle(newRectangle, true);
(_a = this.canvas) === null || _a === void 0 ? void 0 : _a.redraw();
}
async _onItemPropertyChanged(sender, propertyName) {
var _a;
switch (propertyName) {
case "textRectangle":
case "boundingPaths":
case "firstBaselineOffset":
case "firstBaselineMinOffset":
this.update(null, null, "frame");
break;
case "wrappingPath":
case "wrappingMargin":
this.update(null, null, this.item.transform.isEmpty ? "wrappingPath" : null);
break;
case "verticalAlignment":
case "isVertical":
case "shrinkMode":
case "scheduledFitMode":
case "paragraphSettings":
case "characterLimit":
this.update();
break;
case "text":
case "overflowStrategy":
await ((_a = this.textEditorController) === null || _a === void 0 ? void 0 : _a.initialize());
if (this.item instanceof BoundedTextItem && this.item.overflowStrategy != OverflowStrategy.Clip)
this.setRectangleValidated();
break;
default:
}
super._onItemPropertyChanged(sender, propertyName);
}
_transformText(transform) {
if (this.canvas.isSelectionResizing || this.item.wrappingPath != null)
this.update(null, null, "frame");
else
super._transformText(transform);
}
renderFill(itemHandlerCtx) {
const item = this.item;
const center = this.getControlCenter();
const transform = this.item.transform;
const { fillColorPreview } = this._getItemColorPreviews();
this._getFrames().forEach(path => {
Graphics.fillPath(itemHandlerCtx, path, center, transform, fillColorPreview === null || fillColorPreview === void 0 ? void 0 : fillColorPreview.toString(), item.opacity);
});
}
renderStroke(itemHandlerCtx) {
const borderWidth = this._getActualBorderWidth();
if (borderWidth <= 0)
return;
const item = this.item;
const center = this.getControlCenter();
const transform = this.item.transform;
const { borderColorPreview, altBorderColorPreview } = this._getItemColorPreviews();
this._getFrames().forEach(path => {
const 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);
});
}
_onTextManagerExitedEditMode() {
super._onTextManagerExitedEditMode();
this.setRectangleValidated();
}
_applyTransform(item, transform, center) {
if (item instanceof BoundedTextItem) {
const newRect = RotatedRectangleF.fromRectangleF(item.textRectangle);
newRect.setTransform(transform);
let rectangle = newRect.toRectangleF();
item.textRectangle = rectangle;
this.controlPoints = [new PointF(rectangle.left, rectangle.top), new PointF(rectangle.right, rectangle.bottom)];
}
if (item instanceof PathBoundedTextItem) {
for (let path of item.boundingPaths) {
path.transform(transform, center);
}
}
}
async _validateRect(newRectangle) {
if (this._textEditorController === null)
return null;
if (this.item instanceof BoundedTextItem && this.item.overflowStrategy == OverflowStrategy.ExpandBox) {
const measuredRect = await this._textEditorController.measureTextFrame();
if (measuredRect.height > newRectangle.height) {
let delta = measuredRect.height - newRectangle.height;
if (EqualsOfFloatNumbers(delta, 0))
return null;
const incRect = newRectangle.clone();
incRect.centerY += delta / 2;
incRect.height = measuredRect.height;
return incRect;
}
}
return null;
}
_validateRectAndSet(rectangle, suppressOnChanged) {
this._validateRect(rectangle)
.then(validRect => {
if (validRect != null) {
super.setRectangle(validRect, suppressOnChanged);
this.update();
}
else {
super.setRectangle(rectangle, suppressOnChanged);
}
});
}
_getFrames() {
var _a;
if (this.item instanceof BoundedTextItem) {
let 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;
}
}
_firstBaselineOffsetToTextWhizz(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";
//# sourceMappingURL=NewBoundedTextItemHandler.js.map