NewFileDialog.vue 3.7 KB
<template>
  <div class="dialog-mask">
    <div class="dialog-panel">
      <div class="dialog-title">{{ $t("KnowledgeBase.newFile") }}</div>
      <input
        v-model="fileName"
        class="dialog-input"
        :class="{ error: errorMessage }"
        :placeholder="$t('KnowledgeBase.fileNamePlaceholder')"
        @keyup.enter="onCreate"
        @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="!fileName.trim()"
          @click="onCreate"
        >
          {{ $t("common.save") }}
        </button>
      </div>
    </div>
  </div>
</template>

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

interface Props {
  folderId?: string | number;
}

const props = defineProps<Props>();
const emit = defineEmits<{
  (e: "close"): void;
  (
    e: "create",
    data: { name: string; folderId: string | number | undefined; type: string },
  ): void;
}>();

const { t } = useI18n();
const fileName = ref("");
const errorMessage = ref("");

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 onCreate = () => {
  if (fileName.value.trim()) {
    let fileNameValue = fileName.value.trim();

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

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

    // 确保文件名以.md结尾
    if (!fileNameValue.toLowerCase().endsWith(".md")) {
      fileNameValue += ".md";
    }

    emit("create", {
      name: fileNameValue,
      folderId: props.folderId,
      type: "md",
    });
  }
};
</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>