elk/composables/statusDrafts.ts

117 lines
3.1 KiB
TypeScript
Raw Normal View History

2022-12-13 15:03:30 +01:00
import type { Account, Status } from 'masto'
2022-11-24 07:54:54 +01:00
import { STORAGE_KEY_DRAFTS } from '~/constants'
2022-12-13 15:03:30 +01:00
import type { Draft, DraftMap } from '~/types'
2022-11-24 07:54:54 +01:00
export const currentUserDrafts = process.server ? computed<DraftMap>(() => ({})) : useUserLocalStorage<DraftMap>(STORAGE_KEY_DRAFTS, () => ({}))
2022-11-24 12:35:26 +01:00
2022-11-29 21:45:20 +01:00
export function getDefaultDraft(options: Partial<Draft['params'] & Omit<Draft, 'params'>> = {}): Draft {
const {
status = '',
inReplyToId,
visibility = 'public',
attachments = [],
initialText = '',
sensitive = false,
spoilerText = '',
language,
2022-11-29 21:45:20 +01:00
} = options
2022-11-30 05:50:29 +01:00
2022-11-24 12:35:26 +01:00
return {
2022-11-24 15:32:20 +01:00
params: {
2022-11-25 12:39:21 +01:00
status,
2022-11-24 15:32:20 +01:00
inReplyToId,
2022-11-25 12:39:21 +01:00
visibility,
sensitive,
spoilerText,
language,
2022-11-24 15:32:20 +01:00
},
attachments,
initialText,
2022-11-24 12:35:26 +01:00
}
}
2023-01-01 16:30:21 +01:00
export async function getDraftFromStatus(status: Status): Promise<Draft> {
return getDefaultDraft({
2023-01-01 16:30:21 +01:00
status: await convertMastodonHTML(status.content),
2022-11-24 12:35:26 +01:00
mediaIds: status.mediaAttachments.map(att => att.id),
visibility: status.visibility,
attachments: status.mediaAttachments,
sensitive: status.sensitive,
spoilerText: status.spoilerText,
language: status.language,
})
}
function mentionHTML(acct: string) {
return `<span data-type="mention" data-id="${acct}" contenteditable="false">@${acct}</span>`
}
export function getReplyDraft(status: Status) {
2022-12-13 14:49:22 +01:00
const accountsToMention: string[] = []
const userId = currentUser.value?.account.id
if (status.account.id !== userId)
2022-12-13 14:49:22 +01:00
accountsToMention.push(status.account.acct)
accountsToMention.push(...(status.mentions.filter(mention => mention.id !== userId).map(mention => mention.acct)))
return {
key: `reply-${status.id}`,
draft: () => {
return getDefaultDraft({
2022-12-13 14:49:22 +01:00
initialText: accountsToMention.map(acct => mentionHTML(acct)).join(' '),
inReplyToId: status!.id,
visibility: status.visibility,
})
},
2022-11-24 12:35:26 +01:00
}
}
2022-11-28 18:46:00 +01:00
export const isEmptyDraft = (draft: Draft | null | undefined) => {
if (!draft)
return true
const { params, attachments } = draft
const status = params.status || ''
return (status.length === 0 || status === '<p></p>')
&& attachments.length === 0
&& (params.spoilerText || '').length === 0
}
export function useDraft(
draftKey: string,
2022-11-30 05:50:29 +01:00
initial: () => Draft = () => getDefaultDraft({}),
) {
2022-11-24 12:35:26 +01:00
const draft = computed({
get() {
2022-11-24 15:32:20 +01:00
if (!currentUserDrafts.value[draftKey])
currentUserDrafts.value[draftKey] = initial()
2022-11-24 12:35:26 +01:00
return currentUserDrafts.value[draftKey]
},
set(val) {
currentUserDrafts.value[draftKey] = val
},
})
const isEmpty = computed(() => isEmptyDraft(draft.value))
onUnmounted(async () => {
// Remove draft if it's empty
if (isEmpty.value) {
await nextTick()
delete currentUserDrafts.value[draftKey]
}
2022-11-24 12:35:26 +01:00
})
return { draft, isEmpty }
}
2022-11-25 12:39:21 +01:00
export function mentionUser(account: Account) {
openPublishDialog('dialog', getDefaultDraft({
status: `@${account.acct} `,
}), true)
2022-11-25 12:39:21 +01:00
}
export function directMessageUser(account: Account) {
openPublishDialog('dialog', getDefaultDraft({
2022-11-25 12:39:21 +01:00
status: `@${account.acct} `,
visibility: 'direct',
}), true)
2022-11-25 12:39:21 +01:00
}