刘光辉
7 小时以前 0dfe84494048ce27ba8449831782128412d3eb13
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
import type { NormalizedOfficeDocumentConfig, OpenOfficeDocumentConfig } from '../types';
 
/** 后端按扩展名决定 documentType,缺扩展名会直接被拒,故前端先给出更易懂的报错 */
export function validateOfficeDocumentConfig(config: OpenOfficeDocumentConfig) {
  if (!config?.file?.fileId) {
    throw new Error('[GlobalOfficeDocumentModal] file.fileId is required');
  }
  if (!resolveFileName(config)) {
    throw new Error('[GlobalOfficeDocumentModal] file.name is required');
  }
}
 
/**
 * 文件名优先取 file.name;没有就退回从 url 里截。
 * 后端靠文件名的扩展名推导 documentType 并判断能否编辑,所以这个值不能丢。
 */
export function resolveFileName(config: OpenOfficeDocumentConfig): string {
  const name = config?.file?.name?.trim();
  if (name) return name;
 
  const url = config?.file?.url;
  if (!url) return '';
  const path = url.split(/[#?]/)[0] || '';
  const lastSegment = path.split('/').pop() || '';
  return lastSegment.includes('.') ? lastSegment : '';
}
 
export function normalizeOfficeDocumentConfig(config: OpenOfficeDocumentConfig): NormalizedOfficeDocumentConfig {
  const fileName = resolveFileName(config);
  return {
    ...config,
    mode: config.mode === 'view' ? 'view' : 'edit',
    title: config.title?.trim() || fileName,
  };
}