UNPKG

media-transcript

Version:

A web component for an interactive transcript built from WebVTT cues.

63 lines (56 loc) 1.62 kB
export const generateId = (namespace) => `${namespace}-${ // append a 6-digit 24-bit number Math.floor((1 + Math.random()) * 0x1000000).toString(24) }`; export const inCue = (cue, time) => cue.startTime <= time && cue.endTime > time; export const formatTimeDefault = (timeInSeconds, formatStr = 'mm:ss') => { const date = new Date(0, 0); date.setSeconds(timeInSeconds); const strFormat = formatStr.split(':').map((token) => { const t = token.toLowerCase(); let val; if (t.includes('h')) val = date.getHours(); if (t.includes('m')) val = date.getMinutes(); if (t.includes('s')) val = date.getSeconds(); val = val.toString(); if (t.length > 1) { val = val.padStart(2, '0'); } return val; }); return strFormat.join(':'); }; export const timeStrToNumber = (timeStr) => { if (typeof timeStr === 'number') return timeStr; const timeArr = timeStr.split(':'); let h = '0'; let m = '0'; let s = '0'; switch (timeArr.length) { case 3: [h, m, s] = timeArr; break; case 2: [m, s] = timeArr; break; case 1: return Number(timeArr[0].replace(',', '.')); default: throw new Error('time strings must be in the format hh:mm:ss'); } return parseInt(h, 10) * 60 * 60 + parseInt(m, 10) * 60 + Number(s.replace(',', '.')); }; export function hasAttributeToken(attr, ...tokens) { return (this.getAttribute(attr) || '') .split(/\s/g) .some((v) => tokens.some((t) => t === v)); } export const debug = ({ name, message }) => { console.debug(`[${name}]:`, message); // eslint-disable-line no-console }; export const TranscriptConfig = { focusClass: 'focused', namespace: 'mt', };