2022-11-25 20:39:21 +09:00
|
|
|
import type { Account, Attachment, CreateStatusParams, Status } from 'masto'
|
2022-11-24 15:54:54 +09:00
|
|
|
import { STORAGE_KEY_DRAFTS } from '~/constants'
|
2022-11-24 18:15:58 +09:00
|
|
|
import type { Mutable } from '~/types/utils'
|
2022-11-24 15:54:54 +09:00
|
|
|
|
2022-11-24 23:32:20 +09:00
|
|
|
export interface Draft {
|
2022-11-24 20:35:26 +09:00
|
|
|
editingStatus?: Status
|
2022-11-24 23:32:20 +09:00
|
|
|
params: Omit<Mutable<CreateStatusParams>, 'status'> & {
|
|
|
|
status?: Exclude<CreateStatusParams['status'], null>
|
|
|
|
}
|
2022-11-24 15:54:54 +09:00
|
|
|
attachments: Attachment[]
|
2022-11-24 23:32:20 +09:00
|
|
|
}
|
|
|
|
export type DraftMap = Record<string, Draft>
|
2022-11-24 15:54:54 +09:00
|
|
|
|
|
|
|
const allDrafts = useLocalStorage<Record<string, DraftMap>>(STORAGE_KEY_DRAFTS, {})
|
|
|
|
|
|
|
|
export const currentUserDrafts = computed(() => {
|
2022-11-25 00:48:52 +09:00
|
|
|
if (!currentUser.value?.account.id)
|
2022-11-24 15:54:54 +09: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 20:35:26 +09:00
|
|
|
|
2022-11-25 20:39:21 +09:00
|
|
|
export function getDefaultDraft({
|
|
|
|
status = '',
|
|
|
|
inReplyToId,
|
|
|
|
visibility = 'public',
|
|
|
|
}: Partial<Draft['params']> = {}): Draft {
|
2022-11-24 20:35:26 +09:00
|
|
|
return {
|
2022-11-24 23:32:20 +09:00
|
|
|
params: {
|
2022-11-25 20:39:21 +09:00
|
|
|
status,
|
2022-11-24 23:32:20 +09:00
|
|
|
inReplyToId,
|
2022-11-25 20:39:21 +09:00
|
|
|
visibility,
|
2022-11-24 23:32:20 +09:00
|
|
|
},
|
|
|
|
attachments: [],
|
2022-11-24 20:35:26 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-26 11:01:50 +09:00
|
|
|
export function getParamsFromStatus(status: Status): Draft['params'] {
|
2022-11-24 20:35:26 +09:00
|
|
|
return {
|
|
|
|
status: status.content,
|
|
|
|
mediaIds: status.mediaAttachments.map(att => att.id),
|
|
|
|
visibility: status.visibility,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function useDraft(draftKey: string, inReplyToId?: string) {
|
|
|
|
const draft = computed({
|
|
|
|
get() {
|
2022-11-24 23:32:20 +09:00
|
|
|
if (!currentUserDrafts.value[draftKey])
|
2022-11-25 20:39:21 +09:00
|
|
|
currentUserDrafts.value[draftKey] = getDefaultDraft({ inReplyToId })
|
2022-11-24 23:32:20 +09:00
|
|
|
|
2022-11-24 20:35:26 +09:00
|
|
|
return currentUserDrafts.value[draftKey]
|
|
|
|
},
|
|
|
|
set(val) {
|
|
|
|
currentUserDrafts.value[draftKey] = val
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
const isEmpty = computed(() => {
|
|
|
|
return (draft.value.params.status ?? '').trim().length === 0
|
|
|
|
&& draft.value.attachments.length === 0
|
|
|
|
})
|
|
|
|
|
|
|
|
return { draft, isEmpty }
|
|
|
|
}
|
|
|
|
|
|
|
|
export const dialogDraft = useDraft('dialog')
|
2022-11-25 20:39:21 +09:00
|
|
|
|
|
|
|
export function mentionUser(account: Account) {
|
|
|
|
openPublishDialog(getDefaultDraft({ status: `@${account.acct} ` }))
|
|
|
|
}
|
|
|
|
|
|
|
|
export function directMessageUser(account: Account) {
|
|
|
|
openPublishDialog(getDefaultDraft({
|
|
|
|
status: `@${account.acct} `,
|
|
|
|
visibility: 'direct',
|
|
|
|
}))
|
|
|
|
}
|
2022-11-27 04:33:36 +09: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]
|
|
|
|
}
|