添加自定义表情配置项

This commit is contained in:
John Smith 2022-02-26 23:27:10 +08:00
parent 4924f15614
commit 3da8cc4227
4 changed files with 79 additions and 21 deletions

View File

@ -1,3 +1,5 @@
import _ from 'lodash'
import { mergeConfig } from '@/utils'
export const DEFAULT_CONFIG = {
@ -19,7 +21,13 @@ export const DEFAULT_CONFIG = {
relayMessagesByServer: false,
autoTranslate: false,
giftUsernamePronunciation: ''
giftUsernamePronunciation: '',
emoticons: [] // [{ keyword: '', url: '' }, ...]
}
export function deepCloneDefaultConfig() {
return _.cloneDeep(DEFAULT_CONFIG)
}
export function setLocalConfig(config) {
@ -29,8 +37,32 @@ export function setLocalConfig(config) {
export function getLocalConfig() {
try {
return mergeConfig(JSON.parse(window.localStorage.config), DEFAULT_CONFIG)
let config = JSON.parse(window.localStorage.config)
config = mergeConfig(config, deepCloneDefaultConfig())
sanitizeConfig(config)
return config
} catch {
return { ...DEFAULT_CONFIG }
return deepCloneDefaultConfig()
}
}
export function sanitizeConfig(config) {
let newEmoticons = []
if (config.emoticons instanceof Array) {
for (let emoticon of config.emoticons) {
try {
let newEmoticon = {
keyword: emoticon.keyword,
url: emoticon.url
}
if ((typeof newEmoticon.keyword !== 'string') || (typeof newEmoticon.url !== 'string')) {
continue
}
newEmoticons.push(newEmoticon)
} catch {
continue
}
}
}
config.emoticons = newEmoticons
}

View File

@ -211,12 +211,12 @@ export default {
this.delMessages([id])
},
delMessages(ids) {
this.enqueueMessages(ids.map(id => {
return {
this.enqueueMessages(ids.map(
id => ({
type: constants.MESSAGE_TYPE_DEL,
id
}
}))
})
))
},
clearMessages() {
this.messages = []

View File

@ -154,8 +154,8 @@ export default {
loaderUrl: ''
},
form: {
roomId: parseInt(window.localStorage.roomId || '1'),
...chatConfig.getLocalConfig()
...chatConfig.getLocalConfig(),
roomId: parseInt(window.localStorage.roomId || '1')
}
}
},
@ -205,6 +205,7 @@ export default {
let query = {
...this.form,
emoticons: JSON.stringify(this.form.emoticons),
lang: this.$i18n.locale
}
delete query.roomId
@ -239,12 +240,19 @@ export default {
this.$message.error(this.$t('home.failedToParseConfig') + e)
return
}
cfg = mergeConfig(cfg, chatConfig.DEFAULT_CONFIG)
this.form = { roomId: this.form.roomId, ...cfg }
this.importConfigFromObj(cfg)
}
reader.readAsText(input.files[0])
}
input.click()
},
importConfigFromObj(cfg) {
cfg = mergeConfig(cfg, chatConfig.deepCloneDefaultConfig())
chatConfig.sanitizeConfig(cfg)
this.form = {
...cfg,
roomId: this.form.roomId
}
}
}
}

View File

@ -30,7 +30,7 @@ export default {
},
data() {
return {
config: { ...chatConfig.DEFAULT_CONFIG },
config: chatConfig.deepCloneDefaultConfig(),
chatClient: null,
pronunciationConverter: null
}
@ -76,7 +76,7 @@ export default {
cfg[i] = this.strConfig[i]
}
}
cfg = mergeConfig(cfg, chatConfig.DEFAULT_CONFIG)
cfg = mergeConfig(cfg, chatConfig.deepCloneDefaultConfig())
cfg.minGiftPrice = toInt(cfg.minGiftPrice, chatConfig.DEFAULT_CONFIG.minGiftPrice)
cfg.showDanmaku = toBool(cfg.showDanmaku)
@ -85,16 +85,30 @@ export default {
cfg.mergeSimilarDanmaku = toBool(cfg.mergeSimilarDanmaku)
cfg.mergeGift = toBool(cfg.mergeGift)
cfg.maxNumber = toInt(cfg.maxNumber, chatConfig.DEFAULT_CONFIG.maxNumber)
cfg.blockGiftDanmaku = toBool(cfg.blockGiftDanmaku)
cfg.blockLevel = toInt(cfg.blockLevel, chatConfig.DEFAULT_CONFIG.blockLevel)
cfg.blockNewbie = toBool(cfg.blockNewbie)
cfg.blockNotMobileVerified = toBool(cfg.blockNotMobileVerified)
cfg.blockMedalLevel = toInt(cfg.blockMedalLevel, chatConfig.DEFAULT_CONFIG.blockMedalLevel)
cfg.relayMessagesByServer = toBool(cfg.relayMessagesByServer)
cfg.autoTranslate = toBool(cfg.autoTranslate)
cfg.emoticons = this.toObjIfJson(cfg.emoticons)
chatConfig.sanitizeConfig(cfg)
this.config = cfg
},
toObjIfJson(str) {
if (typeof str !== 'string') {
return str
}
try {
return JSON.parse(str)
} catch {
return {}
}
},
initChatClient() {
if (this.roomId === null) {
this.chatClient = new ChatClientTest()
@ -201,9 +215,7 @@ export default {
this.$refs.renderer.addMessage(message)
},
onDelSuperChat(data) {
for (let id of data.ids) {
this.$refs.renderer.delMessage(id)
}
this.$refs.renderer.delMessages(data.ids)
},
onUpdateTranslation(data) {
if (!this.config.autoTranslate) {
@ -224,19 +236,25 @@ export default {
} else if (this.config.blockMedalLevel > 0 && data.medalLevel < this.config.blockMedalLevel) {
return false
}
return this.filterSuperChatMessage(data)
return this.filterByContent(data.content) && this.filterByAuthorName(data.authorName)
},
filterSuperChatMessage(data) {
return this.filterByContent(data.content) && this.filterByAuthorName(data.authorName)
},
filterNewMemberMessage(data) {
return this.filterByAuthorName(data.authorName)
},
filterByContent(content) {
for (let keyword of this.blockKeywords) {
if (data.content.indexOf(keyword) !== -1) {
if (content.indexOf(keyword) !== -1) {
return false
}
}
return this.filterNewMemberMessage(data)
return true
},
filterNewMemberMessage(data) {
filterByAuthorName(authorName) {
for (let user of this.blockUsers) {
if (data.authorName === user) {
if (authorName === user) {
return false
}
}