diff --git a/packages/core/src/defs.mjs b/packages/core/src/defs.mjs
new file mode 100644
index 00000000000..1e1955a2d65
--- /dev/null
+++ b/packages/core/src/defs.mjs
@@ -0,0 +1,94 @@
+//////////////////////////////////////////////
+// CONSTRUCTOR //
+//////////////////////////////////////////////
+
+/**
+ * Constructor for Defs
+ *
+ * @constructor
+ * @return {Defs} this - The Defs instance
+ */
+export function Defs() {
+ this.list = {}
+
+ return this
+}
+
+//////////////////////////////////////////////
+// PUBLIC METHODS //
+//////////////////////////////////////////////
+
+/**
+ * Return a deep copy of this
+ *
+ * @return {object} this - The Defs instance
+ */
+Defs.prototype.clone = function () {
+ let clone = new Defs()
+ clone.list = JSON.parse(JSON.stringify(this.list))
+
+ return clone
+}
+
+/**
+ * Retrieve a def
+ *
+ * @param {string} name - Name of the def to get
+ * @return value - The value under name
+ */
+Defs.prototype.get = function (name) {
+ if (typeof this.list[name] === 'undefined') return false
+ else return this.list[name]
+}
+
+/**
+ * Remove a def
+ *
+ * @param {string} name - Name of the def to set
+ * @return {object} this - The Defs instance
+ */
+Defs.prototype.remove = function (name) {
+ delete this.list[name]
+
+ return this
+}
+
+/**
+ * Return SVG code for Defs
+ *
+ * @return {string} svg - The SVG code
+ */
+Defs.prototype.render = function () {
+ let svg = ''
+ for (let key in this.list) {
+ svg += ` ${key}="${this.list[key].join('')}"`
+ }
+
+ return svg
+}
+
+/**
+ * Set a def, overwriting existing value
+ *
+ * @param {string} name - Name of the defs to set
+ * @param {string} value - Value of the defs to set
+ * @return {Defs} this - The Defs instance
+ */
+Defs.prototype.set = function (name, value) {
+ this.list[name] = [value]
+
+ return this
+}
+
+/**
+ * Sets a def, but only if it's not currently set
+ *
+ * @param {string} name - Name of the def to set
+ * @param {string} value - Value of the def to set
+ * @return {Defs} this - The Defs instance
+ */
+Defs.prototype.setIfUnset = function (name, value) {
+ if (typeof this.list[name] === 'undefined') this.list[name] = [value]
+
+ return this
+}
diff --git a/packages/core/src/svg.mjs b/packages/core/src/svg.mjs
index d77412ce6ac..0dd58f963ea 100644
--- a/packages/core/src/svg.mjs
+++ b/packages/core/src/svg.mjs
@@ -1,4 +1,5 @@
import { Attributes } from './attributes.mjs'
+import { Defs } from './defs.mjs'
import { __addNonEnumProp, round } from './utils.mjs'
import { version } from '../data.mjs'
@@ -31,7 +32,7 @@ export function Svg(pattern) {
this.layout = {}
this.body = ''
this.style = ''
- this.defs = ''
+ this.defs = new Defs()
}
//////////////////////////////////////////////
@@ -215,7 +216,7 @@ Svg.prototype.__renderCircle = function (point) {
Svg.prototype.__renderDefs = function () {
let svg = ''
this.__indent()
- svg += this.__nl() + this.defs
+ svg += this.__nl() + this.defs.render()
this.__outdent()
svg += this.__nl() + '' + this.__nl()
diff --git a/plugins/plugin-annotations/src/buttons.mjs b/plugins/plugin-annotations/src/buttons.mjs
index e49f78c36c4..6c54b94d8ca 100644
--- a/plugins/plugin-annotations/src/buttons.mjs
+++ b/plugins/plugin-annotations/src/buttons.mjs
@@ -1,6 +1,7 @@
const defs = [
- // button
- `
+ {
+ name: 'button',
+ def: `
`,
- // buttonhole
- `
+ },
+ {
+ name: 'buttonhole',
+ def: `
`,
- // snaps
- `
+ },
+ {
+ name: 'snaps',
+ def: `
@@ -61,6 +66,7 @@ const defs = [
d="M -1.7,-1 L -1.7,1 M 1.7,-1 L 1.7,1" id="snap-socket-lines"
/>
`,
+ },
]
// Export hooks
@@ -68,7 +74,7 @@ export const buttonsHooks = {
preRender: [
function (svg) {
for (const def of defs) {
- if (svg.defs.indexOf(def) === -1) svg.defs += def
+ svg.defs.setIfUnset(def.name, def.def)
}
},
],
diff --git a/plugins/plugin-annotations/src/cutonfold.mjs b/plugins/plugin-annotations/src/cutonfold.mjs
index e98ae2dd818..1a5d76073b4 100644
--- a/plugins/plugin-annotations/src/cutonfold.mjs
+++ b/plugins/plugin-annotations/src/cutonfold.mjs
@@ -11,7 +11,7 @@ const markers = `
export const cutonfoldHooks = {
preRender: [
function (svg) {
- if (svg.defs.indexOf(markers) === -1) svg.defs += markers
+ svg.defs.setIfUnset('cutonfold', markers)
},
],
}
diff --git a/plugins/plugin-annotations/src/dimensions.mjs b/plugins/plugin-annotations/src/dimensions.mjs
index 52e777fc371..3e975eb0aca 100644
--- a/plugins/plugin-annotations/src/dimensions.mjs
+++ b/plugins/plugin-annotations/src/dimensions.mjs
@@ -74,7 +74,7 @@ function lleader(so, type, props, id) {
export const dimensionsHooks = {
preRender: [
function (svg) {
- if (svg.defs.indexOf(markers) === -1) svg.defs += markers
+ svg.defs.setIfUnset('dimensions', markers)
},
],
}
diff --git a/plugins/plugin-annotations/src/grainline.mjs b/plugins/plugin-annotations/src/grainline.mjs
index a2203925ae9..d04267b3bde 100644
--- a/plugins/plugin-annotations/src/grainline.mjs
+++ b/plugins/plugin-annotations/src/grainline.mjs
@@ -12,7 +12,7 @@ const dflts = { text: 'grainline' }
export const grainlineHooks = {
preRender: [
function (svg) {
- if (svg.defs.indexOf(markers) === -1) svg.defs += markers
+ svg.defs.setIfUnset('grainline', markers)
},
],
}
diff --git a/plugins/plugin-annotations/src/logo.mjs b/plugins/plugin-annotations/src/logo.mjs
index 92639ea4fe2..3b23908fa4b 100644
--- a/plugins/plugin-annotations/src/logo.mjs
+++ b/plugins/plugin-annotations/src/logo.mjs
@@ -5,7 +5,7 @@ const logo = (scale) =>
export const logoHooks = {
preRender: [
function (svg) {
- if (svg.defs.indexOf('id="logo"') === -1) svg.defs += logo(svg.pattern.settings[0].scale)
+ svg.defs.setIfUnset('logo', logo(svg.pattern.settings[0].scale))
},
],
}
diff --git a/plugins/plugin-annotations/src/notches.mjs b/plugins/plugin-annotations/src/notches.mjs
index 77764b38aa8..e7e33561de4 100644
--- a/plugins/plugin-annotations/src/notches.mjs
+++ b/plugins/plugin-annotations/src/notches.mjs
@@ -12,7 +12,7 @@ const markers = `
export const notchesHooks = {
preRender: [
function (svg) {
- if (svg.defs.indexOf(`id="notch"`) === -1) svg.defs += markers
+ svg.defs.setIfUnset('notch', markers)
},
],
}
diff --git a/plugins/plugin-annotations/src/pleat.mjs b/plugins/plugin-annotations/src/pleat.mjs
index 0e37b1d85c0..cc0ce01eb44 100644
--- a/plugins/plugin-annotations/src/pleat.mjs
+++ b/plugins/plugin-annotations/src/pleat.mjs
@@ -8,7 +8,7 @@ const markers = `
export const pleatHooks = {
preRender: [
function (svg) {
- if (svg.defs.indexOf(markers) === -1) svg.defs += markers
+ svg.defs.setIfUnset('pleat', markers)
},
],
}
diff --git a/plugins/plugin-annotations/src/sewtogether.mjs b/plugins/plugin-annotations/src/sewtogether.mjs
index 16d2a4aa3f7..54ecb06ee7f 100644
--- a/plugins/plugin-annotations/src/sewtogether.mjs
+++ b/plugins/plugin-annotations/src/sewtogether.mjs
@@ -14,7 +14,7 @@ const markers = `
export const sewtogetherHooks = {
preRender: [
function (svg) {
- if (svg.defs.indexOf(markers) === -1) svg.defs += markers
+ svg.defs.setIfUnset('sewTogether', markers)
},
],
}