添加自定义表情配置项

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' import { mergeConfig } from '@/utils'
export const DEFAULT_CONFIG = { export const DEFAULT_CONFIG = {
@ -19,7 +21,13 @@ export const DEFAULT_CONFIG = {
relayMessagesByServer: false, relayMessagesByServer: false,
autoTranslate: false, autoTranslate: false,
giftUsernamePronunciation: '' giftUsernamePronunciation: '',
emoticons: [] // [{ keyword: '', url: '' }, ...]
}
export function deepCloneDefaultConfig() {
return _.cloneDeep(DEFAULT_CONFIG)
} }
export function setLocalConfig(config) { export function setLocalConfig(config) {
@ -29,8 +37,32 @@ export function setLocalConfig(config) {
export function getLocalConfig() { export function getLocalConfig() {
try { 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 { } 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]) this.delMessages([id])
}, },
delMessages(ids) { delMessages(ids) {
this.enqueueMessages(ids.map(id => { this.enqueueMessages(ids.map(
return { id => ({
type: constants.MESSAGE_TYPE_DEL, type: constants.MESSAGE_TYPE_DEL,
id id
} })
})) ))
}, },
clearMessages() { clearMessages() {
this.messages = [] this.messages = []

View File

@ -154,8 +154,8 @@ export default {
loaderUrl: '' loaderUrl: ''
}, },
form: { 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 = { let query = {
...this.form, ...this.form,
emoticons: JSON.stringify(this.form.emoticons),
lang: this.$i18n.locale lang: this.$i18n.locale
} }
delete query.roomId delete query.roomId
@ -239,12 +240,19 @@ export default {
this.$message.error(this.$t('home.failedToParseConfig') + e) this.$message.error(this.$t('home.failedToParseConfig') + e)
return return
} }
cfg = mergeConfig(cfg, chatConfig.DEFAULT_CONFIG) this.importConfigFromObj(cfg)
this.form = { roomId: this.form.roomId, ...cfg }
} }
reader.readAsText(input.files[0]) reader.readAsText(input.files[0])
} }
input.click() 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() { data() {
return { return {
config: { ...chatConfig.DEFAULT_CONFIG }, config: chatConfig.deepCloneDefaultConfig(),
chatClient: null, chatClient: null,
pronunciationConverter: null pronunciationConverter: null
} }
@ -76,7 +76,7 @@ export default {
cfg[i] = this.strConfig[i] 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.minGiftPrice = toInt(cfg.minGiftPrice, chatConfig.DEFAULT_CONFIG.minGiftPrice)
cfg.showDanmaku = toBool(cfg.showDanmaku) cfg.showDanmaku = toBool(cfg.showDanmaku)
@ -85,16 +85,30 @@ export default {
cfg.mergeSimilarDanmaku = toBool(cfg.mergeSimilarDanmaku) cfg.mergeSimilarDanmaku = toBool(cfg.mergeSimilarDanmaku)
cfg.mergeGift = toBool(cfg.mergeGift) cfg.mergeGift = toBool(cfg.mergeGift)
cfg.maxNumber = toInt(cfg.maxNumber, chatConfig.DEFAULT_CONFIG.maxNumber) cfg.maxNumber = toInt(cfg.maxNumber, chatConfig.DEFAULT_CONFIG.maxNumber)
cfg.blockGiftDanmaku = toBool(cfg.blockGiftDanmaku) cfg.blockGiftDanmaku = toBool(cfg.blockGiftDanmaku)
cfg.blockLevel = toInt(cfg.blockLevel, chatConfig.DEFAULT_CONFIG.blockLevel) cfg.blockLevel = toInt(cfg.blockLevel, chatConfig.DEFAULT_CONFIG.blockLevel)
cfg.blockNewbie = toBool(cfg.blockNewbie) cfg.blockNewbie = toBool(cfg.blockNewbie)
cfg.blockNotMobileVerified = toBool(cfg.blockNotMobileVerified) cfg.blockNotMobileVerified = toBool(cfg.blockNotMobileVerified)
cfg.blockMedalLevel = toInt(cfg.blockMedalLevel, chatConfig.DEFAULT_CONFIG.blockMedalLevel) cfg.blockMedalLevel = toInt(cfg.blockMedalLevel, chatConfig.DEFAULT_CONFIG.blockMedalLevel)
cfg.relayMessagesByServer = toBool(cfg.relayMessagesByServer) cfg.relayMessagesByServer = toBool(cfg.relayMessagesByServer)
cfg.autoTranslate = toBool(cfg.autoTranslate) cfg.autoTranslate = toBool(cfg.autoTranslate)
cfg.emoticons = this.toObjIfJson(cfg.emoticons)
chatConfig.sanitizeConfig(cfg)
this.config = cfg this.config = cfg
}, },
toObjIfJson(str) {
if (typeof str !== 'string') {
return str
}
try {
return JSON.parse(str)
} catch {
return {}
}
},
initChatClient() { initChatClient() {
if (this.roomId === null) { if (this.roomId === null) {
this.chatClient = new ChatClientTest() this.chatClient = new ChatClientTest()
@ -201,9 +215,7 @@ export default {
this.$refs.renderer.addMessage(message) this.$refs.renderer.addMessage(message)
}, },
onDelSuperChat(data) { onDelSuperChat(data) {
for (let id of data.ids) { this.$refs.renderer.delMessages(data.ids)
this.$refs.renderer.delMessage(id)
}
}, },
onUpdateTranslation(data) { onUpdateTranslation(data) {
if (!this.config.autoTranslate) { if (!this.config.autoTranslate) {
@ -224,19 +236,25 @@ export default {
} else if (this.config.blockMedalLevel > 0 && data.medalLevel < this.config.blockMedalLevel) { } else if (this.config.blockMedalLevel > 0 && data.medalLevel < this.config.blockMedalLevel) {
return false return false
} }
return this.filterSuperChatMessage(data) return this.filterByContent(data.content) && this.filterByAuthorName(data.authorName)
}, },
filterSuperChatMessage(data) { 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) { for (let keyword of this.blockKeywords) {
if (data.content.indexOf(keyword) !== -1) { if (content.indexOf(keyword) !== -1) {
return false return false
} }
} }
return this.filterNewMemberMessage(data) return true
}, },
filterNewMemberMessage(data) { filterByAuthorName(authorName) {
for (let user of this.blockUsers) { for (let user of this.blockUsers) {
if (data.authorName === user) { if (authorName === user) {
return false return false
} }
} }