AppendModal.vue 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <script setup lang="ts">
  2. import { ref, reactive, toRaw, watch } from "vue";
  3. import type { FormInstance, FormRules } from "element-plus";
  4. import _ from "lodash";
  5. import type { TreeData } from "./Tree";
  6. const props = defineProps<{ contextData: TreeData; appendLoading: boolean }>();
  7. const show = defineModel<boolean>("show");
  8. const emit = defineEmits(["ok", "cancel"]);
  9. const form$ = ref<FormInstance>();
  10. const formData = reactive({
  11. isDir: false,
  12. filename: "",
  13. });
  14. const rules = reactive<FormRules<typeof formData>>({
  15. filename: [
  16. {
  17. validator: (rule, value, callback, source, options) => {
  18. if (
  19. _.includes(
  20. props.contextData.children?.map((c) => c.key),
  21. `${props.contextData.key ? `${props.contextData.key}/` : ""}${value}`
  22. )
  23. ) {
  24. callback("文件重名");
  25. }
  26. callback();
  27. },
  28. trigger: "change",
  29. },
  30. {
  31. pattern: /[^\\/:"*?<>|]/,
  32. message: '有非法字符: \\/:"*?<>|',
  33. trigger: "change",
  34. },
  35. ],
  36. });
  37. const onOk = async () => {
  38. try {
  39. const result = await form$.value?.validate();
  40. if (result) {
  41. const data = _.cloneDeep(toRaw(formData))
  42. if (!formData.isDir) {
  43. data.filename = `${data.filename}.md`
  44. }
  45. emit("ok", data);
  46. }
  47. } catch (e) {
  48. console.warn(e);
  49. }
  50. };
  51. const onCancel = () => {
  52. emit("cancel");
  53. show.value = false;
  54. };
  55. watch(
  56. () => show.value,
  57. () => {
  58. if (!show.value) {
  59. form$.value?.resetFields();
  60. }
  61. }
  62. );
  63. </script>
  64. <template>
  65. <el-dialog width="400px" v-model="show">
  66. <el-form ref="form$" :model="formData" :rules="rules">
  67. <el-form-item label="是否文件夹" prop="isDir">
  68. <el-switch v-model="formData.isDir" inline-prompt></el-switch>
  69. </el-form-item>
  70. <el-form-item :label="formData.isDir ? '文件夹名' : '文件名'" prop="filename">
  71. <el-input v-model="formData.filename">
  72. <template v-if="!formData.isDir" #append>.md</template>
  73. </el-input>
  74. </el-form-item>
  75. <el-form-item>
  76. <el-button
  77. :disabled="!formData.filename"
  78. :loading="appendLoading"
  79. @click="onOk"
  80. type="primary"
  81. >ok</el-button
  82. >
  83. <el-button @click="onCancel">cancel</el-button>
  84. </el-form-item>
  85. </el-form>
  86. </el-dialog>
  87. </template>