-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathFileUploader.editorConfig.ts
More file actions
139 lines (123 loc) · 4.63 KB
/
Copy pathFileUploader.editorConfig.ts
File metadata and controls
139 lines (123 loc) · 4.63 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
import { FileUploaderPreviewProps } from "../typings/FileUploaderProps";
import { parseAllowedFormats } from "./utils/parseAllowedFormats";
import { hideNestedPropertiesIn, hidePropertiesIn, Problem, Properties } from "@mendix/pluggable-widgets-tools";
import { predefinedFormats } from "./utils/predefinedFormats";
export function getProperties(
values: FileUploaderPreviewProps,
properties: Properties /* , target: Platform*/
): Properties {
if (!values.readOnlyMode) {
if (values.uploadMode === "files") {
hidePropertiesIn(properties, values, ["associatedImages", "createImageAction"]);
processAllowedFormats(values, properties);
} else {
hidePropertiesIn(properties, values, ["associatedFiles", "createFileAction", "allowedFileFormats"]);
}
} else {
hidePropertiesIn(properties, values, [
values.uploadMode === "files" ? "associatedImages" : "associatedFiles",
"createImageAction",
"createFileAction",
"allowedFileFormats",
"maxFilesPerUpload",
"maxFileSize",
"objectCreationTimeout"
]);
}
if (values.enableCustomButtons) {
values.customButtons.forEach((_button, index) => {
hideNestedPropertiesIn(
properties,
values,
"customButtons",
index,
values.uploadMode === "files" ? ["buttonActionImage"] : ["buttonActionFile"]
);
});
} else {
hidePropertiesIn(properties, values, ["customButtons"]);
}
return properties;
}
function processAllowedFormats(values: FileUploaderPreviewProps, properties: Properties): void {
values.allowedFileFormats.forEach((p, index) => {
if (p.configMode === "simple") {
hideNestedPropertiesIn(properties, values, "allowedFileFormats", index, [
"mimeType",
"extensions",
"typeFormatDescription"
]);
} else {
hideNestedPropertiesIn(properties, values, "allowedFileFormats", index, ["predefinedType"]);
}
});
const fileFormats = properties[0].properties?.find(p => p.key === "allowedFileFormats");
if (fileFormats) {
// place object headers
fileFormats.objectHeaders = ["File type", ""];
fileFormats.objects?.forEach((o, index) => {
const val = values.allowedFileFormats[index];
if (val.configMode === "simple") {
o.captions = [predefinedFormats[val.predefinedType].description, " "];
} else {
o.captions = [val.typeFormatDescription, `${mimeConfigColumnText(val.mimeType, val.extensions)}`];
}
});
}
}
export function check(values: FileUploaderPreviewProps): Problem[] {
const errors: Problem[] = [];
// Add errors to the above array to throw errors in Studio and Studio Pro.
if (!values.readOnlyMode) {
if (values.allowedFileFormats.length) {
try {
parseAllowedFormats(values.allowedFileFormats);
} catch (e: unknown) {
if (e instanceof Error) {
errors.push({
property: "allowedFileFormats",
message: e.message
});
}
}
}
if (values.enableCustomButtons) {
// check that at max one actions is default
const defaultIdx = new Set<number>();
values.customButtons.forEach((_button, index) => {
if (_button.buttonIsDefault) {
defaultIdx.add(index);
}
});
if (defaultIdx.size > 1) {
errors.push({
property: `customButtons`,
message: `Only one default button is allowed.`
});
}
}
}
return errors;
}
function mimeConfigColumnText(mime: string, exts: string): string {
if (mime && exts) {
return `[${mime}](${exts})`;
}
if (mime && !exts) {
return `[${mime}]`;
}
if (!mime && exts) {
return `(${exts})`;
}
return "<not set>";
}
// export function getPreview(values: FileUploaderPreviewProps, isDarkMode: boolean, version: number[]): PreviewProps {
// // Customize your pluggable widget appearance for Studio Pro.
// return {
// type: "Container",
// children: []
// }
// }
export function getCustomCaption(_values: FileUploaderPreviewProps): string {
return `File uploader (${_values.uploadMode}${_values.readOnlyMode && ", read only"})`;
}