199 lines
5.2 KiB
JavaScript
Raw Normal View History

2021-10-04 16:56:40 -07:00
/*
2021-12-02 20:38:53 -08:00
* Copyright [2021] [wisemapping]
2021-10-04 16:56:40 -07:00
*
* Licensed under WiseMapping Public License, Version 1.0 (the "License").
* It is basically the Apache License, Version 2.0 (the "License") plus the
* "powered by wisemapping" text requirement on every single page;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the license at
*
* http://www.wisemapping.org/license
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
2021-12-02 21:23:14 -08:00
import { $defined } from '@wisemapping/core-js';
import $ from 'jquery';
import ElementPeer from './ElementPeer';
2021-12-04 15:39:20 -08:00
class TextPeer extends ElementPeer {
constructor(Font) {
const svgElement = window.document.createElementNS(ElementPeer.svgNamespace, 'text');
super(svgElement);
this.Font = Font;
2021-10-04 16:56:40 -07:00
this._position = { x: 0, y: 0 };
this._font = new Font('Arial', this);
2021-12-04 15:39:20 -08:00
}
2021-10-04 16:56:40 -07:00
append(element) {
this._native.appendChild(element._native);
2021-12-04 15:39:20 -08:00
}
2021-10-04 16:56:40 -07:00
setTextAlignment(align) {
this._textAlign = align;
2021-12-04 15:39:20 -08:00
}
2021-10-04 16:56:40 -07:00
getTextAlignment() {
2021-12-02 21:23:14 -08:00
return $defined(this._textAlign) ? this._textAlign : 'left';
2021-12-04 15:39:20 -08:00
}
2021-10-04 16:56:40 -07:00
setText(text) {
// Remove all previous nodes ...
while (this._native.firstChild) {
this._native.removeChild(this._native.firstChild);
}
this._text = text;
if (text) {
const lines = text.split('\n');
const me = this;
lines.forEach((line) => {
2021-12-04 16:42:00 -08:00
const tspan = window.document.createElementNS(ElementPeer.svgNamespace, 'tspan');
2021-10-04 16:56:40 -07:00
tspan.setAttribute('dy', '1em');
tspan.setAttribute('x', me.getPosition().x);
tspan.textContent = line.length === 0 ? ' ' : line;
2021-10-04 16:56:40 -07:00
me._native.appendChild(tspan);
});
}
2021-12-04 15:39:20 -08:00
}
2021-10-04 16:56:40 -07:00
getText() {
return this._text;
2021-12-04 15:39:20 -08:00
}
2021-10-04 16:56:40 -07:00
setPosition(x, y) {
this._position = { x, y };
this._native.setAttribute('y', y);
this._native.setAttribute('x', x);
// tspan must be positioned manually.
$(this._native).children('tspan').attr('x', x);
2021-12-04 15:39:20 -08:00
}
2021-10-04 16:56:40 -07:00
getPosition() {
return this._position;
2021-12-04 15:39:20 -08:00
}
2021-10-04 16:56:40 -07:00
getNativePosition() {
return $(this._native).position();
2021-12-04 15:39:20 -08:00
}
2021-10-04 16:56:40 -07:00
setFont(font, size, style, weight) {
2021-12-02 21:23:14 -08:00
if ($defined(font)) {
this._font = new this.Font(font, this);
2021-10-04 16:56:40 -07:00
}
2021-12-02 21:23:14 -08:00
if ($defined(style)) {
2021-10-04 16:56:40 -07:00
this._font.setStyle(style);
}
2021-12-02 21:23:14 -08:00
if ($defined(weight)) {
2021-10-04 16:56:40 -07:00
this._font.setWeight(weight);
}
2021-12-02 21:23:14 -08:00
if ($defined(size)) {
2021-10-04 16:56:40 -07:00
this._font.setSize(size);
}
this._updateFontStyle();
2021-12-04 15:39:20 -08:00
}
2021-10-04 16:56:40 -07:00
_updateFontStyle() {
this._native.setAttribute('font-family', this._font.getFontFamily());
this._native.setAttribute('font-size', this._font.getGraphSize());
this._native.setAttribute('font-style', this._font.getStyle());
this._native.setAttribute('font-weight', this._font.getWeight());
2021-12-04 15:39:20 -08:00
}
2021-10-04 16:56:40 -07:00
setColor(color) {
this._native.setAttribute('fill', color);
2021-12-04 15:39:20 -08:00
}
2021-10-04 16:56:40 -07:00
getColor() {
return this._native.getAttribute('fill');
2021-12-04 15:39:20 -08:00
}
2021-10-04 16:56:40 -07:00
setTextSize(size) {
this._font.setSize(size);
this._updateFontStyle();
2021-12-04 15:39:20 -08:00
}
2021-10-04 16:56:40 -07:00
setContentSize(width, height) {
this._native.xTextSize = `${width.toFixed(1)},${height.toFixed(1)}`;
2021-12-04 15:39:20 -08:00
}
2021-10-04 16:56:40 -07:00
setStyle(style) {
this._font.setStyle(style);
this._updateFontStyle();
2021-12-04 15:39:20 -08:00
}
2021-10-04 16:56:40 -07:00
setWeight(weight) {
this._font.setWeight(weight);
this._updateFontStyle();
2021-12-04 15:39:20 -08:00
}
2021-10-04 16:56:40 -07:00
setFontFamily(family) {
const oldFont = this._font;
this._font = new this.Font(family, this);
2021-10-04 16:56:40 -07:00
this._font.setSize(oldFont.getSize());
this._font.setStyle(oldFont.getStyle());
this._font.setWeight(oldFont.getWeight());
this._updateFontStyle();
2021-12-04 15:39:20 -08:00
}
2021-10-04 16:56:40 -07:00
getFont() {
return {
font: this._font.getFont(),
size: parseInt(this._font.getSize(), 10),
2021-10-04 16:56:40 -07:00
style: this._font.getStyle(),
weight: this._font.getWeight(),
};
2021-12-04 15:39:20 -08:00
}
2021-10-04 16:56:40 -07:00
setSize(size) {
this._font.setSize(size);
this._updateFontStyle();
2021-12-04 15:39:20 -08:00
}
2021-10-04 16:56:40 -07:00
getWidth() {
let computedWidth;
// Firefox hack for this issue:http://stackoverflow.com/questions/6390065/doing-ajax-updates-in-svg-breaks-getbbox-is-there-a-workaround
try {
computedWidth = this._native.getBBox().width;
// Chrome bug is producing this error, oly during page loading.
// Remove the hack if it works. The issue seems to be
2021-10-04 16:56:40 -07:00
// caused when the element is hidden. I don't know why, but it works ...
if (computedWidth === 0) {
2021-10-04 16:56:40 -07:00
const bbox = this._native.getBBox();
computedWidth = bbox.width;
}
} catch (e) {
// eslint-disable-next-line no-console
2021-12-04 15:39:20 -08:00
console.error(e);
2021-10-04 16:56:40 -07:00
computedWidth = 10;
}
let width = parseInt(computedWidth, 10);
2021-10-04 16:56:40 -07:00
width += this._font.getWidthMargin();
return width;
2021-12-04 15:39:20 -08:00
}
2021-10-04 16:56:40 -07:00
getHeight() {
// Firefox hack for this
// issue:http://stackoverflow.com/questions/6390065/doing-ajax-updates-in-svg-breaks-getbbox-is-there-a-workaround
let computedHeight;
2021-10-04 16:56:40 -07:00
try {
computedHeight = this._native.getBBox().height;
2021-10-04 16:56:40 -07:00
} catch (e) {
computedHeight = 10;
}
return parseInt(computedHeight, 10);
2021-12-04 15:39:20 -08:00
}
2021-10-04 16:56:40 -07:00
getHtmlFontSize() {
return this._font.getHtmlSize();
2021-12-04 15:39:20 -08:00
}
}
2021-10-04 16:56:40 -07:00
export default TextPeer;