Skip to content

Commit a3998bd

Browse files
committed
fix: refactor showIn logic - now upload plugin respects showIn configuration from path column. If it is set to ediot:false it will not show widget on edit page. Also it supports dynamic values
``` showIn: { edit: ({ adminUser }) => adminUser.role === 'admin' } ```
1 parent ac452a3 commit a3998bd

File tree

2 files changed

+41
-47
lines changed

2 files changed

+41
-47
lines changed

index.ts

Lines changed: 41 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -156,32 +156,35 @@ getBucketLifecycleConfiguration on bucket ${this.options.s3Bucket} in region ${t
156156
if (!resourceConfig.columns[pathColumnIndex].components) {
157157
resourceConfig.columns[pathColumnIndex].components = {};
158158
}
159+
const pathColumn = resourceConfig.columns[pathColumnIndex];
159160

160-
if (this.options.preview?.showInList || this.options.preview?.showInList === undefined) {
161-
// add preview column to list
162-
resourceConfig.columns[pathColumnIndex].components.list = {
163-
file: this.componentPath('preview.vue'),
164-
meta: pluginFrontendOptions,
165-
};
166-
}
161+
// add preview column to list
162+
resourceConfig.columns[pathColumnIndex].components.list = {
163+
file: this.componentPath('preview.vue'),
164+
meta: pluginFrontendOptions,
165+
};
167166

168-
if (this.options.preview?.showInShow || this.options.preview?.showInShow === undefined) {
169-
resourceConfig.columns[pathColumnIndex].components.show = {
170-
file: this.componentPath('preview.vue'),
171-
meta: pluginFrontendOptions,
172-
};
173-
}
167+
resourceConfig.columns[pathColumnIndex].components.show = {
168+
file: this.componentPath('preview.vue'),
169+
meta: pluginFrontendOptions,
170+
};
174171

175172
// insert virtual column after path column if it is not already there
176173
const virtualColumnIndex = resourceConfig.columns.findIndex((column: any) => column.name === virtualColumn.name);
177174
if (virtualColumnIndex === -1) {
178175
resourceConfig.columns.splice(pathColumnIndex + 1, 0, virtualColumn);
179176
}
180177

181-
// if showIn of path column has 'create' or 'edit' remove it
182-
const pathColumn = resourceConfig.columns[pathColumnIndex];
183-
if (pathColumn.showIn && (pathColumn.showIn.create || pathColumn.showIn.edit)) {
184-
pathColumn.showIn = { ...pathColumn.showIn, create: false, edit: false };
178+
179+
// if showIn of path column has 'create' or 'edit' remove it but use it for virtual column
180+
if (pathColumn.showIn && (pathColumn.showIn.create !== undefined)) {
181+
virtualColumn.showIn = { ...virtualColumn.showIn, create: pathColumn.showIn.create };
182+
pathColumn.showIn = { ...pathColumn.showIn, create: false };
183+
}
184+
185+
if (pathColumn.showIn && (pathColumn.showIn.edit !== undefined)) {
186+
virtualColumn.showIn = { ...virtualColumn.showIn, edit: pathColumn.showIn.edit };
187+
pathColumn.showIn = { ...pathColumn.showIn, edit: false };
185188
}
186189

187190
virtualColumn.required = pathColumn.required;
@@ -229,30 +232,33 @@ getBucketLifecycleConfiguration on bucket ${this.options.s3Bucket} in region ${t
229232

230233

231234
// add show hook to get presigned URL
232-
resourceConfig.hooks.show.afterDatasourceResponse.push(async ({ response }: { response: any }) => {
233-
const record = response[0];
234-
if (!record) {
235-
return { ok: true };
236-
}
237-
if (record[pathColumnName]) {
238-
const s3 = new S3({
239-
credentials: {
240-
accessKeyId: this.options.s3AccessKeyId,
241-
secretAccessKey: this.options.s3SecretAccessKey,
242-
},
235+
if (pathColumn.showIn.show) {
243236

244-
region: this.options.s3Region,
245-
});
237+
resourceConfig.hooks.show.afterDatasourceResponse.push(async ({ response }: { response: any }) => {
238+
const record = response[0];
239+
if (!record) {
240+
return { ok: true };
241+
}
242+
if (record[pathColumnName]) {
243+
const s3 = new S3({
244+
credentials: {
245+
accessKeyId: this.options.s3AccessKeyId,
246+
secretAccessKey: this.options.s3SecretAccessKey,
247+
},
248+
249+
region: this.options.s3Region,
250+
});
246251

247-
await this.genPreviewUrl(record, s3);
248-
}
249-
return { ok: true };
250-
});
252+
await this.genPreviewUrl(record, s3);
253+
}
254+
return { ok: true };
255+
});
256+
}
251257

252258
// ** HOOKS FOR LIST **//
253259

254260

255-
if (this.options.preview?.showInList || this.options.preview?.showInList === undefined) {
261+
if (pathColumn.showIn.list) {
256262
resourceConfig.hooks.list.afterDatasourceResponse.push(async ({ response }: { response: any }) => {
257263
const s3 = new S3({
258264
credentials: {

types.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -64,18 +64,6 @@ export type PluginOptions = {
6464

6565
preview: {
6666

67-
/**
68-
* Whether to show preview of image instead of path in list field
69-
* By default true
70-
*/
71-
showInList?: boolean,
72-
73-
/**
74-
* Whether to show preview of image instead of path in list field
75-
* By default true
76-
*/
77-
showInShow?: boolean,
78-
7967
/**
8068
* Maximum width of the preview image
8169
*/

0 commit comments

Comments
 (0)