1
0
Fork 0

feat(shared): Add pattern information to PDF cover sheet

This commit is contained in:
Benjamin Fan 2024-02-13 08:09:57 -08:00
parent 8f7fffb81e
commit bc58919bb1
3 changed files with 77 additions and 6 deletions

View file

@ -13,6 +13,8 @@ import {
printSettingsPath,
} from 'shared/components/workbench/views/print/config.mjs'
import get from 'lodash.get'
import mustache from 'mustache'
import he from 'he'
export const ns = ['cut', 'plugin', 'common']
export const exportTypes = {
@ -187,6 +189,18 @@ export const handleExport = async ({
pattern.draft()
workerArgs.svg = pattern.render()
// Save pattern forName, notes, warnings info for coversheet
const store = pattern.setStores[pattern.activeSet]
workerArgs.strings.forName = store.get('data.for', 'ephemeral')
const notes = store?.plugins?.['plugin-annotations']?.flags?.note
? store?.plugins?.['plugin-annotations']?.flags?.note
: []
const warns = store?.plugins?.['plugin-annotations']?.flags?.warn
? store?.plugins?.['plugin-annotations']?.flags?.warn
: []
workerArgs.strings.notes = flagsToString(notes, mustache, t)
workerArgs.strings.warns = flagsToString(warns, mustache, t)
if (format === 'pdf') pageSettings.size = [pattern.width, pattern.height]
// add the svg and pages data to the worker args
@ -209,3 +223,23 @@ export const handleExport = async ({
// post a message to the worker with all needed data
worker.postMessage(workerArgs)
}
/**
* Convert pattern flags to a formatted string for printing
*/
const flagsToString = (flags, mustache, t) => {
let first = true
let string = ''
for (const flag of Object.values(flags)) {
let title = flag.replace ? mustache.render(t(flag.title), flag.replace) : t(flag.title)
title = he.decode(title)
let desc = flag.replace ? mustache.render(t(flag.desc), flag.replace) : t(flag.desc)
desc = desc.replaceAll('\n\n', '\n')
desc = desc.replaceAll('\n', ' ')
desc = he.decode(desc)
if (!first) string += '\n'
first = false
string += '- ' + title + ': ' + desc
}
return string
}