uikit/dist/watermark.js
Mohamadzadeh 38a5935d29
All checks were successful
Build and Deploy Next.js + Nginx Docker Image / build-and-deploy (push) Successful in 52s
Build and Deploy Next.js + Nginx Docker Image / deploy (push) Successful in 7s
uikit-v1.7.0-watermark-v31-full
2026-11-04 18:52:10 +03:30

320 lines
11 KiB
JavaScript

"use client";
import U, { useState as X, useRef as L, useEffect as N, useCallback as Y } from "react";
import { d as rt } from "./chunks/feature-registry-B2iuEiEx.js";
import { u as nt } from "./chunks/provider-runtime-CxEpkUq5.js";
const ot = [87, 77], V = 24, ct = 20, O = 10, at = V * O, st = ct * O, B = 160, it = 3, C = {
cool: { r: 0, g: 195, b: 255 },
warm: { r: 250, g: 130, b: 0 }
};
function Z(r) {
if (typeof r != "string")
throw new TypeError("User id must be a UUID string");
const t = r.toLowerCase();
if (!/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/.test(t))
throw new TypeError("User id must be a canonical 36-character UUID");
return t;
}
function ut(r) {
let t = 65535;
for (const s of r) {
t ^= s << 8;
for (let e = 0; e < 8; e++)
t = t & 32768 ? (t << 1 ^ 4129) & 65535 : t << 1 & 65535;
}
return t;
}
function J(r) {
const s = Z(r).replaceAll("-", ""), e = new Uint8Array(20);
e.set(ot, 0);
for (let o = 0; o < 16; o++)
e[o + 2] = Number.parseInt(s.slice(o * 2, o * 2 + 2), 16);
const c = ut(e.subarray(0, 18));
e[18] = c >> 8, e[19] = c & 255;
const n = [];
for (const o of e)
for (let a = 7; a >= 0; a--) n.push(o >> a & 1);
return n;
}
function lt(r) {
let t = Math.imul(r + 1, 2654435761) + 2135587861 >>> 0;
return t ^= t >>> 16, t = Math.imul(t, 2246822507) >>> 0, t ^= t >>> 13, t >>> 0;
}
function ft(r) {
const t = lt(r), s = {
x: 1 + (t & 1),
y: 2 + (t >> 1 & 1),
w: 3,
h: 3
}, e = {
x: 5 + (t >> 2 & 1),
y: 2 + (t >> 3 & 1),
w: 3,
h: 3
};
return { left: s, right: e };
}
const R = 20, Q = 17, m = 12, M = R * m, W = Q * m, $ = R * Q, dt = 97, ht = [1, 0, 1, 1, 0, 1, 0, 0], q = (r) => {
let t = r;
return t ^= t >>> 4, t ^= t >>> 2, t ^= t >>> 1, t & 1;
};
function tt(r) {
const t = [...J(r), ...Array(6).fill(0)], s = [];
let e = 0;
for (const c of t) {
const n = e << 1 | c;
s.push(q(n & 121), q(n & 91)), e = n & 63;
}
return [...s, ...ht];
}
const D = (r) => r * dt % $;
function wt(r) {
const t = r % R, s = Math.floor(r / R), e = Math.imul(r + 1, 2654435761) >>> 28 & 3, c = s * m + 2 + e % 2, n = e >> 1 & 1;
return {
left: { x: t * m + 1 + n, y: c, w: 4, h: 6 },
right: { x: t * m + 7 + n, y: c, w: 4, h: 6 }
};
}
const et = 0.014;
function mt(r, { opacity: t = et } = {}) {
if (!(t > 0 && t <= 0.08))
throw new RangeError("opacity must be between 0 and 0.08");
const s = tt(r), e = document.createElement("canvas");
e.width = M, e.height = W;
const c = e.getContext("2d", { alpha: !0 });
if (!c) throw new Error("2D canvas is unavailable");
const n = `rgba(255,255,255,${t})`, o = `rgba(0,0,0,${t})`;
for (let a = 0; a < $; a++) {
const { left: f, right: u } = wt(D(a));
c.fillStyle = s[a] ? n : o, c.fillRect(f.x, f.y, f.w, f.h), c.fillStyle = s[a] ? o : n, c.fillRect(u.x, u.y, u.w, u.h);
}
return e;
}
const P = 0.014, gt = Object.freeze([
3,
8,
11,
12,
11,
8,
-3,
-8,
-11,
-12,
-11,
-8
]), K = Object.freeze([5, 7, 8, 9, 10, 10, 10, 10, 9, 8, 7, 5]);
function pt(r) {
const t = D(r), e = Math.imul(t + 13, 2654435761) >>> 29 & 3, c = (t * 7 + (t >> 2) * 5) % 12, n = new Int16Array(m * m);
for (let o = 0; o < m; o++)
for (let a = 0; a < m; a++) {
const u = (((e === 0 ? a : e === 1 ? o : e === 2 ? a + o : a - o) + c) % 12 + 12) % 12;
n[o * m + a] = gt[u] * K[a] * K[o];
}
return n;
}
function Et(r, t = P) {
return Math.abs(r) >= 480 ? Math.round(255 * t) : 0;
}
function bt(r, { opacity: t = P } = {}) {
if (!(t > 0 && t <= 0.08))
throw new RangeError("opacity must be between 0 and 0.08");
const s = tt(r), e = document.createElement("canvas");
e.width = M, e.height = W;
const c = e.getContext("2d", { alpha: !0 });
if (!c) throw new Error("2D canvas is unavailable");
const n = c.createImageData(M, W);
for (let o = 0; o < $; o++) {
const a = D(o), f = a % R * m, u = Math.floor(a / R) * m, E = pt(o), i = s[o] ? 1 : -1;
for (let h = 0; h < m; h++)
for (let d = 0; d < m; d++) {
const l = E[h * m + d] * i, b = Et(l, t), g = ((u + h) * M + f + d) * 4, x = l > 0 ? 255 : 0;
n.data[g] = x, n.data[g + 1] = x, n.data[g + 2] = x, n.data[g + 3] = b;
}
}
return c.putImageData(n, 0, 0), e;
}
function xt(r, { opacity: t, version: s = 4 } = {}) {
if (s === 4)
return bt(r, { opacity: t ?? P });
if (s === 3)
return mt(r, { opacity: t ?? et });
if (s !== 2) throw new RangeError("Unsupported watermark version");
if (t ?? (t = 0.014), !(t > 0 && t <= 0.08))
throw new RangeError("opacity must be between 0 and 0.08");
const e = J(r), c = document.createElement("canvas");
c.width = at, c.height = st;
const n = c.getContext("2d", { alpha: !0 });
if (!n) throw new Error("2D canvas is unavailable");
const o = `rgba(${C.cool.r},${C.cool.g},${C.cool.b},${t})`, a = `rgba(${C.warm.r},${C.warm.g},${C.warm.b},${t})`;
for (let f = 0; f < B * it; f++) {
const u = e[f % B], E = f % V * O, i = Math.floor(f / V) * O, { left: h, right: d } = ft(f);
n.fillStyle = u ? a : o, n.fillRect(E + h.x, i + h.y, h.w, h.h), n.fillStyle = u ? o : a, n.fillRect(E + d.x, i + d.y, d.w, d.h);
}
return c;
}
function yt(r, t, s = 1) {
const e = Math.max(1, window.innerWidth), c = Math.max(1, window.innerHeight), n = Math.max(1, s);
r.width = Math.round(e * n), r.height = Math.round(c * n), r.style.width = `${e}px`, r.style.height = `${c}px`;
const o = r.getContext("2d", { alpha: !0 });
if (!o) return !1;
o.setTransform(n, 0, 0, n, 0, 0), o.imageSmoothingEnabled = !1;
const a = o.createPattern(t, "repeat");
return a ? (o.clearRect(0, 0, e, c), o.fillStyle = a, o.fillRect(0, 0, e, c), !0) : !1;
}
const vt = "https://api.d.aiengines.ir/user_api/v1/users/my_user";
function kt({
getAccessToken: r,
enabled: t = !0,
userUrl: s = vt,
opacity: e,
version: c = 4,
sessionKey: n,
onError: o,
onReady: a,
onStatusChange: f
}) {
const [u, E] = X({ sessionKey: n, userId: null }), i = L({ onError: o, onReady: a, onStatusChange: f, getAccessToken: r });
i.current = { onError: o, onReady: a, onStatusChange: f, getAccessToken: r }, N(() => {
var b, g, x, T;
let d = !1;
const l = new AbortController();
return E({ sessionKey: n, userId: null }), (g = (b = i.current).onStatusChange) == null || g.call(b, "loading"), t ? ((async () => {
var I, v, k, p;
try {
if (typeof i.current.getAccessToken != "function")
throw new Error("Watermark requires getAccessToken or an authenticated Keycloak token");
const w = await i.current.getAccessToken();
if (d) return;
if (typeof w != "string" || !w.trim())
throw new Error("Watermark access token is unavailable");
const y = await fetch(s, {
method: "GET",
headers: { Authorization: `Bearer ${w}`, Accept: "application/json" },
cache: "no-store",
credentials: "omit",
signal: l.signal
});
if (!y.ok) throw new Error(`Watermark identity HTTP ${y.status}`);
const S = await y.json(), A = Z(S == null ? void 0 : S.id);
d || E({ sessionKey: n, userId: A });
} catch (w) {
if (d || (w == null ? void 0 : w.name) === "AbortError") return;
E({ sessionKey: n, userId: null }), (v = (I = i.current).onStatusChange) == null || v.call(I, "error"), (p = (k = i.current).onError) == null || p.call(k, w);
}
})(), () => {
d = !0, l.abort();
}) : ((T = (x = i.current).onStatusChange) == null || T.call(x, "disabled"), () => l.abort());
}, [t, s, n]);
const h = u.sessionKey === n ? u.userId : null;
return N(() => {
var g, x, T, I, v, k, p, w, y, S, A, H, z, F, j, G;
if (!t || !h || typeof document > "u") return;
let d;
try {
d = xt(h, { opacity: e, version: c });
} catch (_) {
(x = (g = i.current).onStatusChange) == null || x.call(g, "error"), (I = (T = i.current).onError) == null || I.call(T, _);
return;
}
if (document.querySelector("canvas[data-forensic-watermark]")) {
const _ = new Error("A forensic watermark canvas is already mounted");
(k = (v = i.current).onStatusChange) == null || k.call(v, "error"), (w = (p = i.current).onError) == null || w.call(p, _);
return;
}
const l = document.createElement("canvas");
l.setAttribute("aria-hidden", "true"), l.setAttribute("data-forensic-watermark", ""), Object.assign(l.style, {
position: "fixed",
inset: "0",
zIndex: "2147483647",
pointerEvents: "none",
userSelect: "none",
width: "100vw",
height: "100vh",
display: "block"
});
const b = () => yt(l, d, window.devicePixelRatio || 1);
document.body.appendChild(l);
try {
if (b() !== !0) throw new Error("Could not paint watermark canvas");
(S = (y = i.current).onStatusChange) == null || S.call(y, "ready"), (H = (A = i.current).onReady) == null || H.call(A);
} catch (_) {
l.remove(), (F = (z = i.current).onStatusChange) == null || F.call(z, "error"), (G = (j = i.current).onError) == null || G.call(j, _);
return;
}
return window.addEventListener("resize", b, { passive: !0 }), () => {
window.removeEventListener("resize", b), l.remove();
};
}, [t, h, e, c, n]), null;
}
async function Tt(r, t) {
var s;
if (r != null && r.authenticated) {
const e = r.instance;
try {
e != null && e.refreshToken && await ((s = r.updateToken) == null ? void 0 : s.call(r, 5));
} catch {
}
let c = !1;
try {
c = typeof (e == null ? void 0 : e.isTokenExpired) == "function" ? e.isTokenExpired(0) : !1;
} catch {
c = !0;
}
if (!c) {
const n = (e == null ? void 0 : e.token) || r.token;
if (typeof n == "string" && n.trim()) return n;
}
}
return typeof t == "function" && await t() || null;
}
function It({
children: r,
enabled: t = !0,
getAccessToken: s,
getToken: e,
sessionKey: c,
blockUntilReady: n = !1,
onReady: o,
onError: a,
...f
}) {
var v, k;
const u = nt(), E = L(u);
E.current = u;
const i = L(s ?? e);
i.current = s ?? e;
const h = L({ onReady: o, onError: a });
h.current = { onReady: o, onError: a };
const d = Y(
() => Tt(E.current, i.current),
[]
), l = ((k = (v = u == null ? void 0 : u.instance) == null ? void 0 : v.tokenParsed) == null ? void 0 : k.sub) ?? c, [b, g] = X({ sessionKey: l, status: "loading" }), x = b.sessionKey === l ? b.status : "loading", T = Y((p) => {
g({ sessionKey: l, status: p });
}, [l]), I = t && x === "ready";
return U.createElement(
U.Fragment,
null,
U.createElement(kt, {
...f,
enabled: t,
sessionKey: l,
getAccessToken: d,
onStatusChange: T,
onReady: () => {
var p, w;
return (w = (p = h.current).onReady) == null ? void 0 : w.call(p);
},
onError: (p) => {
var w, y;
return (y = (w = h.current).onError) == null ? void 0 : y.call(w, p);
}
}),
!n || !t || I ? r : null
);
}
const _t = rt("Watermark", 65, It);
export {
kt as ForensicWatermark,
_t as Watermark
};