@aurigma/design-atoms
Version:
Design Atoms is a part of Customer's Canvas SDK which allows for manipulating individual design elements through your code.
44 lines • 1.97 kB
JavaScript
import { Path, RectangleF } from "@aurigma/design-atoms-model/Math";
import { toTextWhizzPath } from "@aurigma/design-atoms-text/Utils/PathUtils";
export class PathHandler {
static set textWhizz(textWhizz) {
this._textWhizz = textWhizz;
}
static set designAtomsApiClient(designAtomsApiClient) {
this._designAtomsApiClient = designAtomsApiClient;
}
static async getBounds(path) {
var _a, _b;
return (_a = this.getBoundsSync(path)) !== null && _a !== void 0 ? _a : await ((_b = this._designAtomsApiClient) === null || _b === void 0 ? void 0 : _b.getPathBoundsFromServer(path));
}
static getBoundsSync(path) {
if (this._textWhizz != null) {
const rect = toTextWhizzPath(this._textWhizz, path).getBounds();
return new RectangleF(rect.x, rect.y, rect.width, rect.height);
}
if (this._isDomBoundsEnabled)
return this._getDOMBounds(path);
return null;
}
static get _isDomBoundsEnabled() {
return !this._getDOMBounds(this._testRectangle).isEmpty();
}
static _getDOMBounds(path) {
const svg = document.createElementNS("http://www.w3.org/2000/svg", "svg");
svg.style.position = "absolute";
svg.style.visibility = "hidden";
const svgPath = document.createElementNS("http://www.w3.org/2000/svg", "path");
svg.appendChild(svgPath);
svgPath.setAttribute("d", path.toString());
const body = document.querySelector("body");
body.appendChild(svg);
const svgRect = svgPath.getBBox();
const pathBounds = new RectangleF(svgRect.x, svgRect.y, svgRect.width, svgRect.height);
body.removeChild(svg);
return pathBounds;
}
}
PathHandler._testRectangle = Path.rectangle(0, 0, 10, 10);
PathHandler._textWhizz = null;
PathHandler._designAtomsApiClient = null;
//# sourceMappingURL=PathHandler.js.map