videoPreview.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*---------------------------------------------------------------------------------------------
  2. * Copyright (c) Microsoft Corporation. All rights reserved.
  3. * Licensed under the MIT License. See License.txt in the project root for license information.
  4. *--------------------------------------------------------------------------------------------*/
  5. // @ts-check
  6. "use strict";
  7. (function () {
  8. // @ts-ignore
  9. const vscode = acquireVsCodeApi();
  10. function getSettings() {
  11. const element = document.getElementById('settings');
  12. if (element) {
  13. const data = element.getAttribute('data-settings');
  14. if (data) {
  15. return JSON.parse(data);
  16. }
  17. }
  18. throw new Error(`Could not load settings`);
  19. }
  20. const settings = getSettings();
  21. // State
  22. let hasLoadedMedia = false;
  23. // Elements
  24. const video = document.createElement('video');
  25. if (settings.src !== null) {
  26. video.src = settings.src;
  27. }
  28. video.playsInline = true;
  29. video.controls = true;
  30. video.autoplay = settings.autoplay;
  31. video.muted = settings.autoplay;
  32. video.loop = settings.loop;
  33. function onLoaded() {
  34. if (hasLoadedMedia) {
  35. return;
  36. }
  37. hasLoadedMedia = true;
  38. document.body.classList.remove('loading');
  39. document.body.classList.add('ready');
  40. document.body.append(video);
  41. }
  42. video.addEventListener('error', e => {
  43. if (hasLoadedMedia) {
  44. return;
  45. }
  46. hasLoadedMedia = true;
  47. document.body.classList.add('error');
  48. document.body.classList.remove('loading');
  49. });
  50. if (settings.src === null) {
  51. onLoaded();
  52. } else {
  53. video.addEventListener('canplaythrough', () => {
  54. onLoaded();
  55. });
  56. }
  57. document.querySelector('.open-file-link')?.addEventListener('click', (e) => {
  58. e.preventDefault();
  59. vscode.postMessage({
  60. type: 'reopen-as-text',
  61. });
  62. });
  63. }());