feat: add youtube video embeds

This commit is contained in:
Xylight committed 2023-11-27 18:16:12 -08:00
1 parent 00840227d2
commit eeca5a826a
7 files changed
+133 -8

No files matched your search

+10 -2
View File
@@ -9,7 +9,11 @@
import { Material, toast } from 'mono-svelte'
import Markdown from '$lib/components/markdown/Markdown.svelte'
import ExpandableImage from '$lib/components/ui/ExpandableImage.svelte'
import { bestImageURL, postLink } from '$lib/components/lemmy/post/helpers.js'
import {
bestImageURL,
mediaType,
postLink,
} from '$lib/components/lemmy/post/helpers.js'
import Empty from '$lib/components/helper/Empty.svelte'
import { publishedToDate } from '$lib/components/util/date.js'
import { Icon, VideoCamera } from 'svelte-hero-icons'
@@ -70,7 +74,11 @@
</a>
{/if}
</div>
<PostMedia bind:post={post.post} {view} />
<PostMedia
bind:post={post.post}
{view}
type={mediaType(post.post, view)}
/>
{#if post.post.body && !post.post.nsfw && view != 'compact'}
<div
class="text-sm relative overflow-hidden
+24
View File
@@ -1,4 +1,6 @@
import { getInstance } from '$lib/lemmy.js'
import type { View } from '$lib/settings'
import { isImage, isVideo } from '$lib/ui/image'
import type { CommentView, PersonView, Post, PostView } from 'lemmy-js-client'
export const isCommentMutable = (comment: CommentView, me: PersonView) =>
@@ -25,4 +27,26 @@ export const bestImageURL = (
return post.url ?? ''
}
const YOUTUBE_REGEX = /^(?:https?:\/\/)?(?:www\.)?(?:youtube\.com|youtu\.be)\/(?:watch\?v=)?(\w|-){11}$/
export const isYoutubeLink = (url?: string): boolean => {
if (!url) return false
return YOUTUBE_REGEX.test(url)
}
export const postLink = (post: Post) => `/post/${getInstance()}/${post.id}`
export type MediaType = 'video' | 'image' | 'iframe' | 'embed' | 'none'
export type IframeType = 'youtube'
export const mediaType = (post: Post, view: View = 'cozy'): MediaType => {
if (post.url) {
if (isImage(post.url)) return 'image'
if (isVideo(post.url)) return 'video'
if (isYoutubeLink(post.url)) return 'iframe'
return 'embed'
}
return 'none'
}
@@ -0,0 +1,64 @@
<script lang="ts" context="module">
const youtubeDomain = (place: 'youtube' | 'invidious' | 'piped') => {
switch (place) {
case 'youtube': {
return 'www.youtube-nocookie.com'
}
case 'invidious': {
return 'vid.puffyan.us'
}
case 'piped': {
return 'piped.video'
}
}
}
</script>
<script lang="ts">
import { Icon, PuzzlePiece } from 'svelte-hero-icons'
import type { IframeType, MediaType } from '../helpers'
import { userSettings } from '$lib/settings'
export let type: IframeType = 'none'
export let url: string
let opened = false
const urlToEmbed = (inputUrl: string) => {
if (type == 'youtube') {
const url = new URL(inputUrl)
const videoID = url.searchParams.get('v')
url.searchParams.delete('v')
return `https://${youtubeDomain(
$userSettings.embeds.youtube
)}/embed/${videoID}?${url.searchParams.toString()}`
}
return ''
}
$: embedUrl = urlToEmbed(url)
</script>
{#if opened}
<iframe
src={embedUrl}
title="Embed player"
frameborder="0"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share"
allowfullscreen
class="aspect-video rounded-xl dark:bg-zinc-900"
/>
{:else}
<button
on:click={() => (opened = true)}
class="aspect-video w-full h-full z-0 overflow-hidden relative rounded-xl flex flex-col gap-2 items-center justify-center"
>
<div
class="absolute blur-3xl -z-10 top-0 left-0 w-full h-full bg-gradient-to-br from-green-800 via-blue-900 via-20% to-red-700"
/>
<Icon src={PuzzlePiece} solid size="48" />
<h1 class="font-bold text-xl">Embed</h1>
<p>Click to view this content.</p>
</button>
{/if}
@@ -1,18 +1,24 @@
<script lang="ts">
import Empty from '$lib/components/helper/Empty.svelte'
import PostLink from '$lib/components/lemmy/post/PostLink.svelte'
import { bestImageURL, postLink } from '$lib/components/lemmy/post/helpers.js'
import {
bestImageURL,
postLink,
type MediaType,
} from '$lib/components/lemmy/post/helpers.js'
import ExpandableImage from '$lib/components/ui/ExpandableImage.svelte'
import { userSettings } from '$lib/settings.js'
import { isImage, isVideo } from '$lib/ui/image'
import type { Post } from 'lemmy-js-client'
import { Icon, VideoCamera } from 'svelte-hero-icons'
import PostIframe from './PostIframe.svelte'
export let view: 'card' | 'cozy' | 'list' | 'compact' = 'cozy'
export let post: Post
export let type: MediaType = 'none'
</script>
{#if isImage(post.url)}
{#if type == 'image'}
{#if view == 'card' || view == 'cozy'}
<!--disabled preloads here since most people will hover over every image while scrolling-->
<svelte:component
@@ -71,7 +77,7 @@
</svelte:element>
</svelte:component>
{/if}
{:else if isVideo(post.url) && (view == 'cozy' || view == 'card')}
{:else if type == 'video' && (view == 'cozy' || view == 'card')}
<a
href={postLink(post)}
style="height: 300px;"
@@ -85,7 +91,9 @@
<span class="font-bold text-2xl">Video</span>
<p class="text-base">Go to the post to view this video.</p>
</a>
{:else if post.url}
{:else if type == 'iframe' && (view == 'cozy' || view == 'card') && post.url}
<PostIframe type="youtube" url={post.url} />
{:else if type == 'embed' && post.url}
<PostLink
url={post.url}
thumbnail_url={post.thumbnail_url}
+8 -1
View File
@@ -75,6 +75,10 @@ interface Settings {
openLinksInNewTab: boolean
crosspostOriginalLink: boolean
embeds: {
youtube: 'youtube' | 'invidious' | 'piped'
}
}
export const defaultSettings: Settings = {
@@ -124,7 +128,10 @@ export const defaultSettings: Settings = {
markPostsAsRead: true,
separateVotes: false,
openLinksInNewTab: false,
crosspostOriginalLink: true
crosspostOriginalLink: true,
embeds: {
youtube: 'youtube',
}
}
export const userSettings = writable(defaultSettings)