Skip to content

Commit

Permalink
feat: 支持文章绑定
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed Aug 7, 2023
1 parent a230ff0 commit e3a92d0
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 17 deletions.
7 changes: 6 additions & 1 deletion siyuan/invoke/widgetInvoke.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,12 @@ export class WidgetInvoke {
}

public async showPublisherGeneralSettingDialog() {
await this.showPage("/setting/general")
let pageId: string | undefined = PageUtil.getPageId()
if (pageId == "") {
pageId = undefined
}
this.logger.debug("pageId=>", pageId)
await this.showPage(`/setting/general?id=${pageId}`)
}

private async showPage(pageUrl: string, isReload?: boolean, w?: string, h?: string, noscroll?: boolean) {
Expand Down
17 changes: 11 additions & 6 deletions src/components/set/GeneralSetting.vue
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,18 @@
-->

<script setup lang="ts">
import ChangeLocal from "~/src/components/set/preference/ChangeLocal.vue"
import { useVueI18n } from "~/src/composables/useVueI18n.ts"
const { t } = useVueI18n()
</script>

<template>
<div class="general-setting">
<change-local />
</div>
<el-tabs tab-position="left">
<el-tab-pane :label="t('service.tab.change.local')">
<change-local />
</el-tab-pane>
<el-tab-pane :label="t('service.tab.post.bind')">
<post-bind />
</el-tab-pane>
</el-tabs>
</template>

<style scoped></style>
124 changes: 124 additions & 0 deletions src/components/set/preference/PostBind.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<!--
- Copyright (c) 2023, Terwer . All rights reserved.
- DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
-
- This code is free software; you can redistribute it and/or modify it
- under the terms of the GNU General Public License version 2 only, as
- published by the Free Software Foundation. Terwer designates this
- particular file as subject to the "Classpath" exception as provided
- by Terwer in the LICENSE file that accompanied this code.
-
- This code is distributed in the hope that it will be useful, but WITHOUT
- ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
- version 2 for more details (a copy is included in the LICENSE file that
- accompanied this code).
-
- You should have received a copy of the GNU General Public License version
- 2 along with this work; if not, write to the Free Software Foundation,
- Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
-
- Please contact Terwer, Shenzhen, Guangdong, China, youweics@163.com
- or visit www.terwer.space if you need additional information or have any
- questions.
-->

<script setup lang="ts">
import { onMounted, reactive, ref, toRaw } from "vue"
import { ElMessage, FormRules } from "element-plus"
import { usePublishConfig } from "~/src/composables/usePublishConfig.ts"
import { DynamicConfig } from "~/src/platforms/dynamicConfig.ts"
import { useVueI18n } from "~/src/composables/useVueI18n.ts"
import { ObjectUtil, StrUtil } from "zhi-common"
import { useRoute } from "vue-router"
import { getWidgetId } from "~/src/utils/widgetUtils.ts"
import { useSettingStore } from "~/src/stores/useSettingStore.ts"
import { createAppLogger } from "~/src/utils/appLogger.ts"
const logger = createAppLogger("post-bind")
// uses
const { t } = useVueI18n()
const { query } = useRoute()
const { getPublishCfg } = usePublishConfig()
const { updateSetting } = useSettingStore()
// datas
const id = (query.id ?? getWidgetId()) as string
const ruleFormRef = ref()
const ruleForm = reactive({})
const rules = reactive<FormRules>({})
const formData = reactive({
dynamicConfigArray: [] as DynamicConfig[],
postIdMap: {} as any,
})
// methods
const submitForm = async (formEl: any) => {
if (StrUtil.isEmptyString(id)) {
ElMessage.error("")
return
}
try {
const publishCfg = await getPublishCfg()
const setting = publishCfg.setting
const postMeta = ObjectUtil.getProperty(setting, id, {})
formData.dynamicConfigArray = formData.dynamicConfigArray.map((item: DynamicConfig) => {
const postid = formData.postIdMap[item.platformKey]
const cfg = ObjectUtil.getProperty(setting, item.platformKey, {})
const posidKey = cfg?.posidKey
if (!StrUtil.isEmptyString(posidKey) && !StrUtil.isEmptyString(id)) {
postMeta[posidKey] = postid
}
return item
})
setting[id] = postMeta
logger.debug("prepare to save setting =>", { setting: toRaw(setting) })
await updateSetting(setting)
ElMessage.success(t("main.opt.success"))
} catch (e) {
logger.error(e)
ElMessage.error(t("main.opt.failure") + "=>" + e)
}
}
// lifecycles
onMounted(async () => {
const publishCfg = await getPublishCfg()
const setting = publishCfg.setting
formData.dynamicConfigArray = publishCfg.dynamicConfigArray
const postMeta = ObjectUtil.getProperty(setting, id, {})
formData.dynamicConfigArray.forEach((item: DynamicConfig) => {
let postid = ""
const cfg = ObjectUtil.getProperty(setting, item.platformKey, {})
const posidKey = cfg?.posidKey
if (!StrUtil.isEmptyString(posidKey) && !StrUtil.isEmptyString(id)) {
postid = ObjectUtil.getProperty(postMeta, posidKey)
}
formData.postIdMap[item.platformKey] = postid
})
})
</script>

<template>
<el-form label-width="85px" class="post-bind-form" ref="ruleFormRef" :model="ruleForm" :rules="rules" status-icon>
<!-- 动态配置 -->
<el-form-item
v-for="(cfg, index) in formData.dynamicConfigArray"
:key="index"
:label="cfg.platformName"
v-show="cfg.isEnabled && cfg.isEnabled"
>
<el-input v-model="formData.postIdMap[cfg.platformKey]" />
</el-form-item>

<el-form-item>
<el-button type="primary" @click="submitForm(ruleFormRef)">{{ t("post.bind.conf.save") }} </el-button>
</el-form-item>
</el-form>
</template>

<style scoped lang="stylus"></style>
30 changes: 20 additions & 10 deletions src/composables/usePublishConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import { CommonblogConfig } from "~/src/adaptors/api/base/CommonblogConfig.ts"
* @since 1.3.2
*/
const usePublishConfig = () => {
const { getSetting, updateSetting } = useSettingStore()
const { getSetting } = useSettingStore()
const appInstance = new AppInstance()

/**
Expand All @@ -50,22 +50,32 @@ const usePublishConfig = () => {
* @param {string} key - 配置键名
* @returns {Promise<IPublishCfg>} - 返回一个 Promise 对象,包含发布配置项
*/
const getPublishCfg = async (key: string): Promise<IPublishCfg> => {
const getPublishCfg = async (key?: string): Promise<IPublishCfg> => {
// 加载配置
const setting = await getSetting()

// 平台配置
const cfg = JsonUtil.safeParse<any>(setting[key], {} as any)

// 平台定义
const dynJsonCfg = JsonUtil.safeParse<DynamicJsonCfg>(setting[DYNAMIC_CONFIG_KEY], {} as DynamicJsonCfg)
const dynamicConfigArray = dynJsonCfg?.totalCfg || []
const dynCfg = getDynCfgByKey(dynamicConfigArray, key)

return {
setting,
cfg,
dynCfg,
// 平台配置
if (key) {
return {
setting,
dynamicConfigArray,
cfg: undefined,
dynCfg: undefined,
}
} else {
const cfg = JsonUtil.safeParse<any>(setting[key], {} as any)
const dynCfg = getDynCfgByKey(dynamicConfigArray, key)

return {
setting,
dynamicConfigArray,
cfg,
dynCfg,
}
}
}

Expand Down
1 change: 1 addition & 0 deletions src/composables/useVueRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ const routes: RouteRecordRaw[] = [
path: "/setting/platform/single/:key",
component: SettingEntry,
},
// /?id=<id>
{
path: "/setting/general",
component: GeneralSetting,
Expand Down
5 changes: 5 additions & 0 deletions src/types/IPublishCfg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ interface IPublishCfg {
*/
setting: typeof SypConfig

/**
* 平台配置集合
*/
dynamicConfigArray: DynamicConfig[]

/**
* CommonblogConfig 类型的配置对象
*
Expand Down

0 comments on commit e3a92d0

Please sign in to comment.