Implement composition API in Rename.vue file
This commit is contained in:
parent
ffcaa8bb96
commit
7e97615ae2
@ -6,8 +6,7 @@
|
|||||||
|
|
||||||
<div class="card-content">
|
<div class="card-content">
|
||||||
<p>
|
<p>
|
||||||
{{ $t("prompts.renameMessage") }} <code>{{ oldName() }}</code
|
{{ $t("prompts.renameMessage") }} <code>{{ oldName() }}</code>:
|
||||||
>:
|
|
||||||
</p>
|
</p>
|
||||||
<input
|
<input
|
||||||
id="focus-prompt"
|
id="focus-prompt"
|
||||||
@ -40,85 +39,75 @@
|
|||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script>
|
<script setup>
|
||||||
import { mapActions, mapState, mapWritableState } from "pinia";
|
import { ref, inject, onMounted } from "vue";
|
||||||
import { useFileStore } from "@/stores/file";
|
import { useFileStore } from "@/stores/file";
|
||||||
import { useLayoutStore } from "@/stores/layout";
|
import { useLayoutStore } from "@/stores/layout";
|
||||||
import url from "@/utils/url";
|
import url from "@/utils/url";
|
||||||
import { files as api } from "@/api";
|
import { files as api } from "@/api";
|
||||||
import { removePrefix } from "@/api/utils";
|
import { removePrefix } from "@/api/utils";
|
||||||
|
import { useRouter } from "vue-router";
|
||||||
|
import { useI18n } from "vue-i18n";
|
||||||
|
|
||||||
export default {
|
const { t } = useI18n();
|
||||||
name: "rename",
|
const $showError = inject("$showError");
|
||||||
data: function () {
|
const router = useRouter();
|
||||||
return {
|
|
||||||
name: "",
|
|
||||||
};
|
|
||||||
},
|
|
||||||
created() {
|
|
||||||
this.name = this.oldName();
|
|
||||||
},
|
|
||||||
inject: ["$showError"],
|
|
||||||
computed: {
|
|
||||||
...mapState(useFileStore, [
|
|
||||||
"req",
|
|
||||||
"selected",
|
|
||||||
"selectedCount",
|
|
||||||
"isListing",
|
|
||||||
]),
|
|
||||||
...mapWritableState(useFileStore, ["reload", "preselect"]),
|
|
||||||
},
|
|
||||||
methods: {
|
|
||||||
...mapActions(useLayoutStore, ["closeHovers"]),
|
|
||||||
cancel: function () {
|
|
||||||
this.closeHovers();
|
|
||||||
},
|
|
||||||
oldName: function () {
|
|
||||||
if (!this.isListing) {
|
|
||||||
return this.req.name;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (this.selectedCount === 0 || this.selectedCount > 1) {
|
const fileStore = useFileStore();
|
||||||
// This shouldn't happen.
|
const layoutStore = useLayoutStore();
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
return this.req.items[this.selected[0]].name;
|
const name = ref("");
|
||||||
},
|
|
||||||
submit: async function () {
|
|
||||||
if(this.name.includes('/')) {
|
|
||||||
this.$showError(new Error(this.$t('errors.invalidName')));
|
|
||||||
this.closeHovers();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
let oldLink = "";
|
|
||||||
let newLink = "";
|
|
||||||
|
|
||||||
if (!this.isListing) {
|
const oldName = () => {
|
||||||
oldLink = this.req.url;
|
if (!fileStore.isListing) {
|
||||||
} else {
|
return fileStore.req.name;
|
||||||
oldLink = this.req.items[this.selected[0]].url;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
newLink =
|
if (fileStore.selectedCount === 0 || fileStore.selectedCount > 1) {
|
||||||
url.removeLastDir(oldLink) + "/" + encodeURIComponent(this.name);
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
return fileStore.req.items[fileStore.selected[0]].name;
|
||||||
await api.move([{ from: oldLink, to: newLink }]);
|
|
||||||
if (!this.isListing) {
|
|
||||||
this.$router.push({ path: newLink });
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
this.preselect = removePrefix(newLink);
|
|
||||||
|
|
||||||
this.reload = true;
|
|
||||||
} catch (e) {
|
|
||||||
this.$showError(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
this.closeHovers();
|
|
||||||
},
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
onMounted(() => {
|
||||||
|
name.value = oldName();
|
||||||
|
});
|
||||||
|
|
||||||
|
const submit = async () => {
|
||||||
|
if (name.value.includes("/")) {
|
||||||
|
$showError(new Error(t("errors.invalidName")));
|
||||||
|
layoutStore.closeHovers();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let oldLink = "";
|
||||||
|
let newLink = "";
|
||||||
|
|
||||||
|
if (!fileStore.isListing) {
|
||||||
|
oldLink = fileStore.req.url;
|
||||||
|
} else {
|
||||||
|
oldLink = fileStore.req.items[fileStore.selected[0]].url;
|
||||||
|
}
|
||||||
|
|
||||||
|
newLink = url.removeLastDir(oldLink) + "/" + encodeURIComponent(name.value);
|
||||||
|
|
||||||
|
try {
|
||||||
|
await api.move([{ from: oldLink, to: newLink }]);
|
||||||
|
|
||||||
|
if (!fileStore.isListing) {
|
||||||
|
router.push({ path: newLink });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
fileStore.preselect = removePrefix(newLink);
|
||||||
|
fileStore.reload = true;
|
||||||
|
} catch (e) {
|
||||||
|
$showError(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
layoutStore.closeHovers();
|
||||||
|
};
|
||||||
|
|
||||||
|
const closeHovers = layoutStore.closeHovers;
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user