RenameFileDialog.vue 3.65 KB
<template>
  <div class="dialog-mask">
    <div class="dialog-panel">
      <div class="dialog-title">{{ $t("KnowledgeBase.renameFile") }}</div>
      <input
        v-model="newName"
        class="dialog-input"
        :class="{ error: errorMessage }"
        :placeholder="$t('KnowledgeBase.fileNamePlaceholder')"
        @keyup.enter="onRename"
        @input="errorMessage = ''"
      />
      <div v-if="errorMessage" class="error-message">
        {{ errorMessage }}
      </div>
      <div class="dialog-actions">
        <button class="dialog-btn" @click="emit('close')">
          {{ $t("common.cancel") }}
        </button>
        <button
          class="dialog-btn primary"
          :disabled="!newName.trim()"
          @click="onRename"
        >
          {{ $t("common.save") }}
        </button>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, watch } from "vue";
import { useI18n } from "vue-i18n";

interface FileItem {
  id?: string | number;
  name: string;
  [key: string]: any;
}

interface Props {
  file: FileItem;
}

const props = defineProps<Props>();
const emit = defineEmits<{
  (e: "close"): void;
  (e: "rename", data: FileItem): void;
}>();

const { t } = useI18n();
const newName = ref(props.file?.name || "");
const errorMessage = ref("");

watch(
  () => props.file,
  (newVal) => {
    newName.value = newVal?.name || "";
  },
);

const validateFileName = (name: string): string | null => {
  // 检查长度限制
  if (name.length > 30) {
    return t("KnowledgeBase.fileNameTooLong") || "文件名最长不超过30个字符";
  }

  // 检查禁用字符
  const forbiddenChars = /[/\\:*"<>|?]/;
  if (forbiddenChars.test(name)) {
    return (
      t("KnowledgeBase.fileNameInvalidChars") ||
      '文件名不能包含以下字符:/ \\ : * " < > | ?'
    );
  }

  return null;
};

const onRename = () => {
  if (newName.value && newName.value.trim()) {
    const fileName = newName.value.trim();

    // 验证文件名
    const error = validateFileName(fileName);
    if (error) {
      errorMessage.value = error;
      return;
    }

    // 清除错误信息
    errorMessage.value = "";

    emit("rename", { ...props.file, name: fileName });
  }
};
</script>

<style scoped lang="scss">
.dialog-mask {
  position: fixed;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.35);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 2000;
}

.dialog-panel {
  background: var(--color-card);
  border-radius: 10px;
  padding: 28px 32px 20px 32px;
  min-width: 320px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.18);
  display: flex;
  flex-direction: column;
  gap: 18px;
}

.dialog-title {
  font-size: 18px;
  font-weight: 700;
  color: var(--color-text);
  margin-bottom: 6px;
}

.dialog-input {
  padding: 8px 12px;
  border: 1px solid var(--color-border);
  border-radius: 6px;
  background: var(--color-bg);
  color: var(--color-text);
  font-size: 15px;
  margin-bottom: 8px;

  &:focus {
    border-color: var(--color-primary);
    outline: none;
  }

  &.error {
    border-color: var(--color-danger);
  }
}

.error-message {
  color: var(--color-danger);
  font-size: 13px;
  margin-top: -8px;
  margin-bottom: 8px;
}

.dialog-actions {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
}

.dialog-btn {
  padding: 7px 18px;
  border-radius: 6px;
  border: none;
  background: var(--color-card);
  color: var(--color-text);
  font-size: 15px;
  font-weight: 400;
  cursor: pointer;
  transition:
    background 0.2s,
    color 0.2s;

  &.primary {
    background: var(--color-primary);
    color: #fff;
  }

  &:disabled {
    opacity: 0.5;
    cursor: not-allowed;
  }
}
</style>