UNPKG

@inweb/markup

Version:
192 lines (151 loc) 5.78 kB
/////////////////////////////////////////////////////////////////////////////// // Copyright (C) 2002-2025, Open Design Alliance (the "Alliance"). // All rights reserved. // // This software and its documentation and related materials are owned by // the Alliance. The software may only be incorporated into application // programs owned by members of the Alliance, subject to a signed // Membership Agreement and Supplemental Software License Agreement with the // Alliance. The structure and organization of this software are the valuable // trade secrets of the Alliance and its suppliers. The software is also // protected by copyright law and international treaty provisions. Application // programs incorporating this software must include the following statement // with their copyright notices: // // This application incorporates Open Design Alliance software pursuant to a // license agreement with Open Design Alliance. // Open Design Alliance Copyright (C) 2002-2025 by Open Design Alliance. // All rights reserved. // // By use of this software, its documentation or related materials, you // acknowledge and accept the above terms. /////////////////////////////////////////////////////////////////////////////// import Konva from "konva"; import { IMarkupText, IMarkupTextParams } from "../IMarkupText"; import { IWorldTransform } from "../IWorldTransform"; import { WorldTransform } from "../WorldTransform"; export class KonvaText implements IMarkupText { private _ref: Konva.Text; private _worldTransformer: IWorldTransform; private readonly TEXT_FONT_FAMILY = "Calibri"; constructor(params: IMarkupTextParams, ref = null, worldTransformer = new WorldTransform()) { this._worldTransformer = worldTransformer; if (ref) { this._ref = ref; const wcsStart = this._ref.getAttr("wcsStart"); if (!wcsStart) { this._ref.setAttr("wcsStart", this._worldTransformer.screenToWorld({ x: ref.x(), y: ref.y() })); } return; } if (!params) params = {}; if (!params.position) params.position = { x: 0, y: 0 }; if (!params.text) params.text = "default"; this._ref = new Konva.Text({ x: params.position.x, y: params.position.y, text: params.text, fontSize: params.fontSize ?? 34, fontFamily: this.TEXT_FONT_FAMILY, fill: params.color ?? "#ff0000", align: "left", draggable: true, rotation: params.rotation ?? 0, }); this._ref.width(this._ref.getTextWidth()); this._ref.setAttr("wcsStart", this._worldTransformer.screenToWorld({ x: params.position.x, y: params.position.y })); this._ref.on("transform", (e) => { const attrs = e.target.attrs; if (attrs.rotation !== this._ref.rotation()) this._ref.rotation(attrs.rotation); const scaleByX = Math.abs(attrs.scaleX - 1) > 10e-6; const scaleByY = Math.abs(attrs.scaleY - 1) > 10e-6; let newWidth = this._ref.width(); if (scaleByX) newWidth *= attrs.scaleX; let newHeight = this._ref.height(); if (scaleByY) newHeight *= attrs.scaleY; const minWidth = 50; if (newWidth < minWidth) newWidth = minWidth; if (newHeight < Math.round(this.getFontSize())) newHeight = Math.round(this.getFontSize()); if (scaleByX) { this._ref.width(newWidth); } if (scaleByY) { this._ref.height(newHeight); } this._ref.scale({ x: 1, y: 1 }); }); this._ref.on("transformend", (e) => { const attrs = e.target.attrs; if (attrs.rotation !== this._ref.rotation()) this._ref.rotation(attrs.rotation); const absoluteTransform = this._ref.getStage().getAbsoluteTransform(); const position = absoluteTransform.point({ x: this._ref.x(), y: this._ref.y() }); this._ref.setAttr("wcsStart", this._worldTransformer.screenToWorld(position)); }); this._ref.on("dragend", (e) => { const absoluteTransform = this._ref.getStage().getAbsoluteTransform(); const position = absoluteTransform.point({ x: this._ref.x(), y: this._ref.y() }); this._ref.setAttr("wcsStart", this._worldTransformer.screenToWorld(position)); }); this._ref.id(this._ref._id.toString()); } ref() { return this._ref; } id(): string { return this._ref.id(); } enableMouseEditing(value: boolean): void { this._ref.draggable(value); } type(): string { return "Text"; } getColor(): string { return this._ref.fill() as string; } setColor(hex: string): void { this._ref.fill(hex); } getRotation(): number { return this._ref.rotation(); } setRotation(degrees: number): void { this._ref.rotation(degrees); } getZIndex(): number { return this._ref.zIndex(); } setZIndex(zIndex: number): void { this._ref.zIndex(zIndex); } delete(): void { this._ref.destroy(); this._ref = null; } getText(): string { return this._ref.text(); } setText(text: string): void { this._ref.text(text); } getPosition(): { x: number; y: number } { return this._ref.getPosition(); } setPosition(x: number, y: number): void { this._ref.setPosition({ x, y }); this._ref.setAttr("wcsStart", this._worldTransformer.screenToWorld({ x, y })); } getFontSize(): number { return this._ref.fontSize(); } setFontSize(size: number): void { this._ref.fontSize(size); } updateScreenCoordinates(): void { let screenPositionStart = this._worldTransformer.worldToScreen(this._ref.getAttr("wcsStart")); let invert = this._ref.getStage().getAbsoluteTransform().copy(); invert = invert.invert(); const positionStart = invert.point(screenPositionStart); this._ref.position({ x: positionStart.x, y: positionStart.y }); } }