RecentlyVisited.vue 9.2 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
<template>
  <div class="recently-visited-container">
    <div class="recent-header">
      <div class="title-wrapper">
        <img src="/lishijilu.svg" alt="Logo" class="icon-svg" />
        <h3 class="recent-title">{{ $t("workspace.recentlyVisited") }}</h3>
      </div>
    </div>
    <div class="recent-content">
      <div v-if="recentDocuments.length === 0" class="empty-state">
        <img src="/wenjian.svg" class="empty-icon" alt="empty" />
        <p class="empty-text">暂无最近访问的文档</p>
      </div>
      <div v-else class="document-table">
        <table class="recent-table">
          <tbody>
            <tr
              v-for="doc in recentDocuments"
              :key="doc.fileId"
              class="document-row"
              @click="handleDocumentClick(doc)"
              @dblclick="handleDocumentClick(doc)"
              title="单击或双击打开文件"
            >
              <td class="file-name-cell">
                <div class="file-info">
                  <img
                    :src="getFileIcon(doc.extension)"
                    class="file-icon"
                    :alt="doc.extension || 'file'"
                  />
                  <span class="file-name">{{ doc.displayName }}</span>
                </div>
              </td>
              <td class="last-visited-cell">
                {{ formatDate(doc.lastAccessedAt) }}
              </td>
              <td class="action-cell">
                <img
                  src="/shanchu1.svg"
                  class="delete-icon"
                  @click.stop="deleteHistoryItem(doc.fileId)"
                  title="删除此记录"
                  alt="delete"
                />
              </td>
            </tr>
          </tbody>
        </table>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from "vue";
import { useRouter } from "vue-router";
import { ElMessage } from "element-plus";
import { useI18n } from "vue-i18n";

const router = useRouter();
const { t: _t } = useI18n();

interface RecentDocument {
  fileId: string;
  displayName: string;
  extension: string;
  lastAccessedAt: string;
  folderId?: string;
  folderPath?: string;
}

const recentDocuments = ref<RecentDocument[]>([]);

const loadRecentDocuments = async () => {
  try {
    // TODO: 替换为真实的 API 调用
    // const response = await fileHistoryApi.getList({
    //   page: 1,
    //   size: 10,
    //   sortBy: "last_accessed_at",
    //   sortOrder: "DESC",
    // });

    // 暂时使用空数组,等待后端 API 接入
    recentDocuments.value = [];

    // 如果需要,可以使用模拟数据进行测试:
    // recentDocuments.value = [
    //   {
    //     fileId: "1",
    //     displayName: "示例文档.md",
    //     extension: "md",
    //     lastAccessedAt: new Date().toISOString(),
    //   },
    // ];
  } catch (error) {
    console.error("加载最近文档失败:", error);
    recentDocuments.value = [];
  }
};

const deleteHistoryItem = async (fileId: string) => {
  try {
    // TODO: 替换为真实的 API 调用
    // const response = await fileHistoryApi.deleteHistory(fileId);

    // 从列表中移除该项
    recentDocuments.value = recentDocuments.value.filter(
      (doc) => doc.fileId !== fileId,
    );
    ElMessage.success("已删除该访问记录");
  } catch (error) {
    console.error("删除历史记录失败:", error);
    ElMessage.error("删除历史记录失败");
  }
};

const getFileIcon = (type: string): string => {
  const iconMap: Record<string, string> = {
    pdf: "/pdf_icon.svg",
    doc: "/word_icon.svg",
    docx: "/word_icon.svg",
    xls: "/excel_icon.svg",
    xlsx: "/excel_icon.svg",
    ppt: "/PPT_icon.svg",
    pptx: "/PPT_icon.svg",
    txt: "/TXT_icon.svg",
    md: "/md_icon.svg",
    jpg: "/jpg_icon.svg",
    jpeg: "/jpg_icon.svg",
    png: "/png_icon.svg",
    gif: "/gif_icon.svg",
  };
  return iconMap[type] || "/TXT_icon.svg";
};

const formatDate = (dateString: string): string => {
  try {
    const date = new Date(dateString);
    const now = new Date();
    const diffTime = Math.abs(now.getTime() - date.getTime());
    const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24));

    // 格式化时间,显示时分秒
    const timeStr = date.toLocaleTimeString("zh-CN", {
      hour: "2-digit",
      minute: "2-digit",
      second: "2-digit",
      hour12: false,
    });

    if (diffDays === 1) {
      return `今天 ${timeStr}`;
    } else if (diffDays === 2) {
      return `昨天 ${timeStr}`;
    } else if (diffDays <= 7) {
      return `${diffDays - 1}天前 ${timeStr}`;
    } else {
      return `${date.toLocaleDateString()} ${timeStr}`;
    }
  } catch (error) {
    return dateString;
  }
};

