feat: add embeds

This commit is contained in:
Xyphyn committed 2023-10-05 16:21:30 -07:00
1 parent ec72b62c0b
commit 869afbd0cb
8 files changed
+110 -48

No files matched your search

+16 -9
View File
@@ -1,20 +1,27 @@
<script lang="ts">
export let href: string
export let highlight: boolean = false
export let nowrap: boolean = false
const parseURL = (href: string) => {
<script lang="ts" context="module">
export const parseURL = (href: string) => {
try {
return new URL(href)
} catch (e) {
return undefined
}
}
$: richURL = parseURL(href)
</script>
<a {href} class="{highlight ? 'text-blue-500' : ''} hover:underline max-w-full">
<script lang="ts">
export let href: string
export let highlight: boolean = false
export let nowrap: boolean = false
$: richURL = $$slots.default ? undefined : parseURL(href)
</script>
<a
{href}
class="{highlight
? 'text-blue-500'
: ''} hover:underline max-w-full inline-flex items-center gap-1 {$$props.class}"
>
<slot name="icon" />
<slot>
<span
+3 -5
View File
@@ -137,12 +137,10 @@
{:else if post.post.url}
<PostLink
url={post.post.url}
thumbnail_url={view == 'card'
? post.post.thumbnail_url
? `${post.post.thumbnail_url}?format=webp&thumbnail=512`
: undefined
: undefined}
thumbnail_url={post.post.thumbnail_url}
nsfw={post.post.nsfw}
embed_description={post.post.embed_description}
embed_title={post.post.embed_title}
/>
{/if}
{#if post.post.body && !post.post.nsfw && view != 'compact'}
+48 -4
View File
@@ -1,14 +1,58 @@
<script lang="ts">
import Link from '$lib/components/input/Link.svelte'
import Link, { parseURL } from '$lib/components/input/Link.svelte'
import { Material } from 'mono-svelte'
import { Icon, Link as LinkIcon } from 'svelte-hero-icons'
export let url: string
export let thumbnail_url: string | undefined = undefined
export let nsfw: boolean = false
export let embed_title: string | undefined = undefined
export let embed_description: string | undefined = undefined
$: richURL = parseURL(url)
</script>
<Link href={url} highlight nowrap />
{#if thumbnail_url}
{#if embed_title}
<Material class="flex flex-row gap-4">
<div class="flex flex-col gap-2">
{#if richURL}
<Link
href={url}
class="text-slate-600 dark:text-zinc-400 inline-flex items-center gap-1 text-xs"
>
<Icon src={LinkIcon} size="12" mini slot="icon" />
{richURL.hostname}
</Link>
{/if}
<a href={url} class="font-medium text-base hover:underline">
{embed_title}
</a>
{#if embed_description}
<p
class="text-sm text-transparent bg-clip-text bg-gradient-to-b
from-slate-700 dark:from-zinc-400
via-slate-700 dark:via-zinc-400
to-slate-700/0 dark:to-zinc-400/0"
>
{embed_description.slice(0, 300)}
</p>
{/if}
</div>
{#if thumbnail_url}
<a href={url}>
<img
src={thumbnail_url}
class="rounded-lg max-w-[200px] w-full h-auto aspect-video object-cover bg-slate-200 dark:bg-zinc-800"
width={600}
height={400}
alt=""
class:blur-3xl={nsfw}
/>
</a>
{/if}
</Material>
{:else if thumbnail_url}
<Link href={url} highlight nowrap />
<a href={url}>
<img
src={thumbnail_url}
@@ -12,6 +12,7 @@
<svelte:window bind:scrollY={scroll} />
<div class="shell {$$props.class}">
<slot />
<slot
name="navbar"
class="sticky top-0 border-b border-slate-200 dark:border-zinc-900
+7 -3
View File
@@ -34,9 +34,13 @@ async function customFetch(
const f = func ? func : fetch
if (init?.body && auth) {
const json = JSON.parse(init.body.toString())
json.auth = auth
init.body = JSON.stringify(json)
try {
const json = JSON.parse(init.body.toString())
json.auth = auth
init.body = JSON.stringify(json)
} catch (e) {
// It seems this isn't a JSON request. Ignore adding an auth parameter.
}
}
const url = toURL(input)
+32 -22
View File
@@ -4,6 +4,8 @@ import { userSettings } from '$lib/settings.js'
import { get } from 'svelte/store'
import type { SubscribedType } from 'lemmy-js-client'
import { page } from '$app/stores'
import { feature } from '$lib/version.js'
import { client, site } from '$lib/lemmy.js'
export const findClosestNumber = (numbers: number[], target: number): number =>
numbers.reduce((prev, curr) =>
@@ -143,30 +145,38 @@ export async function uploadImage(
const formData = new FormData()
formData.append('images[]', image)
const response = await fetch(
`${
window.location.origin
}/cors/${instance}/pictrs/image?${new URLSearchParams({
auth: jwt,
})}`,
{
method: 'POST',
body: formData,
if (feature('unproxiedImageUpload', get(site)?.version)) {
const res = await client({ auth: jwt, instanceURL: instance }).uploadImage({
image: image,
})
if (res.url) return res.url
else throw new Error(`Failed to upload image. ${res.msg}`)
} else {
const response = await fetch(
`${
window.location.origin
}/cors/${instance}/pictrs/image?${new URLSearchParams({
auth: jwt,
})}`,
{
method: 'POST',
body: formData,
}
)
const json = await response.json()
if (json.msg == 'ok') {
return `https://${instance}/pictrs/image/${json.files?.[0]?.file}`
}
)
const json = await response.json()
if (json.msg == 'ok') {
return `https://${instance}/pictrs/image/${json.files?.[0]?.file}`
throw new Error(
`${
(await response.text().catch((_) => undefined)) ??
'Failed to upload image'
}: ${response.status}: ${response.statusText}`
)
}
throw new Error(
`${
(await response.text().catch((_) => undefined)) ??
'Failed to upload image'
}: ${response.status}: ${response.statusText}`
)
}
export const instanceToURL = (input: string) =>
+2 -4
View File
@@ -74,10 +74,6 @@
<Icon src={Forward} mini size="16" slot="prefix" />
Skip Navigation
</Button>
<Moderation />
<ToastContainer />
<Shell
class="min-h-screen {$userSettings.font == 'inter'
? 'font-inter'
@@ -86,6 +82,8 @@
: 'font-sans'}"
route={$page.route}
>
<Moderation />
<ToastContainer />
<Navbar
slot="navbar"
let:style={s}