1
0
Fork 0

Merge pull request #3863 from freesewing/snippets

Allow `defs` in the `svg` to be overwritten.
This commit is contained in:
Joost De Cock 2023-05-02 20:03:33 +02:00 committed by GitHub
commit f5f4acaad2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
19 changed files with 301 additions and 143 deletions

View file

@ -35,7 +35,7 @@ Attributes.prototype.add = function (name, value) {
}
/**
* Return a props object for attributes with a fiven prefix (typically used for data-text)
* Return a props object for attributes with a given prefix (typically used for data-text)
*
* @param {string} prefix - The prefix to filter attributes on
* @return {object} props - The attributes as props

View file

@ -0,0 +1,93 @@
//////////////////////////////////////////////
// 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]}"`
}
return svg
}
/**
* Set a def, overwriting existing value
*
* @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.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
}

View file

@ -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 = '<defs>'
this.__indent()
svg += this.__nl() + this.defs
svg += this.__nl() + this.defs.render()
this.__outdent()
svg += this.__nl() + '</defs>' + this.__nl()