fix: preserve mentions when replying (#404)

This commit is contained in:
patak 2022-12-12 23:35:59 +01:00 committed by GitHub
parent 96fbeb235e
commit b61afaab9e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 6 deletions

View file

@ -34,10 +34,16 @@ const { editor } = useTiptap({
get: () => draft.params.status, get: () => draft.params.status,
set: newVal => draft.params.status = newVal, set: newVal => draft.params.status = newVal,
}), }),
placeholder: computed(() => placeholder || draft.params.inReplyToId ? t('placeholder.replying') : t('placeholder.default_1')), placeholder: computed(() => placeholder ?? draft.params.inReplyToId ? t('placeholder.replying') : t('placeholder.default_1')),
autofocus: shouldExpanded, autofocus: shouldExpanded,
onSubmit: publish, onSubmit: publish,
onFocus() { isExpanded = true }, onFocus() {
if (!isExpanded && draft.initialText) {
editor.value?.chain().insertContent(`${draft.initialText} `).focus('end').run()
draft.initialText = ''
}
isExpanded = true
},
onPaste: handlePaste, onPaste: handlePaste,
}) })

View file

@ -4,6 +4,7 @@ import type { Mutable } from '~/types/utils'
export interface Draft { export interface Draft {
editingStatus?: Status editingStatus?: Status
initialText?: string
params: Omit<Mutable<CreateStatusParams>, 'status'> & { params: Omit<Mutable<CreateStatusParams>, 'status'> & {
status?: Exclude<CreateStatusParams['status'], null> status?: Exclude<CreateStatusParams['status'], null>
} }
@ -28,6 +29,7 @@ export function getDefaultDraft(options: Partial<Draft['params'] & Omit<Draft, '
inReplyToId, inReplyToId,
visibility = 'public', visibility = 'public',
attachments = [], attachments = [],
initialText = '',
} = options } = options
return { return {
@ -37,6 +39,7 @@ export function getDefaultDraft(options: Partial<Draft['params'] & Omit<Draft, '
visibility, visibility,
}, },
attachments, attachments,
initialText,
} }
} }
@ -49,13 +52,25 @@ export async function getDraftFromStatus(status: Status, text?: null | string):
}) })
} }
function mentionHTML(acct: string) {
return `<span data-type="mention" data-id="${acct}" contenteditable="false">@${acct}</span>`
}
export function getReplyDraft(status: Status) { export function getReplyDraft(status: Status) {
const acountsToMention: string[] = []
const userId = currentUser.value?.account.id
if (status.account.id !== userId)
acountsToMention.push(status.account.acct)
acountsToMention.push(...(status.mentions.filter(mention => mention.id !== userId).map(mention => mention.acct)))
return { return {
key: `reply-${status.id}`, key: `reply-${status.id}`,
draft: () => getDefaultDraft({ draft: () => {
inReplyToId: status!.id, return getDefaultDraft({
visibility: status.visibility, initialText: acountsToMention.map(acct => mentionHTML(acct)).join(' '),
}), inReplyToId: status!.id,
visibility: status.visibility,
})
},
} }
} }