Skip to content

Commit

Permalink
chore: save bulkrename input history
Browse files Browse the repository at this point in the history
  • Loading branch information
yunyuyuan committed Mar 30, 2024
1 parent a54abf8 commit a927f8b
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 28 deletions.
54 changes: 38 additions & 16 deletions src/components/Dialogs/BulkRenameFilesDialog.vue
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<script setup lang="ts">
import { useDialog } from '@/composables'
import HistoryField from '@/components/Core/HistoryField.vue'
import { getFileIcon } from '@/constants/vuetorrent'
import { useContentStore } from '@/stores'
import { TreeFolder, TreeNode } from '@/types/vuetorrent'
import { storeToRefs } from 'pinia'
import { HistoryKey } from '@/constants/vuetorrent'
import { reactive, ref, onMounted, watch, computed } from 'vue'
import { useI18n } from 'vue-i18n'
import { toast } from 'vue3-toastify'
Expand All @@ -22,11 +23,11 @@ const contentStore = useContentStore()
const form = ref<VForm>()
const isFormValid = ref(false)
const hasDuplicated = ref(false)
const {
bulkRenameRegexp: regexpInput,
bulkRenameRegexpFlags: regexpFlagsInput,
bulkRenameTarget: targetInput
} = storeToRefs(contentStore)
const regexpInput = ref('')
const regexpEl = ref<typeof HistoryField>()
const regexpFlagsInput = ref([])
const targetInput = ref('')
const targetEl = ref<typeof HistoryField>()
const running = ref(false)
const rules = [(v: string) => !!v]
Expand Down Expand Up @@ -157,13 +158,13 @@ const fileCheckChange = (item: ItemRow) => {
}
const dryRunRename = (partialItems?: ItemRow[]) => {
let regexp: RegExp;
let regexp: RegExp
try {
regexp = new RegExp(regexpInput.value, regexpFlagsInput.value.join(''))
} catch {
return
}
(partialItems ? partialItems : items).forEach(item => {
;(partialItems ? partialItems : items).forEach(item => {
if (item.type === 'file') {
if (isFormValid.value && item.selected && regexp.test(item.name)) {
item.targetName = item.name.replace(regexp, targetInput.value)
Expand Down Expand Up @@ -192,7 +193,7 @@ const dryRunRename = (partialItems?: ItemRow[]) => {
const run = async () => {
if (!candidateItems.value.length) {
return toast.warn(t('dialogs.bulkRenameFiles.nothing_no_do'))
return toast.warn(t('dialogs.bulkRenameFiles.nothing_to_do'))
}
const reqList = []
for (const item of candidateItems.value) {
Expand All @@ -202,6 +203,8 @@ const run = async () => {
Promise.all(reqList)
.then(() => {
toast.success(t('dialogs.bulkRenameFiles.success'))
regexpEl.value?.saveValueToHistory()
targetEl.value?.saveValueToHistory()
})
.catch(e => {
toast.error(e.toString())
Expand Down Expand Up @@ -239,7 +242,14 @@ onMounted(() => {
<v-form v-model="isFormValid" ref="form">
<v-row no-gutters align="center" justify="center">
<v-col :cols="$vuetify.display.mobile ? 12 : undefined">
<v-text-field hide-details density="compact" v-model="regexpInput" :rules="rules" :label="$t('dialogs.bulkRenameFiles.regexp')">
<HistoryField
:historyKey="HistoryKey.BULK_RENAME_REGEXP"
ref="regexpEl"
hide-details
density="compact"
v-model="regexpInput"
:rules="rules"
:label="$t('dialogs.bulkRenameFiles.regexp')">
<template v-slot:append>
<v-select
v-model="regexpFlagsInput"
Expand All @@ -250,13 +260,20 @@ onMounted(() => {
multiple
hide-details></v-select>
</template>
</v-text-field>
</HistoryField>
</v-col>
<v-col cols="auto">
<v-icon class="mx-2" icon="mdi-arrow-right" />
<v-icon class="mx-2" :icon="`mdi-arrow-${$vuetify.display.mobile ? 'down' : 'right'}`" />
</v-col>
<v-col :cols="$vuetify.display.mobile ? 12 : undefined">
<v-text-field hide-details density="compact" v-model="targetInput" :rules="rules" :label="$t('dialogs.bulkRenameFiles.target')" />
<HistoryField
:historyKey="HistoryKey.BULK_RENAME_TARGET"
ref="targetEl"
hide-details
density="compact"
v-model="targetInput"
:rules="rules"
:label="$t('dialogs.bulkRenameFiles.target')" />
</v-col>
<v-col cols="auto">
<v-badge :class="$vuetify.display.mobile ? 'mt-2' : 'ml-5'" color="success" location="top left" :content="candidateItems.length">
Expand All @@ -273,7 +290,11 @@ onMounted(() => {
<v-checkbox-btn v-else v-model="item.selected" :indeterminate="item.indeterminate" @change="folderCheckChange(item)" />
</template>
<template v-slot:item.name>
<span class="fold-toggle" :class="{ clickable: item.type === 'folder' }" :style="{'padding-left': `${item.indent*20}px`}" @click="item.type === 'folder' && toggleFolderFolded(item, !item.folded)">
<span
class="fold-toggle"
:class="{ clickable: item.type === 'folder' }"
:style="{ 'padding-left': `${item.indent * 20}px` }"
@click="item.type === 'folder' && toggleFolderFolded(item, !item.folded)">
<v-tooltip v-if="item.type === 'folder'" location="top" activator="parent">
{{ t(`dialogs.bulkRenameFiles.${item.folded ? 'unfold' : 'fold'}`) }}
</v-tooltip>
Expand Down Expand Up @@ -313,7 +334,7 @@ onMounted(() => {
min-width: 100px;
}
.v-card-text {
height: calc(100vh - 112px)
height: calc(100vh - 112px);
}
.v-table {
overflow: auto;
Expand All @@ -329,7 +350,8 @@ onMounted(() => {
.fold-toggle.clickable {
cursor: pointer;
}
.fold-toggle, .target-name {
.fold-toggle,
.target-name {
word-break: keep-all;
white-space: pre;
}
Expand Down
4 changes: 3 additions & 1 deletion src/constants/vuetorrent/HistoryKey.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export enum HistoryKey {
COOKIE = 'cookie',
SEARCH_ENGINE_QUERY = 'searchEngineQuery',
TORRENT_PATH = 'torrentPath'
TORRENT_PATH = 'torrentPath',
BULK_RENAME_REGEXP = 'bulkRenameRegexp',
BULK_RENAME_TARGET = 'bulkRenameTarget'
}
4 changes: 2 additions & 2 deletions src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -364,13 +364,13 @@
"col_result_name": "Result",
"run": "Run",
"success": "Rename Successful",
"nothing_no_do": "No tasks to do",
"duplicated": "Duplicate Filename",
"not_changed": "Filename Not Changed",
"fold": "Collapse",
"unfold": "Expand",
"notForFolder": "Folder Renaming Not Supported",
"select_regex_flags": "Select Regular Expression Flags"
"select_regex_flags": "Select Regular Expression Flags",
"nothing_to_do": "No tasks to do"
},
"rss": {
"feed": {
Expand Down
4 changes: 2 additions & 2 deletions src/locales/zh-Hans.json
Original file line number Diff line number Diff line change
Expand Up @@ -360,13 +360,13 @@
"col_result_name": "结果",
"run": "执行",
"success": "重命名成功",
"nothing_no_do": "无任务可做",
"duplicated": "文件名重复",
"not_changed": "文件名未修改",
"fold": "收起",
"unfold": "展开",
"notForFolder": "不支持修改文件夹名",
"select_regex_flags": "选择正则表达式Flags"
"select_regex_flags": "选择正则表达式Flags",
"nothing_to_do": "无任务可做"
},
"rss": {
"feed": {
Expand Down
7 changes: 0 additions & 7 deletions src/stores/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ export const useContentStore = defineStore('content', () => {
const _lock = ref(false)
const cachedFiles = ref<TorrentFile[]>([])
const openedItems = ref([''])

const bulkRenameRegexp = ref('')
const bulkRenameRegexpFlags = ref([])
const bulkRenameTarget = ref('')
const { tree } = useTreeBuilder(cachedFiles)

const flatTree = computed(() => {
Expand Down Expand Up @@ -158,9 +154,6 @@ export const useContentStore = defineStore('content', () => {
menuData,
cachedFiles,
openedItems,
bulkRenameRegexp,
bulkRenameRegexpFlags,
bulkRenameTarget,
tree,
flatTree,
updateFileTree,
Expand Down

0 comments on commit a927f8b

Please sign in to comment.