2022-11-28 13:23:33 +03:00
|
|
|
import type { Account, Attachment, CreateStatusParams, Status } from 'masto'
|
2022-11-24 09:54:54 +03:00
|
|
|
import { STORAGE_KEY_DRAFTS } from '~/constants'
|
2022-11-24 12:15:58 +03:00
|
|
|
import type { Mutable } from '~/types/utils'
|
2022-11-24 09:54:54 +03:00
|
|
|
|
2022-11-24 17:32:20 +03:00
|
|
|
export interface Draft {
|
2022-11-24 14:35:26 +03:00
|
|
|
editingStatus?: Status
|
2022-11-24 17:32:20 +03:00
|
|
|
params: Omit<Mutable<CreateStatusParams>, 'status'> & {
|
|
|
|
status?: Exclude<CreateStatusParams['status'], null>
|
|
|
|
}
|
2022-11-24 09:54:54 +03:00
|
|
|
attachments: Attachment[]
|
2022-11-28 13:23:33 +03:00
|
|
|
placeholder: string
|
2022-11-24 17:32:20 +03:00
|
|
|
}
|
|
|
|
export type DraftMap = Record<string, Draft>
|
2022-11-24 09:54:54 +03:00
|
|
|
|
|
|
|
const allDrafts = useLocalStorage<Record<string, DraftMap>>(STORAGE_KEY_DRAFTS, {})
|
|
|
|
|
|
|
|
export const currentUserDrafts = computed(() => {
|
2022-11-24 18:48:52 +03:00
|
|
|
if (!currentUser.value?.account.id)
|
2022-11-24 09:54:54 +03:00
|
|
|
return {}
|
|
|
|
const id = `${currentUser.value.account.acct}@${currentUser.value.server}`
|
|
|
|
if (!allDrafts.value[id])
|
|
|
|
allDrafts.value[id] = {}
|
|
|
|
return allDrafts.value[id]
|
|
|
|
})
|
2022-11-24 14:35:26 +03:00
|
|
|
|
2022-11-29 23:45:20 +03:00
|
|
|
export function getDefaultDraft(options: Partial<Draft['params'] & Omit<Draft, 'params'>> = {}): Draft {
|
2022-11-30 02:25:29 +03:00
|
|
|
const { t } = useI18n()
|
2022-11-29 23:45:20 +03:00
|
|
|
const {
|
|
|
|
status = '',
|
|
|
|
inReplyToId,
|
|
|
|
visibility = 'public',
|
2022-11-30 02:25:29 +03:00
|
|
|
placeholder = t('placeholder.default_1'),
|
2022-11-29 23:45:20 +03:00
|
|
|
attachments = [],
|
|
|
|
} = options
|
2022-11-24 14:35:26 +03:00
|
|
|
return {
|
2022-11-24 17:32:20 +03:00
|
|
|
params: {
|
2022-11-25 14:39:21 +03:00
|
|
|
status,
|
2022-11-24 17:32:20 +03:00
|
|
|
inReplyToId,
|
2022-11-25 14:39:21 +03:00
|
|
|
visibility,
|
2022-11-24 17:32:20 +03:00
|
|
|
},
|
2022-11-28 13:23:33 +03:00
|
|
|
attachments,
|
|
|
|
placeholder,
|
2022-11-24 14:35:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-28 13:23:33 +03:00
|
|
|
export function getDraftFromStatus(status: Status, text?: null | string): Draft {
|
|
|
|
return getDefaultDraft({
|
|
|
|
status: text || status.content,
|
2022-11-24 14:35:26 +03:00
|
|
|
mediaIds: status.mediaAttachments.map(att => att.id),
|
|
|
|
visibility: status.visibility,
|
2022-11-28 13:23:33 +03:00
|
|
|
attachments: status.mediaAttachments,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getReplyDraft(status: Status) {
|
2022-11-30 02:25:29 +03:00
|
|
|
const { t } = useI18n()
|
2022-11-28 13:23:33 +03:00
|
|
|
return {
|
|
|
|
key: `reply-${status.id}`,
|
|
|
|
draft: () => getDefaultDraft({
|
|
|
|
inReplyToId: status!.id,
|
2022-11-30 02:25:29 +03:00
|
|
|
placeholder: t('placeholder.reply_to_account', [status?.account ? getDisplayName(status.account) : t('placeholder.the_thread')]),
|
2022-11-28 13:23:33 +03:00
|
|
|
visibility: status.visibility,
|
|
|
|
}),
|
2022-11-24 14:35:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-28 20:46:00 +03:00
|
|
|
export const isEmptyDraft = (draft: Draft | null | undefined) => {
|
|
|
|
if (!draft)
|
|
|
|
return true
|
2022-11-28 13:23:33 +03:00
|
|
|
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,
|
|
|
|
initial: () => Draft = () => getDefaultDraft(),
|
|
|
|
) {
|
2022-11-24 14:35:26 +03:00
|
|
|
const draft = computed({
|
|
|
|
get() {
|
2022-11-24 17:32:20 +03:00
|
|
|
if (!currentUserDrafts.value[draftKey])
|
2022-11-28 13:23:33 +03:00
|
|
|
currentUserDrafts.value[draftKey] = initial()
|
2022-11-24 14:35:26 +03:00
|
|
|
return currentUserDrafts.value[draftKey]
|
|
|
|
},
|
|
|
|
set(val) {
|
|
|
|
currentUserDrafts.value[draftKey] = val
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2022-11-28 13:23:33 +03:00
|
|
|
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 14:35:26 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
return { draft, isEmpty }
|
|
|
|
}
|
|
|
|
|
2022-11-25 14:39:21 +03:00
|
|
|
export function mentionUser(account: Account) {
|
2022-11-28 10:55:57 +03:00
|
|
|
openPublishDialog('dialog', getDefaultDraft({
|
|
|
|
status: `@${account.acct} `,
|
2022-11-28 13:23:33 +03:00
|
|
|
}), true)
|
2022-11-25 14:39:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
export function directMessageUser(account: Account) {
|
2022-11-28 10:55:57 +03:00
|
|
|
openPublishDialog('dialog', getDefaultDraft({
|
2022-11-25 14:39:21 +03:00
|
|
|
status: `@${account.acct} `,
|
|
|
|
visibility: 'direct',
|
2022-11-28 13:23:33 +03:00
|
|
|
}), true)
|
2022-11-25 14:39:21 +03:00
|
|
|
}
|
2022-11-26 22:33:36 +03:00
|
|
|
|
|
|
|
export function clearUserDrafts(account?: Account) {
|
|
|
|
if (!account)
|
|
|
|
account = currentUser.value?.account
|
|
|
|
|
|
|
|
if (!account)
|
|
|
|
return
|
|
|
|
|
|
|
|
const id = `${account.acct}@${currentUser.value?.server}`
|
|
|
|
if (!allDrafts.value[id])
|
|
|
|
return
|
|
|
|
|
|
|
|
delete allDrafts.value[id]
|
|
|
|
}
|