This repository has been archived by the owner on Dec 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 96
/
Image.editorConfig.ts
108 lines (92 loc) · 3.37 KB
/
Image.editorConfig.ts
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
import {
hidePropertiesIn,
hidePropertyIn,
moveProperty,
Problem,
Properties,
StructurePreviewProps,
transformGroupsIntoTabs
} from "@mendix/piw-utils-internal";
import { DatasourceEnum, ImagePreviewProps } from "../typings/ImageProps";
import StructurePreviewImageViewerSvg from "./assets/placeholder.svg";
type ImageViewPreviewPropsKey = keyof ImagePreviewProps;
const dataSourceProperties: ImageViewPreviewPropsKey[] = ["imageObject", "imageUrl", "imageIcon"];
function filterDataSourceProperties(sourceProperty: DatasourceEnum): ImageViewPreviewPropsKey[] {
switch (sourceProperty) {
case "image":
return dataSourceProperties.filter(prop => prop !== "imageObject");
case "imageUrl":
return dataSourceProperties.filter(prop => prop !== "imageUrl");
case "icon":
return dataSourceProperties.filter(prop => prop !== "imageIcon");
default:
return dataSourceProperties;
}
}
function reorderTabsForStudio(tabs: Properties): void {
const dimensionsTabIndex = tabs.findIndex(
tab => tab.caption === "Dimensions" && tab.properties && tab.properties.length > 0
);
const dataSourceTabIndex = tabs.findIndex(
tab => tab.caption === "Data source" && tab.properties && tab.properties.length > 0
);
if (dimensionsTabIndex >= 0 && dataSourceTabIndex >= 0) {
moveProperty(dimensionsTabIndex, dataSourceTabIndex + 1, tabs);
}
}
export function getProperties(
values: ImagePreviewProps,
defaultProperties: Properties,
platform: "web" | "desktop"
): Properties {
hidePropertiesIn(defaultProperties, values, filterDataSourceProperties(values.datasource));
if (values.datasource !== "image") {
hidePropertyIn(defaultProperties, values, "defaultImageDynamic");
}
if (values.heightUnit === "auto") {
hidePropertyIn(defaultProperties, values, "height");
}
if (values.widthUnit === "auto") {
hidePropertyIn(defaultProperties, values, "width");
}
if (values.datasource === "icon" && values.imageIcon?.type === "glyph") {
hidePropertiesIn(defaultProperties, values, ["widthUnit", "width", "heightUnit", "height"]);
} else {
hidePropertyIn(defaultProperties, values, "iconSize");
}
if (values.onClickType !== "action") {
hidePropertyIn(defaultProperties, values, "onClick");
}
if (values.datasource !== "image") {
hidePropertyIn(defaultProperties, values, "displayAs");
}
if (platform === "web") {
transformGroupsIntoTabs(defaultProperties);
reorderTabsForStudio(defaultProperties);
}
return defaultProperties;
}
export function getPreview(): StructurePreviewProps | null {
return {
type: "Image",
document: decodeURIComponent(StructurePreviewImageViewerSvg.replace("data:image/svg+xml,", "")),
height: 100,
width: 100
};
}
export function check(values: ImagePreviewProps): Problem[] {
const errors: Problem[] = [];
if (values.datasource === "imageUrl" && !values.imageUrl) {
errors.push({
property: "imageUrl",
message: "No image link provided"
});
}
if (values.datasource === "icon" && !values.imageIcon) {
errors.push({
property: "imageIcon",
message: "No icon selected"
});
}
return errors;
}