2022-12-13 17:03:30 +03:00
|
|
|
import type { Account, Status } from 'masto'
|
2022-11-24 09:54:54 +03:00
|
|
|
import { STORAGE_KEY_DRAFTS } from '~/constants'
|
2022-12-13 17:03:30 +03:00
|
|
|
import type { Draft, DraftMap } from '~/types'
|
2022-11-24 09:54:54 +03:00
|
|
|
|
2022-12-17 19:55:29 +03:00
|
|
|
export const currentUserDrafts = process.server ? computed<DraftMap>(() => ({})) : useUserLocalStorage<DraftMap>(STORAGE_KEY_DRAFTS, () => ({}))
|
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 {
|
|
|
|
const {
|
|
|
|
status = '',
|
|
|
|
inReplyToId,
|
|
|
|
visibility = 'public',
|
|
|
|
attachments = [],
|
2022-12-13 01:35:59 +03:00
|
|
|
initialText = '',
|
2022-11-29 23:45:20 +03:00
|
|
|
} = options
|
2022-11-30 07:50:29 +03:00
|
|
|
|
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,
|
2022-12-13 01:35:59 +03:00
|
|
|
initialText,
|
2022-11-24 14:35:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-05 01:10:10 +03:00
|
|
|
export async function getDraftFromStatus(status: Status, text?: null | string): Promise<Draft> {
|
2022-11-28 13:23:33 +03:00
|
|
|
return getDefaultDraft({
|
2022-12-05 01:10:10 +03:00
|
|
|
status: text || await convertMastodonHTML(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,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-12-13 01:35:59 +03:00
|
|
|
function mentionHTML(acct: string) {
|
|
|
|
return `<span data-type="mention" data-id="${acct}" contenteditable="false">@${acct}</span>`
|
|
|
|
}
|
|
|
|
|
2022-11-28 13:23:33 +03:00
|
|
|
export function getReplyDraft(status: Status) {
|
2022-12-13 16:49:22 +03:00
|
|
|
const accountsToMention: string[] = []
|
2022-12-13 01:35:59 +03:00
|
|
|
const userId = currentUser.value?.account.id
|
|
|
|
if (status.account.id !== userId)
|
2022-12-13 16:49:22 +03:00
|
|
|
accountsToMention.push(status.account.acct)
|
|
|
|
accountsToMention.push(...(status.mentions.filter(mention => mention.id !== userId).map(mention => mention.acct)))
|
2022-11-28 13:23:33 +03:00
|
|
|
return {
|
|
|
|
key: `reply-${status.id}`,
|
2022-12-13 01:35:59 +03:00
|
|
|
draft: () => {
|
|
|
|
return getDefaultDraft({
|
2022-12-13 16:49:22 +03:00
|
|
|
initialText: accountsToMention.map(acct => mentionHTML(acct)).join(' '),
|
2022-12-13 01:35:59 +03:00
|
|
|
inReplyToId: status!.id,
|
|
|
|
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,
|
2022-11-30 07:50:29 +03:00
|
|
|
initial: () => Draft = () => getDefaultDraft({}),
|
2022-11-28 13:23:33 +03:00
|
|
|
) {
|
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
|
|
|
}
|