Skip to content

Commit 259f7a6

Browse files
authored
Support opening video timestamps in a new window (#4687)
* Support opening video timestamps in a new window * Scroll to top when clicking on a timestamp
1 parent f04317b commit 259f7a6

File tree

1 file changed

+21
-10
lines changed

1 file changed

+21
-10
lines changed

src/renderer/components/ft-timestamp-catcher/ft-timestamp-catcher.js

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,29 @@ export default defineComponent({
99
}
1010
},
1111
methods: {
12-
catchTimestampClick: function(event) {
13-
const match = event.detail.match(/(\d+):(\d+):?(\d+)?/)
14-
if (match[3] !== undefined) { // HH:MM:SS
15-
const seconds = 3600 * Number(match[1]) + 60 * Number(match[2]) + Number(match[3])
16-
this.$emit('timestamp-event', seconds)
17-
} else { // MM:SS
18-
const seconds = 60 * Number(match[1]) + Number(match[2])
19-
this.$emit('timestamp-event', seconds)
20-
}
12+
catchTimestampClick: function (event) {
13+
this.$emit('timestamp-event', event.detail)
2114
},
2215
detectTimestamps: function (input) {
23-
return input.replaceAll(/(\d+(:\d+)+)/g, '<a href="#" onclick="this.dispatchEvent(new CustomEvent(\'timestamp-clicked\',{bubbles:true, detail:\'$1\'}))">$1</a>')
16+
const videoId = this.$route.params.id
17+
18+
return input.replaceAll(/(?:(\d+):)?(\d+):(\d+)/g, (timestamp, hours, minutes, seconds) => {
19+
let time = 60 * Number(minutes) + Number(seconds)
20+
21+
if (hours) {
22+
time += 3600 * Number(hours)
23+
}
24+
25+
const url = this.$router.resolve({
26+
path: `/watch/${videoId}`,
27+
query: {
28+
timestamp: time
29+
}
30+
}).href
31+
32+
// Adding the URL lets the user open the video in a new window at this timestamp
33+
return `<a href="${url}" onclick="event.preventDefault();this.dispatchEvent(new CustomEvent('timestamp-clicked',{bubbles:true,detail:${time}}));window.scrollTo(0,0)">${timestamp}</a>`
34+
})
2435
}
2536
}
2637
})

0 commit comments

Comments
 (0)