md: fix image handling
This commit is contained in:
1 parent
bcc3c48f27
commit
fe7898fa3c
4 files changed
+22
-12
No files matched your search
@@ -36,10 +36,10 @@ export const optimizeImageURL = (
|
|||||||
try {
|
try {
|
||||||
const url = new URL(urlStr)
|
const url = new URL(urlStr)
|
||||||
|
|
||||||
url.searchParams.append('format', 'webp')
|
if (!url.searchParams.has('format')) url.searchParams.set('format', 'webp')
|
||||||
|
|
||||||
if (width > 0) {
|
if (width > 0 && !url.searchParams.has('thumbnail')) {
|
||||||
url.searchParams.append(
|
url.searchParams.set(
|
||||||
'thumbnail',
|
'thumbnail',
|
||||||
findClosestNumber(
|
findClosestNumber(
|
||||||
[128, 196, 256, 512, 728, 1024, 1536],
|
[128, 196, 256, 512, 728, 1024, 1536],
|
||||||
@@ -48,6 +48,10 @@ export const optimizeImageURL = (
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (width == -1) {
|
||||||
|
url.searchParams.delete('thumbnail')
|
||||||
|
}
|
||||||
|
|
||||||
return url.toString()
|
return url.toString()
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log(e)
|
console.log(e)
|
||||||
@@ -74,7 +78,8 @@ export const mediaType = (url?: string, view: View = 'cozy'): MediaType => {
|
|||||||
if (isImage(url)) return 'image'
|
if (isImage(url)) return 'image'
|
||||||
if (isVideo(url)) return 'iframe'
|
if (isVideo(url)) return 'iframe'
|
||||||
if (isYoutubeLink(url)) return 'iframe'
|
if (isYoutubeLink(url)) return 'iframe'
|
||||||
return 'embed'
|
if (URL.canParse(url)) return 'embed'
|
||||||
|
return 'none'
|
||||||
}
|
}
|
||||||
|
|
||||||
return 'none'
|
return 'none'
|
||||||
|
|||||||
@@ -11,11 +11,13 @@
|
|||||||
export let title: string | undefined = undefined
|
export let title: string | undefined = undefined
|
||||||
export let text: string = ''
|
export let text: string = ''
|
||||||
|
|
||||||
|
console.log(href)
|
||||||
|
|
||||||
$: type = mediaType(href, 'cozy')
|
$: type = mediaType(href, 'cozy')
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<div
|
<div
|
||||||
class="max-w-xl h-auto rounded-2xl p-2 border border-slate-200 dark:border-zinc-800
|
class="w-auto h-auto max-h-96 rounded-2xl p-2 border border-slate-200 dark:border-zinc-800
|
||||||
inline-block"
|
inline-block"
|
||||||
>
|
>
|
||||||
{#if type == 'video' || type == 'embed' || type == 'iframe'}
|
{#if type == 'video' || type == 'embed' || type == 'iframe'}
|
||||||
@@ -27,12 +29,15 @@
|
|||||||
class="w-full"
|
class="w-full"
|
||||||
/>
|
/>
|
||||||
{:else}
|
{:else}
|
||||||
<button on:click={() => showImage(href, text)}>
|
<button
|
||||||
|
class="inline"
|
||||||
|
on:click={() => showImage(optimizeImageURL(href, -1), text)}
|
||||||
|
>
|
||||||
<img
|
<img
|
||||||
src={optimizeImageURL(href, 1024)}
|
src={optimizeImageURL(href, 1024)}
|
||||||
{title}
|
{title}
|
||||||
alt={text}
|
alt={text}
|
||||||
class="rounded-lg"
|
class="object-contain w-auto h-auto max-h-80 inline"
|
||||||
/>
|
/>
|
||||||
</button>
|
</button>
|
||||||
{/if}
|
{/if}
|
||||||
|
|||||||
+2
-2
@@ -2,7 +2,7 @@ export const isImage = (url: string | undefined) => {
|
|||||||
try {
|
try {
|
||||||
if (!url) return false
|
if (!url) return false
|
||||||
|
|
||||||
return /\.(jpeg|jpg|gif|png|svg|bmp|webp)$/i.test(url)
|
return /\.(jpeg|jpg|gif|png|svg|bmp|webp|avif)/i.test(url)
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
@@ -12,7 +12,7 @@ export const isVideo = (url: string | undefined) => {
|
|||||||
try {
|
try {
|
||||||
if (!url) return false
|
if (!url) return false
|
||||||
|
|
||||||
return /\.(mp4|mov|webm|mkv)$/i.test(url)
|
return /\.(mp4|mov|webm|mkv|avi)/i.test(url)
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -107,14 +107,14 @@
|
|||||||
on:click={() => markAsRead(!item.read)}
|
on:click={() => markAsRead(!item.read)}
|
||||||
title={$t('post.actions.more.markRead')}
|
title={$t('post.actions.more.markRead')}
|
||||||
>
|
>
|
||||||
<Icon slot="prefix" src={Check} mini size="16" />
|
<Icon slot="prefix" src={Check} micro size="16" />
|
||||||
</Button>
|
</Button>
|
||||||
{/if}
|
{/if}
|
||||||
|
|
||||||
{#if item.type == 'private_message'}
|
{#if item.type == 'private_message'}
|
||||||
{#if item.item.creator.id != $profile?.user?.local_user_view.person.id}
|
{#if item.item.creator.id != $profile?.user?.local_user_view.person.id}
|
||||||
<Button size="square-md" on:click={() => (replying = !replying)}>
|
<Button size="square-md" on:click={() => (replying = !replying)}>
|
||||||
<Icon src={ArrowUturnLeft} size="16" mini />
|
<Icon src={ArrowUturnLeft} size="16" micro />
|
||||||
</Button>
|
</Button>
|
||||||
{/if}
|
{/if}
|
||||||
{:else}
|
{:else}
|
||||||
@@ -123,7 +123,7 @@
|
|||||||
href="/comment/{item.item.comment.id}"
|
href="/comment/{item.item.comment.id}"
|
||||||
size="square-md"
|
size="square-md"
|
||||||
>
|
>
|
||||||
<Icon src={ArrowUturnUp} mini size="16" />
|
<Icon src={ArrowUturnUp} micro size="16" />
|
||||||
</Button>
|
</Button>
|
||||||
{/if}
|
{/if}
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
Reference in new issue
Block a user