@pie-element/shared-utils 0.1.6-next.20261019151920 → 0.1.6

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.
package/dist/index.js CHANGED
@@ -59,12 +59,12 @@ function d(e, t) {
59
59
  }
60
60
  //#endregion
61
61
  //#region src/authored-colors.ts
62
- var f = "data-pie-authored-ink", p = "data-pie-authored-fill", m = "data-pie-authored-border", ee = [
62
+ var f = "data-pie-authored-ink", p = "data-pie-authored-fill", m = "data-pie-authored-border", h = [
63
63
  "border-top-color",
64
64
  "border-right-color",
65
65
  "border-bottom-color",
66
66
  "border-left-color"
67
- ], te = /* @__PURE__ */ new Set([
67
+ ], g = /* @__PURE__ */ new Set([
68
68
  "currentcolor",
69
69
  "inherit",
70
70
  "initial",
@@ -73,35 +73,35 @@ var f = "data-pie-authored-ink", p = "data-pie-authored-fill", m = "data-pie-aut
73
73
  "revert-layer",
74
74
  "transparent"
75
75
  ]);
76
- function h(e) {
76
+ function _(e) {
77
77
  let t = e?.trim().toLowerCase();
78
- return !t || te.has(t) || t.includes("var(") ? !1 : v(t)?.a !== 0;
78
+ return !t || g.has(t) || t.includes("var(") ? !1 : y(t)?.a !== 0;
79
79
  }
80
- function ne(e) {
81
- let t = v(e);
80
+ function ee(e) {
81
+ let t = y(e);
82
82
  if (!t) return "shade";
83
83
  let n = (e) => (e * t.a + 255 * (1 - t.a)) / 255, [r, i, a] = [
84
84
  n(t.r),
85
85
  n(t.g),
86
86
  n(t.b)
87
- ], o = .2126 * I(r) + .7152 * I(i) + .0722 * I(a), s = Math.max(r, i, a) - Math.min(r, i, a);
87
+ ], o = .2126 * P(r) + .7152 * P(i) + .0722 * P(a), s = Math.max(r, i, a) - Math.min(r, i, a);
88
88
  return o >= .85 && s <= .08 ? "light" : "shade";
89
89
  }
90
- function g(e) {
90
+ function v(e) {
91
91
  e.removeAttribute(f), e.removeAttribute(p), e.removeAttribute(m);
92
92
  let t = e.hasAttribute("style") ? e.style : void 0, n = (e) => t?.getPropertyValue(e).trim() ?? "", r = [], i = n("color"), a = e.localName === "font" ? e.getAttribute("color") : null;
93
- h(i || a) && (e.setAttribute(f, ""), i && r.push("color"));
94
- let o = n("background-color"), s = o || _(e.getAttribute("bgcolor"));
95
- h(s) && (e.setAttribute(p, ne(s)), o && r.push("background-color"));
96
- let c = ee.filter((e) => h(n(e)));
93
+ _(i || a) && (e.setAttribute(f, ""), i && r.push("color"));
94
+ let o = n("background-color"), s = o || te(e.getAttribute("bgcolor"));
95
+ _(s) && (e.setAttribute(p, ee(s)), o && r.push("background-color"));
96
+ let c = h.filter((e) => _(n(e)));
97
97
  c.length && (e.setAttribute(m, ""), r.push(...c));
98
98
  for (let e of r) t?.getPropertyPriority(e) === "important" && t.setProperty(e, t.getPropertyValue(e));
99
99
  }
100
- function _(e) {
100
+ function te(e) {
101
101
  let t = e?.trim() ?? "";
102
102
  return /^(?:[\da-f]{3}|[\da-f]{6})$/i.test(t) ? `#${t}` : t;
103
103
  }
104
- function v(e) {
104
+ function y(e) {
105
105
  let t = e.trim().toLowerCase();
106
106
  if (t === "transparent") return {
107
107
  r: 0,
@@ -109,14 +109,14 @@ function v(e) {
109
109
  b: 0,
110
110
  a: 0
111
111
  };
112
- let n = b().get(t);
113
- if (n) return x(n);
114
- if (t.startsWith("#")) return x(t.slice(1));
112
+ let n = x().get(t);
113
+ if (n) return S(n);
114
+ if (t.startsWith("#")) return S(t.slice(1));
115
115
  let r = /^([a-z]+)\(([^()]*)\)$/.exec(t);
116
116
  if (!r) return null;
117
117
  let i = r[2].trim().split(/\s*[\s,/]\s*/);
118
118
  if (i.length !== 3 && i.length !== 4) return null;
119
- let [a, o, s, c] = i, l = re(c), u;
119
+ let [a, o, s, c] = i, l = ne(c), u;
120
120
  switch (r[1]) {
121
121
  case "rgb":
122
122
  case "rgba":
@@ -124,31 +124,31 @@ function v(e) {
124
124
  a,
125
125
  o,
126
126
  s
127
- ].map((e) => C(e, 255) / 255);
127
+ ].map((e) => w(e, 255) / 255);
128
128
  break;
129
129
  case "hsl":
130
130
  case "hsla":
131
- u = E(w(a), C(o, 100) / 100, C(s, 100) / 100);
131
+ u = D(T(a), w(o, 100) / 100, w(s, 100) / 100);
132
132
  break;
133
133
  case "hwb":
134
- u = D(w(a), C(o, 100) / 100, C(s, 100) / 100);
134
+ u = re(T(a), w(o, 100) / 100, w(s, 100) / 100);
135
135
  break;
136
136
  case "lab":
137
- u = M(C(a, 100), C(o, 125), C(s, 125));
137
+ u = A(w(a, 100), w(o, 125), w(s, 125));
138
138
  break;
139
139
  case "lch":
140
- u = M(C(a, 100), ...T(C(o, 150), w(s)));
140
+ u = A(w(a, 100), ...E(w(o, 150), T(s)));
141
141
  break;
142
142
  case "oklab":
143
- u = N(C(a, 1), C(o, .4), C(s, .4));
143
+ u = j(w(a, 1), w(o, .4), w(s, .4));
144
144
  break;
145
145
  case "oklch":
146
- u = N(C(a, 1), ...T(C(o, .4), w(s)));
146
+ u = j(w(a, 1), ...E(w(o, .4), T(s)));
147
147
  break;
148
148
  default: return null;
149
149
  }
150
150
  if (Number.isNaN(l) || u.some(Number.isNaN)) return null;
151
- let [d, f, p] = u.map((e) => L(e, 0, 1) * 255);
151
+ let [d, f, p] = u.map((e) => F(e, 0, 1) * 255);
152
152
  return {
153
153
  r: d,
154
154
  g: f,
@@ -156,11 +156,11 @@ function v(e) {
156
156
  a: l
157
157
  };
158
158
  }
159
- var y;
160
- function b() {
161
- return y ??= new Map("aliceblue:f0f8ff antiquewhite:faebd7 aqua:00ffff aquamarine:7fffd4 azure:f0ffff beige:f5f5dc bisque:ffe4c4 black:000000 blanchedalmond:ffebcd blue:0000ff blueviolet:8a2be2 brown:a52a2a burlywood:deb887 cadetblue:5f9ea0 chartreuse:7fff00 chocolate:d2691e coral:ff7f50 cornflowerblue:6495ed cornsilk:fff8dc crimson:dc143c cyan:00ffff darkblue:00008b darkcyan:008b8b darkgoldenrod:b8860b darkgray:a9a9a9 darkgreen:006400 darkgrey:a9a9a9 darkkhaki:bdb76b darkmagenta:8b008b darkolivegreen:556b2f darkorange:ff8c00 darkorchid:9932cc darkred:8b0000 darksalmon:e9967a darkseagreen:8fbc8f darkslateblue:483d8b darkslategray:2f4f4f darkslategrey:2f4f4f darkturquoise:00ced1 darkviolet:9400d3 deeppink:ff1493 deepskyblue:00bfff dimgray:696969 dimgrey:696969 dodgerblue:1e90ff firebrick:b22222 floralwhite:fffaf0 forestgreen:228b22 fuchsia:ff00ff gainsboro:dcdcdc ghostwhite:f8f8ff gold:ffd700 goldenrod:daa520 gray:808080 green:008000 greenyellow:adff2f grey:808080 honeydew:f0fff0 hotpink:ff69b4 indianred:cd5c5c indigo:4b0082 ivory:fffff0 khaki:f0e68c lavender:e6e6fa lavenderblush:fff0f5 lawngreen:7cfc00 lemonchiffon:fffacd lightblue:add8e6 lightcoral:f08080 lightcyan:e0ffff lightgoldenrodyellow:fafad2 lightgray:d3d3d3 lightgreen:90ee90 lightgrey:d3d3d3 lightpink:ffb6c1 lightsalmon:ffa07a lightseagreen:20b2aa lightskyblue:87cefa lightslategray:778899 lightslategrey:778899 lightsteelblue:b0c4de lightyellow:ffffe0 lime:00ff00 limegreen:32cd32 linen:faf0e6 magenta:ff00ff maroon:800000 mediumaquamarine:66cdaa mediumblue:0000cd mediumorchid:ba55d3 mediumpurple:9370db mediumseagreen:3cb371 mediumslateblue:7b68ee mediumspringgreen:00fa9a mediumturquoise:48d1cc mediumvioletred:c71585 midnightblue:191970 mintcream:f5fffa mistyrose:ffe4e1 moccasin:ffe4b5 navajowhite:ffdead navy:000080 oldlace:fdf5e6 olive:808000 olivedrab:6b8e23 orange:ffa500 orangered:ff4500 orchid:da70d6 palegoldenrod:eee8aa palegreen:98fb98 paleturquoise:afeeee palevioletred:db7093 papayawhip:ffefd5 peachpuff:ffdab9 peru:cd853f pink:ffc0cb plum:dda0dd powderblue:b0e0e6 purple:800080 rebeccapurple:663399 red:ff0000 rosybrown:bc8f8f royalblue:4169e1 saddlebrown:8b4513 salmon:fa8072 sandybrown:f4a460 seagreen:2e8b57 seashell:fff5ee sienna:a0522d silver:c0c0c0 skyblue:87ceeb slateblue:6a5acd slategray:708090 slategrey:708090 snow:fffafa springgreen:00ff7f steelblue:4682b4 tan:d2b48c teal:008080 thistle:d8bfd8 tomato:ff6347 turquoise:40e0d0 violet:ee82ee wheat:f5deb3 white:ffffff whitesmoke:f5f5f5 yellow:ffff00 yellowgreen:9acd32".split(" ").map((e) => e.split(":"))), y;
159
+ var b;
160
+ function x() {
161
+ return b ??= new Map("aliceblue:f0f8ff antiquewhite:faebd7 aqua:00ffff aquamarine:7fffd4 azure:f0ffff beige:f5f5dc bisque:ffe4c4 black:000000 blanchedalmond:ffebcd blue:0000ff blueviolet:8a2be2 brown:a52a2a burlywood:deb887 cadetblue:5f9ea0 chartreuse:7fff00 chocolate:d2691e coral:ff7f50 cornflowerblue:6495ed cornsilk:fff8dc crimson:dc143c cyan:00ffff darkblue:00008b darkcyan:008b8b darkgoldenrod:b8860b darkgray:a9a9a9 darkgreen:006400 darkgrey:a9a9a9 darkkhaki:bdb76b darkmagenta:8b008b darkolivegreen:556b2f darkorange:ff8c00 darkorchid:9932cc darkred:8b0000 darksalmon:e9967a darkseagreen:8fbc8f darkslateblue:483d8b darkslategray:2f4f4f darkslategrey:2f4f4f darkturquoise:00ced1 darkviolet:9400d3 deeppink:ff1493 deepskyblue:00bfff dimgray:696969 dimgrey:696969 dodgerblue:1e90ff firebrick:b22222 floralwhite:fffaf0 forestgreen:228b22 fuchsia:ff00ff gainsboro:dcdcdc ghostwhite:f8f8ff gold:ffd700 goldenrod:daa520 gray:808080 green:008000 greenyellow:adff2f grey:808080 honeydew:f0fff0 hotpink:ff69b4 indianred:cd5c5c indigo:4b0082 ivory:fffff0 khaki:f0e68c lavender:e6e6fa lavenderblush:fff0f5 lawngreen:7cfc00 lemonchiffon:fffacd lightblue:add8e6 lightcoral:f08080 lightcyan:e0ffff lightgoldenrodyellow:fafad2 lightgray:d3d3d3 lightgreen:90ee90 lightgrey:d3d3d3 lightpink:ffb6c1 lightsalmon:ffa07a lightseagreen:20b2aa lightskyblue:87cefa lightslategray:778899 lightslategrey:778899 lightsteelblue:b0c4de lightyellow:ffffe0 lime:00ff00 limegreen:32cd32 linen:faf0e6 magenta:ff00ff maroon:800000 mediumaquamarine:66cdaa mediumblue:0000cd mediumorchid:ba55d3 mediumpurple:9370db mediumseagreen:3cb371 mediumslateblue:7b68ee mediumspringgreen:00fa9a mediumturquoise:48d1cc mediumvioletred:c71585 midnightblue:191970 mintcream:f5fffa mistyrose:ffe4e1 moccasin:ffe4b5 navajowhite:ffdead navy:000080 oldlace:fdf5e6 olive:808000 olivedrab:6b8e23 orange:ffa500 orangered:ff4500 orchid:da70d6 palegoldenrod:eee8aa palegreen:98fb98 paleturquoise:afeeee palevioletred:db7093 papayawhip:ffefd5 peachpuff:ffdab9 peru:cd853f pink:ffc0cb plum:dda0dd powderblue:b0e0e6 purple:800080 rebeccapurple:663399 red:ff0000 rosybrown:bc8f8f royalblue:4169e1 saddlebrown:8b4513 salmon:fa8072 sandybrown:f4a460 seagreen:2e8b57 seashell:fff5ee sienna:a0522d silver:c0c0c0 skyblue:87ceeb slateblue:6a5acd slategray:708090 slategrey:708090 snow:fffafa springgreen:00ff7f steelblue:4682b4 tan:d2b48c teal:008080 thistle:d8bfd8 tomato:ff6347 turquoise:40e0d0 violet:ee82ee wheat:f5deb3 white:ffffff whitesmoke:f5f5f5 yellow:ffff00 yellowgreen:9acd32".split(" ").map((e) => e.split(":"))), b;
162
162
  }
163
- function x(e) {
163
+ function S(e) {
164
164
  if (!/^(?:[\da-f]{3,4}|[\da-f]{6}|[\da-f]{8})$/.test(e)) return null;
165
165
  let [t, n, r, i = 255] = (e.length <= 4 ? [...e].map((e) => e + e) : e.match(/../g) ?? []).map((e) => Number.parseInt(e, 16));
166
166
  return {
@@ -170,28 +170,28 @@ function x(e) {
170
170
  a: i / 255
171
171
  };
172
172
  }
173
- var S = /^([+-]?(?:\d+\.?\d*|\.\d+)(?:e[+-]?\d+)?)(%|deg|rad|grad|turn)?$/;
174
- function C(e, t) {
173
+ var C = /^([+-]?(?:\d+\.?\d*|\.\d+)(?:e[+-]?\d+)?)(%|deg|rad|grad|turn)?$/;
174
+ function w(e, t) {
175
175
  if (e === "none") return 0;
176
- let n = S.exec(e);
176
+ let n = C.exec(e);
177
177
  return !n || n[2] && n[2] !== "%" ? NaN : n[2] ? Number(n[1]) / 100 * t : Number(n[1]);
178
178
  }
179
- function w(e) {
179
+ function T(e) {
180
180
  if (e === "none") return 0;
181
- let t = S.exec(e);
181
+ let t = C.exec(e);
182
182
  if (!t || t[2] === "%") return NaN;
183
183
  let n = Number(t[1]);
184
184
  return ((t[2] === "rad" ? n * 180 / Math.PI : t[2] === "grad" ? n * .9 : t[2] === "turn" ? n * 360 : n) % 360 + 360) % 360;
185
185
  }
186
- function re(e) {
187
- return e === void 0 ? 1 : L(C(e, 1), 0, 1);
186
+ function ne(e) {
187
+ return e === void 0 ? 1 : F(w(e, 1), 0, 1);
188
188
  }
189
- function T(e, t) {
189
+ function E(e, t) {
190
190
  let n = Math.max(0, e), r = t * Math.PI / 180;
191
191
  return [n * Math.cos(r), n * Math.sin(r)];
192
192
  }
193
- function E(e, t, n) {
194
- let r = L(t, 0, 1), i = L(n, 0, 1), a = (t) => {
193
+ function D(e, t, n) {
194
+ let r = F(t, 0, 1), i = F(n, 0, 1), a = (t) => {
195
195
  let n = (t + e / 30) % 12, a = r * Math.min(i, 1 - i);
196
196
  return i - a * Math.max(-1, Math.min(n - 3, 9 - n, 1));
197
197
  };
@@ -201,19 +201,19 @@ function E(e, t, n) {
201
201
  a(4)
202
202
  ];
203
203
  }
204
- function D(e, t, n) {
205
- let r = L(t, 0, 1), i = L(n, 0, 1);
204
+ function re(e, t, n) {
205
+ let r = F(t, 0, 1), i = F(n, 0, 1);
206
206
  return r + i >= 1 ? [
207
207
  ,
208
208
  ,
209
209
  ,
210
- ].fill(r / (r + i)) : E(e, 1, .5).map((e) => e * (1 - r - i) + r);
210
+ ].fill(r / (r + i)) : D(e, 1, .5).map((e) => e * (1 - r - i) + r);
211
211
  }
212
212
  var O = 24389 / 27, k = 216 / 24389, ie = [
213
213
  .3457 / .3585,
214
214
  1,
215
215
  .2958 / .3585
216
- ], A = [
216
+ ], ae = [
217
217
  [
218
218
  .955473421488075,
219
219
  -.02309845494876471,
@@ -229,7 +229,7 @@ var O = 24389 / 27, k = 216 / 24389, ie = [
229
229
  -.020507649298898964,
230
230
  1.330365926242124
231
231
  ]
232
- ], j = [
232
+ ], oe = [
233
233
  [
234
234
  12831 / 3959,
235
235
  -329 / 214,
@@ -246,17 +246,17 @@ var O = 24389 / 27, k = 216 / 24389, ie = [
246
246
  705 / 667
247
247
  ]
248
248
  ];
249
- function M(e, t, n) {
250
- let r = L(e, 0, 100), i = (r + 16) / 116, a = t / 500 + i, o = i - n / 200;
251
- return P(j, P(A, [
249
+ function A(e, t, n) {
250
+ let r = F(e, 0, 100), i = (r + 16) / 116, a = t / 500 + i, o = i - n / 200;
251
+ return M(oe, M(ae, [
252
252
  a ** 3 > k ? a ** 3 : (116 * a - 16) / O,
253
253
  r > O * k ? i ** 3 : r / O,
254
254
  o ** 3 > k ? o ** 3 : (116 * o - 16) / O
255
- ].map((e, t) => e * ie[t]))).map(F);
255
+ ].map((e, t) => e * ie[t]))).map(N);
256
256
  }
257
- function N(e, t, n) {
258
- let r = L(e, 0, 1);
259
- return P([
257
+ function j(e, t, n) {
258
+ let r = F(e, 0, 1);
259
+ return M([
260
260
  [
261
261
  4.0767416621,
262
262
  -3.3077115913,
@@ -276,38 +276,144 @@ function N(e, t, n) {
276
276
  r + .3963377774 * t + .2158037573 * n,
277
277
  r - .1055613458 * t - .0638541728 * n,
278
278
  r - .0894841775 * t - 1.291485548 * n
279
- ].map((e) => e ** 3)).map(F);
279
+ ].map((e) => e ** 3)).map(N);
280
280
  }
281
- function P(e, t) {
281
+ function M(e, t) {
282
282
  return e.map((e) => e[0] * t[0] + e[1] * t[1] + e[2] * t[2]);
283
283
  }
284
- function F(e) {
285
- let t = L(e, 0, 1);
284
+ function N(e) {
285
+ let t = F(e, 0, 1);
286
286
  return t <= .0031308 ? 12.92 * t : 1.055 * t ** (1 / 2.4) - .055;
287
287
  }
288
- function I(e) {
288
+ function P(e) {
289
289
  return e <= .04045 ? e / 12.92 : ((e + .055) / 1.055) ** 2.4;
290
290
  }
291
- function L(e, t, n) {
291
+ function F(e, t, n) {
292
292
  return Math.min(n, Math.max(t, e));
293
293
  }
294
294
  //#endregion
295
295
  //#region src/prompt-audio.ts
296
- var ae = (e) => !!e?.autoplayAudioEnabled && !e?.autoplayAudioHeld, R = ["autoplay"], z = "data-pie-audio-failed", B = (e) => !!e.getAttribute("src") || !!e.querySelector("source[src]:not([src=\"\"])");
297
- function V(e, t, n) {
298
- if (n || t?.audioEndTime) return !1;
299
- let r = e?.querySelector("audio");
300
- return r?.closest(".preview-prompt") ? B(r) && r.error === null && !r.hasAttribute(z) : !1;
296
+ var I = (e) => !!e?.autoplayAudioEnabled && !e?.autoplayAudioHeld, L = ["autoplay"];
297
+ function R(e, t) {
298
+ let n = e.audioStartTime || t.audioStartTime, r = e.audioEndTime || t.audioEndTime, i = {
299
+ ...e,
300
+ ...n ? { audioStartTime: n } : {},
301
+ ...r ? { audioEndTime: r } : {}
302
+ };
303
+ return !i.waitTime && n && r && (i.waitTime = r - n), i;
304
+ }
305
+ var se = class {
306
+ #e;
307
+ #t;
308
+ #n = !1;
309
+ #r = !1;
310
+ constructor(e) {
311
+ this.#e = e;
312
+ }
313
+ get hasEnded() {
314
+ return this.#n;
315
+ }
316
+ get hasFailed() {
317
+ return this.#r;
318
+ }
319
+ get pending() {
320
+ return !this.#n && !this.#r && !this.#e.session()?.audioEndTime;
321
+ }
322
+ follow(e) {
323
+ e !== this.#t && (this.#t = e, this.#n = !1, this.#r = !1);
324
+ }
325
+ recordStart(e = Date.now()) {
326
+ this.#i({ audioStartTime: e });
327
+ }
328
+ recordEnd(e = Date.now()) {
329
+ this.#i({ audioEndTime: e }), !this.#n && (this.#n = !0, this.#e.announce());
330
+ }
331
+ recordFailure() {
332
+ this.#r || (this.#r = !0, this.#e.announce());
333
+ }
334
+ markPlayed(e = !0) {
335
+ this.#n = e;
336
+ }
337
+ #i(e) {
338
+ let t = this.#e.session() ?? {}, n = R(t, e);
339
+ (n.audioStartTime !== t.audioStartTime || n.audioEndTime !== t.audioEndTime || n.waitTime !== t.waitTime) && this.#e.writeSession(n);
340
+ }
341
+ }, z = (e) => e.getAttribute("src") || e.querySelector("source[src]:not([src=\"\"])")?.getAttribute("src") || "";
342
+ function B(e, t) {
343
+ t.follow(z(e));
344
+ let n = () => t.recordStart(), r = () => t.recordEnd(), i = () => t.recordFailure();
345
+ return e.addEventListener("playing", n), e.addEventListener("ended", r), e.addEventListener("error", i, !0), () => {
346
+ e.removeEventListener("playing", n), e.removeEventListener("ended", r), e.removeEventListener("error", i, !0);
347
+ };
301
348
  }
349
+ var V = (e) => {
350
+ let t = e?.querySelector("audio");
351
+ return t?.closest(".preview-prompt") ? t : null;
352
+ };
302
353
  function H(e, t) {
303
- let n = () => {
304
- e.setAttribute(z, ""), t();
305
- };
306
- return e.addEventListener("error", n, !0), () => e.removeEventListener("error", n, !0);
354
+ if (!t.pending) return !1;
355
+ let n = V(e);
356
+ return !!n && z(n) !== "" && n.error === null;
357
+ }
358
+ var U = "play-audio-info", W = 500, G = class {
359
+ #e;
360
+ #t = null;
361
+ #n = null;
362
+ #r = null;
363
+ constructor(e) {
364
+ this.#e = e;
365
+ }
366
+ connect() {
367
+ this.#t || (this.#t = new MutationObserver(() => this.#i()), this.#t.observe(this.#e.root, {
368
+ childList: !0,
369
+ subtree: !0
370
+ }), this.#i());
371
+ }
372
+ disconnect() {
373
+ this.#t?.disconnect(), this.#t = null, this.#o();
374
+ }
375
+ #i() {
376
+ let e = this.#e.settings();
377
+ if (!I(e) && !e?.completeAudioEnabled) return;
378
+ let t = V(this.#e.root);
379
+ t && t !== this.#n && (this.#o(), this.#a(t, I(e)));
380
+ }
381
+ #a(e, t) {
382
+ let { root: n, tracker: r, enableAudioImage: i } = this.#e, a = B(e, r), o = ce(i), s = () => {
383
+ o.isConnected && (e.play()?.catch(() => {}), o.remove()), document.removeEventListener("click", s);
384
+ }, c = () => o.remove();
385
+ e.addEventListener("playing", c);
386
+ let l = t ? setTimeout(() => {
387
+ e.paused && !n.querySelector(`.${U}`) && ((n.querySelector(".main-container") ?? n).appendChild(o), document.addEventListener("click", s));
388
+ }, W) : void 0;
389
+ this.#n = e, this.#r = () => {
390
+ clearTimeout(l), a(), e.removeEventListener("playing", c), document.removeEventListener("click", s), o.remove();
391
+ };
392
+ }
393
+ #o() {
394
+ this.#r?.(), this.#r = null, this.#n = null;
395
+ }
396
+ };
397
+ function ce(e) {
398
+ let t = document.createElement("div");
399
+ t.id = `${U}-${Math.random().toString(36).slice(2, 10)}`, t.className = U, Object.assign(t.style, {
400
+ position: "absolute",
401
+ top: "0",
402
+ width: "100%",
403
+ height: "100%",
404
+ display: "flex",
405
+ justifyContent: "center",
406
+ alignItems: "center",
407
+ background: "white",
408
+ zIndex: "1000",
409
+ cursor: "pointer"
410
+ });
411
+ let n = document.createElement("img");
412
+ return n.src = e, n.alt = "Click anywhere to enable audio autoplay", n.width = 500, n.height = 300, t.appendChild(n), t;
307
413
  }
308
414
  //#endregion
309
415
  //#region src/sanitize-model-html.ts
310
- var U = [
416
+ var K = [
311
417
  "script",
312
418
  "embed",
313
419
  "base",
@@ -316,7 +422,7 @@ var U = [
316
422
  "link",
317
423
  "style",
318
424
  "foreignobject"
319
- ], W = [
425
+ ], q = [
320
426
  "onerror",
321
427
  "onload",
322
428
  "onclick",
@@ -334,7 +440,7 @@ var U = [
334
440
  "onbeforeunload",
335
441
  "formaction",
336
442
  "xlink:href"
337
- ], G = /* @__PURE__ */ new Set([
443
+ ], le = /* @__PURE__ */ new Set([
338
444
  "iframe",
339
445
  "object",
340
446
  "mstack",
@@ -347,7 +453,7 @@ var U = [
347
453
  "semantics",
348
454
  "annotation",
349
455
  "none"
350
- ]), K = /^[a-z_][\w.-]*:[a-z][\w.-]*$/, q = [
456
+ ]), ue = /^[a-z_][\w.-]*:[a-z][\w.-]*$/, de = [
351
457
  "alignment",
352
458
  "target",
353
459
  "allow",
@@ -367,18 +473,18 @@ var U = [
367
473
  "rightoverhang",
368
474
  "mslinethickness",
369
475
  "linebreak"
370
- ], J = /^\s*document\.getElementById\((['"])([^'"]+)\1\)\.play\(\)\s*;?\s*(?:return\s+false\s*;?\s*)?$/, Y = "data-pie-play-audio", oe = {
371
- ADD_TAGS: (e) => G.has(e) || K.test(e),
372
- ADD_ATTR: q,
373
- FORBID_TAGS: U,
374
- FORBID_ATTR: [...W, ...R],
476
+ ], fe = /^\s*document\.getElementById\((['"])([^'"]+)\1\)\.play\(\)\s*;?\s*(?:return\s+false\s*;?\s*)?$/, J = "data-pie-play-audio", Y = {
477
+ ADD_TAGS: (e) => le.has(e) || ue.test(e),
478
+ ADD_ATTR: de,
479
+ FORBID_TAGS: K,
480
+ FORBID_ATTR: [...q, ...L],
375
481
  ALLOW_UNKNOWN_PROTOCOLS: !1,
376
482
  RETURN_TRUSTED_TYPE: !1
377
- }, se = !1;
378
- function ce(e) {
483
+ }, pe = !1;
484
+ function me(e) {
379
485
  for (let t of e.composedPath()) {
380
486
  if (!(t instanceof Element)) continue;
381
- let n = t.getAttribute(Y);
487
+ let n = t.getAttribute(J);
382
488
  if (n === null) continue;
383
489
  e.preventDefault();
384
490
  let r = t.getRootNode().getElementById?.(n) ?? document.getElementById(n);
@@ -387,19 +493,19 @@ function ce(e) {
387
493
  }
388
494
  }
389
495
  var X = null;
390
- function le() {
496
+ function he() {
391
497
  return X || (X = e(window), X.addHook("beforeSanitizeAttributes", (e) => {
392
498
  if (e.nodeName !== "A") return;
393
- let t = J.exec(e.getAttribute("onclick") ?? "")?.[2];
394
- t !== void 0 && (e.removeAttribute("onclick"), e.setAttribute(Y, t), se ||= (document.addEventListener("click", ce), !0));
395
- }), X.addHook("afterSanitizeAttributes", g), X);
499
+ let t = fe.exec(e.getAttribute("onclick") ?? "")?.[2];
500
+ t !== void 0 && (e.removeAttribute("onclick"), e.setAttribute(J, t), pe ||= (document.addEventListener("click", me), !0));
501
+ }), X.addHook("afterSanitizeAttributes", v), X);
396
502
  }
397
503
  var Z = 1e6, Q = /* @__PURE__ */ new Map(), $ = 0;
398
- function ue(t) {
504
+ function ge(t) {
399
505
  if (t == null || t === "" || !e.isSupported) return "";
400
506
  let n = typeof t == "string" ? t : String(t), r = Q.get(n);
401
507
  if (r !== void 0) return r;
402
- let i = String(le().sanitize(n, oe)), a = n.length + i.length;
508
+ let i = String(he().sanitize(n, Y)), a = n.length + i.length;
403
509
  if (a > Z) return i;
404
510
  for (let [e, t] of Q) {
405
511
  if ($ + a <= Z) break;
@@ -408,4 +514,4 @@ function ue(t) {
408
514
  return Q.set(n, i), $ += a, i;
409
515
  }
410
516
  //#endregion
411
- export { m as AUTHORED_BORDER_ATTR, p as AUTHORED_FILL_ATTR, f as AUTHORED_INK_ATTR, R as AUTOPLAY_AUDIO_MARKUP_ATTRS, W as MODEL_HTML_FORBIDDEN_ATTRS, U as MODEL_HTML_FORBIDDEN_TAGS, Y as PLAY_AUDIO_ATTR, u as assignProps, ae as autoplayAudioOn, a as clamp, s as debounce, t as debug, n as isEmpty, g as markAuthoredColors, V as promptAudioPending, ue as sanitizeModelHtml, d as sessionsEqual, r as showFeedback, i as showRationale, c as shuffle, H as trackPromptAudioFailure, o as uuid };
517
+ export { m as AUTHORED_BORDER_ATTR, p as AUTHORED_FILL_ATTR, f as AUTHORED_INK_ATTR, L as AUTOPLAY_AUDIO_MARKUP_ATTRS, q as MODEL_HTML_FORBIDDEN_ATTRS, K as MODEL_HTML_FORBIDDEN_TAGS, J as PLAY_AUDIO_ATTR, se as PromptAudioTracker, G as PromptAudioWatcher, u as assignProps, I as autoplayAudioOn, B as bindPromptAudio, a as clamp, s as debounce, t as debug, n as isEmpty, v as markAuthoredColors, H as promptAudioPending, ge as sanitizeModelHtml, d as sessionsEqual, r as showFeedback, i as showRationale, c as shuffle, o as uuid, R as withPromptAudioTiming };
@@ -1,12 +1,19 @@
1
1
  /**
2
- * Autoplay and completion rules for a prompt's audio, shared by the elements with an authored
3
- * `autoplayAudioEnabled` setting.
2
+ * A prompt's audio, shared by the elements with authored `autoplayAudioEnabled` and
3
+ * `completeAudioEnabled` settings, in three layers: `PromptAudioTracker` holds the playback state
4
+ * and writes its timing into the session without touching the DOM, so an element of any framework
5
+ * feeds it; `bindPromptAudio` feeds it from an `<audio>`; `PromptAudioWatcher` finds the audio in
6
+ * a rendered prompt and offers to enable autoplay a browser blocked.
4
7
  */
5
8
  export interface AutoplayAudioFields {
6
9
  autoplayAudioEnabled?: boolean;
7
10
  /** Written by pie-players' item player when the toolkit holds autoplay for the item. */
8
11
  autoplayAudioHeld?: boolean;
9
12
  }
13
+ export interface PromptAudioSettings extends AutoplayAudioFields {
14
+ /** Keeps the item incomplete until the prompt audio has played to the end. */
15
+ completeAudioEnabled?: boolean;
16
+ }
10
17
  /** Whether the prompt audio starts on its own. A hold outranks the authored setting. */
11
18
  export declare const autoplayAudioOn: (fields?: AutoplayAudioFields | null) => boolean;
12
19
  /**
@@ -15,17 +22,86 @@ export declare const autoplayAudioOn: (fields?: AutoplayAudioFields | null) => b
15
22
  * pie-players' item player removes the same from item markup through its autoplay applier.
16
23
  */
17
24
  export declare const AUTOPLAY_AUDIO_MARKUP_ATTRS: readonly string[];
25
+ /** The session fields a prompt's first playback is recorded in, all epoch or elapsed milliseconds. */
26
+ export interface PromptAudioTiming {
27
+ audioStartTime?: number;
28
+ audioEndTime?: number;
29
+ /** `audioEndTime - audioStartTime`: how long the learner waited on the audio. */
30
+ waitTime?: number;
31
+ }
18
32
  /**
19
- * Whether `completeAudioEnabled` still waits on the prompt audio in `root`. It waits on the
20
- * first `<audio>` when that sits in a prompt, has a source, has not failed to load, and neither
21
- * this mount nor the session (`audioEndTime`, kept across a resume) records it as played.
33
+ * `session` with a playback's timing recorded. The first playback counts, so a replay moves
34
+ * neither timestamp, and `waitTime` is set once both exist.
22
35
  */
23
- export declare function promptAudioPending(root: ParentNode | null | undefined, session: {
24
- audioEndTime?: number;
25
- } | null | undefined, audioComplete: boolean): boolean;
36
+ export declare function withPromptAudioTiming<S extends PromptAudioTiming>(session: S, timing: Pick<PromptAudioTiming, 'audioStartTime' | 'audioEndTime'>): S;
37
+ /** What a `PromptAudioTracker` needs from the element whose prompt audio it tracks. */
38
+ export interface PromptAudioHost<S extends PromptAudioTiming = PromptAudioTiming> {
39
+ /** The session playback timing is recorded in. */
40
+ session(): S | null | undefined;
41
+ /** Stores `next`, the session with playback timing recorded. */
42
+ writeSession(next: S): void;
43
+ /** Dispatches the element's session change: the audio ended or failed, so completion moved. */
44
+ announce(): void;
45
+ }
46
+ /**
47
+ * The playback state of a prompt's audio, which `completeAudioEnabled` gates completion on. The
48
+ * element records what its audio reports; the tracker writes the timing into the session and
49
+ * announces the playback ending or failing as a session change, which hosts gate completion on.
50
+ */
51
+ export declare class PromptAudioTracker<S extends PromptAudioTiming = PromptAudioTiming> {
52
+ #private;
53
+ constructor(host: PromptAudioHost<S>);
54
+ /** The audio played to the end on this mount. */
55
+ get hasEnded(): boolean;
56
+ /** The audio could not load, so completion no longer waits on it. */
57
+ get hasFailed(): boolean;
58
+ /**
59
+ * Whether completion still waits on the audio: it has not failed to load, and neither this mount
60
+ * nor the session (`audioEndTime`, kept across a resume) records it as played.
61
+ */
62
+ get pending(): boolean;
63
+ /**
64
+ * Follows the audio at `source`. A different source starts the playback state over; the same one
65
+ * keeps it, so a re-render or a re-set of the same model keeps a finished playback counted.
66
+ */
67
+ follow(source: string | undefined): void;
68
+ recordStart(now?: number): void;
69
+ recordEnd(now?: number): void;
70
+ recordFailure(): void;
71
+ /**
72
+ * Counts the audio as played, or not, without recording timing or announcing: a host's
73
+ * override of the wait, which hosts set through an element's `audioComplete` before the
74
+ * learner responds.
75
+ */
76
+ markPlayed(played?: boolean): void;
77
+ }
78
+ /**
79
+ * Feeds `audio`'s playback into `tracker`, which follows its source. A `<source>` reports its load
80
+ * failure on itself, so the error listener captures. Returns the listeners' remover.
81
+ */
82
+ export declare function bindPromptAudio(audio: HTMLAudioElement, tracker: PromptAudioTracker<any>): () => void;
26
83
  /**
27
- * Marks the audio failed when it cannot load, so `promptAudioPending` stops waiting on it, and
28
- * calls `onFailure`. A `<source>` reports its failure on itself, so the listener captures.
29
- * Returns the listener's remover.
84
+ * Whether `completeAudioEnabled` still waits on the prompt audio rendered in `root`: the audio has
85
+ * a source, has not failed to load, and `tracker` does not record it as played.
30
86
  */
31
- export declare function trackPromptAudioFailure(audio: HTMLAudioElement, onFailure: () => void): () => void;
87
+ export declare function promptAudioPending(root: ParentNode | null | undefined, tracker: PromptAudioTracker<any>): boolean;
88
+ export interface PromptAudioWatcherOptions {
89
+ /** The element whose rendered prompt holds the audio. */
90
+ root: HTMLElement;
91
+ tracker: PromptAudioTracker<any>;
92
+ /** The current model's audio settings; nothing is tracked while neither setting is on. */
93
+ settings(): PromptAudioSettings | null | undefined;
94
+ /** The image the toast offering to enable blocked autoplay shows. */
95
+ enableAudioImage: string;
96
+ }
97
+ /**
98
+ * Binds the prompt audio an element renders to its tracker, rebinding when the element renders
99
+ * another `<audio>`. When autoplay is on and the browser blocked it, a toast over the element's
100
+ * `.main-container` offers to start the audio on the next click anywhere in the document.
101
+ */
102
+ export declare class PromptAudioWatcher {
103
+ #private;
104
+ constructor(options: PromptAudioWatcherOptions);
105
+ connect(): void;
106
+ disconnect(): void;
107
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pie-element/shared-utils",
3
- "version": "0.1.6-next.20261019151920",
3
+ "version": "0.1.6",
4
4
  "description": "Shared utilities for PIE elements",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -21,7 +21,7 @@
21
21
  "clean": "rm -rf dist"
22
22
  },
23
23
  "dependencies": {
24
- "@pie-element/shared-types": "0.2.3-next.20261019151920",
24
+ "@pie-element/shared-types": "0.2.3",
25
25
  "dompurify": "^3.4.17"
26
26
  },
27
27
  "publishConfig": {