diff options
| author | lens <lens@neural.garden> | 2021-03-23 21:10:11 +0000 |
|---|---|---|
| committer | lens <lens@neural.garden> | 2021-03-23 21:10:11 +0000 |
| commit | cc1d0c52e104245f9f1c0d77eb24a5a33800be38 (patch) | |
| tree | 02d8483dfe47803525b926a43c582dcfbf61c5db /frontend/app/utils/index.js | |
| parent | 81c673f058fda04b96baae7b2302f876479bc0a9 (diff) | |
| parent | 7a3ec205e001e4c071a67ecc5c375612fa72afdc (diff) | |
Merge branch 'master' of asdf.us:swimmer
Diffstat (limited to 'frontend/app/utils/index.js')
| -rw-r--r-- | frontend/app/utils/index.js | 43 |
1 files changed, 42 insertions, 1 deletions
diff --git a/frontend/app/utils/index.js b/frontend/app/utils/index.js index bb5e01d..d67d89a 100644 --- a/frontend/app/utils/index.js +++ b/frontend/app/utils/index.js @@ -8,6 +8,7 @@ export const formatDateTime = dateStr => format(new Date(dateStr), 'd MMM yyyy H export const formatDate = dateStr => format(new Date(dateStr), 'd MMM yyyy') export const formatTime = dateStr => format(new Date(dateStr), 'H:mm') export const formatAge = dateStr => formatDistance(new Date(), new Date(dateStr)) + ' ago.' +export const unslugify = fn => fn.replace(/-/g, ' ').replace(/_/g, ' ').replace('.mp3', '') /* Mobile check */ @@ -49,7 +50,8 @@ export const pad = (n, m) => { } export const courtesyS = (n, s) => n + ' ' + (n === 1 ? s : s + 's') - +export const capitalize = s => s.split(' ').map(capitalizeWord).join(' ') +export const capitalizeWord = s => s.substr(0, 1).toUpperCase() + s.substr(1) export const padSeconds = n => n < 10 ? '0' + n : n export const timestamp = (n = 0, fps = 25) => { @@ -61,6 +63,16 @@ export const timestamp = (n = 0, fps = 25) => { } return (n % 60) + ':' + s } +export const timestampToSeconds = time_str => { + const time_str_parts = (time_str || "").trim().split(":").map(s => parseFloat(s)) + if (time_str_parts.length === 3) { + return (time_str_parts[0] * 60 + time_str_parts[1]) * 60 + time_str_parts[2] + } + if (time_str_parts.length === 2) { + return time_str_parts[0] * 60 + time_str_parts[1] + } + return time_str_parts[0] +} export const percent = n => (n * 100).toFixed(1) + '%' @@ -120,6 +132,35 @@ export const preloadImage = url => ( }) ) +export const preloadVideo = url => ( + new Promise((resolve, reject) => { + const video = document.createElement('video') + let loaded = false + const bind = () => { + video.addEventListener('loadedmetadata', onload) + video.addEventListener('error', onerror) + } + const unbind = () => { + video.removeEventListener('loadedmetadata', onload) + video.removeEventListener('error', onerror) + } + const onload = () => { + if (loaded) return + loaded = true + unbind() + resolve(video) + } + const onerror = (error) => { + if (loaded) return + loaded = true + unbind() + reject(error) + } + bind() + video.src = url + }) +) + export const cropImage = (url, crop) => { return new Promise((resolve, reject) => { let { x, y, w, h } = crop |
