Browse Source

Loop video for animated gif

Thomas Dy 3 years ago
parent
commit
907b1e9d40
1 changed files with 8 additions and 7 deletions
  1. 8 7
      format.ts

+ 8 - 7
format.ts

@@ -65,7 +65,7 @@ function formatPlainText(text: string): SafeString {
 
 class TextFormatter {
   private splices: { text: StringLike, indices: [number, number] }[];
-  private media: { type: 'video' | 'img', url: string, link?: string }[];
+  private media: { type: 'video' | 'img', url: string, loop: boolean, link?: string }[];
   private characters: string[];
 
   constructor(readonly tweet: Tweet, readonly useProxy: boolean) {
@@ -97,7 +97,7 @@ class TextFormatter {
     for (const item of media) {
       if (item.type === 'photo') {
         const url = new URL(item.media_url_https).toString();
-        this.media.push({ type: 'img', url });
+        this.media.push({ type: 'img', url, loop: false });
       } else if (item.video_info !== undefined) {
         let max = -1;
         let maxUrl: string | undefined = undefined;
@@ -110,12 +110,13 @@ class TextFormatter {
             maxUrl = variant.url;
           }
         }
+        const loop = item.type === 'animated_gif';
         if (maxUrl !== undefined) {
           const url = new URL(maxUrl).toString();
-          this.media.push({ type: 'video', url });
+          this.media.push({ type: 'video', url, loop });
         } else {
           const url = new URL(item.media_url_https).toString();
-          this.media.push({ type: 'img', url, link: item.expanded_url });
+          this.media.push({ type: 'img', url, link: item.expanded_url, loop });
         }
       }
     }
@@ -172,7 +173,7 @@ class TextFormatter {
       index = indices[1];
     }
     html.push(formatPlainText(this.getRange(index)));
-    for (const { type, url, link } of this.media) {
+    for (const { type, url, link, loop } of this.media) {
       html.push(tag('br'));
       html.push(tag('br'));
       const src = this.useProxy ? buildProxyUrl(url) : url;
@@ -181,7 +182,7 @@ class TextFormatter {
           tag('img', { loading: 'lazy', src }),
         ]));
       } else if (type === 'video') {
-        html.push(tag('video', { controls: '', src }));
+        html.push(tag('video', { controls: '', src, loop: `${loop}` }));
       }
     }
     return joinChildren(html);
@@ -207,7 +208,7 @@ blockquote {
 div {
   max-width: 600px;
 }
-img {
+img, video {
   max-width: 100%;
 }
 `;