45 lines
1.9 KiB
JavaScript
45 lines
1.9 KiB
JavaScript
// Turns a {kind, components} prefill into the literal string a print label should show/encode.
|
|
// Keeping this keyed by `kind` rather than having each caller build its own string means the
|
|
// format for a given kind of label content only has to be gotten right in one place.
|
|
export const LABEL_CONTENT_BUILDERS = {
|
|
// The self-contained Item URL (see docs/design-in-progress/items-labels.md) - what a
|
|
// printed label actually encodes, since scanning it has to resolve the right
|
|
// frontend/backend/item with no other context, not just this browser's history.
|
|
"item-url": ({user, id}) => `${window.location.origin}/i/${user}/${id}`,
|
|
};
|
|
|
|
export function buildLabelContent(prefill) {
|
|
if (!prefill) {
|
|
return "";
|
|
}
|
|
const build = LABEL_CONTENT_BUILDERS[prefill.kind];
|
|
return build ? build(prefill.components) : "";
|
|
}
|
|
|
|
// Named fields the field-specific label templates (see label-drawing.js's LABEL_TEMPLATES) draw
|
|
// from - keyed by `kind` for the same reason LABEL_CONTENT_BUILDERS is. A field missing from the
|
|
// result (rather than present-but-empty) is what LabelLayoutPreview.vue treats as "not available",
|
|
// so builders should only include a field once its inputs actually check out.
|
|
const LABEL_FIELD_BUILDERS = {
|
|
"item-url": ({user, id}) => {
|
|
if (!user || !id) {
|
|
return {};
|
|
}
|
|
return {
|
|
itemUrl: `${window.location.origin}/i/${user}/${id}`,
|
|
// The compact "owner handle + id" form from docs/design-in-progress/items-labels.md -
|
|
// meaningful only where context already makes clear it's a Toolshed item, unlike itemUrl.
|
|
itemHandle: `${user}:${id}`,
|
|
userHandle: user,
|
|
itemId: String(id),
|
|
};
|
|
},
|
|
};
|
|
|
|
export function buildLabelFields(prefill) {
|
|
if (!prefill) {
|
|
return {};
|
|
}
|
|
const build = LABEL_FIELD_BUILDERS[prefill.kind];
|
|
return build ? build(prefill.components) : {};
|
|
}
|