From 5dcaa15f3233583693d93ebe1790e3e9599d5188 Mon Sep 17 00:00:00 2001 From: Wouter van Wageningen Date: Wed, 19 Apr 2023 06:08:26 +0000 Subject: [PATCH 01/11] First attempt --- packages/core/src/defs.mjs | 94 +++++++++++++++++++ packages/core/src/svg.mjs | 5 +- plugins/plugin-annotations/src/buttons.mjs | 20 ++-- plugins/plugin-annotations/src/cutonfold.mjs | 2 +- plugins/plugin-annotations/src/dimensions.mjs | 2 +- plugins/plugin-annotations/src/grainline.mjs | 2 +- plugins/plugin-annotations/src/logo.mjs | 2 +- plugins/plugin-annotations/src/notches.mjs | 2 +- plugins/plugin-annotations/src/pleat.mjs | 2 +- .../plugin-annotations/src/sewtogether.mjs | 2 +- 10 files changed, 117 insertions(+), 16 deletions(-) create mode 100644 packages/core/src/defs.mjs 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) }, ], } From 2a3c0d11268393ea0f50f9d62ee394d0c1e27f56 Mon Sep 17 00:00:00 2001 From: Wouter van Wageningen Date: Wed, 19 Apr 2023 14:48:03 +0000 Subject: [PATCH 02/11] Tweak --- packages/core/src/defs.mjs | 4 +++- packages/core/src/svg.mjs | 6 ++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/packages/core/src/defs.mjs b/packages/core/src/defs.mjs index 1e1955a2d65..7cd5c5e28d6 100644 --- a/packages/core/src/defs.mjs +++ b/packages/core/src/defs.mjs @@ -88,7 +88,9 @@ Defs.prototype.set = function (name, value) { * @return {Defs} this - The Defs instance */ Defs.prototype.setIfUnset = function (name, value) { + // console.log({defsSetIfUnset:{name:name,value:value}}) if (typeof this.list[name] === 'undefined') this.list[name] = [value] - + // console.log({list:JSON.parse(JSON.stringify(this.list))}) + // console.log({defs:this.render()}) return this } diff --git a/packages/core/src/svg.mjs b/packages/core/src/svg.mjs index 0dd58f963ea..f2f8f2f530f 100644 --- a/packages/core/src/svg.mjs +++ b/packages/core/src/svg.mjs @@ -46,6 +46,8 @@ export function Svg(pattern) { * @return {string} svg - The rendered SVG output */ Svg.prototype.render = function () { + // console.log('render') + this.idPrefix = this.pattern?.settings?.[0]?.idPrefix || 'fs-' this.__runHooks('preRender') if (!this.pattern.settings[0].embed) { @@ -214,8 +216,10 @@ Svg.prototype.__renderCircle = function (point) { * @return {string} svg - The SVG markup for the defs block */ Svg.prototype.__renderDefs = function () { + console.log('__renderDefs') let svg = '' this.__indent() + // console.log({defs:this.defs.render()}) svg += this.__nl() + this.defs.render() this.__outdent() svg += this.__nl() + '' + this.__nl() @@ -230,6 +234,7 @@ Svg.prototype.__renderDefs = function () { * @return {string} svg - The SVG markup for the head section */ Svg.prototype.__renderHead = function () { + // console.log('__renderHead') let svg = this.__renderStyle() svg += this.__renderDefs() svg += this.__openGroup(this.idPrefix + 'container') @@ -291,6 +296,7 @@ Svg.prototype.__renderPart = function (part) { `${this.idPrefix}stack-${this.activeStack}-part-${part.name}`, part.attributes ) + svg += this.__openGroup(`${this.idPrefix}stack-${this.activeStack}-part-${part.name}`, part.defs) for (let key in part.paths) { let path = part.paths[key] if (!path.hidden) svg += this.__renderPath(path) From 895baf48d5f3216b4d4e73b3063968025d7fd94e Mon Sep 17 00:00:00 2001 From: Wouter van Wageningen Date: Thu, 20 Apr 2023 05:43:05 +0000 Subject: [PATCH 03/11] Spelling mistake --- packages/core/src/attributes.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core/src/attributes.mjs b/packages/core/src/attributes.mjs index f04a1211b67..211c7b28fe6 100644 --- a/packages/core/src/attributes.mjs +++ b/packages/core/src/attributes.mjs @@ -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 From 3e91e23ed5e338221b68e93facb974ae638f98ee Mon Sep 17 00:00:00 2001 From: Wouter van Wageningen Date: Thu, 20 Apr 2023 05:44:39 +0000 Subject: [PATCH 04/11] Working version --- packages/core/src/defs.mjs | 3 --- packages/core/src/svg.mjs | 6 ------ sites/shared/components/workbench/draft/defs.mjs | 2 +- 3 files changed, 1 insertion(+), 10 deletions(-) diff --git a/packages/core/src/defs.mjs b/packages/core/src/defs.mjs index 7cd5c5e28d6..9fc13d25b31 100644 --- a/packages/core/src/defs.mjs +++ b/packages/core/src/defs.mjs @@ -88,9 +88,6 @@ Defs.prototype.set = function (name, value) { * @return {Defs} this - The Defs instance */ Defs.prototype.setIfUnset = function (name, value) { - // console.log({defsSetIfUnset:{name:name,value:value}}) if (typeof this.list[name] === 'undefined') this.list[name] = [value] - // console.log({list:JSON.parse(JSON.stringify(this.list))}) - // console.log({defs:this.render()}) return this } diff --git a/packages/core/src/svg.mjs b/packages/core/src/svg.mjs index f2f8f2f530f..0dd58f963ea 100644 --- a/packages/core/src/svg.mjs +++ b/packages/core/src/svg.mjs @@ -46,8 +46,6 @@ export function Svg(pattern) { * @return {string} svg - The rendered SVG output */ Svg.prototype.render = function () { - // console.log('render') - this.idPrefix = this.pattern?.settings?.[0]?.idPrefix || 'fs-' this.__runHooks('preRender') if (!this.pattern.settings[0].embed) { @@ -216,10 +214,8 @@ Svg.prototype.__renderCircle = function (point) { * @return {string} svg - The SVG markup for the defs block */ Svg.prototype.__renderDefs = function () { - console.log('__renderDefs') let svg = '' this.__indent() - // console.log({defs:this.defs.render()}) svg += this.__nl() + this.defs.render() this.__outdent() svg += this.__nl() + '' + this.__nl() @@ -234,7 +230,6 @@ Svg.prototype.__renderDefs = function () { * @return {string} svg - The SVG markup for the head section */ Svg.prototype.__renderHead = function () { - // console.log('__renderHead') let svg = this.__renderStyle() svg += this.__renderDefs() svg += this.__openGroup(this.idPrefix + 'container') @@ -296,7 +291,6 @@ Svg.prototype.__renderPart = function (part) { `${this.idPrefix}stack-${this.activeStack}-part-${part.name}`, part.attributes ) - svg += this.__openGroup(`${this.idPrefix}stack-${this.activeStack}-part-${part.name}`, part.defs) for (let key in part.paths) { let path = part.paths[key] if (!path.hidden) svg += this.__renderPath(path) diff --git a/sites/shared/components/workbench/draft/defs.mjs b/sites/shared/components/workbench/draft/defs.mjs index 07cb9ab5173..92b25ee379a 100644 --- a/sites/shared/components/workbench/draft/defs.mjs +++ b/sites/shared/components/workbench/draft/defs.mjs @@ -49,7 +49,7 @@ const grids = { } export const Defs = (props) => { - let defs = props.svg.defs + let defs = props.svg.defs.render() if (props.settings[0].paperless) { defs += grids[props.settings[0].units || 'metric'] for (let p in props.parts[0]) { From f47e09768ed59206f885703a1f9e954c090972f5 Mon Sep 17 00:00:00 2001 From: Wouter van Wageningen Date: Thu, 20 Apr 2023 17:50:36 +0000 Subject: [PATCH 05/11] spelling --- packages/core/src/defs.mjs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/core/src/defs.mjs b/packages/core/src/defs.mjs index 9fc13d25b31..dc3ab935ba0 100644 --- a/packages/core/src/defs.mjs +++ b/packages/core/src/defs.mjs @@ -70,8 +70,8 @@ Defs.prototype.render = function () { /** * Set a def, overwriting existing value * - * @param {string} name - Name of the defs to set - * @param {string} value - Value of the defs to 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.set = function (name, value) { From 9ed72e9f4543658d222fb41c5153b344f6245b83 Mon Sep 17 00:00:00 2001 From: Wouter van Wageningen Date: Thu, 20 Apr 2023 18:30:40 +0000 Subject: [PATCH 06/11] Now with tests! --- packages/core/src/defs.mjs | 6 +++--- packages/core/tests/defs.test.mjs | 34 +++++++++++++++++++++++++++++++ packages/core/tests/svg.test.mjs | 3 ++- 3 files changed, 39 insertions(+), 4 deletions(-) create mode 100644 packages/core/tests/defs.test.mjs diff --git a/packages/core/src/defs.mjs b/packages/core/src/defs.mjs index dc3ab935ba0..e12ef530029 100644 --- a/packages/core/src/defs.mjs +++ b/packages/core/src/defs.mjs @@ -61,7 +61,7 @@ Defs.prototype.remove = function (name) { Defs.prototype.render = function () { let svg = '' for (let key in this.list) { - svg += ` ${key}="${this.list[key].join('')}"` + svg += ` ${key}="${this.list[key]}"` } return svg @@ -75,7 +75,7 @@ Defs.prototype.render = function () { * @return {Defs} this - The Defs instance */ Defs.prototype.set = function (name, value) { - this.list[name] = [value] + this.list[name] = value return this } @@ -88,6 +88,6 @@ Defs.prototype.set = function (name, value) { * @return {Defs} this - The Defs instance */ Defs.prototype.setIfUnset = function (name, value) { - if (typeof this.list[name] === 'undefined') this.list[name] = [value] + if (typeof this.list[name] === 'undefined') this.list[name] = value return this } diff --git a/packages/core/tests/defs.test.mjs b/packages/core/tests/defs.test.mjs new file mode 100644 index 00000000000..4ea0bb22d0d --- /dev/null +++ b/packages/core/tests/defs.test.mjs @@ -0,0 +1,34 @@ +import chai from 'chai' +import { Defs } from '../src/defs.mjs' + +const expect = chai.expect + +describe('Defs', () => { + let defs = new Defs() + + it('Should set a def', () => { + defs.set('test', 'passed') + expect(defs.get('test')).to.equal('passed') + }) + + it('Should remove a def', () => { + defs.remove('test') + expect(defs.get('test')).to.equal(false) + }) + + it('Should only set an unset def', () => { + defs.setIfUnset('test', 'passed') + expect(defs.get('test')).to.equal('passed') + defs.setIfUnset('test', 'failed') + expect(defs.get('test')).to.equal('passed') + }) + + it('Should return false when getting an unset def', () => { + expect(defs.get('doNotTest')).to.equal(false) + }) + + it('Should render defs correctly', () => { + console.log(defs.render()) + expect(defs.render()).to.equal(' test="passed"') + }) +}) diff --git a/packages/core/tests/svg.test.mjs b/packages/core/tests/svg.test.mjs index 0306e5767f5..9f46183d618 100644 --- a/packages/core/tests/svg.test.mjs +++ b/packages/core/tests/svg.test.mjs @@ -2,6 +2,7 @@ import chai from 'chai' import chaiString from 'chai-string' import { Svg } from '../src/svg.mjs' import { Design, Attributes } from '../src/index.mjs' +import { Defs } from '../src/defs.mjs' import { version } from '../data.mjs' import render from './fixtures/render.mjs' @@ -42,7 +43,7 @@ describe('Svg', () => { expect(svg.freeId).to.equal(0) expect(svg.body).to.equal('') expect(svg.style).to.equal('') - expect(svg.defs).to.equal('') + expect(svg.defs instanceof Defs).to.equal(true) expect(svg.prefix).to.equal('') expect(svg.attributes.get('xmlns')).to.equal('http://www.w3.org/2000/svg') expect(svg.attributes.get('xmlns:svg')).to.equal('http://www.w3.org/2000/svg') From 0213c92b0f674d6918947e3761e618a8ea467cde Mon Sep 17 00:00:00 2001 From: Wouter van Wageningen Date: Thu, 20 Apr 2023 18:55:07 +0000 Subject: [PATCH 07/11] Test fixes --- plugins/plugin-annotations/tests/buttons.test.mjs | 2 +- plugins/plugin-annotations/tests/logo.test.mjs | 2 +- plugins/plugin-annotations/tests/notches.test.mjs | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/plugins/plugin-annotations/tests/buttons.test.mjs b/plugins/plugin-annotations/tests/buttons.test.mjs index ea2b28a4db3..749142c7501 100644 --- a/plugins/plugin-annotations/tests/buttons.test.mjs +++ b/plugins/plugin-annotations/tests/buttons.test.mjs @@ -11,7 +11,7 @@ pattern.draft().render() describe('Buttons Plugin Test', () => { for (const snippet of ['button', 'buttonhole', 'snap-stud', 'snap-socket']) { it(`Should add the ${snippet} snippet to defs`, () => { - expect(pattern.svg.defs.indexOf(``)).to.not.equal(-1) + expect(pattern.svg.defs.render().indexOf(``)).to.not.equal(-1) }) } diff --git a/plugins/plugin-annotations/tests/logo.test.mjs b/plugins/plugin-annotations/tests/logo.test.mjs index 440a8f3bc9d..9f6ca4102b8 100644 --- a/plugins/plugin-annotations/tests/logo.test.mjs +++ b/plugins/plugin-annotations/tests/logo.test.mjs @@ -9,7 +9,7 @@ describe('Logo Plugin Tests', () => { const Pattern = new Design() const pattern = new Pattern().use(annotationsPlugin) pattern.draft().render() - expect(pattern.svg.defs).to.contain( + expect(pattern.svg.defs.render()).to.contain( '`, + }, + { + name: 'snap-stud', + def: ` - +`, + }, + { + name: 'snap-socket', + def: ` - +`, + }, + { + name: 'cutonfoldTo', + def: ` - -` +`, + }, +] // Export hooks export const cutonfoldHooks = { preRender: [ function (svg) { - svg.defs.setIfUnset('cutonfold', markers) + for (const def of defs) { + svg.defs.setIfUnset(def.name, def.def) + } }, ], } diff --git a/plugins/plugin-annotations/src/dimensions.mjs b/plugins/plugin-annotations/src/dimensions.mjs index 3e975eb0aca..53a6032d653 100644 --- a/plugins/plugin-annotations/src/dimensions.mjs +++ b/plugins/plugin-annotations/src/dimensions.mjs @@ -1,11 +1,20 @@ -const markers = ` +const defs = [ + { + name: 'dimensionFrom', + def: ` - +`, + }, + { + name: 'dimensionTo', + def: ` - -` +`, + }, +] + const prefix = '__paperless' function drawDimension(from, to, so, { Path, units }) { @@ -74,7 +83,9 @@ function lleader(so, type, props, id) { export const dimensionsHooks = { preRender: [ function (svg) { - svg.defs.setIfUnset('dimensions', markers) + for (const def of defs) { + svg.defs.setIfUnset(def.name, def.def) + } }, ], } diff --git a/plugins/plugin-annotations/src/grainline.mjs b/plugins/plugin-annotations/src/grainline.mjs index d04267b3bde..d7933d5b79c 100644 --- a/plugins/plugin-annotations/src/grainline.mjs +++ b/plugins/plugin-annotations/src/grainline.mjs @@ -1,10 +1,19 @@ -const markers = ` +const defs = [ + { + name: 'grainlineFrom', + def: ` - +`, + }, + { + name: 'grainlineTo', + def: ` -` +`, + }, +] const dflts = { text: 'grainline' } @@ -12,7 +21,9 @@ const dflts = { text: 'grainline' } export const grainlineHooks = { preRender: [ function (svg) { - svg.defs.setIfUnset('grainline', markers) + for (const def of defs) { + svg.defs.setIfUnset(def.name, def.def) + } }, ], } diff --git a/plugins/plugin-annotations/src/notches.mjs b/plugins/plugin-annotations/src/notches.mjs index e7e33561de4..f04f5c94317 100644 --- a/plugins/plugin-annotations/src/notches.mjs +++ b/plugins/plugin-annotations/src/notches.mjs @@ -1,18 +1,29 @@ -const markers = ` +const defs = [ + { + name: 'notch', + def: ` - +`, + }, + { + name: 'bnotch', + def: ` -` +`, + }, +] // Export hooks export const notchesHooks = { preRender: [ function (svg) { - svg.defs.setIfUnset('notch', markers) + for (const def of defs) { + svg.defs.setIfUnset(def.name, def.def) + } }, ], } diff --git a/plugins/plugin-annotations/src/pleat.mjs b/plugins/plugin-annotations/src/pleat.mjs index cc0ce01eb44..3fe463455f0 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) { - svg.defs.setIfUnset('pleat', markers) + svg.defs.setIfUnset('pleatTo', markers) }, ], } diff --git a/plugins/plugin-annotations/src/sewtogether.mjs b/plugins/plugin-annotations/src/sewtogether.mjs index 54ecb06ee7f..d17c5cebd44 100644 --- a/plugins/plugin-annotations/src/sewtogether.mjs +++ b/plugins/plugin-annotations/src/sewtogether.mjs @@ -1,20 +1,34 @@ -const markers = ` +const defs = [ + { + name: 'sewTogetherStart', + def: ` - +`, + }, + { + name: 'sewTogetherEnd', + def: ` - +`, + }, + { + name: 'sewTogetherCross', + def: ` - -` +`, + }, +] // Export hooks export const sewtogetherHooks = { preRender: [ function (svg) { - svg.defs.setIfUnset('sewTogether', markers) + for (const def of defs) { + svg.defs.setIfUnset(def.name, def.def) + } }, ], } From 8887a97dd283fe862bc683405bbe002e6d92ca47 Mon Sep 17 00:00:00 2001 From: Wouter van Wageningen Date: Fri, 21 Apr 2023 03:48:37 +0000 Subject: [PATCH 09/11] @eriese recommendations --- packages/core/tests/svg.test.mjs | 2 +- plugins/plugin-annotations/src/buttons.mjs | 14 +----- plugins/plugin-annotations/src/cutonfold.mjs | 13 +----- plugins/plugin-annotations/src/dimensions.mjs | 14 ++---- plugins/plugin-annotations/src/grainline.mjs | 14 ++---- plugins/plugin-annotations/src/index.mjs | 46 +++++++++++-------- plugins/plugin-annotations/src/logo.mjs | 19 ++++---- plugins/plugin-annotations/src/notches.mjs | 14 +----- plugins/plugin-annotations/src/pleat.mjs | 19 ++++---- .../plugin-annotations/src/sewtogether.mjs | 14 +----- .../plugin-annotations/tests/buttons.test.mjs | 2 +- .../plugin-annotations/tests/logo.test.mjs | 4 +- .../plugin-annotations/tests/notches.test.mjs | 10 ++-- 13 files changed, 65 insertions(+), 120 deletions(-) diff --git a/packages/core/tests/svg.test.mjs b/packages/core/tests/svg.test.mjs index 9f46183d618..664f6f86434 100644 --- a/packages/core/tests/svg.test.mjs +++ b/packages/core/tests/svg.test.mjs @@ -43,7 +43,7 @@ describe('Svg', () => { expect(svg.freeId).to.equal(0) expect(svg.body).to.equal('') expect(svg.style).to.equal('') - expect(svg.defs instanceof Defs).to.equal(true) + expect(svg.defs).to.be.an.instanceof(Defs) expect(svg.prefix).to.equal('') expect(svg.attributes.get('xmlns')).to.equal('http://www.w3.org/2000/svg') expect(svg.attributes.get('xmlns:svg')).to.equal('http://www.w3.org/2000/svg') diff --git a/plugins/plugin-annotations/src/buttons.mjs b/plugins/plugin-annotations/src/buttons.mjs index 690a0ce56f5..cf870fbdd4d 100644 --- a/plugins/plugin-annotations/src/buttons.mjs +++ b/plugins/plugin-annotations/src/buttons.mjs @@ -1,4 +1,5 @@ -const defs = [ +// Export defs +export const buttonsDefs = [ { name: 'button', def: ` @@ -84,14 +85,3 @@ const defs = [ `, }, ] - -// Export hooks -export const buttonsHooks = { - preRender: [ - function (svg) { - for (const def of defs) { - svg.defs.setIfUnset(def.name, def.def) - } - }, - ], -} diff --git a/plugins/plugin-annotations/src/cutonfold.mjs b/plugins/plugin-annotations/src/cutonfold.mjs index 2ca8ab1cdb7..eec457b1ee5 100644 --- a/plugins/plugin-annotations/src/cutonfold.mjs +++ b/plugins/plugin-annotations/src/cutonfold.mjs @@ -1,4 +1,5 @@ -const defs = [ +// Export defs +export const cutonfoldDefs = [ { name: 'cutonfoldFrom', def: ` @@ -15,16 +16,6 @@ const defs = [ }, ] -// Export hooks -export const cutonfoldHooks = { - preRender: [ - function (svg) { - for (const def of defs) { - svg.defs.setIfUnset(def.name, def.def) - } - }, - ], -} // Export macros export const cutonfoldMacros = { cutonfold: function (so, { points, paths, Path, complete, setCutOnFold, setGrain, scale }) { diff --git a/plugins/plugin-annotations/src/dimensions.mjs b/plugins/plugin-annotations/src/dimensions.mjs index 53a6032d653..a4facf35b46 100644 --- a/plugins/plugin-annotations/src/dimensions.mjs +++ b/plugins/plugin-annotations/src/dimensions.mjs @@ -1,4 +1,5 @@ -const defs = [ +// Export defs +export const dimensionsDefs = [ { name: 'dimensionFrom', def: ` @@ -79,16 +80,7 @@ function lleader(so, type, props, id) { return point } -// Export hooks and macros -export const dimensionsHooks = { - preRender: [ - function (svg) { - for (const def of defs) { - svg.defs.setIfUnset(def.name, def.def) - } - }, - ], -} +// Export macros export const dimensionsMacros = { // horizontal hd: function (so, props) { diff --git a/plugins/plugin-annotations/src/grainline.mjs b/plugins/plugin-annotations/src/grainline.mjs index d7933d5b79c..d80bf28afd5 100644 --- a/plugins/plugin-annotations/src/grainline.mjs +++ b/plugins/plugin-annotations/src/grainline.mjs @@ -1,4 +1,5 @@ -const defs = [ +// Export defs +export const grainlineDefs = [ { name: 'grainlineFrom', def: ` @@ -17,16 +18,7 @@ const defs = [ const dflts = { text: 'grainline' } -// Export hooks and macros -export const grainlineHooks = { - preRender: [ - function (svg) { - for (const def of defs) { - svg.defs.setIfUnset(def.name, def.def) - } - }, - ], -} +// Export macros export const grainlineMacros = { grainline: function (so = {}, { points, paths, Path, complete, setGrain }) { if (so === false) { diff --git a/plugins/plugin-annotations/src/index.mjs b/plugins/plugin-annotations/src/index.mjs index 6596c99ba06..a81d61dd4a4 100644 --- a/plugins/plugin-annotations/src/index.mjs +++ b/plugins/plugin-annotations/src/index.mjs @@ -1,8 +1,8 @@ import { name, version } from '../data.mjs' -// Hooks only -import { buttonsHooks } from './buttons.mjs' -import { logoHooks } from './logo.mjs' -import { notchesHooks } from './notches.mjs' +// Defs only +import { buttonsDefs } from './buttons.mjs' +import { logoDefs } from './logo.mjs' +import { notchesDefs } from './notches.mjs' // Macros only import { bannerMacros } from './banner.mjs' import { bannerboxMacros } from './bannerbox.mjs' @@ -10,26 +10,36 @@ import { bartackMacros } from './bartack.mjs' import { crossboxMacros } from './crossbox.mjs' import { scaleboxMacros } from './scalebox.mjs' import { titleMacros } from './title.mjs' -// Hooks and Macros -import { cutonfoldMacros, cutonfoldHooks } from './cutonfold.mjs' -import { dimensionsMacros, dimensionsHooks } from './dimensions.mjs' -import { grainlineMacros, grainlineHooks } from './grainline.mjs' -import { pleatMacros, pleatHooks } from './pleat.mjs' -import { sewtogetherMacros, sewtogetherHooks } from './sewtogether.mjs' +// Defs and Macros +import { cutonfoldMacros, cutonfoldDefs } from './cutonfold.mjs' +import { dimensionsMacros, dimensionsDefs } from './dimensions.mjs' +import { grainlineMacros, grainlineDefs } from './grainline.mjs' +import { pleatMacros, pleatDefs } from './pleat.mjs' +import { sewtogetherMacros, sewtogetherDefs } from './sewtogether.mjs' export const plugin = { name, version, hooks: { preRender: [ - ...buttonsHooks.preRender, - ...logoHooks.preRender, - ...notchesHooks.preRender, - ...cutonfoldHooks.preRender, - ...dimensionsHooks.preRender, - ...grainlineHooks.preRender, - ...pleatHooks.preRender, - ...sewtogetherHooks.preRender, + function (svg) { + const defs = [ + ...buttonsDefs, + ...cutonfoldDefs, + ...dimensionsDefs, + ...grainlineDefs, + ...logoDefs, + ...notchesDefs, + ...pleatDefs, + ...sewtogetherDefs, + ] + for (const def of defs) { + svg.defs.setIfUnset( + def.name, + typeof def.def === 'function' ? def.def(svg.pattern.settings[0].scale) : def.def + ) + } + }, ], }, macros: { diff --git a/plugins/plugin-annotations/src/logo.mjs b/plugins/plugin-annotations/src/logo.mjs index 3b23908fa4b..40013a0dd41 100644 --- a/plugins/plugin-annotations/src/logo.mjs +++ b/plugins/plugin-annotations/src/logo.mjs @@ -1,11 +1,8 @@ -const logo = (scale) => - `` - -// Export hooks -export const logoHooks = { - preRender: [ - function (svg) { - svg.defs.setIfUnset('logo', logo(svg.pattern.settings[0].scale)) - }, - ], -} +// Export defs +export const logoDefs = [ + { + name: 'logo', + def: (scale) => + ``, + }, +] diff --git a/plugins/plugin-annotations/src/notches.mjs b/plugins/plugin-annotations/src/notches.mjs index f04f5c94317..eccb5ed6fb7 100644 --- a/plugins/plugin-annotations/src/notches.mjs +++ b/plugins/plugin-annotations/src/notches.mjs @@ -1,4 +1,5 @@ -const defs = [ +// Export hooks +export const notchesDefs = [ { name: 'notch', def: ` @@ -16,14 +17,3 @@ const defs = [ `, }, ] - -// Export hooks -export const notchesHooks = { - preRender: [ - function (svg) { - for (const def of defs) { - svg.defs.setIfUnset(def.name, def.def) - } - }, - ], -} diff --git a/plugins/plugin-annotations/src/pleat.mjs b/plugins/plugin-annotations/src/pleat.mjs index 3fe463455f0..3bc0f59c9ac 100644 --- a/plugins/plugin-annotations/src/pleat.mjs +++ b/plugins/plugin-annotations/src/pleat.mjs @@ -1,17 +1,14 @@ -const markers = ` +// Export defs +export const pleatDefs = [ + { + name: 'notch', + def: ` -` - -// Export hooks -export const pleatHooks = { - preRender: [ - function (svg) { - svg.defs.setIfUnset('pleatTo', markers) - }, - ], -} +`, + }, +] // Export macros export const pleatMacros = { diff --git a/plugins/plugin-annotations/src/sewtogether.mjs b/plugins/plugin-annotations/src/sewtogether.mjs index d17c5cebd44..6884865e6a7 100644 --- a/plugins/plugin-annotations/src/sewtogether.mjs +++ b/plugins/plugin-annotations/src/sewtogether.mjs @@ -1,4 +1,5 @@ -const defs = [ +// Export defs +export const sewtogetherDefs = [ { name: 'sewTogetherStart', def: ` @@ -22,17 +23,6 @@ const defs = [ }, ] -// Export hooks -export const sewtogetherHooks = { - preRender: [ - function (svg) { - for (const def of defs) { - svg.defs.setIfUnset(def.name, def.def) - } - }, - ], -} - // Export macros export const sewtogetherMacros = { sewTogether: function (so, { points, paths, Path, complete, sa }) { diff --git a/plugins/plugin-annotations/tests/buttons.test.mjs b/plugins/plugin-annotations/tests/buttons.test.mjs index 749142c7501..3539cc90058 100644 --- a/plugins/plugin-annotations/tests/buttons.test.mjs +++ b/plugins/plugin-annotations/tests/buttons.test.mjs @@ -11,7 +11,7 @@ pattern.draft().render() describe('Buttons Plugin Test', () => { for (const snippet of ['button', 'buttonhole', 'snap-stud', 'snap-socket']) { it(`Should add the ${snippet} snippet to defs`, () => { - expect(pattern.svg.defs.render().indexOf(``)).to.not.equal(-1) + expect(pattern.svg.defs.get(snippet)).to.not.equal(false) }) } diff --git a/plugins/plugin-annotations/tests/logo.test.mjs b/plugins/plugin-annotations/tests/logo.test.mjs index 9f6ca4102b8..3e0c6bc5c09 100644 --- a/plugins/plugin-annotations/tests/logo.test.mjs +++ b/plugins/plugin-annotations/tests/logo.test.mjs @@ -9,8 +9,6 @@ describe('Logo Plugin Tests', () => { const Pattern = new Design() const pattern = new Pattern().use(annotationsPlugin) pattern.draft().render() - expect(pattern.svg.defs.render()).to.contain( - '