feat: message i18n

This commit is contained in:
KUN1007 2023-08-30 19:38:37 +08:00
parent a8d017e373
commit 32cdbcbcba
5 changed files with 95 additions and 43 deletions

View file

@ -2,9 +2,9 @@ export interface EditCreateTopicRequestData {
title: string
content: string
time: number
tags: string
category: string
uid: string
tags: Array<string>
category: Array<string>
uid: number
}
export interface EditKUNGalgameTopic {

View file

@ -1,14 +1,25 @@
<script setup lang="ts">
import { onBeforeMount, ref } from 'vue'
import { Icon } from '@iconify/vue'
const { message, type} = defineProps([
'message',
'type',
])
// store
import { useKUNGalgameSettingsStore } from '@/store/modules/settings'
// 使 store
const settingsStore = useKUNGalgameSettingsStore()
const props = defineProps<{
messageCN: string
messageEN: string
type: `warn` | `success` | `error` | `info`
}>()
const message =
settingsStore.showKUNGalgameLanguage === 'en'
? props.messageEN
: props.messageCN
const messageClass = (type: string): string => {
if (type === 'warning') {
if (type === 'warn') {
return `animate__animated animate__headShake ${type}`
} else if (type === 'success') {
return `animate__animated animate__bounceInDown ${type}`
@ -25,7 +36,7 @@ const messageClass = (type: string): string => {
<template>
<div class="container">
<div class="message" :class="messageClass(type)">
<span class="icon" v-if="type === 'warning'"
<span class="icon" v-if="type === 'warn'"
><Icon icon="line-md:alert"
/></span>
<span class="icon" v-else-if="type === 'success'"
@ -62,7 +73,7 @@ const messageClass = (type: string): string => {
display: flex;
align-items: center;
justify-content: center;
padding: 10px 17px;
padding: 1vh 10vw;
box-shadow: var(--shadow);
z-index: 9999;
span {
@ -77,7 +88,7 @@ const messageClass = (type: string): string => {
margin-right: 17px;
}
.warning {
.warn {
border: 1px solid var(--kungalgame-yellow-3);
.icon {
color: var(--kungalgame-yellow-3);

View file

@ -1,17 +1,30 @@
import { render, h } from 'vue'
import Message from './KUNGalgameMessage.vue'
type MessageType = `warn` | `success` | `error` | `info`
/**
* @param {string} messageEN -
* @param {string} messageCN -
* @param {type} type - `warn` | `success` | `error` | `info`
* @param {number} duration - 3s
*/
export default (
message: string,
type: string,
duration: number
messageEN: string,
messageCN: string,
type: MessageType,
duration?: number
) => {
const messageNode = h(Message, {
message,
messageCN,
messageEN,
type,
duration,
})
const time = duration ? duration : 3000
const handleDestroy = () => {
// 从 body 上移除组件
render(null, document.body)
@ -19,7 +32,7 @@ export default (
setTimeout(() => {
handleDestroy()
}, duration)
}, time)
render(messageNode, document.body)
}

View file

@ -17,6 +17,11 @@ s {
text-decoration: line-through;
}
/* 下划线,不然会不显示 */
u {
text-decoration: underline;
}
/* 设置为 none不然黑夜模式代码块会花屏 */
* {
text-shadow: none;

View file

@ -1,6 +1,9 @@
<script setup lang="ts">
//
//
import { useKUNGalgameMessageStore } from '@/store/modules/message'
//
import message from '@/components/alert/Message'
// Vue
import { toRaw } from 'vue'
// store
import { useKUNGalgameEditStore } from '@/store/modules/edit'
@ -9,15 +12,35 @@ import { useKUNGalgamerStore } from '@/store/modules/kungalgamer'
import { storeToRefs } from 'pinia'
//
import { useRouter } from 'vue-router'
//
import { getPlainText } from '@/utils/getPlainText'
//
import {
EditCreateTopicRequestData,
EditCreateTopicResponseData,
} from '@/api/index'
const router = useRouter()
const topicData = storeToRefs(useKUNGalgameEditStore())
const message = useKUNGalgameMessageStore()
const messageStore = useKUNGalgameMessageStore()
const checkPublish = (topicData: EditCreateTopicRequestData) => {
if (!topicData.title.trim()) {
//
message('Title cannot be empty!', '标题不可为空!', 'warn')
return false
} else if (topicData.content.trim()) {
console.log(getPlainText(topicData.content.trim()).length)
return true
} else {
return true
}
}
const handlePublish = async () => {
const res = await message.alert('AlertInfo.edit.publish', true)
const res = await messageStore.alert('AlertInfo.edit.publish', true)
//
if (res) {
// storeToRefs vue ref reactive toRaw
@ -28,21 +51,20 @@ const handlePublish = async () => {
title: rawData.title,
content: rawData.content,
time: Date.now(),
tags: JSON.stringify(rawData.tags),
category: JSON.stringify(rawData.category),
uid: useKUNGalgamerStore().uid.toString(),
tags: rawData.tags,
category: rawData.category,
uid: useKUNGalgamerStore().uid,
}
//
if (checkPublish(topicToCreate)) {
//
const createdTopic = await useKUNGalgameEditStore().createNewTopic(
topicToCreate
)
const createdTopic: EditCreateTopicResponseData =
await useKUNGalgameEditStore().createNewTopic(topicToCreate)
// tid
const tid = createdTopic.data.tid
console.log(tid)
// push tid
router.push({
name: 'Topic',
@ -51,18 +73,19 @@ const handlePublish = async () => {
},
})
message.info('AlertInfo.edit.publishSuccess')
messageStore.info('AlertInfo.edit.publishSuccess')
// 使 pinia $reset
useKUNGalgameEditStore().$reset()
}
} else {
message.info('AlertInfo.edit.publishCancel')
messageStore.info('AlertInfo.edit.publishCancel')
}
}
const handleSave = () => {
// true
topicData.isSave.value = true
message.info('AlertInfo.edit.draft')
messageStore.info('AlertInfo.edit.draft')
}
</script>