You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
paopao-ce/web/src/components/post-item.vue

390 lines
11 KiB

<template>
<div class="post-item" @click="goPostDetail(post.id)">
<n-thing content-indented>
<template #avatar>
<n-avatar round :size="30" :src="post.user.avatar" />
</template>
<template #header>
<span class="nickname-wrap">
<router-link
@click.stop
class="username-link"
:to="{
name: 'user',
query: { s: post.user.username },
}"
>
{{ post.user.nickname }}
</router-link>
</span>
<span class="username-wrap"> @{{ post.user.username }} </span>
<n-tag
v-if="post.is_top"
class="top-tag"
type="warning"
size="small"
round
>
</n-tag>
<n-tag
v-if="post.visibility == 1"
class="top-tag"
type="error"
size="small"
round
>
</n-tag>
<n-tag
v-if="post.visibility == 2"
class="top-tag"
type="info"
size="small"
round
>
</n-tag>
</template>
<template #header-extra>
<div class="item-header-extra">
<span class="timestamp">
{{ post.ip_loc ? post.ip_loc + ' · ' : post.ip_loc }}
{{ formatPrettyDate(post.created_on) }}
</span>
<n-dropdown
placement="bottom-end"
trigger="hover"
size="small"
:options="tweetOptions"
@select="handleTweetAction"
>
<n-button quaternary circle>
<template #icon>
<n-icon>
<more-horiz-filled />
</n-icon>
</template>
</n-button>
</n-dropdown>
</div>
</template>
<template #description v-if="post.texts.length > 0">
<span
v-for="content in post.texts"
:key="content.id"
class="post-text hover"
@click.stop="doClickText($event, post.id)"
v-html="parsePostTag(content.content).content"
></span>
</template>
<template #footer>
<post-attachment
v-if="post.attachments.length > 0"
:attachments="post.attachments" />
<post-attachment
v-if="post.charge_attachments.length > 0"
:attachments="post.charge_attachments"
:price="post.attachment_price"
/>
<post-image
v-if="post.imgs.length > 0"
:imgs="post.imgs" />
<post-video
v-if="post.videos.length > 0"
:videos="post.videos" />
<post-link
v-if="post.links.length > 0"
:links="post.links" />
</template>
<template #action>
<n-space justify="space-between">
<div class="opt-item hover" @click.stop="handlePostStar">
<n-icon size="18" class="opt-item-icon">
<heart-outline />
</n-icon>
{{ post.upvote_count }}
</div>
<div class="opt-item hover" @click.stop="goPostDetail(post.id)">
<n-icon size="18" class="opt-item-icon">
<chatbox-outline />
</n-icon>
{{ post.comment_count }}
</div>
<div class="opt-item hover" @click.stop="handlePostCollection">
<n-icon size="18" class="opt-item-icon">
<bookmark-outline />
</n-icon>
{{ post.collection_count }}
</div>
</n-space>
</template>
</n-thing>
</div>
</template>
<script setup lang="ts">
import { h, computed } from 'vue';
import { useStore } from 'vuex';
import { useRouter } from 'vue-router';
import { NIcon } from 'naive-ui'
import type { Component } from 'vue'
import type { DropdownOption } from 'naive-ui';
import { formatPrettyDate } from '@/utils/formatTime';
import { parsePostTag } from '@/utils/content';
import {
postStar,
postCollection,
} from '@/api/post';
import {
PaperPlaneOutline,
HeartOutline,
BookmarkOutline,
ChatboxOutline,
ShareSocialOutline,
} from '@vicons/ionicons5';
import { MoreHorizFilled } from '@vicons/material';
import copy from "copy-to-clipboard";
const router = useRouter();
const store = useStore();
const props = withDefaults(defineProps<{
post: Item.PostProps,
}>(), {});
const emit = defineEmits<{
(e: 'send-whisper', user: Item.UserInfo): void;
}>();
const renderIcon = (icon: Component) => {
return () => {
return h(NIcon, null, {
default: () => h(icon)
})
}
};
const tweetOptions = computed(() => {
let options: DropdownOption[] = [];
// TODO: f*k 为什么这里会卡?
// if (store.state.userinfo.id > 0) {
// options.push({
// label: '私信',
// key: 'whisper',
// icon: renderIcon(PaperPlaneOutline)
// });
// }
options.push({
label: '',
key: 'whisper',
icon: renderIcon(PaperPlaneOutline)
});
options.push({
label: '',
key: 'copyTweetLink',
icon: renderIcon(ShareSocialOutline),
});
return options;
});
const handleTweetAction = async (
item: 'copyTweetLink' | 'whisper'
) => {
switch (item) {
case 'copyTweetLink':
copy(`${window.location.origin}/#/post?id=${post.value.id}&share=copy_link&t=${new Date().getTime()}`);
window.$message.success('');
break;
case 'whisper':
emit('send-whisper', props.post.user);
break;
default:
break;
}
};
const post = computed({
get: () => {
let post: Item.PostComponentProps = Object.assign(
{
texts: [],
imgs: [],
videos: [],
links: [],
attachments: [],
charge_attachments: [],
},
props.post
);
post.contents.map((content) => {
if (+content.type === 1 || +content.type === 2) {
post.texts.push(content);
}
if (+content.type === 3) {
post.imgs.push(content);
}
if (+content.type === 4) {
post.videos.push(content);
}
if (+content.type === 6) {
post.links.push(content);
}
if (+content.type === 7) {
post.attachments.push(content);
}
if (+content.type === 8) {
post.charge_attachments.push(content);
}
});
return post;
},
set: (newVal) => {
props.post.upvote_count = newVal.upvote_count;
props.post.collection_count = newVal.collection_count;
},
});
const handlePostStar = () => {
postStar({
id: post.value.id,
})
.then((res) => {
if (res.status) {
post.value = {
...post.value,
upvote_count: post.value.upvote_count + 1,
};
} else {
post.value = {
...post.value,
upvote_count: post.value.upvote_count > 0 ? post.value.upvote_count - 1 : 0,
};
}
})
.catch((err) => {
console.log(err);
});
};
const handlePostCollection = () => {
postCollection({
id: post.value.id,
})
.then((res) => {
if (res.status) {
post.value = {
...post.value,
collection_count: post.value.collection_count + 1,
};
} else {
post.value = {
...post.value,
collection_count: post.value.collection_count > 0 ? post.value.collection_count - 1 : 0,
};
}
})
.catch((err) => {
console.log(err);
});
};
const goPostDetail = (id: number) => {
router.push({
name: 'post',
query: {
id,
},
});
};
const doClickText = (e: MouseEvent, id: number) => {
if ((e.target as any).dataset.detail) {
const d = (e.target as any).dataset.detail.split(':');
if (d.length === 2) {
store.commit('refresh');
if (d[0] === 'tag') {
router.push({
name: 'home',
query: {
q: d[1],
t: 'tag',
},
});
} else {
router.push({
name: 'user',
query: {
s: d[1],
},
});
}
return;
}
}
goPostDetail(id);
};
</script>
<style lang="less">
.post-item {
width: 100%;
padding: 16px;
box-sizing: border-box;
.nickname-wrap {
font-size: 14px;
}
.username-wrap {
font-size: 14px;
opacity: 0.75;
}
.top-tag {
transform: scale(0.75);
}
.item-header-extra {
display: flex;
align-items: center;
opacity: 0.75;
.timestamp {
font-size: 12px;
}
}
.post-text {
text-align: justify;
overflow: hidden;
white-space: pre-wrap;
word-break: break-all;
}
.opt-item {
display: flex;
align-items: center;
opacity: 0.7;
.opt-item-icon {
margin-right: 10px;
}
}
&:hover {
background: #f7f9f9;
}
&.hover {
cursor: pointer;
}
.n-thing-avatar {
margin-top: 0;
}
.n-thing-header {
line-height: 16px;
margin-bottom: 8px !important;
}
}
.dark {
.post-item {
&:hover {
background: #18181c;
}
background-color: rgba(16, 16, 20, 0.75);
}
}
</style>