aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/elements/Element.js10
-rw-r--r--src/elements/Text.js9
-rw-r--r--src/utils/utils.js16
3 files changed, 25 insertions, 10 deletions
diff --git a/src/elements/Element.js b/src/elements/Element.js
index 61db7b7..7a563d6 100644
--- a/src/elements/Element.js
+++ b/src/elements/Element.js
@@ -9,7 +9,7 @@ import {
} from '../utils/adopter.js'
import { globals } from '../utils/window.js'
import { point } from '../types/Point.js'
-import { proportionalSize } from '../utils/utils.js'
+import { proportionalSize, writeDataToDom } from '../utils/utils.js'
import { reference } from '../modules/core/regex.js'
import Dom from './Dom.js'
import List from '../types/List.js'
@@ -151,13 +151,7 @@ export default class Element extends Dom {
// write svgjs data to the dom
writeDataToDom() {
- // remove previously set data
- this.node.removeAttribute('svgjs:data')
-
- if (Object.keys(this.dom).length) {
- this.node.setAttribute('svgjs:data', JSON.stringify(this.dom)) // see #428
- }
-
+ writeDataToDom(this, this.dom)
return super.writeDataToDom()
}
diff --git a/src/elements/Text.js b/src/elements/Text.js
index 0c5815d..c703e3b 100644
--- a/src/elements/Text.js
+++ b/src/elements/Text.js
@@ -10,14 +10,14 @@ import SVGNumber from '../types/SVGNumber.js'
import Shape from './Shape.js'
import { globals } from '../utils/window.js'
import * as textable from '../modules/core/textable.js'
-import { isDescriptive } from '../utils/utils.js'
+import { isDescriptive, writeDataToDom } from '../utils/utils.js'
export default class Text extends Shape {
// Initialize node
constructor(node, attrs = node) {
super(nodeOrNew('text', node), attrs)
- this.dom.leading = new SVGNumber(1.3) // store leading value for rebuilding
+ this.dom.leading = this.dom.leading ?? new SVGNumber(1.3) // store leading value for rebuilding
this._rebuild = true // enable automatic updating of dy values
this._build = false // disable build mode for adding multiple lines
}
@@ -82,6 +82,11 @@ export default class Text extends Shape {
return this
}
+ writeDataToDom() {
+ writeDataToDom(this, this.dom, { leading: 1.3 })
+ return this
+ }
+
// Set the text content
text(text) {
// act as getter
diff --git a/src/utils/utils.js b/src/utils/utils.js
index 0d297ec..2438431 100644
--- a/src/utils/utils.js
+++ b/src/utils/utils.js
@@ -124,3 +124,19 @@ export function getOrigin(o, element) {
const descriptiveElements = new Set(['desc', 'metadata', 'title'])
export const isDescriptive = (element) =>
descriptiveElements.has(element.nodeName)
+
+export const writeDataToDom = (element, data, defaults = {}) => {
+ const cloned = { ...data }
+
+ for (const key in cloned) {
+ if (cloned[key].valueOf() === defaults[key]) {
+ delete cloned[key]
+ }
+ }
+
+ if (Object.keys(cloned).length) {
+ element.node.setAttribute('svgjs:data', JSON.stringify(cloned)) // see #428
+ } else {
+ element.node.removeAttribute('svgjs:data')
+ }
+}