From 3cb5384df52bbff2a1640bac4b708363a942cd32 Mon Sep 17 00:00:00 2001 From: Joost De Cock Date: Tue, 27 Sep 2022 18:22:56 +0200 Subject: [PATCH] feat(core): Added path.addText and path.setText methods Also added a parameter to path.reverse to make it a deep clone. --- packages/core/src/path.mjs | 30 +++++++++++++++++++++++++++++- packages/core/src/pattern.mjs | 5 +++-- packages/core/src/point.mjs | 3 ++- 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/packages/core/src/path.mjs b/packages/core/src/path.mjs index 8e6f23954be..3110731ebb5 100644 --- a/packages/core/src/path.mjs +++ b/packages/core/src/path.mjs @@ -67,6 +67,20 @@ Path.prototype.addClass = function (className = false) { return this } +/** + * A chainable way to add text to a Path + * + * @param {string} text - The text to add to the Path + * @param {string} className - The CSS classes to apply to the text + * @return {Path} this - The Path instance + */ +Path.prototype.addText = function (text = '', className = false) { + this.attributes.add('data-text', text) + if (className) this.attributes.add('data-text-class', className) + + return this +} + /** * Returns the SVG pathstring for this path * @@ -536,7 +550,7 @@ Path.prototype.offset = function (distance) { * * @return {object} reverse - A Path instance that is the reversed version of this Path */ -Path.prototype.reverse = function () { +Path.prototype.reverse = function (cloneAttributes = false) { let sections = [] let current let closed = false @@ -555,6 +569,7 @@ Path.prototype.reverse = function () { let rev = new Path().__withLog(this.log).move(current) for (let section of sections.reverse()) rev.ops.push(section.ops[1]) if (closed) rev.close() + if (cloneAttributes) rev.attributes = this.attributes.clone() return rev } @@ -613,6 +628,19 @@ Path.prototype.setHidden = function (hidden = false) { return this } +/** + * A chainable way to set text on a Path + * + * @param {string} text - The text to add to the Path + * @param {string} className - The CSS classes to apply to the text + * @return {Path} this - The Path instance + */ +Path.prototype.setText = function (text = '', className = false) { + this.attributes.set('data-text', text) + if (className) this.attributes.set('data-text-class', className) + + return this +} /** * Returns a point that lies at distance along this Path * diff --git a/packages/core/src/pattern.mjs b/packages/core/src/pattern.mjs index 67c6a7280a3..a9846e2aee8 100644 --- a/packages/core/src/pattern.mjs +++ b/packages/core/src/pattern.mjs @@ -149,8 +149,9 @@ Pattern.prototype.draft = function () { this.setStores[set].log.error( `Unable to draft pattern part __${partName}__. Part.draft() is not callable` ) - this.parts[set][partName].hidden = - this.parts[set][partName].hidden === true ? true : !this.__wants(partName, set) + // FIXME: THis won't work not that this is immutable + // But is it still needed? + // this.parts[set][partName].hidden === true ? true : !this.__wants(partName, set) } else { this.setStores[set].log.debug( `Part \`${partName}\` is not needed. Skipping draft and setting hidden to \`true\`` diff --git a/packages/core/src/point.mjs b/packages/core/src/point.mjs index 744b9c18935..0a4d5e3ad8a 100644 --- a/packages/core/src/point.mjs +++ b/packages/core/src/point.mjs @@ -50,6 +50,7 @@ Point.prototype.addText = function (text = '', className = false) { return this.__check() } + /** * Returns the angle between this Point and that Point * @@ -202,7 +203,7 @@ Point.prototype.setCircle = function (radius = false, className = false) { } /** - * A chainable way to add text to a Point + * A chainable way to set text on a Point * * @param {string} text - The text to add to the Point * @param {string} className - The CSS classes to apply to the text