tree-upload-vue3 1.1.11 → 1.1.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -37,7 +37,7 @@ npm install tree-upload-vue3 element-plus @element-plus/icons-vue file-preview-v
37
37
  <TreeUpload
38
38
  ref="treeUploadRef"
39
39
  :schema="mySchema"
40
- mode="edit"
40
+ mode="view"
41
41
  @upload-before="handleBeforeUpload"
42
42
  @upload-success="handleUploadSuccess"
43
43
  />
@@ -51,9 +51,9 @@ import 'tree-upload-vue3/dist/tree-upload-vue3.css'; // 引入样式
51
51
 
52
52
  const treeUploadRef = ref();
53
53
 
54
- const handleBeforeUpload = ({ file, context }) => {
54
+ const handleBeforeUpload = ({ file, context, currentNode }) => {
55
55
  console.log('准备上传:', file.name);
56
- console.log('当前选中节点:', context.variables.currentNode);
56
+ console.log('当前选中节点:', currentNode || context.variables.currentNode);
57
57
  };
58
58
 
59
59
  const handleUploadSuccess = (response) => {
@@ -69,9 +69,18 @@ const mySchema = reactive<TreeUploadSchema>({
69
69
  {
70
70
  id: '1',
71
71
  label: '必填文档',
72
+ mode: 'edit',
72
73
  required: true,
73
74
  minCount: 1,
74
75
  accept: '.pdf',
76
+ beforeUpload: ({ file }) => {
77
+ return !file.name.startsWith('tmp_');
78
+ },
79
+ meta: {
80
+ upload: {
81
+ beforeUpload: ({ file }) => file.name.endsWith('.pdf')
82
+ }
83
+ },
75
84
  children: []
76
85
  }
77
86
  ]
@@ -85,6 +94,9 @@ const mySchema = reactive<TreeUploadSchema>({
85
94
  enabled: true,
86
95
  action: 'https://api.example.com/upload',
87
96
  multiple: true,
97
+ beforeUpload: ({ file }) => {
98
+ return file.size < 20 * 1024 * 1024;
99
+ },
88
100
  // 可选:转换接口响应为组件所需的 FileItem 格式
89
101
  transformResponse: (response) => {
90
102
  return {
@@ -103,6 +115,22 @@ const mySchema = reactive<TreeUploadSchema>({
103
115
  { prop: 'name', label: '文件名' },
104
116
  { prop: 'size', label: '大小', formatter: 'fileSize' }
105
117
  ],
118
+ actions: [
119
+ { key: 'preview', label: '预览', type: 'primary' },
120
+ { key: 'download', label: '下载' },
121
+ { key: 'delete', label: '删除', type: 'danger' }
122
+ ],
123
+ download: {
124
+ // 可选,默认从 row.url / row.name 读取
125
+ urlField: 'url',
126
+ nameField: 'name',
127
+ // 文件接口需要鉴权时可透传请求头
128
+ requestOptions: {
129
+ headers: {
130
+ Authorization: 'Bearer xxx'
131
+ }
132
+ }
133
+ },
106
134
  ui: {
107
135
  pagination: { enabled: true, pageSize: 20 }
108
136
  }
@@ -118,13 +146,13 @@ const mySchema = reactive<TreeUploadSchema>({
118
146
  | 属性名 | 类型 | 默认值 | 说明 |
119
147
  | --- | --- | --- | --- |
120
148
  | `schema` | `TreeUploadSchema` | **必填** | 组件的核心配置对象 |
121
- | `mode` | `'view' \| 'edit'` | `'edit'` | 视图模式。`view` 模式下隐藏上传和模板区域 |
149
+ | `mode` | `'view' \| 'edit'` | `'edit'` | 全局视图模式。可被节点级 `mode` 覆盖 |
122
150
 
123
151
  ### Events
124
152
 
125
153
  | 事件名 | 参数 | 说明 |
126
154
  | --- | --- | --- |
127
- | `upload-before` | `{ file: File, context: SchemaContext }` | 上传前触发,包含文件对象和当前上下文(如选中节点) |
155
+ | `upload-before` | `{ file: File, context: SchemaContext, currentNode?: CategoryNode }` | 上传前触发,包含文件对象和当前上下文(如选中节点) |
128
156
  | `upload-success` | `response: any` | 上传成功后触发 |
129
157
  | `upload-error` | `error: any` | 上传失败后触发 |
130
158
  | `delete-before` | `file: FileItem` | 用户点击删除确认后,执行删除逻辑前触发 |
@@ -148,28 +176,60 @@ const mySchema = reactive<TreeUploadSchema>({
148
176
 
149
177
  **节点配置 (CategoryNode)**:
150
178
  - `required`: 是否必须上传文件。
179
+ - `mode`: 节点模式。可单独设置为 `view` 或 `edit`,优先级高于组件 `mode`。
151
180
  - `minCount` / `maxCount`: 文件数量限制。
152
181
  - `maxSize`: 单个文件大小限制 (Bytes)。
153
182
  - `accept`: 允许的文件类型 (如 `.jpg,.png`)。
183
+ - `beforeUpload`: 节点级上传前校验钩子,返回 `false` 可阻止上传。
184
+ - `meta.upload.beforeUpload`: 节点 meta 内上传前校验钩子,返回 `false` 可阻止上传。
154
185
  - `meta.templates`: 模板文件列表,选中节点时会自动显示在顶部。
155
186
 
187
+ 模式优先级:
188
+ - `currentNode.mode`
189
+ - `currentNode.meta.mode`
190
+ - 组件 `mode`(`<TreeUpload mode="...">`)
191
+
192
+ 当最终模式为 `view` 时,上传区隐藏且文件删除操作不可用;为 `edit` 时可上传并可删除。
193
+
156
194
  #### 2. Upload Schema (`upload`)
157
195
 
158
196
  | 字段 | 类型 | 说明 |
159
197
  | --- | --- | --- |
160
198
  | `action` | `string` | 上传接口地址 |
161
199
  | `accept` | `string` | 全局文件类型限制 (会被节点配置覆盖) |
200
+ | `beforeUpload` | `(payload) => boolean \| void \| Promise<boolean \| void>` | 全局上传前校验钩子,返回 `false` 阻止上传 |
162
201
  | `transformResponse` | `(res: any) => Partial<FileItem>` | 将接口响应转换为组件所需的 `FileItem` 格式 |
163
202
  | `ui.showTip` | `boolean` | 是否显示动态提示文案 (自动聚合大小、数量、格式限制) |
164
203
 
204
+ 上传前钩子执行顺序:
205
+ - `upload.beforeUpload`(全局)
206
+ - `currentNode.meta.upload.beforeUpload`(节点 meta)
207
+ - `currentNode.beforeUpload`(节点字段)
208
+
209
+ 任意一个钩子返回 `false` 都会取消上传。
210
+
165
211
  #### 3. Table Schema (`table`)
166
212
 
167
213
  | 字段 | 类型 | 说明 |
168
214
  | --- | --- | --- |
169
215
  | `dataSource` | `{ type: 'static' \| 'api', ... }` | 表格数据源。`api` 模式支持动态参数 (如 `requirementId: '${currentNode.id}'`) |
170
216
  | `columns` | `TableColumnSchema[]` | 列定义。支持 `formatter: 'fileSize'` |
217
+ | `actions` | `TableActionSchema[]` | 行操作按钮配置。内置支持 `preview` / `download` / `delete` |
218
+ | `download` | `TableDownloadSchema` | 下载配置。支持自定义文件地址字段、文件名字段和请求头 |
171
219
  | `ui.pagination` | `{ enabled: boolean, pageSize: number }` | 分页配置 (无数据时自动隐藏) |
172
220
 
221
+ 行级按钮控制(FileItem):
222
+ - `actions`: 当前行允许的操作列表,支持字符串数组或对象数组。
223
+ - `disabledActions`: 当前行禁用的操作 key 列表。
224
+
225
+ 下载配置(`table.download`):
226
+ - `urlField`: 下载地址字段名,默认 `url`。
227
+ - `nameField`: 下载文件名字段名,默认 `name`。
228
+ - 默认行为:统一通过请求文件流后触发浏览器下载,避免部分图片 / PDF / 跨域直链在当前窗口打开。
229
+ - `requestOptions.headers`: 需要鉴权时透传请求头。
230
+ - `requestOptions.method`: 下载请求方法,默认 `GET`。
231
+ - `requestOptions.withCredentials`: 是否携带 Cookie。
232
+
173
233
  ## 🛠 开发与构建
174
234
 
175
235
  ```bash
@@ -8,5 +8,7 @@ declare const _default: import('vue').DefineComponent<__VLS_Props, {}, {}, {}, {
8
8
  "update:modelValue": (...args: any[]) => void;
9
9
  }, string, import('vue').PublicProps, Readonly<__VLS_Props> & Readonly<{
10
10
  "onUpdate:modelValue"?: ((...args: any[]) => any) | undefined;
11
- }>, {}, {}, {}, {}, string, import('vue').ComponentProvideOptions, false, {}, any>;
11
+ }>, {}, {}, {}, {}, string, import('vue').ComponentProvideOptions, false, {
12
+ dialogRef: any;
13
+ }, any>;
12
14
  export default _default;
@@ -2,6 +2,7 @@ import { TableSchema, FileItem } from '../types';
2
2
  type __VLS_Props = {
3
3
  schema: TableSchema;
4
4
  files?: FileItem[];
5
+ mode?: 'view' | 'edit';
5
6
  };
6
7
  declare const _default: import('vue').DefineComponent<__VLS_Props, {}, {}, {}, {}, import('vue').ComponentOptionsMixin, import('vue').ComponentOptionsMixin, {
7
8
  action: (...args: any[]) => void;