@dice-roller/svg-presenter 0.0.0-stage → 1.0.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,14 @@
1
+ /** Bundled templates for Node and browsers, with no runtime asset loading.
2
+ * Keep these in sync with assets/dice/*.svg; the artwork tests check parity.
3
+ */
4
+ export const defaultArtwork = Object.freeze({
5
+ d2: '<svg xmlns="http://www.w3.org/2000/svg"\n class="die die-d2"\n width="128" height="128" viewBox="0 0 100 100"\n fill="none" stroke="currentColor" stroke-width="2.2"\n stroke-linecap="round" stroke-linejoin="round"\n role="img" aria-label="Two-sided die">\n <circle class="die-outline" cx="50" cy="50" r="45"/>\n <text class="die-value" data-slot="value"\n x="50" y="50" text-anchor="middle" dominant-baseline="central"\n font-size="22" data-max-width="62" fill="currentColor" stroke="none"></text>\n</svg>\n',
6
+ d4: '<svg xmlns="http://www.w3.org/2000/svg"\n class="die die-d4"\n width="128" height="128" viewBox="0 0 100 100"\n fill="none" stroke="currentColor" stroke-width="2.2"\n stroke-linecap="round" stroke-linejoin="round"\n role="img" aria-label="Four-sided die">\n <path class="die-outline" d="M50 7 94 87H6Z"/>\n <text class="die-value" data-slot="value"\n x="50" y="60" text-anchor="middle" dominant-baseline="central"\n font-size="22" data-max-width="40" fill="currentColor" stroke="none"></text>\n</svg>\n',
7
+ d6: '<svg xmlns="http://www.w3.org/2000/svg"\n class="die die-d6"\n width="128" height="128" viewBox="0 0 100 100"\n fill="none" stroke="currentColor" stroke-width="2.2"\n stroke-linecap="round" stroke-linejoin="round"\n role="img" aria-label="Six-sided die">\n <rect class="die-outline" x="6" y="6" width="88" height="88" rx="2"/>\n <text class="die-value" data-slot="value"\n x="50" y="50" text-anchor="middle" dominant-baseline="central"\n font-size="22" data-max-width="62" fill="currentColor" stroke="none"></text>\n</svg>\n',
8
+ d8: '<svg xmlns="http://www.w3.org/2000/svg"\n class="die die-d8"\n width="128" height="128" viewBox="0 0 100 100"\n fill="none" stroke="currentColor" stroke-width="2.2"\n stroke-linecap="round" stroke-linejoin="round"\n role="img" aria-label="Eight-sided die">\n <path class="die-outline" d="M50 5 89 28 89 72 50 95 11 72 11 28Z"/>\n <path class="die-facets" stroke-width="1.4"\n d="M50 5 11 72H89Z"/>\n <text class="die-value" data-slot="value"\n x="50" y="51" text-anchor="middle" dominant-baseline="central"\n font-size="22" data-max-width="40" fill="currentColor" stroke="none"></text>\n</svg>\n',
9
+ d10: '<svg xmlns="http://www.w3.org/2000/svg"\n class="die die-d10"\n width="128" height="128" viewBox="0 0 100 100"\n fill="none" stroke="currentColor" stroke-width="2.2"\n stroke-linecap="round" stroke-linejoin="round"\n role="img" aria-label="Ten-sided die">\n <path class="die-outline" d="M50 5 88 37 94 62 50 95 6 62 12 37Z"/>\n <path class="die-facets" stroke-width="1.4"\n d="M50 5 22 57 50 76 78 57Z\n M6 62 22 57\n M78 57 94 62\n M50 76V95"/>\n <text class="die-value" data-slot="value"\n x="50" y="49" text-anchor="middle" dominant-baseline="central"\n font-size="22" data-max-width="35" fill="currentColor" stroke="none"></text>\n</svg>\n',
10
+ d12: '<svg xmlns="http://www.w3.org/2000/svg"\n class="die die-d12"\n width="128" height="128" viewBox="0 0 100 100"\n fill="none" stroke="currentColor" stroke-width="2.2"\n stroke-linecap="round" stroke-linejoin="round"\n role="img" aria-label="Twelve-sided die">\n <path class="die-outline"\n d="M50 5 76 14 93 37 93 65 76 86 50 95 24 86 7 65 7 37 24 14Z"/>\n <path class="die-facets" stroke-width="1.4"\n d="M50 22 80 44 68 77H32L20 44Z\n M50 5V22\n M80 44 93 37\n M68 77 76 86\n M32 77 24 86\n M20 44 7 37"/>\n <text class="die-value" data-slot="value"\n x="50" y="53" text-anchor="middle" dominant-baseline="central"\n font-size="22" data-max-width="44" fill="currentColor" stroke="none"></text>\n</svg>\n',
11
+ d20: '<svg xmlns="http://www.w3.org/2000/svg"\n class="die die-d20"\n width="128" height="128" viewBox="0 0 100 100"\n fill="none" stroke="currentColor" stroke-width="2.2"\n stroke-linecap="round" stroke-linejoin="round"\n role="img" aria-label="Twenty-sided die">\n <path class="die-outline" d="M50 5 90 27 90 73 50 95 10 73 10 27Z"/>\n <path class="die-facets" stroke-width="1.4"\n d="M50 5V20\n M10 27 50 20 90 27\n M90 27 82 72 90 73\n M82 72 50 95 18 72\n M18 72 10 73\n M10 27 18 72\n M50 20 18 72H82Z"/>\n <text class="die-value" data-slot="value"\n x="50" y="54" text-anchor="middle" dominant-baseline="central"\n font-size="22" data-max-width="36" fill="currentColor" stroke="none"></text>\n</svg>\n',
12
+ generic: '<svg xmlns="http://www.w3.org/2000/svg"\n class="die die-generic"\n width="128" height="128" viewBox="0 0 100 100"\n fill="none" stroke="currentColor" stroke-width="2.2"\n stroke-linecap="round" stroke-linejoin="round"\n role="img" aria-label="Die with an arbitrary number of faces">\n <path class="die-outline"\n d="M34 6H66Q69 6 71 8L92 29Q94 31 94 34V66Q94 69 92 71L71 92Q69 94 66 94H34Q31 94 29 92L8 71Q6 69 6 66V34Q6 31 8 29L29 8Q31 6 34 6Z"/>\n <text class="die-value" data-slot="value"\n x="50" y="50" text-anchor="middle" dominant-baseline="central"\n font-size="22" data-max-width="64" fill="currentColor" stroke="none"></text>\n</svg>\n',
13
+ });
14
+ //# sourceMappingURL=default-artwork.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"default-artwork.js","sourceRoot":"","sources":["../src/default-artwork.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,CAAC,MAAM,cAAc,GAEvB,MAAM,CAAC,MAAM,CAAC;IAChB,EAAE,EAAE,0gBAA0gB;IAC9gB,EAAE,EAAE,qgBAAqgB;IACzgB,EAAE,EAAE,2hBAA2hB;IAC/hB,EAAE,EAAE,smBAAsmB;IAC1mB,GAAG,EAAE,wqBAAwqB;IAC7qB,GAAG,EAAE,ovBAAovB;IACzvB,GAAG,EAAE,qvBAAqvB;IAC1vB,OAAO,EACL,wpBAAwpB;CAC3pB,CAAC,CAAC"}
@@ -0,0 +1,5 @@
1
+ export { defaultArtwork } from "./default-artwork.js";
2
+ export { SvgPresenter, renderSvg } from "./SvgPresenter.js";
3
+ export type { Appearance, SvgHistory, SvgRoll } from "./SvgPresenter.js";
4
+ export type { ModifierIconPosition } from "./modifier-icons.js";
5
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AACtD,OAAO,EAAE,YAAY,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAC;AAC5D,YAAY,EAAE,UAAU,EAAE,UAAU,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAEzE,YAAY,EAAE,oBAAoB,EAAE,MAAM,qBAAqB,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,3 @@
1
+ export { defaultArtwork } from "./default-artwork.js";
2
+ export { SvgPresenter, renderSvg } from "./SvgPresenter.js";
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AACtD,OAAO,EAAE,YAAY,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAC"}
@@ -0,0 +1,24 @@
1
+ /** The built-in visual modifier families. Once/limited variants share their family icon. */
2
+ export type ModifierIcon = "explode" | "compound" | "penetrate" | "re-roll" | "unique" | "min" | "max" | "target-success" | "target-failure" | "critical-success" | "critical-failure";
3
+ export type ModifierIconPosition = "corner" | "below";
4
+ export declare function modifierIcons(flags: readonly string[]): ModifierIcon[];
5
+ export declare const modifierIconStyles: string;
6
+ export interface PlacedModifierIcon {
7
+ readonly kind: ModifierIcon;
8
+ readonly x: number;
9
+ readonly y: number;
10
+ readonly width: number;
11
+ readonly height: number;
12
+ }
13
+ export interface ModifierIconLayout {
14
+ readonly items: readonly PlacedModifierIcon[];
15
+ readonly top: number;
16
+ readonly bottom: number;
17
+ readonly padding: number;
18
+ }
19
+ /** Coordinates are relative to the artwork's origin. Extra rows grow away from the dice. */
20
+ export declare function layoutModifierIcons(kinds: readonly ModifierIcon[], width: number, height: number, iconSize: number, position: ModifierIconPosition): ModifierIconLayout;
21
+ /** A luminance cutout removes artwork, rather than painting a background-coloured cover. */
22
+ export declare function renderModifierCutout(id: string, items: readonly PlacedModifierIcon[], width: number, height: number, scale?: number): string;
23
+ export declare function renderModifierIcon(icon: PlacedModifierIcon, id: string, x?: number, y?: number): string;
24
+ //# sourceMappingURL=modifier-icons.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"modifier-icons.d.ts","sourceRoot":"","sources":["../src/modifier-icons.ts"],"names":[],"mappings":"AAAA,4FAA4F;AAC5F,MAAM,MAAM,YAAY,GACpB,SAAS,GACT,UAAU,GACV,WAAW,GACX,SAAS,GACT,QAAQ,GACR,KAAK,GACL,KAAK,GACL,gBAAgB,GAChB,gBAAgB,GAChB,kBAAkB,GAClB,kBAAkB,CAAC;AACvB,MAAM,MAAM,oBAAoB,GAAG,QAAQ,GAAG,OAAO,CAAC;AA6DtD,wBAAgB,aAAa,CAAC,KAAK,EAAE,SAAS,MAAM,EAAE,GAAG,YAAY,EAAE,CAYtE;AAED,eAAO,MAAM,kBAAkB,QAOqF,CAAC;AAErH,MAAM,WAAW,kBAAkB;IACjC,QAAQ,CAAC,IAAI,EAAE,YAAY,CAAC;IAC5B,QAAQ,CAAC,CAAC,EAAE,MAAM,CAAC;IACnB,QAAQ,CAAC,CAAC,EAAE,MAAM,CAAC;IACnB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;CACzB;AACD,MAAM,WAAW,kBAAkB;IACjC,QAAQ,CAAC,KAAK,EAAE,SAAS,kBAAkB,EAAE,CAAC;IAC9C,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;CAC1B;AAED,4FAA4F;AAC5F,wBAAgB,mBAAmB,CACjC,KAAK,EAAE,SAAS,YAAY,EAAE,EAC9B,KAAK,EAAE,MAAM,EACb,MAAM,EAAE,MAAM,EACd,QAAQ,EAAE,MAAM,EAChB,QAAQ,EAAE,oBAAoB,GAC7B,kBAAkB,CA2CpB;AAED,4FAA4F;AAC5F,wBAAgB,oBAAoB,CAClC,EAAE,EAAE,MAAM,EACV,KAAK,EAAE,SAAS,kBAAkB,EAAE,EACpC,KAAK,EAAE,MAAM,EACb,MAAM,EAAE,MAAM,EACd,KAAK,SAAI,GACR,MAAM,CAER;AAED,wBAAgB,kBAAkB,CAChC,IAAI,EAAE,kBAAkB,EACxB,EAAE,EAAE,MAAM,EACV,CAAC,SAAI,EACL,CAAC,SAAI,GACJ,MAAM,CASR"}
@@ -0,0 +1,132 @@
1
+ const burst = "M16 1 19.37 7.87 26.61 5.39 24.13 12.63 31 16 24.13 19.37 26.61 26.61 19.37 24.13 16 31 12.63 24.13 5.39 26.61 7.87 19.37 1 16 7.87 12.63 5.39 5.39 12.63 7.87Z";
2
+ const definitions = {
3
+ explode: {
4
+ title: "Explosion triggered",
5
+ colour: "#9b5d0a",
6
+ body: `<path d="${burst}"/>`,
7
+ },
8
+ compound: { title: "Compounding explosion", colour: "#7546b2", body: "" },
9
+ penetrate: {
10
+ title: "Penetrating explosion",
11
+ colour: "#176e8e",
12
+ body: '<path d="M16 2h5v7l-3 4-2-3ZM16 22l3-3 2 3v8h-5Z" fill="currentColor" fill-opacity=".12"/><path d="M2 16h27m-6-5 7 5-7 5m0-12 2-2m-1 17 2 2"/>',
13
+ },
14
+ "re-roll": {
15
+ title: "Re-rolled",
16
+ colour: "#19735d",
17
+ body: '<path d="M27 11A12 12 0 1 0 27 23M27 3v8h-8"/>',
18
+ },
19
+ unique: {
20
+ title: "Re-rolled for uniqueness",
21
+ colour: "#a04476",
22
+ body: '<circle cx="5" cy="16" r="3"/><path d="m16 7 7 9-7 9-7-9Z"/><circle cx="27" cy="16" r="3"/>',
23
+ },
24
+ min: {
25
+ title: "Raised to the minimum",
26
+ colour: "#316eac",
27
+ body: '<path d="M6 27h20M16 23V5m-6 6 6-6 6 6"/>',
28
+ },
29
+ max: {
30
+ title: "Capped at the maximum",
31
+ colour: "#997012",
32
+ body: '<path d="M6 5h20M16 9v18m-6-6 6 6 6-6"/>',
33
+ },
34
+ "target-success": {
35
+ title: "Target success",
36
+ colour: "#247d4a",
37
+ body: '<path d="m5 17 7 7L27 8"/>',
38
+ },
39
+ "target-failure": {
40
+ title: "Target failure",
41
+ colour: "#b34654",
42
+ body: '<path d="m7 7 18 18M25 7 7 25"/>',
43
+ },
44
+ "critical-success": {
45
+ title: "Critical success",
46
+ colour: "#96700b",
47
+ body: '<path d="M16 2 20 11 30 12 22.5 19.5 25.5 29 16 24 6.5 29 9.5 19.5 2 12 12 11Z"/>',
48
+ },
49
+ "critical-failure": {
50
+ title: "Critical failure",
51
+ colour: "#a5406b",
52
+ body: '<path d="M9 28v-6C5 20 3 17 3 12a13 10 0 0 1 26 0c0 5-2 8-6 10v6ZM13 24v4m6-4v4"/><circle cx="10" cy="13" r="2.2" fill="currentColor" stroke="none"/><circle cx="22" cy="13" r="2.2" fill="currentColor" stroke="none"/><path d="m16 17-2 4h4Z" fill="currentColor" stroke="none"/>',
53
+ },
54
+ };
55
+ export function modifierIcons(flags) {
56
+ const icons = [];
57
+ const add = (name) => {
58
+ if (!icons.includes(name))
59
+ icons.push(name);
60
+ };
61
+ for (const flag of flags) {
62
+ if (flag === "explode" && flags.includes("compound"))
63
+ continue;
64
+ if (flag === "re-roll-once")
65
+ add("re-roll");
66
+ else if (flag === "unique-once")
67
+ add("unique");
68
+ else if (Object.hasOwn(definitions, flag))
69
+ add(flag);
70
+ }
71
+ return icons;
72
+ }
73
+ export const modifierIconStyles = Object.entries(definitions)
74
+ .map(([name, icon]) => `.result-preview .modifier-${name}{color:var(--modifier-${name}-color,var(--modifier-icon-color,${icon.colour}))}`)
75
+ .join("") +
76
+ ".result-preview .modifier-compound-back{stroke:var(--modifier-explode-color,var(--modifier-icon-color,#9b5d0a))}";
77
+ /** Coordinates are relative to the artwork's origin. Extra rows grow away from the dice. */
78
+ export function layoutModifierIcons(kinds, width, height, iconSize, position) {
79
+ if (!kinds.length)
80
+ return { items: [], top: 0, bottom: 0, padding: 6 };
81
+ const gap = Math.max(4, iconSize * 0.2);
82
+ const rows = [[]];
83
+ let used = 0;
84
+ for (const kind of kinds) {
85
+ const iconWidth = iconSize * (kind === "compound" ? 1.3 : 1);
86
+ if (used && used + gap + iconWidth > width) {
87
+ rows.push([]);
88
+ used = 0;
89
+ }
90
+ rows[rows.length - 1].push({ kind, width: iconWidth });
91
+ used += (used ? gap : 0) + iconWidth;
92
+ }
93
+ const items = [];
94
+ for (const [index, row] of rows.entries()) {
95
+ const rowWidth = row.reduce((sum, item) => sum + item.width, 0) + (row.length - 1) * gap;
96
+ let x = position === "corner"
97
+ ? width + row.at(-1).width / 2 - rowWidth
98
+ : (width - rowWidth) / 2;
99
+ const y = position === "corner"
100
+ ? -iconSize / 2 - index * (iconSize + gap)
101
+ : height + 6 + index * (iconSize + gap);
102
+ for (const item of row) {
103
+ items.push({ ...item, x, y, height: iconSize });
104
+ x += item.width + gap;
105
+ }
106
+ }
107
+ return {
108
+ items,
109
+ top: position === "corner"
110
+ ? iconSize / 2 + (rows.length - 1) * (iconSize + gap) + 3
111
+ : 0,
112
+ bottom: position === "below"
113
+ ? 10 + rows.length * iconSize + (rows.length - 1) * gap
114
+ : 0,
115
+ padding: position === "corner" ? Math.max(6, iconSize * 0.65 + 5) : 6,
116
+ };
117
+ }
118
+ /** A luminance cutout removes artwork, rather than painting a background-coloured cover. */
119
+ export function renderModifierCutout(id, items, width, height, scale = 1) {
120
+ return `<defs><mask id="${id}" class="modifier-cutout" stroke="none" maskUnits="userSpaceOnUse" x="0" y="0" width="${width * scale}" height="${height * scale}"><rect width="${width * scale}" height="${height * scale}" fill="white"/>${items.map((icon) => `<circle cx="${(icon.x + icon.width / 2) * scale}" cy="${(icon.y + icon.height / 2) * scale}" r="${(Math.max(icon.width, icon.height) / 2 + 5) * scale}" fill="black"/>`).join("")}</mask></defs>`;
121
+ }
122
+ export function renderModifierIcon(icon, id, x = 0, y = 0) {
123
+ const { kind, width, height } = icon;
124
+ const definition = definitions[kind];
125
+ let body = definition.body;
126
+ if (kind === "compound") {
127
+ body = `<defs><mask id="${id}" class="modifier-overlap-mask" maskUnits="userSpaceOnUse" x="-2" y="-5" width="48" height="40"><rect x="-2" y="-5" width="48" height="40" fill="white" stroke="none"/><path d="${burst}" fill="black" stroke="black" stroke-width="1.8"/></mask></defs><g mask="url(#${id})"><path class="modifier-compound-back" stroke="#9b5d0a" transform="translate(10 -2)" d="${burst}"/></g><path d="${burst}"/>`;
128
+ }
129
+ const box = kind === "compound" ? "0 -3 43 35" : "0 0 32 32";
130
+ return `<svg class="modifier-icon modifier-${kind}" data-modifier="${kind}" color="${definition.colour}" x="${x + icon.x}" y="${y + icon.y}" width="${width}" height="${height}" viewBox="${box}" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round" role="img" aria-label="${definition.title}"><title>${definition.title}</title><rect x="0" y="${kind === "compound" ? -3 : 0}" width="${kind === "compound" ? 43 : 32}" height="${kind === "compound" ? 35 : 32}" fill="transparent" stroke="none"/>${body}</svg>`;
131
+ }
132
+ //# sourceMappingURL=modifier-icons.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"modifier-icons.js","sourceRoot":"","sources":["../src/modifier-icons.ts"],"names":[],"mappings":"AAeA,MAAM,KAAK,GACT,iKAAiK,CAAC;AACpK,MAAM,WAAW,GAGb;IACF,OAAO,EAAE;QACP,KAAK,EAAE,qBAAqB;QAC5B,M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AAE,CAAC,CAAC,CAAC,EAAE,aAAa,IAAI,KAAK,UAAU,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,uCAAuC,IAAI,QAAQ,CAAC;AAC1iB,CAAC"}
@@ -0,0 +1,3 @@
1
+ /** Inserts a value into a trusted dice SVG template without requiring a DOM. */
2
+ export declare function fillValueSlot(svg: string, value: string | number): string;
3
+ //# sourceMappingURL=value-slot.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"value-slot.d.ts","sourceRoot":"","sources":["../src/value-slot.ts"],"names":[],"mappings":"AAAA,gFAAgF;AAChF,wBAAgB,aAAa,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM,CAsCzE"}
@@ -0,0 +1,27 @@
1
+ /** Inserts a value into a trusted dice SVG template without requiring a DOM. */
2
+ export function fillValueSlot(svg, value) {
3
+ const text = String(value);
4
+ const escaped = text.replace(/[&<>"']/g, (character) => {
5
+ return {
6
+ "&": "&amp;",
7
+ "<": "&lt;",
8
+ ">": "&gt;",
9
+ '"': "&quot;",
10
+ "'": "&apos;",
11
+ }[character];
12
+ });
13
+ return svg.replace(/<text\b([^>]*\bdata-slot="value"[^>]*)>[\s\S]*?<\/text>/g, (_slot, attributes) => {
14
+ const fontSize = Number(attributes.match(/\bfont-size="([\d.]+)"/)?.[1]);
15
+ const maxWidth = Number(attributes.match(/\bdata-max-width="([\d.]+)"/)?.[1]);
16
+ if (fontSize > 0 && maxWidth > 0 && text.length > 0) {
17
+ // Conservative estimate: keeps common numeric values inside the face
18
+ // without relying on browser font measurement. CSS can override the size.
19
+ // Plus/minus glyphs occupy less of the face than numerals.
20
+ const symbolScale = text === "+" || text === "−" ? 2 : 1;
21
+ const fittedSize = Math.min(fontSize * symbolScale, maxWidth / (Array.from(text).length * 0.7));
22
+ attributes = attributes.replace(/\bfont-size="[\d.]+"/, `font-size="${Number(fittedSize.toFixed(2))}"`);
23
+ }
24
+ return `<text${attributes}>${escaped}</text>`;
25
+ });
26
+ }
27
+ //# sourceMappingURL=value-slot.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"value-slot.js","sourceRoot":"","sources":["../src/value-slot.ts"],"names":[],"mappings":"AAAA,gFAAgF;AAChF,MAAM,UAAU,aAAa,CAAC,GAAW,EAAE,KAAsB;IAC/D,MAAM,IAAI,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC;IAC3B,MAAM,OAAO,GAAG,IAAI,CAAC,OAAO,CAAC,UAAU,EAAE,CAAC,SAAS,EAAE,EAAE;QACrD,OAAO;YACL,GAAG,EAAE,OAAO;YACZ,GAAG,EAAE,MAAM;YACX,GAAG,EAAE,MAAM;YACX,GAAG,EAAE,QAAQ;YACb,GAAG,EAAE,QAAQ;SACd,CAAC,SAAS,CAAE,CAAC;IAChB,CAAC,CAAC,CAAC;IAEH,OAAO,GAAG,CAAC,OAAO,CAChB,0DAA0D,EAC1D,CAAC,KAAK,EAAE,UAAkB,EAAE,EAAE;QAC5B,MAAM,QAAQ,GAAG,MAAM,CAAC,UAAU,CAAC,KAAK,CAAC,wBAAwB,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC;QACzE,MAAM,QAAQ,GAAG,MAAM,CACrB,UAAU,CAAC,KAAK,CAAC,6BAA6B,CAAC,EAAE,CAAC,CAAC,CAAC,CACrD,CAAC;QAEF,IAAI,QAAQ,GAAG,CAAC,IAAI,QAAQ,GAAG,CAAC,IAAI,IAAI,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;YACpD,qEAAqE;YACrE,0EAA0E;YAC1E,2DAA2D;YAC3D,MAAM,WAAW,GAAG,IAAI,KAAK,GAAG,IAAI,IAAI,KAAK,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;YACzD,MAAM,UAAU,GAAG,IAAI,CAAC,GAAG,CACzB,QAAQ,GAAG,WAAW,EACtB,QAAQ,GAAG,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,MAAM,GAAG,GAAG,CAAC,CAC3C,CAAC;YACF,UAAU,GAAG,UAAU,CAAC,OAAO,CAC7B,sBAAsB,EACtB,cAAc,MAAM,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,GAAG,CAC/C,CAAC;QACJ,CAAC;QAED,OAAO,QAAQ,UAAU,IAAI,OAAO,SAAS,CAAC;IAChD,CAAC,CACF,CAAC;AACJ,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,42 @@
1
1
  {
2
2
  "name": "@dice-roller/svg-presenter",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "1.0.0-beta.1",
4
+ "description": "CSS-stylable SVG results for RPG Dice Roller.",
5
+ "type": "module",
6
+ "sideEffects": false,
7
+ "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "import": "./dist/index.js"
12
+ },
13
+ "./icons/*.svg": "./assets/dice/*.svg"
14
+ },
15
+ "files": [
16
+ "dist",
17
+ "assets/dice"
18
+ ],
19
+ "publishConfig": {
20
+ "access": "public"
21
+ },
22
+ "repository": {
23
+ "type": "git",
24
+ "url": "https://github.com/dice-roller/presenters.git",
25
+ "directory": "packages/svg"
26
+ },
27
+ "engines": {
28
+ "node": ">=22.12.0"
29
+ },
30
+ "scripts": {
31
+ "build": "tsc -p tsconfig.build.json",
32
+ "typecheck": "tsc --noEmit -p tsconfig.json && tsc -p tests/tsconfig.json",
33
+ "test": "npm run build && node --import tsx --test tests/*.test.ts",
34
+ "prepack": "npm run build"
35
+ },
36
+ "peerDependencies": {
37
+ "@dice-roller/rpg-dice-roller": ">=6.0.0-0"
38
+ },
39
+ "devDependencies": {
40
+ "@dice-roller/rpg-dice-roller": "^6.0.0-beta.5"
41
+ }
42
+ }