/
PostBind.vue
168 lines (153 loc) · 5.8 KB
/
PostBind.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<!--
- 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 { usePublishSettingStore } from "~/src/stores/usePublishSettingStore.ts"
import { createAppLogger } from "~/src/utils/appLogger.ts"
import { getSiyuanPageId } from "~/src/utils/siyuanUtils.ts"
import { Utils } from "~/src/utils/utils.ts"
const logger = createAppLogger("post-bind")
// uses
const { t } = useVueI18n()
const { query } = useRoute()
const { getPublishCfg } = usePublishConfig()
const { updateSetting } = usePublishSettingStore()
// datas
const ruleFormRef = ref()
const ruleForm = reactive({})
const rules = reactive<FormRules>({})
const formData = reactive({
pageId: "",
dynamicConfigArray: [] as DynamicConfig[],
postIdMap: {} as any,
})
// methods
const submitForm = async (formEl: any) => {
if (StrUtil.isEmptyString(formData.pageId)) {
ElMessage.error("")
return
}
try {
const publishCfg = await getPublishCfg()
const setting = publishCfg.setting
const postMeta = ObjectUtil.getProperty(setting, formData.pageId, {})
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(formData.pageId)) {
postMeta[posidKey] = postid
}
return item
})
setting[formData.pageId] = postMeta
logger.debug("prepare to save setting =>", { setting: toRaw(setting) })
await updateSetting(setting)
ElMessage.success(t("main.opt.success"))
} catch (e) {
logger.error(t("main.opt.failure") + "=>", e)
ElMessage.error(t("main.opt.failure") + "=>" + e)
}
}
// lifecycles
onMounted(async () => {
const id = Utils.emptyOrDefault(query.id as string, getWidgetId())
const siyuanPageId = await getSiyuanPageId(id)
formData.pageId = siyuanPageId
const publishCfg = await getPublishCfg()
const setting = publishCfg.setting
formData.dynamicConfigArray = publishCfg.dynamicConfigArray
const postMeta = ObjectUtil.getProperty(setting, formData.pageId, {})
formData.dynamicConfigArray.forEach((item: DynamicConfig) => {
let postid = ""
const cfg = ObjectUtil.getProperty(setting, item.platformKey, {})
const posidKey = cfg?.posidKey
if (!StrUtil.isEmptyString(posidKey) && !StrUtil.isEmptyString(formData.pageId)) {
postid = ObjectUtil.getProperty(postMeta, posidKey)
}
formData.postIdMap[item.platformKey] = postid
})
})
</script>
<template>
<div>
<el-alert class="top-tip" :title="t('post.bind.auto.tips')" type="error" :closable="false" />
<el-form-item>
<el-input v-model="formData.pageId" placeholder="请输入需要修复的文档根 ID" />
</el-form-item>
<el-divider border-style="dashed" />
<div v-if="StrUtil.isEmptyString(formData.pageId)">
<el-alert class="top-tip" :title="t('post.bind.auto.error')" type="warning" :closable="false" />
</div>
<el-form
v-else
label-width="85px"
class="post-bind-form"
ref="ruleFormRef"
:model="ruleForm"
:rules="rules"
status-icon
>
<el-alert
class="top-tip"
:title="'将为文章「' + formData.pageId + '」进行修复'"
type="warning"
:closable="false"
/>
<!-- 动态配置 -->
<div v-if="formData.dynamicConfigArray.length > 0">
<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>
</div>
<div v-else>
<el-alert class="top-tip" :title="t('post.bind.auto.empty')" type="info" :closable="false" />
</div>
<el-form-item>
<el-button type="primary" @click="submitForm(ruleFormRef)">{{ t("post.bind.conf.save") }}</el-button>
<el-button type="warning">{{ t("post.bind.conf.v081") }}</el-button>
</el-form-item>
</el-form>
</div>
</template>
<style scoped lang="stylus">
.post-bind-form
padding 0 10px
padding-left 0
.top-tip
margin 10px 0
</style>