misc: improve alt text handling

This commit is contained in:
Xylight committed 2024-07-02 10:13:29 -07:00
1 parent 3874160ce5
commit ed0236a5f6
4 files changed
+214 -199

No files matched your search

BIN
View File
Binary file not shown.
+178 -193
View File
@@ -1,213 +1,198 @@
<script lang="ts"> <script lang="ts">
import CommunityLink from "$lib/components/lemmy/community/CommunityLink.svelte"; import CommunityLink from '$lib/components/lemmy/community/CommunityLink.svelte'
import Avatar from "$lib/components/ui/Avatar.svelte"; import Avatar from '$lib/components/ui/Avatar.svelte'
import { Badge } from "mono-svelte"; import { Badge } from 'mono-svelte'
import UserLink from "$lib/components/lemmy/user/UserLink.svelte"; import UserLink from '$lib/components/lemmy/user/UserLink.svelte'
import RelativeDate from "$lib/components/util/RelativeDate.svelte"; import RelativeDate from '$lib/components/util/RelativeDate.svelte'
import type { Community, Person, SubscribedType } from "lemmy-js-client"; import type { Community, Person, SubscribedType } from 'lemmy-js-client'
import { import {
Bookmark, Bookmark,
Check, Check,
ExclamationTriangle, ExclamationTriangle,
Icon, Icon,
LockClosed, LockClosed,
Megaphone, Megaphone,
Plus, Plus,
Trash, Trash,
} from "svelte-hero-icons"; } from 'svelte-hero-icons'
import { getInstance } from "$lib/lemmy.js"; import { getInstance } from '$lib/lemmy.js'
import { profile } from "$lib/auth.js"; import { profile } from '$lib/auth.js'
import Subscribe from "../../../../routes/communities/Subscribe.svelte"; import Subscribe from '../../../../routes/communities/Subscribe.svelte'
import ShieldIcon from "../moderation/ShieldIcon.svelte"; import ShieldIcon from '../moderation/ShieldIcon.svelte'
import { userSettings } from "$lib/settings"; import { userSettings } from '$lib/settings'
import Markdown from "$lib/components/markdown/Markdown.svelte"; import Markdown from '$lib/components/markdown/Markdown.svelte'
import { t } from "$lib/translations"; import { t } from '$lib/translations'
export let community: Community | undefined = undefined; export let community: Community | undefined = undefined
export let subscribed: SubscribedType | undefined = undefined; export let subscribed: SubscribedType | undefined = undefined
export let user: Person | undefined = undefined; export let user: Person | undefined = undefined
export let published: Date | undefined = undefined; export let published: Date | undefined = undefined
export let title: string | undefined = undefined; export let title: string | undefined = undefined
export let id: number | undefined = undefined; export let id: number | undefined = undefined
export let read: boolean = false; export let read: boolean = false
// Badges // Badges
export let badges = { export let badges = {
nsfw: false, nsfw: false,
saved: false, saved: false,
featured: false, featured: false,
deleted: false, deleted: false,
removed: false, removed: false,
locked: false, locked: false,
moderator: false, moderator: false,
admin: false, admin: false,
}; }
</script> </script>
<div <div
class="grid w-full meta {community class="grid w-full meta {community
? 'grid-rows-2' ? 'grid-rows-2'
: 'grid-rows-1'} text-xs min-w-0 max-w-full" : 'grid-rows-1'} text-xs min-w-0 max-w-full"
style={$$props.style ?? ""} style={$$props.style ?? ''}
> >
{#if community} {#if community}
<Subscribe let:subscribe let:subscribing> <Subscribe let:subscribe let:subscribing>
<button <button
on:click={async () => { on:click={async () => {
if (!community) return; if (!community) return
await subscribe(community.id, subscribed); await subscribe(community.id, subscribed)
subscribed = subscribed =
subscribed == "NotSubscribed" subscribed == 'NotSubscribed' ? 'Subscribed' : 'NotSubscribed'
? "Subscribed" }}
: "NotSubscribed"; class="relative cursor-pointer pr-2 row-span-2 flex-shrink-0"
}} >
class="relative cursor-pointer pr-2 row-span-2 flex-shrink-0" <Avatar
> url={community.icon}
<Avatar width={28}
url={community.icon} alt={community.name}
width={28} style="grid-area: avatar; height: 100%;"
alt={community.name} class="flex-shrink-0"
style="grid-area: avatar; height: 100%;" />
class="flex-shrink-0" {#if subscribed != undefined && $profile?.jwt}
/> <div
{#if subscribed != undefined && $profile?.jwt} class="absolute w-3.5 h-3.5 {subscribed == 'NotSubscribed'
<div ? 'bg-primary-900 dark:bg-primary-100 text-white dark:text-black'
class="absolute w-3.5 h-3.5 {subscribed == : 'bg-primary-100 dark:bg-primary-900 text-black dark:text-white'} rounded-full ring-2 box-border
'NotSubscribed'
? 'bg-primary-900 dark:bg-primary-100 text-white dark:text-black'
: 'bg-primary-100 dark:bg-primary-900 text-black dark:text-white'} rounded-full ring-2 box-border
ring-slate-50 dark:ring-zinc-950 ring-slate-50 dark:ring-zinc-950
-bottom-1 right-1 grid place-items-center transition-all" -bottom-1 right-1 grid place-items-center transition-all"
> >
<Icon <Icon
src={subscribed == "NotSubscribed" ? Plus : Check} src={subscribed == 'NotSubscribed' ? Plus : Check}
mini mini
size="12" size="12"
/> />
</div> </div>
{/if} {/if}
</button> </button>
</Subscribe> </Subscribe>
{/if}
{#if community}
<CommunityLink
{community}
style="grid-area: community;"
class="flex-shrink"
/>
{/if}
<span
class="text-slate-600 dark:text-zinc-400 flex flex-row gap-2 items-center"
style="grid-area: stats;"
>
{#if user}
<UserLink avatarSize={20} {user} avatar={!community}>
<svelte:fragment slot="badges">
{#if badges.moderator}
<ShieldIcon filled width={14} class="text-green-500" />
{/if}
{#if badges.admin}
<ShieldIcon filled width={14} class="text-red-500" />
{/if}
</svelte:fragment>
</UserLink>
{/if} {/if}
{#if community} {#if published}
<CommunityLink <RelativeDate date={published} class="flex-shrink-0" />
{community}
style="grid-area: community;"
class="flex-shrink"
/>
{/if} {/if}
<span </span>
class="text-slate-600 dark:text-zinc-400 flex flex-row gap-2 items-center" <div
style="grid-area: stats;" class="flex flex-row items-center self-center flex-wrap gap-2 [&>*]:flex-shrink-0 badges"
> style="grid-area: badges;"
{#if user} >
<UserLink avatarSize={20} {user} avatar={!community}> {#if badges.nsfw}
<svelte:fragment slot="badges"> <Badge label={$t('post.badges.nsfw')} color="red-subtle" allowIconOnly>
{#if badges.moderator} <Icon src={ExclamationTriangle} size="14" micro slot="icon" />
<ShieldIcon filled width={14} class="text-green-500" /> {$t('post.badges.nsfw')}
{/if} </Badge>
{#if badges.admin} {/if}
<ShieldIcon filled width={14} class="text-red-500" /> {#if badges.saved}
{/if} <Badge
</svelte:fragment> label={$t('post.badges.saved')}
</UserLink> color="yellow-subtle"
{/if} allowIconOnly
{#if published} >
<RelativeDate date={published} class="flex-shrink-0" /> <Icon src={Bookmark} micro size="14" slot="icon" />
{/if} {$t('post.badges.saved')}
</span> </Badge>
<div {/if}
class="flex flex-row items-center self-center flex-wrap gap-2 [&>*]:flex-shrink-0 badges" {#if badges.locked}
style="grid-area: badges;" <Badge
> label={$t('post.badges.locked')}
{#if badges.nsfw} color="yellow-subtle"
<Badge allowIconOnly
label={$t("post.badges.nsfw")} >
color="red-subtle" <Icon src={LockClosed} micro size="14" slot="icon" />
allowIconOnly <span class="max-md:hidden">{$t('post.badges.locked')}</span>
> </Badge>
<Icon src={ExclamationTriangle} size="14" micro slot="icon" /> {/if}
{$t("post.badges.nsfw")} {#if badges.removed}
</Badge> <Badge label={$t('post.badges.removed')} color="red-subtle" allowIconOnly>
{/if} <Icon src={Trash} micro size="14" slot="icon" />
{#if badges.saved} <span class="max-md:hidden">{$t('post.badges.removed')}</span>
<Badge </Badge>
label={$t("post.badges.saved")} {/if}
color="yellow-subtle" {#if badges.deleted}
allowIconOnly <Badge label={$t('post.badges.deleted')} color="red-subtle" allowIconOnly>
> <Icon src={Trash} micro size="14" slot="icon" />
<Icon src={Bookmark} micro size="14" slot="icon" /> <span class="max-md:hidden">{$t('post.badges.deleted')}</span>
{$t("post.badges.saved")} </Badge>
</Badge> {/if}
{/if} {#if badges.featured}
{#if badges.locked} <Badge
<Badge label={$t('post.badges.featured')}
label={$t("post.badges.locked")} color="green-subtle"
color="yellow-subtle" allowIconOnly
allowIconOnly >
> <Icon src={Megaphone} micro size="14" slot="icon" />
<Icon src={LockClosed} micro size="14" slot="icon" /> <span class="max-md:hidden">{$t('post.badges.featured')}</span>
<span class="max-md:hidden">{$t("post.badges.locked")}</span> </Badge>
</Badge> {/if}
{/if} <slot name="badges" />
{#if badges.removed} </div>
<Badge
label={$t("post.badges.removed")}
color="red-subtle"
allowIconOnly
>
<Icon src={Trash} micro size="14" slot="icon" />
<span class="max-md:hidden">{$t("post.badges.removed")}</span>
</Badge>
{/if}
{#if badges.deleted}
<Badge
label={$t("post.badges.deleted")}
color="red-subtle"
allowIconOnly
>
<Icon src={Trash} micro size="14" slot="icon" />
<span class="max-md:hidden">{$t("post.badges.deleted")}</span>
</Badge>
{/if}
{#if badges.featured}
<Badge
label={$t("post.badges.featured")}
color="green-subtle"
allowIconOnly
>
<Icon src={Megaphone} micro size="14" slot="icon" />
<span class="max-md:hidden">{$t("post.badges.featured")}</span>
</Badge>
{/if}
<slot name="badges" />
</div>
</div> </div>
{#if title && id} {#if title && id}
<a <a
href="/post/{getInstance()}/{id}" href="/post/{getInstance()}/{id}"
class="inline text-lg hover:underline class="inline text-lg hover:underline
hover:text-primary-900 hover:dark:text-primary-100 transition-colors hover:text-primary-900 hover:dark:text-primary-100 transition-colors
{$userSettings.font == 'satoshi/nunito' {$userSettings.font == 'satoshi/nunito'
? 'font-display font-semibold' ? 'font-display font-semibold'
: 'font-medium'}" : 'font-medium'}"
class:text-slate-600={$userSettings.markReadPosts && read} class:text-slate-600={$userSettings.markReadPosts && read}
class:dark:text-zinc-400={$userSettings.markReadPosts && read} class:dark:text-zinc-400={$userSettings.markReadPosts && read}
style="grid-area: title;" style="grid-area: title;"
> >
<Markdown source={title} inline noStyle class="leading-6"></Markdown> <Markdown source={title} inline noStyle class="leading-[1.3]"></Markdown>
</a> </a>
{/if} {/if}
<style> <style>
.meta { .meta {
display: grid; display: grid;
grid-template-areas: grid-template-areas:
"avatar community badges" 'avatar community badges'
"avatar stats badges"; 'avatar stats badges';
gap: 0; gap: 0;
grid-template-rows: auto auto; grid-template-rows: auto auto;
grid-template-columns: auto 1fr; grid-template-columns: auto 1fr auto;
} }
</style> </style>
@@ -13,11 +13,14 @@
import { userSettings } from '$lib/settings.js' import { userSettings } from '$lib/settings.js'
import type { Post } from 'lemmy-js-client' import type { Post } from 'lemmy-js-client'
import PostIframe from './PostIframe.svelte' import PostIframe from './PostIframe.svelte'
import { Button, Material } from 'mono-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' export let type: MediaType = 'none'
export let opened: boolean | undefined = undefined export let opened: boolean | undefined = undefined
let showAltText = false
</script> </script>
{#if type == 'image'} {#if type == 'image'}
@@ -70,12 +73,22 @@
/> />
</picture> </picture>
{#if post.alt_text} {#if post.alt_text}
<button <Button
class="absolute bottom-0 left-0 text-sm m-2 bg-white dark:bg-zinc-900 px-3 py-1.5 on:click={(e) => {
rounded-full border border-slate-200 dark:border-zinc-800 shadow-lg text-left" e.stopPropagation()
showAltText = !showAltText
}}
class="absolute bottom-0 left-0 text-sm m-2 text-left"
size="sm"
rounding="pill"
alignment="left"
> >
{post.alt_text} {#if showAltText}
</button> {post.alt_text}
{:else}
<span class="font-bold">ALT</span>
{/if}
</Button>
{/if} {/if}
</svelte:element> </svelte:element>
{/if} {/if}
@@ -9,6 +9,7 @@
import { userSettings } from '$lib/settings.js' import { userSettings } from '$lib/settings.js'
import { isImage } from '$lib/ui/image' import { isImage } from '$lib/ui/image'
import type { Post } from 'lemmy-js-client' import type { Post } from 'lemmy-js-client'
import { Material, Popover } from 'mono-svelte'
import { DocumentText, Icon, Link, VideoCamera } from 'svelte-hero-icons' import { DocumentText, Icon, Link, VideoCamera } from 'svelte-hero-icons'
export let view: 'card' | 'cozy' | 'list' | 'compact' = 'cozy' export let view: 'card' | 'cozy' | 'list' | 'compact' = 'cozy'
@@ -18,9 +19,25 @@
{#if view == 'compact' || (view == 'list' && (!post.embed_title || type == 'iframe') && type != 'none')} {#if view == 'compact' || (view == 'list' && (!post.embed_title || type == 'iframe') && type != 'none')}
<div <div
class="w-24 sm:w-32 h-24 group/media {$$props.class ?? ''}" class="w-24 sm:w-32 h-24 relative group/media {$$props.class ?? ''}"
style={$$props.style ?? ''} style={$$props.style ?? ''}
> >
{#if post.alt_text}
<Popover openOnHover placement="bottom-end">
<Material
slot="target"
padding="none"
rounding="full"
elevation="high"
class="w-max absolute bottom-0 left-0 py-0.5 px-1.5 m-1 font-bold"
>
ALT
</Material>
<div class="max-w-sm">
{post.alt_text}
</div>
</Popover>
{/if}
<svelte:element <svelte:element
this={!$userSettings.expandImages || type != 'image' ? 'a' : 'button'} this={!$userSettings.expandImages || type != 'image' ? 'a' : 'button'}
href={postLink(post)} href={postLink(post)}