1
0
Fork 0
freesewing/packages/plugin-scalebox/src/index.js

133 lines
5.5 KiB
JavaScript
Raw Normal View History

2019-08-03 15:03:33 +02:00
import { version, name } from '../package.json'
2018-08-20 14:00:26 +02:00
export default {
name: name,
version: version,
hooks: {
preRender: function(svg) {
2019-08-03 15:03:33 +02:00
svg.attributes.set('freesewing:plugin-scalebox', version)
2018-08-20 14:00:26 +02:00
}
},
macros: {
scalebox: function(so) {
// Passing `false` will remove the scalebox
if (so === false) {
for (let id of [
2019-08-03 15:03:33 +02:00
'__scaleboxMetricTopLeft',
'__scaleboxMetricTopRight',
'__scaleboxMetricBottomRight',
'__scaleboxMetricBottomLeft',
'__scaleboxImperialTopLeft',
'__scaleboxImperialTopRight',
'__scaleboxImperialBottomRight',
'__scaleboxImperialBottomLeft',
'__scaleboxLead',
'__scaleboxTitle',
'__scaleboxText',
'__scaleboxLink',
'__scaleboxMetric',
'__scaleboxImperial'
])
2019-08-03 15:03:33 +02:00
delete this.points[id]
for (let id of ['__scaleboxMetric', '__scaleboxImperial']) delete this.paths[id]
return true
}
2018-09-11 14:37:36 +02:00
// Box points
2019-08-03 15:03:33 +02:00
this.points.__scaleboxMetricTopLeft = new this.Point(so.at.x - 50, so.at.y - 25)
this.points.__scaleboxMetricTopRight = new this.Point(so.at.x + 50, so.at.y - 25)
this.points.__scaleboxMetricBottomLeft = new this.Point(so.at.x - 50, so.at.y + 25)
this.points.__scaleboxMetricBottomRight = new this.Point(so.at.x + 50, so.at.y + 25)
this.points.__scaleboxImperialTopLeft = new this.Point(so.at.x - 50.8, so.at.y - 25.4)
this.points.__scaleboxImperialTopRight = new this.Point(so.at.x + 50.8, so.at.y - 25.4)
this.points.__scaleboxImperialBottomLeft = new this.Point(so.at.x - 50.8, so.at.y + 25.4)
this.points.__scaleboxImperialBottomRight = new this.Point(so.at.x + 50.8, so.at.y + 25.4)
2018-09-11 14:37:36 +02:00
// Text anchor points
2019-08-03 15:03:33 +02:00
this.points.__scaleboxLead = new this.Point(so.at.x - 45, so.at.y - 15)
this.points.__scaleboxTitle = this.points.__scaleboxLead.shift(-90, 10)
this.points.__scaleboxText = this.points.__scaleboxTitle.shift(-90, 8)
this.points.__scaleboxLink = this.points.__scaleboxText.shift(-90, 8)
this.points.__scaleboxMetric = new this.Point(so.at.x, so.at.y + 20)
this.points.__scaleboxImperial = new this.Point(so.at.x, so.at.y + 24)
2018-09-11 14:37:36 +02:00
// Rotation
if (so.rotate) {
let points = [
2019-08-03 15:03:33 +02:00
'__scaleboxMetricTopLeft',
'__scaleboxMetricTopRight',
'__scaleboxMetricBottomLeft',
'__scaleboxMetricBottomRight',
'__scaleboxImperialTopLeft',
'__scaleboxImperialTopRight',
'__scaleboxImperialBottomLeft',
'__scaleboxImperialBottomRight',
'__scaleboxLead',
'__scaleboxTitle',
'__scaleboxText',
'__scaleboxLink',
'__scaleboxMetric',
'__scaleboxImperial'
]
for (let pid of points) this.points[pid] = this.points[pid].rotate(so.rotate, so.at)
2018-09-11 14:37:36 +02:00
for (let pid of points.slice(8)) {
this.points[pid].attributes.set(
2019-08-03 15:03:33 +02:00
'data-text-transform',
`rotate(${so.rotate * -1}, ${this.points[pid].x}, ${this.points[pid].y})`
)
2018-09-11 14:37:36 +02:00
}
}
// Paths
2018-08-20 14:00:26 +02:00
this.paths.__scaleboxImperial = new this.Path()
2019-08-03 15:03:33 +02:00
.attr('class', 'scalebox imperial')
2018-08-20 14:00:26 +02:00
.move(this.points.__scaleboxImperialTopLeft)
.line(this.points.__scaleboxImperialBottomLeft)
.line(this.points.__scaleboxImperialBottomRight)
.line(this.points.__scaleboxImperialTopRight)
2019-08-03 15:03:33 +02:00
.close()
2018-08-20 14:00:26 +02:00
this.paths.__scaleboxMetric = new this.Path()
2019-08-03 15:03:33 +02:00
.attr('class', 'scalebox metric')
2018-08-20 14:00:26 +02:00
.move(this.points.__scaleboxMetricTopLeft)
.line(this.points.__scaleboxMetricBottomLeft)
.line(this.points.__scaleboxMetricBottomRight)
.line(this.points.__scaleboxMetricTopRight)
2019-08-03 15:03:33 +02:00
.close()
2018-08-20 14:00:26 +02:00
// Lead
2018-09-11 14:37:36 +02:00
this.points.__scaleboxLead = this.points.__scaleboxLead
2019-08-03 15:03:33 +02:00
.attr('data-text', so.lead || 'freesewing')
.attr('data-text-class', 'text-sm')
2018-08-20 14:00:26 +02:00
// Title
2019-08-03 15:03:33 +02:00
if (so.title) this.points.__scaleboxTitle.attributes.set('data-text', so.title)
2018-09-11 14:37:36 +02:00
else {
this.points.__scaleboxTitle = this.points.__scaleboxTitle
2019-08-03 15:03:33 +02:00
.attr('data-text', this.context.config.name)
.attr('data-text', 'v' + this.context.config.version)
2018-09-11 14:37:36 +02:00
}
2019-08-03 15:03:33 +02:00
this.points.__scaleboxTitle.attributes.add('data-text-class', 'text-lg')
2018-08-20 14:00:26 +02:00
// Text
2019-08-03 15:03:33 +02:00
if (typeof so.text === 'string') {
this.points.__scaleboxText.attr('data-text', so.text)
2018-08-20 14:00:26 +02:00
} else {
this.points.__scaleboxText
2019-08-03 15:03:33 +02:00
.attr('data-text', 'freesewingIsMadeByJoostDeCockAndContributors')
.attr('data-text', '\n')
.attr('data-text', 'withTheFinancialSupportOfOurPatrons')
2018-09-11 14:37:36 +02:00
this.points.__scaleboxLink = this.points.__scaleboxLink
2019-08-03 15:03:33 +02:00
.attr('data-text', 'freesewing.org/patrons/join')
.attr('data-text-class', 'text-xs fill-note')
2018-08-20 14:00:26 +02:00
}
2019-08-03 15:03:33 +02:00
this.points.__scaleboxText.attr('data-text-class', 'text-xs').attr('data-text-lineheight', 4)
2018-08-20 14:00:26 +02:00
// Instructions
2018-09-11 14:37:36 +02:00
this.points.__scaleboxMetric = this.points.__scaleboxMetric
2019-08-03 15:03:33 +02:00
.attr('data-text', 'theWhiteInsideOfThisBoxShouldMeasure')
.attr('data-text', '10cm')
.attr('data-text', 'x')
.attr('data-text', '5cm')
.attr('data-text-class', 'text-xs center')
2018-09-11 14:37:36 +02:00
this.points.__scaleboxImperial = this.points.__scaleboxImperial
2019-08-03 15:03:33 +02:00
.attr('data-text', 'theBlackOutsideOfThisBoxShouldMeasure')
.attr('data-text', '4"')
.attr('data-text', 'x')
.attr('data-text', '2"')
.attr('data-text-class', 'text-xs center ')
2018-08-20 14:00:26 +02:00
}
}
2019-08-03 15:03:33 +02:00
}