aboutsummaryrefslogtreecommitdiffstats
path: root/src/elements/Tspan.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/elements/Tspan.js')
-rw-r--r--src/elements/Tspan.js56
1 files changed, 31 insertions, 25 deletions
diff --git a/src/elements/Tspan.js b/src/elements/Tspan.js
index 11f7fc3..00934ab 100644
--- a/src/elements/Tspan.js
+++ b/src/elements/Tspan.js
@@ -7,20 +7,28 @@ import {
import { globals } from '../utils/window.js'
import { registerMethods } from '../utils/methods.js'
import SVGNumber from '../types/SVGNumber.js'
+import Shape from './Shape.js'
import Text from './Text.js'
import * as textable from '../modules/core/textable.js'
-export default class Tspan extends Text {
+export default class Tspan extends Shape {
// Initialize node
- constructor (node) {
- super(nodeOrNew('tspan', node), node)
+ constructor (node, attrs = node) {
+ super(nodeOrNew('tspan', node), attrs)
+ this._build = false // disable build mode for adding multiple lines
}
// Set text content
text (text) {
if (text == null) return this.node.textContent + (this.dom.newLined ? '\n' : '')
- typeof text === 'function' ? text.call(this, this) : this.plain(text)
+ if (typeof text === 'function') {
+ this.clear().build(true)
+ text.call(this, this)
+ this.build(false)
+ } else {
+ this.plain(text)
+ }
return this
}
@@ -35,32 +43,27 @@ export default class Tspan extends Text {
return this.attr('dy', dy)
}
- x (x) {
- return this.attr('x', x)
- }
-
- y (y) {
- return this.attr('x', y)
- }
-
- move (x, y) {
- return this.x(x).y(y)
- }
-
// Create new line
newLine () {
- // fetch text parent
- var t = this.parent(Text)
-
// mark new line
this.dom.newLined = true
+ // fetch parent
+ var text = this.parent()
+
+ // early return in case we are not in a text element
+ if (!(text instanceof Text)) {
+ return this
+ }
+
+ var i = text.index(this)
+
var fontSize = globals.window.getComputedStyle(this.node)
.getPropertyValue('font-size')
- var dy = t.dom.leading * new SVGNumber(fontSize)
+ var dy = text.dom.leading * new SVGNumber(fontSize)
// apply new position
- return this.dy(dy).attr('x', t.x())
+ return this.dy(i ? dy : 0).attr('x', text.x())
}
}
@@ -68,7 +71,7 @@ extend(Tspan, textable)
registerMethods({
Tspan: {
- tspan: wrapWithAttrCheck(function (text) {
+ tspan: wrapWithAttrCheck(function (text = '') {
var tspan = new Tspan()
// clear if build mode is disabled
@@ -77,10 +80,13 @@ registerMethods({
}
// add new tspan
- this.node.appendChild(tspan.node)
-
- return tspan.text(text)
+ return this.put(tspan).text(text)
})
+ },
+ Text: {
+ newLine: function (text = '') {
+ return this.tspan(text).newLine()
+ }
}
})