UNPKG

@oplayer/plugins

Version:
116 lines (115 loc) 4.1 kB
/** * name: @oplayer/vttThumbnails * version: v1.0.15-beta.3 * description: oplayer's plugin * author: shiyiya * homepage: https://github.com/shiyiya/oplayer */ import { $ as C } from "@oplayer/core"; function O(c, n) { const { $progress: P } = c.context.ui, y = P.firstElementChild; let h = [], x = {}, p, v = !1, a = n == null ? void 0 : n.src; const l = C.render(C.create(`div.${c.context.ui.vttThumbnailsCls}`), y); l.style.width = ((n == null ? void 0 : n.width) || 160) + "px", l.style.height = ((n == null ? void 0 : n.height) || 90) + "px"; const S = (s) => { fetch(s).then((t) => t.text()).then((t) => { h = I(t), v = !0, l.style.opacity = "1"; }).catch((t) => { c.emit("notice", { text: "Failed to load vtt thumbnails", reason: t }); }); }; n != null && n.src && S(n.src), c.on("videosourcechange", () => { c.context.ui.progressHoverCallback.splice( c.context.ui.progressHoverCallback.findIndex((s) => s == T), 1 ), v = !1, l.style.opacity = "0", h = [], x = {}; }); const I = (s) => { const t = []; return s.split(/[\r\n][\r\n]/i).forEach((r) => { if (r.match( /([0-9]{2}:)?([0-9]{2}:)?[0-9]{2}(.[0-9]{3})?( ?--> ?)([0-9]{2}:)?([0-9]{2}:)?[0-9]{2}(.[0-9]{3})?[\r\n]{1}.*/gi )) { const i = r.split(/[\r\n]/i), u = isFinite(+i[0]) ? i[1] : i[0], f = isFinite(+i[0]) ? i[2] : i[1], o = u.split(/ ?--> ?/i), d = o[0], g = o[1], m = k(f); t.push({ start: w(d), end: w(g), css: m }); } return t; }), t; }, w = (s) => { const t = F(s); return t.hours * (60 * 60) + t.minutes * 60 + t.seconds + t.milliseconds / 1e3; }, F = (s) => { const t = s.split("."), r = t[0].split(":"); return { milliseconds: parseInt(t[1], 10) || 0, seconds: parseInt(r.pop(), 10) || 0, minutes: parseInt(r.pop(), 10) || 0, hours: parseInt(r.pop(), 10) || 0 }; }, k = (s) => { const t = {}; if (!s.match(/#xywh=/i)) return t.background = 'url("' + b(s) + '")', t; const e = D(s); return t.background = 'url("' + b(e.image) + '") no-repeat -' + e.x + "px -" + e.y + "px", t.width = e.w + "px", t.height = e.h + "px", t.url = b(e.image), t; }, b = /* @__PURE__ */ (() => { const s = {}; return (t) => { if (!a) return t; if (s[t]) return s[t]; if (/(https?:)?\/\//.test(t)) return t; let e = a; /(https?:)?\/\//.test(t) && (e = a.substring(0, a.indexOf("?") || void 0)); const r = e.lastIndexOf("/"); if (e.startsWith("https://") && r < 8 || e.startsWith("http://") && r < 7) return a + t; const i = e.substring(r + 1); return s[t] = ((n == null ? void 0 : n.prefix) || "") + a.replace(i, t), s[t]; }; })(), D = (s) => { const t = s.split(/#xywh=/i), e = t[0], i = t[1].match(/[0-9]+/gi); return { x: i[0], y: i[1], w: i[2], h: i[3], image: e }; }, T = (s) => { if (!v || !s) return; const t = c.duration, e = s * t, r = W(e); if (!r) { l.style.opacity = "0"; return; } const i = y.clientWidth, u = s * i, f = parseInt(r.width, 10), o = f >> 1, d = i - (u + o), g = u - o; if (l.style.opacity = "1", g > 0 && d > 0 ? l.style.transform = "translateX(" + (u - o) + "px)" : g <= 0 ? l.style.transform = "translateX(0px)" : d <= 0 && (l.style.transform = "translateX(" + (i - f) + "px)"), !(p && p === r)) { p = r; for (const m in r) r.hasOwnProperty(m) && (l.style[m] = r[m]); } }, W = (s) => { for (let t = 0; t < h.length; ++t) { const e = h[t]; if (s >= e.start && s < e.end) { if (e.css.url && !x[e.css.url]) { const r = new Image(); r.src = e.css.url, x[e.css.url] = r; } return e.css; } } }; c.context.ui.progressHoverCallback.push(T), c.context.ui.changThumbnails = ({ src: s }) => S(s); } const E = (c) => ({ name: "oplayer-vtt-thumbnails", version: "1.0.15-beta.3", apply: (n) => O(n, c) }); export { E as default };