UNPKG

cozy-iiif

Version:

A developer-friendly collection of abstractions and utilities built on top of @iiif/presentation-3 and @iiif/parser

94 lines (93 loc) 2.9 kB
const n = []; for (let t = 0; t < 256; ++t) n.push((t + 256).toString(16).slice(1)); function h(t, e = 0) { return (n[t[e + 0]] + n[t[e + 1]] + n[t[e + 2]] + n[t[e + 3]] + "-" + n[t[e + 4]] + n[t[e + 5]] + "-" + n[t[e + 6]] + n[t[e + 7]] + "-" + n[t[e + 8]] + n[t[e + 9]] + "-" + n[t[e + 10]] + n[t[e + 11]] + n[t[e + 12]] + n[t[e + 13]] + n[t[e + 14]] + n[t[e + 15]]).toLowerCase(); } let p; const m = new Uint8Array(16); function y() { if (!p) { if (typeof crypto > "u" || !crypto.getRandomValues) throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported"); p = crypto.getRandomValues.bind(crypto); } return p(m); } const b = typeof crypto < "u" && crypto.randomUUID && crypto.randomUUID.bind(crypto), c = { randomUUID: b }; function x(t, e, i) { var d; if (c.randomUUID && !t) return c.randomUUID(); t = t || {}; const u = t.random ?? ((d = t.rng) == null ? void 0 : d.call(t)) ?? y(); if (u.length < 16) throw new Error("Random bytes length must be >= 16"); return u[6] = u[6] & 15 | 64, u[8] = u[8] & 63 | 128, h(u); } const a = (t) => t.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"), U = (t, e) => { if (e) { const i = t.annotations; if (i.length > 0) { const u = new RegExp(`${a(t.id)}/${a(e)}/page/p(\\d+)$`), d = i.reduce((s, o) => { const r = o.id.match(u); if (r && r[1]) { const g = parseInt(r[1]); return Math.max(s, g); } else return s; }, 1); return `${t.id}/${e}/annotations/page/p${d}`; } else return `${t.id}/${e}/annotations/page/p1`; } else return `${t.id}/annotations/page/${x()}`; }, l = (t, e, i) => { const u = { id: U(t, i), type: "AnnotationPage", items: e }; return { source: { ...t.source, annotations: [...t.annotations, u] }, id: t.id, width: t.width, height: t.height, images: [...t.images], annotations: [...t.annotations, u], getLabel: t.getLabel, getMetadata: t.getMetadata, getThumbnailURL: t.getThumbnailURL }; }, $ = (t, e, i) => { const u = (o) => { const r = o.target; if (r) return typeof r == "string" ? r.substring(0, r.indexOf("#")) : r.source; }, d = e.reduce((o, r) => { const g = u(r); return g && (o[g] || (o[g] = []), o[g].push(r)), o; }, {}), s = t.canvases.map((o) => { const r = d[o.id] || []; return r.length > 0 ? l(o, r, i) : o; }); return { source: { ...t.source, items: s.map((o) => o.source) }, id: t.id, majorVersion: t.majorVersion, canvases: s, structure: t.structure, getLabel: t.getLabel, getMetadata: t.getMetadata, getTableOfContents: t.getTableOfContents }; }, I = (t, e, i) => t.source.type === "Canvas" ? l(t, e, i) : $(t, e, i); export { I as importAnnotations };