feat: add contextual actions to command palette

This commit is contained in:
Xylight committed 2024-10-18 14:06:30 -07:00
1 parent 3738413184
commit d7d8981cc6
6 files changed
+176 -69

No files matched your search

+4
View File
@@ -1,5 +1,6 @@
// See https://kit.svelte.dev/docs/types#app // See https://kit.svelte.dev/docs/types#app
import type { Action } from '$lib/components/ui/navbar/commands/actions'
import type { ComponentType, SvelteComponent } from 'svelte' import type { ComponentType, SvelteComponent } from 'svelte'
// for information about these interfaces // for information about these interfaces
@@ -16,6 +17,9 @@ declare global {
props?: any props?: any
} }
} }
contextual?: {
actions?: Action[]
}
} }
interface PageState { interface PageState {
openImage: url openImage: url
@@ -10,6 +10,7 @@
import { profile, profileData } from '$lib/auth' import { profile, profileData } from '$lib/auth'
import { getGroups, type Action, type Group } from './actions' import { getGroups, type Action, type Group } from './actions'
import { themeData } from '$lib/ui/colors' import { themeData } from '$lib/ui/colors'
import { page } from '$app/stores'
export let open = false export let open = false
$: if (open) search = '' $: if (open) search = ''
@@ -20,7 +21,8 @@
$resumables, $resumables,
$profile, $profile,
$profileData.profiles, $profileData.profiles,
$themeData $themeData,
$page.data.contextual?.actions
) )
let search = '' let search = ''
@@ -43,6 +43,7 @@ import {
ChatBubbleOvalLeftEllipsis, ChatBubbleOvalLeftEllipsis,
ChatBubbleLeftRight, ChatBubbleLeftRight,
Swatch, Swatch,
Plus,
} from 'svelte-hero-icons' } from 'svelte-hero-icons'
import { get } from 'svelte/store' import { get } from 'svelte/store'
@@ -66,7 +67,8 @@ export function getGroups(
resumables: ResumableItem[], resumables: ResumableItem[],
profile: Profile, profile: Profile,
profiles: Profile[], profiles: Profile[],
td: ThemeData td: ThemeData,
contextual?: Action[]
) { ) {
return [ return [
{ {
@@ -77,6 +79,10 @@ export function getGroups(
href: r.url, href: r.url,
})), })),
}, },
{
name: t.get('nav.commands.contextual'),
actions: contextual ?? [],
},
{ {
name: t.get('nav.commands.main'), name: t.get('nav.commands.main'),
actions: [ actions: [
@@ -254,6 +260,26 @@ export function getGroups(
name: t.get('account.accounts'), name: t.get('account.accounts'),
icon: UserGroup, icon: UserGroup,
}, },
{
href: '/login',
name: t.get('account.login'),
icon: ArrowRightOnRectangle,
},
{
href: '/signup',
name: t.get('account.signup'),
icon: Identification,
},
{
href: '/login/guest',
name: t.get('account.addGuest'),
icon: Plus,
},
{
href: '/accounts',
name: t.get('account.accounts'),
icon: UserGroup,
},
] ]
: [ : [
{ {
@@ -266,6 +292,11 @@ export function getGroups(
name: t.get('account.signup'), name: t.get('account.signup'),
icon: Identification, icon: Identification,
}, },
{
href: '/login/guest',
name: t.get('account.addGuest'),
icon: Plus,
},
{ {
href: '/accounts', href: '/accounts',
name: t.get('account.accounts'), name: t.get('account.accounts'),
+2 -1
View File
@@ -46,7 +46,8 @@
"feeds": "Feeds", "feeds": "Feeds",
"downloading": "Downloading actions", "downloading": "Downloading actions",
"setTheme": "Set theme", "setTheme": "Set theme",
"setThemeTo": "Set theme to {{default}}" "setThemeTo": "Set theme to {{default}}",
"contextual": "On this page"
} }
}, },
"account": { "account": {
+24 -11
View File
@@ -6,6 +6,8 @@ import { error } from '@sveltejs/kit'
import { browser } from '$app/environment' import { browser } from '$app/environment'
import { instance } from '$lib/instance' import { instance } from '$lib/instance'
import { postFeed, postFeeds, shouldReload } from '$lib/lemmy/postfeed.js' import { postFeed, postFeeds, shouldReload } from '$lib/lemmy/postfeed.js'
import { t } from '$lib/translations.js'
import { ChevronDoubleUp } from 'svelte-hero-icons'
export async function load({ url, fetch }) { export async function load({ url, fetch }) {
const cursor = url.searchParams.get('cursor') as string | undefined const cursor = url.searchParams.get('cursor') as string | undefined
@@ -18,16 +20,27 @@ export async function load({ url, fetch }) {
const listingType: ListingType = const listingType: ListingType =
(url.searchParams.get('type') as ListingType) || settings.defaultSort.feed (url.searchParams.get('type') as ListingType) || settings.defaultSort.feed
return await postFeed({ return {
id: 'main', ...(await postFeed({
request: { id: 'main',
page_cursor: cursor, request: {
sort: sort, page_cursor: cursor,
type_: listingType, sort: sort,
limit: 20, type_: listingType,
show_hidden: settings.posts.showHidden, limit: 20,
show_hidden: settings.posts.showHidden,
},
url: url,
fetch: fetch,
})),
contextual: {
actions: [
{
name: t.get('routes.post.scrollToTop'),
handle: () => window?.scrollTo({ top: 0, behavior: 'instant' }),
icon: ChevronDoubleUp,
},
],
}, },
url: url, }
fetch: fetch,
})
} }
@@ -1,7 +1,7 @@
<script lang="ts"> <script lang="ts">
import { buildCommentsTree } from '$lib/components/lemmy/comment/comments.js' import { buildCommentsTree } from '$lib/components/lemmy/comment/comments.js'
import { isImage } from '$lib/ui/image.js' import { isImage } from '$lib/ui/image.js'
import { getClient } from '$lib/lemmy.js' import { client, getClient } from '$lib/lemmy.js'
import CommentForm from '$lib/components/lemmy/comment/CommentForm.svelte' import CommentForm from '$lib/components/lemmy/comment/CommentForm.svelte'
import { onMount } from 'svelte' import { onMount } from 'svelte'
import Markdown from '$lib/components/markdown/Markdown.svelte' import Markdown from '$lib/components/markdown/Markdown.svelte'
@@ -16,6 +16,10 @@
Home, Home,
PlusCircle, PlusCircle,
ChatBubbleLeftRight, ChatBubbleLeftRight,
Bookmark,
BookmarkSlash,
ArrowUp,
ArrowDown,
} from 'svelte-hero-icons' } from 'svelte-hero-icons'
import PostMeta from '$lib/components/lemmy/post/PostMeta.svelte' import PostMeta from '$lib/components/lemmy/post/PostMeta.svelte'
import { Select, removeToast, toast } from 'mono-svelte' import { Select, removeToast, toast } from 'mono-svelte'
@@ -42,34 +46,80 @@
export let data export let data
let post = data.post $: type = mediaType(data.post.post_view.post.url, 'cozy')
$: type = mediaType(post.post_view.post.url, 'cozy') const updateActions = () => {
// @ts-ignore
data.contextual = {
actions: [
{
name: $t('post.actions.vote.upvote'),
icon: ArrowUp,
handle: async () => {
data.post.post_view.my_vote = (
await client().likePost({
post_id: data.post.post_view.post.id,
score: data.post.post_view.my_vote == 1 ? 0 : 1,
})
).post_view.my_vote
},
},
{
name: $t('post.actions.vote.downvote'),
icon: ArrowDown,
handle: async () => {
data.post.post_view.my_vote = (
await client().likePost({
post_id: data.post.post_view.post.id,
score: data.post.post_view.my_vote == -1 ? 0 : -1,
})
).post_view.my_vote
},
},
{
name: data.post.post_view.saved
? $t('post.actions.unsave')
: $t('post.actions.save'),
handle: async () => {
data.post.post_view.saved = (
await client().savePost({
post_id: data.post.post_view.post.id,
save: !data.post.post_view.saved,
})
).post_view.saved
},
icon: data.post.post_view.saved ? BookmarkSlash : Bookmark,
},
],
}
}
$: if (data.post) updateActions()
onMount(async () => { onMount(async () => {
if ( if (
!(post.post_view.read && $userSettings.markPostsAsRead) && !(data.post.post_view.read && $userSettings.markPostsAsRead) &&
$profile?.jwt $profile?.jwt
) { ) {
getClient().markPostAsRead({ getClient().markPostAsRead({
read: $userSettings.markPostsAsRead, read: $userSettings.markPostsAsRead,
post_ids: [post.post_view.post.id], post_ids: [data.post.post_view.post.id],
// @ts-ignore // @ts-ignore
post_id: post.post_view.post.id, post_id: data.post.post_view.post.id,
}) })
} }
resumables.add({ resumables.add({
name: post.post_view.post.name, name: data.post.post_view.post.name,
type: 'post', type: 'post',
url: $page.url.toString(), url: $page.url.toString(),
avatar: post.post_view.post.thumbnail_url, avatar: data.post.post_view.post.thumbnail_url,
}) })
}) })
afterNavigate(async () => { afterNavigate(async () => {
// reactivity hack // reactivity hack
post = data.post data.post = data.post
}) })
const fetchOnHome = async (jwt: string) => { const fetchOnHome = async (jwt: string) => {
@@ -80,7 +130,7 @@
try { try {
const res = await getClient().resolveObject({ const res = await getClient().resolveObject({
q: post.post_view.post.ap_id, q: data.post.post_view.post.ap_id,
}) })
if (res.post) { if (res.post) {
@@ -104,7 +154,7 @@
page: 1, page: 1,
limit: 25, limit: 25,
type_: 'All', type_: 'All',
post_id: post.post_view.post.id, post_id: data.post.post_view.post.id,
sort: commentSort, sort: commentSort,
max_depth: data.post.post_view.counts.comments > 100 ? 1 : 3, max_depth: data.post.post_view.counts.comments > 100 ? 1 : 3,
}) })
@@ -120,29 +170,35 @@
</script> </script>
<svelte:head> <svelte:head>
<title>{post.post_view.post.name}</title> <title>{data.post.post_view.post.name}</title>
<meta property="og:title" content={post.post_view.post.name} /> <meta property="og:title" content={data.post.post_view.post.name} />
<meta property="twitter:title" content={post.post_view.post.name} /> <meta property="twitter:title" content={data.post.post_view.post.name} />
<meta property="og:url" content={$page.url.toString()} /> <meta property="og:url" content={$page.url.toString()} />
{#if isImage(post.post_view.post.url)} {#if isImage(data.post.post_view.post.url)}
<meta property="og:image" content={post.post_view.post.url} /> <meta property="og:image" content={data.post.post_view.post.url} />
<meta property="twitter:card" content={post.post_view.post.url} /> <meta property="twitter:card" content={data.post.post_view.post.url} />
{:else if post.post_view.post.thumbnail_url} {:else if data.post.post_view.post.thumbnail_url}
<meta property="og:image" content={post.post_view.post.thumbnail_url} /> <meta
<meta property="twitter:card" content={post.post_view.post.thumbnail_url} /> property="og:image"
content={data.post.post_view.post.thumbnail_url}
/>
<meta
property="twitter:card"
content={data.post.post_view.post.thumbnail_url}
/>
{/if} {/if}
{#if post.post_view.post.body} {#if data.post.post_view.post.body}
<meta <meta
property="description" property="description"
content={post.post_view.post.body.slice(0, 500)} content={data.post.post_view.post.body.slice(0, 500)}
/> />
<meta <meta
property="og:description" property="og:description"
content={post.post_view.post.body.slice(0, 500)} content={data.post.post_view.post.body.slice(0, 500)}
/> />
<meta <meta
property="twitter:description" property="twitter:description"
content={post.post_view.post.body.slice(0, 500)} content={data.post.post_view.post.body.slice(0, 500)}
/> />
{/if} {/if}
</svelte:head> </svelte:head>
@@ -188,24 +244,24 @@
<header class="flex flex-col gap-2"> <header class="flex flex-col gap-2">
<div class="flex flex-row justify-between items-center gap-2 flex-wrap"> <div class="flex flex-row justify-between items-center gap-2 flex-wrap">
<PostMeta <PostMeta
community={post.post_view.community} community={data.post.post_view.community}
user={post.post_view.creator} user={data.post.post_view.creator}
bind:subscribed={post.community_view.subscribed} bind:subscribed={data.post.community_view.subscribed}
badges={{ badges={{
deleted: post.post_view.post.deleted, deleted: data.post.post_view.post.deleted,
removed: post.post_view.post.removed, removed: data.post.post_view.post.removed,
locked: post.post_view.post.locked, locked: data.post.post_view.post.locked,
featured: featured:
post.post_view.post.featured_community || data.post.post_view.post.featured_community ||
post.post_view.post.featured_local, data.post.post_view.post.featured_local,
nsfw: post.post_view.post.nsfw, nsfw: data.post.post_view.post.nsfw,
saved: post.post_view.saved, saved: data.post.post_view.saved,
admin: post.post_view.creator_is_admin, admin: data.post.post_view.creator_is_admin,
moderator: post.post_view.creator_is_moderator, moderator: data.post.post_view.creator_is_moderator,
}} }}
published={publishedToDate(post.post_view.post.published)} published={publishedToDate(data.post.post_view.post.published)}
edited={post.post_view.post.updated} edited={data.post.post_view.post.updated}
bind:title={post.post_view.post.name} bind:title={data.post.post_view.post.name}
style="width: max-content;" style="width: max-content;"
/> />
<Button on:click={() => history.back()} size="square-md"> <Button on:click={() => history.back()} size="square-md">
@@ -213,23 +269,23 @@
</Button> </Button>
</div> </div>
<h1 class="font-bold text-xl font-display leading-5"> <h1 class="font-bold text-xl font-display leading-5">
<Markdown source={post.post_view.post.name} inline /> <Markdown source={data.post.post_view.post.name} inline />
</h1> </h1>
</header> </header>
<PostMedia <PostMedia
type={mediaType(post.post_view.post.url)} type={mediaType(data.post.post_view.post.url)}
post={post.post_view.post} post={data.post.post_view.post}
opened opened
view="cozy" view="cozy"
/> />
{#if post.post_view.post.body} {#if data.post.post_view.post.body}
<div class="text-base text-slate-800 dark:text-zinc-300 leading-[1.5]"> <div class="text-base text-slate-800 dark:text-zinc-300 leading-[1.5]">
<Markdown source={post.post_view.post.body} /> <Markdown source={data.post.post_view.post.body} />
</div> </div>
{/if} {/if}
<div class="w-full relative"> <div class="w-full relative">
<PostActions <PostActions
bind:post={post.post_view} bind:post={data.post.post_view}
on:edit={() => on:edit={() =>
toast({ toast({
content: 'The post was edited successfully.', content: 'The post was edited successfully.',
@@ -238,22 +294,22 @@
{type} {type}
/> />
</div> </div>
{#if post.cross_posts?.length > 0} {#if data.post.cross_posts?.length > 0}
<Expandable <Expandable
class="text-base mt-2 w-full cursor-pointer" class="text-base mt-2 w-full cursor-pointer"
open={post.cross_posts?.length <= 3} open={data.post.cross_posts?.length <= 3}
> >
<div <div
slot="title" slot="title"
class="inline-block w-full text-left text-base font-normal" class="inline-block w-full text-left text-base font-normal"
> >
<span class="font-bold">{post.cross_posts.length}</span> <span class="font-bold">{data.post.cross_posts.length}</span>
{$t('routes.post.crosspostCount')} {$t('routes.post.crosspostCount')}
</div> </div>
<div <div
class="!divide-y divide-slate-200 dark:divide-zinc-800 flex flex-col" class="!divide-y divide-slate-200 dark:divide-zinc-800 flex flex-col"
> >
{#each post.cross_posts as crosspost} {#each data.post.cross_posts as crosspost}
<Post view="compact" actions={false} post={crosspost} /> <Post view="compact" actions={false} post={crosspost} />
{/each} {/each}
</div> </div>
@@ -294,7 +350,7 @@
<header> <header>
<div class="text-base"> <div class="text-base">
<span class="font-bold"> <span class="font-bold">
<FormattedNumber number={post.post_view.counts.comments} /> <FormattedNumber number={data.post.post_view.counts.comments} />
</span> </span>
{$t('routes.post.commentCount')} {$t('routes.post.commentCount')}
</div> </div>
@@ -339,13 +395,13 @@
</EndPlaceholder> </EndPlaceholder>
{:else} {:else}
<CommentForm <CommentForm
postId={post.post_view.post.id} postId={data.post.post_view.post.id}
on:comment={(comment) => on:comment={(comment) =>
(comments.comments = [ (comments.comments = [
comment.detail.comment_view, comment.detail.comment_view,
...comments.comments, ...comments.comments,
])} ])}
locked={(post.post_view.post.locked && locked={(data.post.post_view.post.locked &&
!( !(
$profile?.user?.local_user_view.local_user.admin || $profile?.user?.local_user_view.local_user.admin ||
$profile?.user?.moderates $profile?.user?.moderates
@@ -381,7 +437,7 @@
</div> </div>
{/if} {/if}
<CommentListVirtualizer <CommentListVirtualizer
post={post.post_view.post} post={data.post.post_view.post}
nodes={buildCommentsTree( nodes={buildCommentsTree(
comments.comments, comments.comments,
undefined, undefined,
@@ -401,10 +457,10 @@
></Placeholder> ></Placeholder>
{/if} {/if}
{/await} {/await}
{#if post.post_view.counts.comments > 5} {#if data.post.post_view.counts.comments > 5}
<EndPlaceholder> <EndPlaceholder>
<span class="text-black dark:text-white font-bold"> <span class="text-black dark:text-white font-bold">
{post.post_view.counts.comments} {data.post.post_view.counts.comments}
</span> </span>
{$t('routes.post.commentCount')} {$t('routes.post.commentCount')}