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 title: string
content: string content: string
time: number time: number
tags: string tags: Array<string>
category: string category: Array<string>
uid: string uid: number
} }
export interface EditKUNGalgameTopic { export interface EditKUNGalgameTopic {

View file

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

View file

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

View file

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

View file

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