Skip to content

Commit

Permalink
🐛 Fix: can't add new config for picbed
Browse files Browse the repository at this point in the history
ISSUES CLOSED: #1184
  • Loading branch information
Molunerfinn committed Oct 22, 2023
1 parent cd07b33 commit 050a3dd
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 23 deletions.
9 changes: 5 additions & 4 deletions src/main/events/picgoCoreIPC.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ import {
OPEN_WINDOW,
GET_LANGUAGE_LIST,
SET_CURRENT_LANGUAGE,
GET_CURRENT_LANGUAGE
GET_CURRENT_LANGUAGE,
GET_PICBED_CONFIG
} from '#/events/constants'

import { GalleryDB } from 'apis/core/datastore'
Expand Down Expand Up @@ -223,14 +224,14 @@ const handleNPMError = (): IDispose => {
}

const handleGetPicBedConfig = () => {
ipcMain.on('getPicBedConfig', (event: IpcMainEvent, type: string) => {
ipcMain.on(GET_PICBED_CONFIG, (event: IpcMainEvent, type: string) => {
const name = picgo.helper.uploader.get(type)?.name || type
if (picgo.helper.uploader.get(type)?.config) {
const _config = picgo.helper.uploader.get(type)!.config!(picgo)
const config = handleConfigWithFunction(_config)
event.sender.send('getPicBedConfig', config, name)
event.sender.send(GET_PICBED_CONFIG, config, name)
} else {
event.sender.send('getPicBedConfig', [], name)
event.sender.send(GET_PICBED_CONFIG, [], name)
}
})
}
Expand Down
20 changes: 10 additions & 10 deletions src/renderer/components/ConfigForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,7 @@ const $route = useRoute()
const $form = ref<IFormInstance>()
const configList = ref<IPicGoPluginConfig[]>([])
const formModel = reactive<IStringKeyMap>({})
watch(toRefs(props.config), (val: IPicGoPluginConfig[]) => {
handleConfig(val)
}, {
deep: true,
immediate: true
})
const isUploader = props.type === 'uploader'
async function validate (): Promise<IStringKeyMap | false> {
const res = await $form.value?.validate() || false
Expand All @@ -74,14 +68,13 @@ function getConfigType () {
}
}
const isUploader = props.type === 'uploader'
const handleConfigForm = useConfigForm()
async function handleConfig (inputConfig: IPicGoPluginConfig[]) {
const currentConfig = await getCurConfigFormData()
const resetConfig = isUploader && !$route.params.configId
Object.assign(formModel, currentConfig)
configList.value = handleConfigForm(inputConfig, formModel, currentConfig)
configList.value = handleConfigForm(inputConfig, formModel, currentConfig, resetConfig)
}
async function getCurConfigFormData () {
Expand All @@ -101,6 +94,13 @@ async function resetConfig () {
Object.assign(formModel, config)
}
watch(toRefs(props.config), (val: IPicGoPluginConfig[]) => {
handleConfig(val)
}, {
deep: true,
immediate: true
})
defineExpose({
validate,
getConfigType,
Expand Down
4 changes: 3 additions & 1 deletion src/renderer/hooks/useConfigForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import { cloneDeep, union } from 'lodash'
* @param configList origin config list
* @param formModel el-form form model for default value
* @param currentConfig current config
* @param resetConfigForm reset form model -> clear default value
* @returns transformed config list
*/
export const useConfigForm = () => {
return (configList: IPicGoPluginConfig[], formModel: IStringKeyMap, currentConfig?: IStringKeyMap) => {
return (configList: IPicGoPluginConfig[], formModel: IStringKeyMap, currentConfig?: IStringKeyMap, resetConfigForm?: boolean) => {
if (configList.length > 0) {
return cloneDeep(configList).map((item) => {
// if (!configId) return item
if (resetConfigForm) return item
let defaultValue = item.default !== undefined
? item.default
: item.type === 'checkbox'
Expand Down
14 changes: 6 additions & 8 deletions src/renderer/pages/picbeds/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,17 @@
</template>
<script lang="ts" setup>
import { IRPCActionType } from '~/universal/types/enum'
import { ref, onBeforeUnmount, onBeforeMount } from 'vue'
import { ref, onBeforeMount } from 'vue'
import { T as $T } from '@/i18n/index'
import { sendToMain, triggerRPC } from '@/utils/dataSender'
import { useRoute, useRouter } from 'vue-router'
import ConfigForm from '@/components/ConfigForm.vue'
// import mixin from '@/utils/ConfirmButtonMixin'
import {
ipcRenderer,
IpcRendererEvent
} from 'electron'
import { GET_PICBED_CONFIG } from '~/universal/events/constants'
import { useIPCOn } from '@/hooks/useIPC'
const type = ref('')
const config = ref<IPicGoPluginConfig[]>([])
const picBedName = ref('')
Expand All @@ -61,9 +62,10 @@ const $router = useRouter()
const $configForm = ref<InstanceType<typeof ConfigForm> | null>(null)
type.value = $route.params.type as string
useIPCOn(GET_PICBED_CONFIG, getPicBeds)
onBeforeMount(() => {
sendToMain('getPicBedConfig', $route.params.type)
ipcRenderer.on('getPicBedConfig', getPicBeds)
sendToMain(GET_PICBED_CONFIG, $route.params.type)
})
const handleConfirm = async () => {
Expand All @@ -85,10 +87,6 @@ function getPicBeds (event: IpcRendererEvent, _config: IPicGoPluginConfig[], nam
picBedName.value = name
}
onBeforeUnmount(() => {
ipcRenderer.removeListener('getPicBedConfig', getPicBeds)
})
</script>
<script lang="ts">
export default {
Expand Down
1 change: 1 addition & 0 deletions src/universal/events/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export const FORCE_UPDATE = 'FORCE_UPDATE'
export const OPEN_WINDOW = 'OPEN_WINDOW'
export const GET_PICBEDS = 'GET_PICBEDS'
export const RPC_ACTIONS = 'RPC_ACTIONS'
export const GET_PICBED_CONFIG = 'GET_PICBED_CONFIG'
// i18n
export const GET_CURRENT_LANGUAGE = 'GET_CURRENT_LANGUAGE'
export const GET_LANGUAGE_LIST = 'GET_LANGUAGE_LIST'
Expand Down

0 comments on commit 050a3dd

Please sign in to comment.