fix: publish content

This commit is contained in:
Anthony Fu 2022-11-26 00:17:15 +08:00
parent f165eebed3
commit d177753775
3 changed files with 33 additions and 19 deletions

View file

@ -29,15 +29,8 @@ const { editor } = useTiptap({
onPaste: handlePaste, onPaste: handlePaste,
}) })
const status = $computed(() => {
return {
...draft.params,
mediaIds: draft.attachments.map(a => a.id),
} as CreateStatusParams
})
const currentVisibility = $computed(() => { const currentVisibility = $computed(() => {
return STATUS_VISIBILITIES.find(v => v.value === status.visibility) || STATUS_VISIBILITIES[0] return STATUS_VISIBILITIES.find(v => v.value === draft.params.visibility) || STATUS_VISIBILITIES[0]
}) })
let isUploading = $ref<boolean>(false) let isUploading = $ref<boolean>(false)
@ -97,16 +90,30 @@ function chooseVisibility(visibility: StatusVisibility) {
} }
async function publish() { async function publish() {
const payload = {
...draft.params,
status: htmlToText(draft.params.status || ''),
mediaIds: draft.attachments.map(a => a.id),
} as CreateStatusParams
if (process.dev) { if (process.dev) {
alert(JSON.stringify(draft.params, null, 2)) // eslint-disable-next-line no-console
return console.info({
raw: draft.params.status,
...payload,
})
const result = confirm('[DEV] Payload logged to console, do you want to publish it?')
if (!result)
return
} }
try { try {
isSending = true isSending = true
if (!draft.editingStatus) if (!draft.editingStatus)
await masto.statuses.create(status) await masto.statuses.create(payload)
else else
await masto.statuses.update(draft.editingStatus.id, status) await masto.statuses.update(draft.editingStatus.id, payload)
draft = getDefaultDraft({ inReplyToId }) draft = getDefaultDraft({ inReplyToId })
isPublishDialogOpen.value = false isPublishDialogOpen.value = false

View file

@ -68,7 +68,7 @@ const timeago = useTimeAgo(() => status.createdAt, {
</script> </script>
<template> <template>
<div ref="el" flex flex-col gap-2 px-4 transition-100 cursor-pointer :class="{ 'hover:bg-active': hover }" @click="onclick"> <div ref="el" flex flex-col gap-2 px-4 transition-100 :class="{ 'hover:bg-active': hover }" @click="onclick">
<div v-if="rebloggedBy" pl8> <div v-if="rebloggedBy" pl8>
<div flex="~ wrap" gap-1 items-center text-gray:75 text-sm> <div flex="~ wrap" gap-1 items-center text-gray:75 text-sm>
<div i-ri:repeat-fill mr-1 /> <div i-ri:repeat-fill mr-1 />

View file

@ -104,13 +104,15 @@ export function treeToVNode(
return null return null
} }
function htmlToText(html: string) { export function htmlToText(html: string) {
const tree = parseFragment(html) const tree = parseFragment(html)
return tree.childNodes.map(n => treeToText(n)).join('') return tree.childNodes.map(n => treeToText(n)).join('').trim()
} }
function treeToText(input: Node): string { function treeToText(input: Node): string {
let pre = '' let pre = ''
let body = ''
let post = ''
if (input.nodeName === '#text') if (input.nodeName === '#text')
// @ts-expect-error casing // @ts-expect-error casing
@ -119,11 +121,16 @@ function treeToText(input: Node): string {
if (input.nodeName === 'br') if (input.nodeName === 'br')
return '\n' return '\n'
if (input.nodeName === 'p') if (['p', 'pre'].includes(input.nodeName))
pre = '\n' pre = '\n'
if ('childNodes' in input) if (input.nodeName === 'code') {
return pre + input.childNodes.map(n => treeToText(n)).join('') pre = '`'
post = '`'
}
return pre if ('childNodes' in input)
body = input.childNodes.map(n => treeToText(n)).join('')
return pre + body + post
} }