]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - client/src/assets/player/peertube-player.ts
Fix playback rate hotkey
[github/Chocobozzz/PeerTube.git] / client / src / assets / player / peertube-player.ts
index bf02ce91c695f31fc08285afda98af717fc86b5e..2c16c87eca0a37452b21291918341dcc5747c40f 100644 (file)
@@ -2,7 +2,6 @@ import { VideoFile } from '../../../../shared/models/videos'
 
 import 'videojs-hotkeys'
 import 'videojs-dock'
-import 'videojs-contextmenu'
 import 'videojs-contextmenu-ui'
 import './peertube-link-button'
 import './resolution-menu-button'
@@ -32,9 +31,10 @@ function getVideojsOptions (options: {
   inactivityTimeout: number,
   peertubeLink: boolean,
   poster: string,
-  startTime: number
+  startTime: number | string
   theaterMode: boolean,
   videoCaptions: VideoJSCaption[],
+  language?: string,
   controls?: boolean,
   muted?: boolean,
   loop?: boolean
@@ -48,7 +48,7 @@ function getVideojsOptions (options: {
     poster: options.poster,
     autoplay: false,
     inactivityTimeout: options.inactivityTimeout,
-    playbackRates: [ 0.5, 1, 1.5, 2 ],
+    playbackRates: [ 0.5, 0.75, 1, 1.25, 1.5, 2 ],
     plugins: {
       peertube: {
         autoplay: options.autoplay, // Use peertube plugin autoplay because we get the file by webtorrent
@@ -69,11 +69,33 @@ function getVideojsOptions (options: {
     Object.assign(videojsOptions.plugins, {
       hotkeys: {
         enableVolumeScroll: false,
-        enableModifiersForNumbers: false
+        enableModifiersForNumbers: false,
+        customKeys: {
+          increasePlaybackRateKey: {
+            key: function (event) {
+              return event.key === '>'
+            },
+            handler: function (player) {
+              player.playbackRate((player.playbackRate() + 0.1).toFixed(2))
+            }
+          },
+          decreasePlaybackRateKey: {
+            key: function (event) {
+              return event.key === '<'
+            },
+            handler: function (player) {
+              player.playbackRate((player.playbackRate() - 0.1).toFixed(2))
+            }
+          }
+        }
       }
     })
   }
 
+  if (options.language && !isDefaultLocale(options.language)) {
+    Object.assign(videojsOptions, { language: options.language })
+  }
+
   return videojsOptions
 }
 
@@ -174,18 +196,55 @@ function addContextMenu (player: any, videoEmbedUrl: string) {
   })
 }
 
-function loadLocale (serverUrl: string, videojs: any, locale: string) {
+function loadLocaleInVideoJS (serverUrl: string, videojs: any, locale: string) {
+  const path = getLocalePath(serverUrl, locale)
+  // It is the default locale, nothing to translate
+  if (!path) return Promise.resolve(undefined)
+
+  let p: Promise<any>
+
+  if (loadLocaleInVideoJS.cache[path]) {
+    p = Promise.resolve(loadLocaleInVideoJS.cache[path])
+  } else {
+    p = fetch(path + '/player.json')
+      .then(res => res.json())
+      .then(json => {
+        loadLocaleInVideoJS.cache[path] = json
+        return json
+      })
+  }
+
   const completeLocale = getCompleteLocale(locale)
+  return p.then(json => videojs.addLanguage(getShortLocale(completeLocale), json))
+}
+namespace loadLocaleInVideoJS {
+  export const cache: { [ path: string ]: any } = {}
+}
 
-  if (!is18nLocale(completeLocale) || isDefaultLocale(completeLocale)) return Promise.resolve(undefined)
+function getServerTranslations (serverUrl: string, locale: string) {
+  const path = getLocalePath(serverUrl, locale)
+  // It is the default locale, nothing to translate
+  if (!path) return Promise.resolve(undefined)
 
-  return fetch(serverUrl + '/client/locales/' + completeLocale + '/player.json')
+  return fetch(path + '/server.json')
     .then(res => res.json())
-    .then(json => videojs.addLanguage(getShortLocale(completeLocale), json))
 }
 
+// ############################################################################
+
 export {
-  loadLocale,
+  getServerTranslations,
+  loadLocaleInVideoJS,
   getVideojsOptions,
   addContextMenu
 }
+
+// ############################################################################
+
+function getLocalePath (serverUrl: string, locale: string) {
+  const completeLocale = getCompleteLocale(locale)
+
+  if (!is18nLocale(completeLocale) || isDefaultLocale(completeLocale)) return undefined
+
+  return serverUrl + '/client/locales/' + completeLocale
+}