-
-
Notifications
You must be signed in to change notification settings - Fork 493
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #270 from XPoet/dev
feat: local settings data can be saved to cloud repository (#227)
- Loading branch information
Showing
14 changed files
with
306 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,11 @@ | ||
export const PICX_REPO_NAME = 'XPoet/picx' | ||
export const INIT_REPO_NAME = 'picx-images-hosting' | ||
export const INIT_REPO_DESC = 'PicX images hosting repository' | ||
export const INIT_REPO_DESC = 'PicX image hosting repository' | ||
export const INIT_REPO_BARNCH = 'master' | ||
export const GH_PAGES = 'gh-pages' | ||
export const PICX_UPLOAD_IMG_DESC = 'Upload image via PicX(https://github.com/XPoet/picx)' | ||
export const PICX_UPLOAD_IMGS_DESC = 'Upload images via PicX(https://github.com/XPoet/picx)' | ||
export const PICX_DEL_IMG_DESC = 'Delete image via PicX(https://github.com/XPoet/picx)' | ||
export const PICX_UPLOAD_IMG_DESC = 'Upload image via PicX (https://github.com/XPoet/picx)' | ||
export const PICX_UPLOAD_IMGS_DESC = 'Upload images via PicX (https://github.com/XPoet/picx)' | ||
export const PICX_DEL_IMG_DESC = 'Delete image via PicX (https://github.com/XPoet/picx)' | ||
export const PICX_INIT_SETTINGS_MSG = 'Init settings via PicX (https://github.com/XPoet/picx)' | ||
export const PICX_UPDATE_SETTINGS_MSG = 'Update settings via PicX (https://github.com/XPoet/picx)' | ||
export const PICX_INIT_REPO_MSG = 'Init repo via PicX (https://github.com/XPoet/picx)' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
src/components/cloud-settings-bar/cloud-settings-bar.model.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export enum CloudSettingsActions { | ||
// eslint-disable-next-line no-unused-vars | ||
save, | ||
// eslint-disable-next-line no-unused-vars | ||
use, | ||
// eslint-disable-next-line no-unused-vars | ||
update, | ||
// eslint-disable-next-line no-unused-vars | ||
equal | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
.cloud-settings-data-box { | ||
display flex | ||
align-items center | ||
justify-content space-between | ||
padding 2rem 0 2rem 12rem | ||
color var(--text-color-2) | ||
border 1rem solid var(--text-color-4) | ||
border-radius 6rem | ||
} |
49 changes: 49 additions & 0 deletions
49
src/components/cloud-settings-bar/cloud-settings-bar.util.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import request from '@/utils/request' | ||
import { PICX_INIT_SETTINGS_MSG, PICX_UPDATE_SETTINGS_MSG } from '@/common/constant' | ||
import { UserConfigInfoModel, UserSettingsModel } from '@/common/model' | ||
|
||
export const getCloudSettings = async (userConfigInfo: UserConfigInfoModel) => { | ||
const { owner, selectedRepo: repo } = userConfigInfo | ||
const res = await request({ | ||
url: `/repos/${owner}/${repo}/contents/.settings`, | ||
method: 'GET', | ||
noShowErrorMsg: true, | ||
cache: { | ||
maxAge: 0 | ||
}, | ||
params: { | ||
timestamp: Date.now() // 添加时间戳参数,防止获取缓存的数据 | ||
} | ||
}) | ||
|
||
return Promise.resolve(res) | ||
} | ||
|
||
export const saveCloudSettings = async ( | ||
userSettings: UserSettingsModel, | ||
userConfigInfo: UserConfigInfoModel | ||
) => { | ||
const { owner, selectedRepo: repo, selectedBranch: branch } = userConfigInfo | ||
|
||
const res = await getCloudSettings(userConfigInfo) | ||
|
||
const data: any = { | ||
message: res ? PICX_UPDATE_SETTINGS_MSG : PICX_INIT_SETTINGS_MSG, | ||
content: window.btoa(JSON.stringify(userSettings)) | ||
} | ||
|
||
if (res) { | ||
data.sha = res.sha | ||
} else { | ||
data.branch = branch | ||
} | ||
|
||
const res2 = await request({ | ||
url: `/repos/${owner}/${repo}/contents/.settings`, | ||
method: 'PUT', | ||
data, | ||
noShowErrorMsg: true | ||
}) | ||
|
||
return Promise.resolve(res2) | ||
} |
151 changes: 151 additions & 0 deletions
151
src/components/cloud-settings-bar/cloud-settings-bar.vue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
<template> | ||
<div class="cloud-settings-data-box border-box"> | ||
<div>{{ actionsTip }}</div> | ||
<el-button | ||
type="primary" | ||
text | ||
:icon="icon.IEpCheck" | ||
:loading="saveLoading" | ||
:disabled="saveDisabled" | ||
@click="onOK" | ||
> | ||
{{ $t('confirm') }} | ||
</el-button> | ||
</div> | ||
</template> | ||
|
||
<script setup lang="ts"> | ||
import { computed, getCurrentInstance, onMounted, ref, shallowRef, watch } from 'vue' | ||
import { store } from '@/stores' | ||
import { UserSettingsModel } from '@/common/model' | ||
import { deepAssignObject, deepObjectEqual } from '@/utils' | ||
import { CloudSettingsActions } from './cloud-settings-bar.model' | ||
import { getCloudSettings, saveCloudSettings } from './cloud-settings-bar.util' | ||
const instance = getCurrentInstance() | ||
const userSettings = computed(() => store.getters.getUserSettings).value | ||
const userConfigInfo = computed(() => store.getters.getUserConfigInfo).value | ||
const icon = shallowRef({ IEpCheck, IEpClose }) | ||
const cloudSettings = ref<null | UserSettingsModel>(null) | ||
const saveLoading = ref(false) | ||
const saveDisabled = ref(false) | ||
const selectedAction = ref<CloudSettingsActions>(CloudSettingsActions.save) | ||
const actionsTip = computed(() => { | ||
switch (selectedAction.value) { | ||
case CloudSettingsActions.save: | ||
return instance?.proxy?.$t('settings.cloud_settings.tip_1') | ||
case CloudSettingsActions.use: | ||
return instance?.proxy?.$t('settings.cloud_settings.tip_2') | ||
case CloudSettingsActions.update: | ||
return instance?.proxy?.$t('settings.cloud_settings.tip_3') | ||
case CloudSettingsActions.equal: | ||
return instance?.proxy?.$t('settings.cloud_settings.tip_4') | ||
default: | ||
return instance?.proxy?.$t('settings.cloud_settings.tip_1') | ||
} | ||
}) | ||
// 保存(更新)到云端 | ||
const saveToCloud = async () => { | ||
saveLoading.value = true | ||
await saveCloudSettings(userSettings, userConfigInfo) | ||
saveLoading.value = false | ||
cloudSettings.value = JSON.parse(JSON.stringify(userSettings)) | ||
ElMessage.success( | ||
cloudSettings.value === null | ||
? instance?.proxy?.$t('settings.cloud_settings.success_msg_1') | ||
: instance?.proxy?.$t('settings.cloud_settings.success_msg_2') | ||
) | ||
} | ||
// 使用云端设置数据 | ||
const useCloudSettings = () => { | ||
if (cloudSettings.value) { | ||
deepAssignObject(userSettings, cloudSettings.value) | ||
store.dispatch('USER_SETTINGS_PERSIST') | ||
} | ||
} | ||
// 初始化云端设置数据 | ||
const initCloudSettings = async () => { | ||
const res = await getCloudSettings(userConfigInfo) | ||
cloudSettings.value = res ? JSON.parse(window.atob(res.content)) : null | ||
} | ||
// 确定操作 | ||
const onOK = () => { | ||
// eslint-disable-next-line default-case | ||
switch (selectedAction.value) { | ||
case CloudSettingsActions.save: | ||
case CloudSettingsActions.update: | ||
saveToCloud() | ||
break | ||
case CloudSettingsActions.use: | ||
useCloudSettings() | ||
break | ||
} | ||
} | ||
watch( | ||
() => userSettings, | ||
(settings) => { | ||
// 本地设置发生变化时,判断和云端设置是否相等 | ||
if (deepObjectEqual(settings, cloudSettings.value!)) { | ||
// 相等情况 | ||
selectedAction.value = CloudSettingsActions.equal | ||
saveDisabled.value = true | ||
} else { | ||
// 不相等情况 | ||
selectedAction.value = CloudSettingsActions.update | ||
saveDisabled.value = false | ||
} | ||
}, | ||
{ | ||
deep: true | ||
} | ||
) | ||
watch( | ||
() => cloudSettings.value, | ||
(settings) => { | ||
// 不存在云端设置数据,提示是否保存 | ||
if (settings === null) { | ||
selectedAction.value = CloudSettingsActions.save | ||
} | ||
// 存在云端设置数据,提示是否使用 | ||
if (settings) { | ||
selectedAction.value = CloudSettingsActions.use | ||
} | ||
// 判断 云端设置 和本地设置 是否相等,相等则禁止点击 | ||
if (settings && deepObjectEqual(settings, userSettings)) { | ||
saveDisabled.value = true | ||
selectedAction.value = CloudSettingsActions.equal | ||
} else { | ||
saveDisabled.value = false | ||
selectedAction.value = CloudSettingsActions.update | ||
} | ||
}, | ||
{ | ||
deep: true | ||
} | ||
) | ||
onMounted(() => { | ||
initCloudSettings() | ||
}) | ||
</script> | ||
|
||
<style scoped lang="stylus"> | ||
@import "cloud-settings-bar.styl" | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.