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

@@ -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}