Skip to content

Commit

Permalink
fix(ui/conf): avoid some conf overrides frontend from the backend (#449)
Browse files Browse the repository at this point in the history
Signed-off-by: qwqcode <[email protected]>
  • Loading branch information
qwqcode authored Mar 7, 2023
1 parent a677443 commit 155a7f9
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
13 changes: 2 additions & 11 deletions ui/packages/artalk/src/api/system.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import ArtalkConfig from '~/types/artalk-config'
import ApiBase from './api-base'
import { handleBackendRefConf } from '../config'

/**
* 系统 API
Expand All @@ -10,17 +11,7 @@ export default class SystemApi extends ApiBase {
const data = await this.POST<any>(`/conf`)
const conf = (data.frontend_conf || {}) as ArtalkConfig

// Patch: `emoticons` config string to json
if (conf.emoticons && typeof conf.emoticons === "string") {
conf.emoticons = conf.emoticons.trim()
if (conf.emoticons.startsWith("[") || conf.emoticons.startsWith("{")) {
conf.emoticons = JSON.parse(conf.emoticons) // pase json
} else if (conf.emoticons === "false") {
conf.emoticons = false
}
}

return conf
return handleBackendRefConf(conf)
}

/** 获取配置数据 */
Expand Down
21 changes: 21 additions & 0 deletions ui/packages/artalk/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,24 @@ export function handelBaseConf(customConf: Partial<ArtalkConfig>): ArtalkConfig

return conf
}

export function handleBackendRefConf(conf: Partial<ArtalkConfig>) {
const DisabledKeys: (keyof ArtalkConfig)[] = [
'el', 'pageKey', 'pageTitle', 'server', 'site', 'darkMode'
]
Object.keys(conf).forEach(k => {
if (DisabledKeys.includes(k as any)) delete conf[k]
})

// Patch: `emoticons` config string to json
if (conf.emoticons && typeof conf.emoticons === "string") {
conf.emoticons = conf.emoticons.trim()
if (conf.emoticons.startsWith("[") || conf.emoticons.startsWith("{")) {
conf.emoticons = JSON.parse(conf.emoticons) // pase json
} else if (conf.emoticons === "false") {
conf.emoticons = false
}
}

return conf
}
4 changes: 3 additions & 1 deletion ui/packages/artalk/src/list/list-lite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import PgHolder, { TPgMode } from './paginator'
import * as ListNest from './list-nest'
import * as ListUi from './list-ui'
import { backendMinVersion, version as ARTALK_VERSION } from '../../package.json'
import { handleBackendRefConf } from '../config'

export default class ListLite extends Component {
/** 列表评论集区域元素 */
Expand Down Expand Up @@ -191,7 +192,8 @@ export default class ListLite extends Component {
// 装载后端提供的配置
if (!this.confLoaded) {
// 仅应用一次配置
if (this.conf.useBackendConf) this.ctx.updateConf(data.conf.frontend_conf)
const backendRefConf = handleBackendRefConf(data.conf.frontend_conf)
if (this.conf.useBackendConf) this.ctx.updateConf(backendRefConf)
else this.ctx.updateConf({}) // 让事件监听 `on('conf-loaded')` 有效,与前者保持相同生命周期环节
this.confLoaded = true
}
Expand Down

0 comments on commit 155a7f9

Please sign in to comment.