feat: add youtube video embeds
This commit is contained in:
1 parent
00840227d2
commit
eeca5a826a
7 files changed
+133
-8
No files matched your search
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "photon-lemmy",
|
"name": "photon-lemmy",
|
||||||
"version": "1.22.2",
|
"version": "1.23.0",
|
||||||
"private": true,
|
"private": true,
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "vite dev",
|
"dev": "vite dev",
|
||||||
|
|||||||
@@ -9,7 +9,11 @@
|
|||||||
import { Material, toast } from 'mono-svelte'
|
import { Material, toast } from 'mono-svelte'
|
||||||
import Markdown from '$lib/components/markdown/Markdown.svelte'
|
import Markdown from '$lib/components/markdown/Markdown.svelte'
|
||||||
import ExpandableImage from '$lib/components/ui/ExpandableImage.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 Empty from '$lib/components/helper/Empty.svelte'
|
||||||
import { publishedToDate } from '$lib/components/util/date.js'
|
import { publishedToDate } from '$lib/components/util/date.js'
|
||||||
import { Icon, VideoCamera } from 'svelte-hero-icons'
|
import { Icon, VideoCamera } from 'svelte-hero-icons'
|
||||||
@@ -70,7 +74,11 @@
|
|||||||
</a>
|
</a>
|
||||||
{/if}
|
{/if}
|
||||||
</div>
|
</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'}
|
{#if post.post.body && !post.post.nsfw && view != 'compact'}
|
||||||
<div
|
<div
|
||||||
class="text-sm relative overflow-hidden
|
class="text-sm relative overflow-hidden
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
import { getInstance } from '$lib/lemmy.js'
|
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'
|
import type { CommentView, PersonView, Post, PostView } from 'lemmy-js-client'
|
||||||
|
|
||||||
export const isCommentMutable = (comment: CommentView, me: PersonView) =>
|
export const isCommentMutable = (comment: CommentView, me: PersonView) =>
|
||||||
@@ -25,4 +27,26 @@ export const bestImageURL = (
|
|||||||
return post.url ?? ''
|
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 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">
|
<script lang="ts">
|
||||||
import Empty from '$lib/components/helper/Empty.svelte'
|
import Empty from '$lib/components/helper/Empty.svelte'
|
||||||
import PostLink from '$lib/components/lemmy/post/PostLink.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 ExpandableImage from '$lib/components/ui/ExpandableImage.svelte'
|
||||||
import { userSettings } from '$lib/settings.js'
|
import { userSettings } from '$lib/settings.js'
|
||||||
import { isImage, isVideo } from '$lib/ui/image'
|
import { isImage, isVideo } from '$lib/ui/image'
|
||||||
import type { Post } from 'lemmy-js-client'
|
import type { Post } from 'lemmy-js-client'
|
||||||
import { Icon, VideoCamera } from 'svelte-hero-icons'
|
import { Icon, VideoCamera } from 'svelte-hero-icons'
|
||||||
|
import PostIframe from './PostIframe.svelte'
|
||||||
|
|
||||||
export let view: 'card' | 'cozy' | 'list' | 'compact' = 'cozy'
|
export let view: 'card' | 'cozy' | 'list' | 'compact' = 'cozy'
|
||||||
export let post: Post
|
export let post: Post
|
||||||
|
export let type: MediaType = 'none'
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
{#if isImage(post.url)}
|
{#if type == 'image'}
|
||||||
{#if view == 'card' || view == 'cozy'}
|
{#if view == 'card' || view == 'cozy'}
|
||||||
<!--disabled preloads here since most people will hover over every image while scrolling-->
|
<!--disabled preloads here since most people will hover over every image while scrolling-->
|
||||||
<svelte:component
|
<svelte:component
|
||||||
@@ -71,7 +77,7 @@
|
|||||||
</svelte:element>
|
</svelte:element>
|
||||||
</svelte:component>
|
</svelte:component>
|
||||||
{/if}
|
{/if}
|
||||||
{:else if isVideo(post.url) && (view == 'cozy' || view == 'card')}
|
{:else if type == 'video' && (view == 'cozy' || view == 'card')}
|
||||||
<a
|
<a
|
||||||
href={postLink(post)}
|
href={postLink(post)}
|
||||||
style="height: 300px;"
|
style="height: 300px;"
|
||||||
@@ -85,7 +91,9 @@
|
|||||||
<span class="font-bold text-2xl">Video</span>
|
<span class="font-bold text-2xl">Video</span>
|
||||||
<p class="text-base">Go to the post to view this video.</p>
|
<p class="text-base">Go to the post to view this video.</p>
|
||||||
</a>
|
</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
|
<PostLink
|
||||||
url={post.url}
|
url={post.url}
|
||||||
thumbnail_url={post.thumbnail_url}
|
thumbnail_url={post.thumbnail_url}
|
||||||
|
|||||||
+8
-1
@@ -75,6 +75,10 @@ interface Settings {
|
|||||||
|
|
||||||
openLinksInNewTab: boolean
|
openLinksInNewTab: boolean
|
||||||
crosspostOriginalLink: boolean
|
crosspostOriginalLink: boolean
|
||||||
|
|
||||||
|
embeds: {
|
||||||
|
youtube: 'youtube' | 'invidious' | 'piped'
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const defaultSettings: Settings = {
|
export const defaultSettings: Settings = {
|
||||||
@@ -124,7 +128,10 @@ export const defaultSettings: Settings = {
|
|||||||
markPostsAsRead: true,
|
markPostsAsRead: true,
|
||||||
separateVotes: false,
|
separateVotes: false,
|
||||||
openLinksInNewTab: false,
|
openLinksInNewTab: false,
|
||||||
crosspostOriginalLink: true
|
crosspostOriginalLink: true,
|
||||||
|
embeds: {
|
||||||
|
youtube: 'youtube',
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const userSettings = writable(defaultSettings)
|
export const userSettings = writable(defaultSettings)
|
||||||
|
|||||||
@@ -155,6 +155,20 @@
|
|||||||
{/if}
|
{/if}
|
||||||
</Section>
|
</Section>
|
||||||
|
|
||||||
|
<Section title="Embeds">
|
||||||
|
<Setting>
|
||||||
|
<span slot="title">YouTube</span>
|
||||||
|
<span slot="description">
|
||||||
|
The website to use to embed YouTube content.
|
||||||
|
</span>
|
||||||
|
<Select bind:value={$userSettings.embeds.youtube}>
|
||||||
|
<option value="youtube">YouTube</option>
|
||||||
|
<option value="invidious">Invidious</option>
|
||||||
|
<option value="piped">Piped</option>
|
||||||
|
</Select>
|
||||||
|
</Setting>
|
||||||
|
</Section>
|
||||||
|
|
||||||
<Section title="Lemmy">
|
<Section title="Lemmy">
|
||||||
<ToggleSetting
|
<ToggleSetting
|
||||||
bind:checked={$userSettings.markPostsAsRead}
|
bind:checked={$userSettings.markPostsAsRead}
|
||||||
|
|||||||
Reference in new issue
Block a user