diff options
author | Chocobozzz <me@florianbigard.com> | 2018-07-16 16:13:35 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2018-07-16 16:13:35 +0200 |
commit | 1f6824c958440ace14f7c7f83c890b848a71f5d9 (patch) | |
tree | f8bd7853f4283bfe72745f5ac491b93a3e618276 /client/src/assets/player/utils.ts | |
parent | 329d9086601b7ae2127be12166ee8c892c289c6e (diff) | |
download | PeerTube-1f6824c958440ace14f7c7f83c890b848a71f5d9.tar.gz PeerTube-1f6824c958440ace14f7c7f83c890b848a71f5d9.tar.zst PeerTube-1f6824c958440ace14f7c7f83c890b848a71f5d9.zip |
Improve start time param
Can handle 2m42s for example
Diffstat (limited to 'client/src/assets/player/utils.ts')
-rw-r--r-- | client/src/assets/player/utils.ts | 44 |
1 files changed, 40 insertions, 4 deletions
diff --git a/client/src/assets/player/utils.ts b/client/src/assets/player/utils.ts index c27e630e5..c02e19929 100644 --- a/client/src/assets/player/utils.ts +++ b/client/src/assets/player/utils.ts | |||
@@ -24,15 +24,50 @@ function isMobile () { | |||
24 | } | 24 | } |
25 | 25 | ||
26 | function buildVideoLink (time?: number) { | 26 | function buildVideoLink (time?: number) { |
27 | let href = window.location.href.replace('/embed/', '/watch/') | 27 | const baseEmbedPath = window.location.pathname.replace('/embed/', '/watch/') |
28 | const baseEmbedURL = window.location.origin + baseEmbedPath | ||
29 | |||
28 | if (time) { | 30 | if (time) { |
29 | const timeInt = Math.floor(time) | 31 | const timeInt = Math.floor(time) |
30 | 32 | ||
31 | if (window.location.search) href += '&start=' + timeInt | 33 | const params = new URLSearchParams(window.location.search) |
32 | else href += '?start=' + timeInt | 34 | params.set('start', secondsToTime(timeInt)) |
35 | |||
36 | return baseEmbedURL + '?' + params.toString() | ||
33 | } | 37 | } |
34 | 38 | ||
35 | return href | 39 | return baseEmbedURL |
40 | } | ||
41 | |||
42 | function timeToInt (time: number | string) { | ||
43 | if (typeof time === 'number') return time | ||
44 | |||
45 | const reg = /^((\d+)h)?((\d+)m)?((\d+)s?)?$/ | ||
46 | const matches = time.match(reg) | ||
47 | |||
48 | if (!matches) return 0 | ||
49 | |||
50 | const hours = parseInt(matches[2] || '0', 10) | ||
51 | const minutes = parseInt(matches[4] || '0', 10) | ||
52 | const seconds = parseInt(matches[6] || '0', 10) | ||
53 | |||
54 | return hours * 3600 + minutes * 60 + seconds | ||
55 | } | ||
56 | |||
57 | function secondsToTime (seconds: number) { | ||
58 | let time = '' | ||
59 | |||
60 | let hours = Math.floor(seconds / 3600) | ||
61 | if (hours >= 1) time = hours + 'h' | ||
62 | |||
63 | seconds %= 3600 | ||
64 | let minutes = Math.floor(seconds / 60) | ||
65 | if (minutes >= 1) time += minutes + 'm' | ||
66 | |||
67 | seconds %= 60 | ||
68 | if (seconds >= 1) time += seconds + 's' | ||
69 | |||
70 | return time | ||
36 | } | 71 | } |
37 | 72 | ||
38 | function buildVideoEmbed (embedUrl: string) { | 73 | function buildVideoEmbed (embedUrl: string) { |
@@ -81,6 +116,7 @@ function videoFileMinByResolution (files: VideoFile[]) { | |||
81 | 116 | ||
82 | export { | 117 | export { |
83 | toTitleCase, | 118 | toTitleCase, |
119 | timeToInt, | ||
84 | buildVideoLink, | 120 | buildVideoLink, |
85 | buildVideoEmbed, | 121 | buildVideoEmbed, |
86 | videoFileMaxByResolution, | 122 | videoFileMaxByResolution, |