1
0
Fork 0

feat: Support notes in title macro

This started out as a fix for #5753 after I felt the approach in #5760
was not the best way to handle this.

The problem lies in the way the cutlist data is added to the pattern by
the title plugin. The whole cutlist thing was added by a less
experienced contributor, and they were a little too focussed on what
they were trying to accomplish, rather than keeping an eye on the big
picture.

In this case, a bunch of points get added to the part to anchor all of
the text for the cutlist. This causes problems when inheriting parts
because points are added per material, and now we don't know what
materials were used and how to remove these points.

I appreciate @woutervdub effort to resolve this in #5760 but as I
mentioned above, I don't love the approach.
Rather than finding a clever way to remove these points, I am of the
opinion that they should never have been added in the first place.

Specifically, all we are doing is adding one or more lines of text. The
idea that you need multiple points for this is an incorrect assumption,
and therein lies the root of this problem.

So, this refactor adds support to the title macro for 'notes'. These
notes will be added below the title. The cutlist info is now just
prepended to the notes. So it stops being something 'special' (which it
never should have been) and instead just behaves like other text that is
placed on the part by the title macro.

The only flanking measure required to make this happen was to add
support for nested arrays in handling text.
This way, you can pass a nested array to concatenate strings in such a
way that each part will still be individually translated.

And as all text in FreeSewing, to force a linebreak, you just use "\n"
as The Dark Lord intended.
This commit is contained in:
joostdecock 2024-01-06 17:54:11 +01:00
parent 9e5fbdf3ba
commit c06a18e06e
4 changed files with 109 additions and 101 deletions

View file

@ -1,25 +1,34 @@
import { name, version } from '../data.mjs'
const translate = (locale, text, t, pattern) => {
let translated = ''
/*
* Call oneself recursively if text is an array
*/
if (Array.isArray(text))
return text.map((string) => translate(locale, string, t, pattern)).join(' ')
if (t instanceof Function) return t(text, locale)
else if (typeof t[locale] === 'object') return t[locale][text] || text
else {
const msg =
"No translation method or object was passed to the i18n plugin. This plugin won't do anything without that"
if (pattern?.store?.log?.warn) {
if (!pattern.store.get(['plugins', 'plugin-i18n', 'missingMethodWarning'])) {
pattern.store.set(['plugins', 'plugin-i18n', 'missingMethodWarning'], true)
pattern.store.log.warn(msg)
}
} else console.log(msg)
return text
}
}
export const plugin = {
name,
version,
hooks: {
insertText: (locale, text, t, pattern) => {
if (t instanceof Function) return t(text, locale)
else if (typeof t[locale] === 'object') return t[locale][text] || text
else {
const msg =
"No translation method or object was passed to the i18n plugin. This plugin won't do anything without that"
if (pattern?.store?.log?.warn) {
if (!pattern.store.get(['plugins', 'plugin-i18n', 'missingMethodWarning'])) {
pattern.store.set(['plugins', 'plugin-i18n', 'missingMethodWarning'], true)
pattern.store.log.warn(msg)
}
} else console.log(msg)
return text
}
},
insertText: (locale, text, t, pattern) => translate(locale, text, t, pattern),
},
}