feat [4.5/6]: add piefed poll creation

This commit is contained in:
Xylight committed 2025-11-15 16:14:13 -08:00
1 parent e4507cbf91
commit 939c36158f
8 files changed
+212 -50

No files matched your search

+4
View File
@@ -644,6 +644,8 @@ export interface CreatePost {
nsfw?: boolean
language_id?: LanguageId
custom_thumbnail?: string
poll?: PostPoll
event?: PostEvent
}
export interface CreatePostLike {
@@ -801,6 +803,8 @@ export interface EditPost {
nsfw?: boolean
language_id?: LanguageId
custom_thumbnail?: string
poll?: PostPoll
event?: PostEvent
}
export interface EditPrivateMessage {
+11 -2
View File
@@ -146,7 +146,12 @@
"unset": "Unset",
"readMore": "Read more",
"flair": "Add flair",
"fetching": "Fetching site metadata"
"fetching": "Fetching site metadata",
"types": {
"normal": "Discussion",
"poll": "Poll",
"event": "Event"
}
},
"profile": {
"displayName": "Display name",
@@ -587,7 +592,11 @@
"show": "Show results",
"hide": "Hide results",
"ends": "Ends {{time}}"
}
},
"choices": "Choices",
"single": "Single choice",
"multiple": "Multiple choice",
"mode": "Mode"
}
},
"comment": {
+135 -26
View File
@@ -5,12 +5,17 @@
import { errorMessage } from '$lib/app/error'
import { t } from '$lib/app/i18n'
import MarkdownEditor from '$lib/app/markdown/MarkdownEditor.svelte'
import { settings } from '$lib/app/settings.svelte'
import { placeholders } from '$lib/app/util.svelte'
import Duration from '$lib/ui/form/Duration.svelte'
import FreeTextInput from '$lib/ui/form/FreeTextInput.svelte'
import ObjectAutocomplete from '$lib/ui/form/ObjectAutocomplete.svelte'
import MultiSelect from '$lib/ui/form/Switch.svelte'
import Avatar from '$lib/ui/generic/Avatar.svelte'
import ErrorContainer, { pushError } from '$lib/ui/info/ErrorContainer.svelte'
import { Header } from '$lib/ui/layout'
import { CommonList, Header } from '$lib/ui/layout'
import EndPlaceholder from '$lib/ui/layout/EndPlaceholder.svelte'
import { publishedToDate } from '$lib/ui/util/date'
import {
Badge,
Button,
@@ -36,6 +41,8 @@
Photo,
Plus,
Sparkles,
Tag,
Trash,
XMark,
} from 'svelte-hero-icons/dist'
import { autofillPost, PostFormState } from './postform.svelte'
@@ -50,6 +57,9 @@
let { editPost, init, title, onsubmit }: Props = $props()
let form = $state<PostFormState>(init ?? new PostFormState())
let type = $derived<'normal' | 'poll' | 'event'>(
init?.poll ? 'poll' : init?.event ? 'event' : 'normal',
)
let extendedCommunity = $derived.by(() => {
const api = client()
@@ -64,6 +74,20 @@
let uploadImage = $state(false)
</script>
{#if client() instanceof PiefedClient}
<div class="mb-3">
<MultiSelect
options={['normal', 'poll', 'event']}
optionNames={[
$t('form.post.types.normal'),
$t('form.post.types.poll'),
$t('form.post.types.event'),
]}
bind:selected={type}
/>
</div>
{/if}
{#if uploadImage}
{#await import('$lib/ui/form/ImageInputModal.svelte') then { default: UploadModal }}
<UploadModal
@@ -147,8 +171,9 @@
onsubmit={(e) => {
e.preventDefault()
loading = true
form
.submit(editPost)
.submit(type, editPost)
.then(onsubmit)
.catch((err) =>
pushError({ message: errorMessage(err as string), scope: 'post-form' }),
@@ -208,6 +233,7 @@
bind:value={form.title}
placeholder={placeholders.get('post')}
label={$t('form.post.title')}
class="font-display font-medium text-2xl"
/>
<MarkdownEditor
label={$t('form.post.body')}
@@ -216,7 +242,7 @@
previewButton
/>
{#if form.url !== undefined}
{#if type == 'normal' && form.url !== undefined}
<TextInput
label={$t('form.post.url')}
bind:value={form.url}
@@ -242,36 +268,119 @@
</TextInput>
{/if}
{#if type == 'poll' && form.poll}
<div>
<Label>{$t('post.poll.choices')}</Label>
<CommonList>
{#each form.poll.choices as choice, index}
<li class="px-4 py-1 flex flex-row items-center xs">
<div class="p-0! font-medium flex-1">
<FreeTextInput
bind:value={form.poll.choices[index].choice_text}
class="w-full"
/>
</div>
<div>
<Button
onclick={() => form.poll?.choices.splice(index, 1)}
size="square-md"
aria-label={$t('post.actions.more.delete')}
>
<Icon src={Trash} size="16" mini />
</Button>
</div>
</li>
{/each}
<li class="xs">
<button
onclick={() =>
form.poll?.choices.push({
choice_text: `Option ${form.poll?.choices.length + 1}`,
id: Math.max(...form.poll.choices.map((i) => i.id)) + 1,
num_votes: 0,
sort_order: 2,
})}
type="button"
class="font-medium w-full px-4 py-2! flex flex-row items-center gap-1 justify-center cursor-pointer"
>
<Icon src={Plus} size="16" micro />
{$t('common.add')}
</button>
</li>
</CommonList>
</div>
<EndPlaceholder margin="bottom-lg">
<Select label={$t('post.poll.mode')} bind:value={form.poll!.mode}>
<Option value="single">
{$t('post.poll.single')}
</Option>
<Option value="multiple">
{$t('post.poll.multiple')}
</Option>
</Select>
{#snippet action()}
{#if !editPost}
<Duration
bind:value={
() => {
return form.poll?.end_poll
? Math.floor(
Date.now() -
publishedToDate(form.poll?.end_poll).getTime(),
)
: Date.now() + 24 * 60 * 60
},
(v) => {
if (v == -1) form.poll!.end_poll = undefined
else
form.poll!.end_poll = new Date(
Date.now() + v * 1000,
).toISOString()
}
}
/>
{/if}
{/snippet}
</EndPlaceholder>
{/if}
{#if settings.debugInfo}
{form.poll?.end_poll}
{/if}
<div class="flex flex-row flex-wrap gap-2">
<ButtonGroup
orientation="horizontal"
class="flex flex-row flex-wrap w-full"
>
<Button
onclick={() => (form.url = '')}
disabled={form.url !== undefined}
icon={Link}
>
{$t('form.post.addUrl')}
</Button>
<Button
onclick={() => {
uploadImage = !uploadImage
}}
icon={Photo}
>
{$t('form.post.uploadImage')}
</Button>
{#if form.url && URL.canParse(form.url)}
{#if type == 'normal'}
<Button
class="animate-pop-in"
color={(form.altText ?? '') != '' ? 'primary' : 'secondary'}
onclick={() =>
modal({ title: $t('form.post.altText'), snippet: altText })}
icon={ChatBubbleBottomCenterText}
onclick={() => (form.url = '')}
disabled={form.url !== undefined}
icon={Link}
>
{$t('form.post.altText')}
{$t('form.post.addUrl')}
</Button>
<Button
onclick={() => {
uploadImage = !uploadImage
}}
icon={Photo}
>
{$t('form.post.uploadImage')}
</Button>
{#if form.url && URL.canParse(form.url)}
<Button
class="animate-pop-in"
color={(form.altText ?? '') != '' ? 'primary' : 'secondary'}
onclick={() =>
modal({ title: $t('form.post.altText'), snippet: altText })}
icon={ChatBubbleBottomCenterText}
>
{$t('form.post.altText')}
</Button>
{/if}
{/if}
{#if form.language === undefined}
<Button
@@ -289,7 +398,7 @@
class="animate-pop-in"
onclick={() =>
modal({ title: $t('form.post.flair'), snippet: flairs })}
icon={ChatBubbleBottomCenterText}
icon={Tag}
>
{$t('form.post.flair')}
</Button>
+40 -2
View File
@@ -1,6 +1,12 @@
import { client } from '$lib/api/client.svelte'
import { PiefedClient } from '$lib/api/piefed/adapter'
import type { Community, CommunityFlair, PostView } from '$lib/api/types'
import type {
Community,
CommunityFlair,
PostEvent,
PostPoll,
PostView,
} from '$lib/api/types'
export type PostFormInit = {
community?: Community
@@ -11,6 +17,8 @@ export type PostFormInit = {
alt_text?: string
thumbnail?: string
language_id?: number
poll?: PostPoll
event?: PostEvent
flair_list?: CommunityFlair[]
}
@@ -23,6 +31,8 @@ export class PostFormState {
altText?: string
thumbnail?: string
language?: string
poll?: PostPoll
event?: PostEvent
flairList: CommunityFlair[]
constructor(post?: PostFormInit) {
@@ -33,6 +43,27 @@ export class PostFormState {
this.nsfw = $state(post?.nsfw ?? false)
this.altText = $state(post?.alt_text)
this.language = $state(post?.language_id?.toString())
this.poll = $state(
post?.poll ?? {
mode: 'single',
local_only: false,
choices: [
{
choice_text: 'Option 1',
id: 1,
num_votes: 0,
sort_order: 0,
},
{
choice_text: 'Option 2',
id: 2,
num_votes: 0,
sort_order: 1,
},
],
},
)
this.event = $state(post?.event)
this.flairList = $state(post?.flair_list ?? [])
}
@@ -43,7 +74,10 @@ export class PostFormState {
return true
}
async submit(postId?: number): Promise<PostView> {
async submit(
type: 'normal' | 'poll' | 'event' = 'normal',
postId?: number,
): Promise<PostView> {
if (!this.validate(postId ? 'edit' : 'create'))
throw new Error('failed validation')
@@ -61,6 +95,8 @@ export class PostFormState {
alt_text: this.altText,
custom_thumbnail: this.thumbnail,
language_id: Number(this.language) || undefined,
poll: type == 'poll' ? this.poll : undefined,
event: type == 'event' ? this.event : undefined,
})
).post_view
} else {
@@ -74,6 +110,8 @@ export class PostFormState {
custom_thumbnail: this.thumbnail,
nsfw: this.nsfw,
language_id: Number(this.language) || undefined,
poll: type == 'poll' ? this.poll : undefined,
event: type == 'event' ? this.event : undefined,
})
).post_view
}
+1 -1
View File
@@ -47,6 +47,6 @@
{required}
{...rest}
rows={1}
class="font-semibold text-2xl focus:outline-hidden w-full bg-transparent resize-none {clazz}"
class={['focus:outline-hidden w-full bg-transparent resize-none', clazz]}
></textarea>
</label>
+4
View File
@@ -314,21 +314,25 @@
.btn-square-sm {
width: calc(var(--spacing) * 6);
height: calc(var(--spacing) * 6);
padding: 0;
}
.btn-square-md {
width: calc(var(--spacing) * 8);
height: calc(var(--spacing) * 8);
padding: 0;
}
.btn-square-lg {
width: calc(var(--spacing) * 9.5);
height: calc(var(--spacing) * 9.5);
padding: 0;
}
.btn-square-xl {
width: calc(var(--spacing) * 12);
height: calc(var(--spacing) * 12);
padding: 0;
}
.btn-disabled {
+1 -1
View File
@@ -39,7 +39,7 @@
.btn-group.btn-group-horizontal :global(> *) {
position: relative;
border-radius: 8px 8px 8px 8px !important;
border-radius: 8px !important;
&:first-child {
border-radius: 24px 12px 12px 24px !important;