const handleDocumentClick = (document: RecentDocument) => {
  // 跳转到工作台编辑文件
  router.push({
    name: "Workspace",
    query: {
      fileId: document.fileId,
      fileName: document.displayName,
      extension: document.extension,
      folderId: document.folderId,
      folderPath: document.folderPath,
    },
  });
};

onMounted(() => {
  loadRecentDocuments();
});
</script>

<style scoped>
.recently-visited-container {
  width: 100%;
  max-width: 800px;
}

.recent-header {
  margin-bottom: 16px;
}

.title-wrapper {
  display: flex;
  align-items: center;
  justify-content: flex-start;
  gap: 8px;
}
.icon-svg {
  width: 18px;
  height: 18px;
  object-fit: contain;
  flex-shrink: 0;
}

.history-icon {
  font-size: 18px;
  color: var(--color-text, #eee);
}

.recent-title {
  font-size: 12px;
  color: var(--color-text-secondary, #999);
  margin: 0;
}

.recent-content {
  background: var(--color-card, #232323);
  border-radius: 12px;
}

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 40px 20px;
  color: var(--color-text-secondary, #999);
}

.empty-icon {
  width: 30px;
  height: 30px;
  object-fit: contain;
  flex-shrink: 0;
  margin-bottom: 16px;
  object-fit: contain;
}

.empty-text {
  font-size: 14px;
  margin: 0;
  text-align: center;
}

.document-table {
  width: 100%;
  max-height: 300px; /* 设置最大高度 */
  overflow-y: auto; /* 添加垂直滚动条 */
}

.recent-table {
  width: 100%;
  border-collapse: collapse;
  border-spacing: 0;
}

.recent-table td {
  padding: 6px 16px 10px 16px;
  border-bottom: 1px solid var(--color-border);
  vertical-align: middle;
  text-align: center;
}

/* 最后一行的td不显示底部边框 */
.recent-table tr:last-child td {
  border-bottom: 0 solid var(--color-border);
}

.document-row {
  cursor: pointer;
  transition: all 0.2s ease;
  user-select: none;
}

.document-row:hover {
  background: var(--color-hover, #37373d);
  transform: translateX(4px);
}

.document-row:active {
  transform: translateX(2px);
  background: var(--color-hover-bar);
}

.file-name-cell {
  text-align: left !important;
}

.file-info {
  display: flex;
  align-items: center;
  justify-content: flex-start;
  gap: 8px;
}

.file-icon {
  width: 18px;
  height: 18px;
  object-fit: contain;
  flex-shrink: 0;
  vertical-align: middle;
}

.file-name {
  font-size: 14px;
  font-weight: 400;
  color: var(--color-text, #eee);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.last-visited-cell {
  font-size: 13px;
  color: var(--color-text-secondary, #999);
  text-align: center !important;
  vertical-align: middle;
}

.action-cell {
  width: 40px;
  text-align: center !important;
  vertical-align: middle;
  padding: 0 5px;
}

.delete-icon {
  width: 18px;
  height: 18px;
  cursor: pointer;
  opacity: 0;
  transition:
    opacity 0.2s,
    filter 0.2s,
    transform 0.2s;
  padding: 5px;
  object-fit: contain;
  vertical-align: middle;
  display: inline-block;
  filter: brightness(0) saturate(100%) invert(100%) sepia(0%) saturate(0%)
    hue-rotate(93deg) brightness(50%) contrast(107%);
}

.document-row:hover .delete-icon {
  opacity: 1;
}

.delete-icon:hover {
  filter: brightness(0) saturate(100%) invert(100%) sepia(0%) saturate(0%)
    hue-rotate(93deg) brightness(90%) contrast(107%);
  transform: scale(1.1);
}

/* 自定义滚动条样式 */
.document-table::-webkit-scrollbar {
  width: 4px;
  height: 4px;
}

.document-table::-webkit-scrollbar-track {
  background: transparent;
}

.document-table::-webkit-scrollbar-thumb {
  background: var(--color-border);
  border-radius: 2px;
}

.document-table::-webkit-scrollbar-thumb:hover {
  background: var(--color-secondary);
}

/* 水平滚动条默认隐藏 */
.document-table::-webkit-scrollbar:horizontal {
  height: 0;
}

/* hover 时也保持水平滚动条隐藏 */
.document-table:hover::-webkit-scrollbar:horizontal {
  height: 0;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .recently-visited-container {
    max-width: 100%;
  }

  .recent-content {
    padding: 12px;
  }

  .recent-table th,
  .recent-table td {
    padding: 10px 12px;
  }

  .file-icon {
    width: 14px;
    height: 14px;
  }

  .file-name {
    font-size: 13px;
  }

  .last-visited-cell {
    font-size: 12px;
  }
}

@media (max-width: 480px) {
  .recent-content {
    padding: 10px;
  }

  .recent-table th,
  .recent-table td {
    padding: 8px 10px;
  }

  .file-icon {
    width: 12px;
    height: 12px;
  }

  .file-name {
    font-size: 12px;
  }

  .last-visited-cell {
    font-size: 11px;
  }
}
</style>