@genexus/mercury 0.3.1 → 0.3.2

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.
@@ -0,0 +1,84 @@
1
+ export type AssetsMetadata = {
2
+ category: string;
3
+ name: string;
4
+ colorType?: string;
5
+ };
6
+ /**
7
+ * For example:
8
+ * ```
9
+ * {
10
+ * icons: {
11
+ * objects: { // Category
12
+ * stencil: { // Icon Name
13
+ * enabled: { // State
14
+ * name: "objects_stencil--enabled"
15
+ * }
16
+ * },
17
+ * },
18
+ * windows-tools: { // Category
19
+ * workflow: { // Icon Name
20
+ * "on-surface": { // Color Type
21
+ * enabled: { // State
22
+ * name: "windows-tools_workflow_on-surface--enabled"
23
+ * },
24
+ * hover: {
25
+ * name: "windows-tools_workflow_on-surface--hover"
26
+ * }
27
+ * }
28
+ * }
29
+ * }
30
+ * }
31
+ * }
32
+ * ```
33
+ */
34
+ export type Assets = {
35
+ icons: {
36
+ [key in string]: AssetsCategories;
37
+ };
38
+ };
39
+ export type AssetsCategories = {
40
+ [key: string]: AssetsIconName;
41
+ };
42
+ export type AssetsIconName = {
43
+ [key: string]: AssetsColorType;
44
+ } | AssetsColorType;
45
+ export type AssetsColorType = {
46
+ [key: string]: AssetsIconMetadata;
47
+ };
48
+ export interface AssetsIconMetadata {
49
+ name: string;
50
+ }
51
+ export type ImageMultiState = {
52
+ base: string;
53
+ hover?: string;
54
+ active?: string;
55
+ focus?: string;
56
+ disabled?: string;
57
+ };
58
+ /**
59
+ * Given a vendor and its assets, it register the assets of the vendor. After
60
+ * the registration, the `getAsset` function can be used to retrieve any assets
61
+ * related to the vendor.
62
+ * @param vendorName The name of the vendor (for example, "Mercury"). Must be unique.
63
+ * @param vendorAlias The alias of the vendor (for example, "mer"). Must be unique.
64
+ * @param assets The assets (generated by the SVG Sass Generator) to register.
65
+ */
66
+ export declare const registerAssets: (vendorName: string, vendorAlias: string, assets: Assets) => void;
67
+ /**
68
+ * @param vendorAlias The name or alias of the vendor.
69
+ * @param assetMetadata The metadata required to retrieve the icon
70
+ * @return The required asset or undefined if not found.
71
+ */
72
+ export declare const getAsset: (vendorAliasOrName: string, assetMetadata: AssetsMetadata) => AssetsColorType | undefined;
73
+ /**
74
+ * Given the metadata of the icon, it transforms the metadata into a string
75
+ * that contains the given information.
76
+ */
77
+ export declare const iconMetadataToPath: (iconMetadata: AssetsMetadata, vendorAlias?: string) => `${string}/${string}/${string}${string}`;
78
+ export declare const getImagePathCallback: (iconPath: string) => ImageMultiState | undefined;
79
+ export declare const getTreeViewImagePathCallback: (item: {
80
+ startImgSrc?: string;
81
+ endImgSrc?: string;
82
+ }, direction: "start" | "end") => {
83
+ default: ImageMultiState;
84
+ } | undefined;
@@ -0,0 +1,126 @@
1
+ import { MERCURY_ASSETS } from "./assets/MERCURY_ASSETS";
2
+ const ASSETS_BY_VENDOR = {};
3
+ const ALIAS_TO_VENDOR_NAME = {};
4
+ const SEPARATOR = "/";
5
+ const MERCURY_ALIAS = "mer";
6
+ /**
7
+ * Given a vendor and its assets, it register the assets of the vendor. After
8
+ * the registration, the `getAsset` function can be used to retrieve any assets
9
+ * related to the vendor.
10
+ * @param vendorName The name of the vendor (for example, "Mercury"). Must be unique.
11
+ * @param vendorAlias The alias of the vendor (for example, "mer"). Must be unique.
12
+ * @param assets The assets (generated by the SVG Sass Generator) to register.
13
+ */
14
+ export const registerAssets = (vendorName, vendorAlias, assets) => {
15
+ // Already registered
16
+ if (ASSETS_BY_VENDOR[vendorName] || ALIAS_TO_VENDOR_NAME[vendorAlias]) {
17
+ return;
18
+ }
19
+ ASSETS_BY_VENDOR[vendorName] = assets;
20
+ ALIAS_TO_VENDOR_NAME[vendorAlias] = vendorName;
21
+ };
22
+ /**
23
+ * @param vendorAlias The name or alias of the vendor.
24
+ * @param assetMetadata The metadata required to retrieve the icon
25
+ * @return The required asset or undefined if not found.
26
+ */
27
+ export const getAsset = (vendorAliasOrName, assetMetadata) => {
28
+ const vendorName = ALIAS_TO_VENDOR_NAME[vendorAliasOrName] ?? vendorAliasOrName;
29
+ const vendorAssets = ASSETS_BY_VENDOR[vendorName];
30
+ if (!vendorAssets) {
31
+ return undefined;
32
+ }
33
+ const iconCategoryObject = vendorAssets.icons[assetMetadata.category];
34
+ // The category does not exists
35
+ if (!iconCategoryObject) {
36
+ return undefined;
37
+ }
38
+ const iconNameObject = iconCategoryObject[assetMetadata.name];
39
+ // The name in the category does not exists
40
+ if (!iconNameObject) {
41
+ return undefined;
42
+ }
43
+ return assetMetadata.colorType
44
+ ? iconNameObject[assetMetadata.colorType]
45
+ : iconNameObject;
46
+ };
47
+ /**
48
+ * Given the metadata of the icon, it transforms the metadata into a string
49
+ * that contains the given information.
50
+ */
51
+ export const iconMetadataToPath = (iconMetadata, vendorAlias = MERCURY_ALIAS) => {
52
+ const additionalInfo = iconMetadata.colorType
53
+ ? `${SEPARATOR}${iconMetadata.colorType}`
54
+ : "";
55
+ return `${vendorAlias}${SEPARATOR}${iconMetadata.category}${SEPARATOR}${iconMetadata.name}${additionalInfo}`;
56
+ };
57
+ const getCustomFullValue = (iconName, vendorAliasOrName, suffix) => {
58
+ const vendorPrefix = vendorAliasOrName === MERCURY_ALIAS ? "" : `${vendorAliasOrName}-`;
59
+ return suffix
60
+ ? `var(--icon__${vendorPrefix}${iconName}--${suffix})`
61
+ : `var(--icon__${vendorPrefix}${iconName})`;
62
+ };
63
+ /**
64
+ * Parses the incoming iconMetadata, assuming Mercury as the default vendor if
65
+ * the vendor is not specified (it is not found in the register)
66
+ */
67
+ const parseIconMetadata = (iconPath) => {
68
+ const iconMetadata = iconPath.split(SEPARATOR);
69
+ const vendorAliasOrName = iconMetadata[0];
70
+ const vendorName = ALIAS_TO_VENDOR_NAME[vendorAliasOrName] ?? vendorAliasOrName;
71
+ const vendorAssets = ASSETS_BY_VENDOR[vendorName];
72
+ // The vendor is not contained in the path, assume by default Mercury.
73
+ if (!vendorAssets) {
74
+ const category = iconMetadata[0]; // Assume that the first value is the category
75
+ const name = iconMetadata[1];
76
+ const colorType = iconMetadata[2];
77
+ return {
78
+ vendor: MERCURY_ALIAS,
79
+ category,
80
+ name,
81
+ colorType: colorType
82
+ };
83
+ }
84
+ const category = iconMetadata[1];
85
+ const name = iconMetadata[2];
86
+ const colorType = iconMetadata[3];
87
+ return {
88
+ vendor: vendorAliasOrName,
89
+ category,
90
+ name,
91
+ colorType: colorType
92
+ };
93
+ };
94
+ export const getImagePathCallback = (iconPath) => {
95
+ const { vendor, category, name, colorType } = parseIconMetadata(iconPath);
96
+ const assetStates = getAsset(vendor, colorType ? { category, name, colorType } : { category, name });
97
+ if (!assetStates) {
98
+ return undefined;
99
+ }
100
+ const result = {
101
+ base: getCustomFullValue(assetStates.enabled.name, vendor)
102
+ };
103
+ if (assetStates.hover) {
104
+ result.hover = getCustomFullValue(assetStates.hover.name, vendor);
105
+ }
106
+ if (assetStates.active) {
107
+ result.active = getCustomFullValue(assetStates.active.name, vendor);
108
+ }
109
+ if (assetStates.disabled) {
110
+ result.disabled = getCustomFullValue(assetStates.disabled.name, vendor);
111
+ }
112
+ return result;
113
+ };
114
+ export const getTreeViewImagePathCallback = (item, direction) => {
115
+ if ((!item.startImgSrc && direction === "start") ||
116
+ (!item.endImgSrc && direction === "end")) {
117
+ return undefined;
118
+ }
119
+ const paths = getImagePathCallback(direction === "start" ? item.startImgSrc : item.endImgSrc);
120
+ if (!paths) {
121
+ return undefined;
122
+ }
123
+ return { default: paths };
124
+ };
125
+ // Initialize Mercury at the start
126
+ registerAssets("Mercury", MERCURY_ALIAS, MERCURY_ASSETS);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@genexus/mercury",
3
- "version": "0.3.1",
3
+ "version": "0.3.2",
4
4
  "description": "Mercury is the design system designed for GeneXus IDE Web and GeneXus Next",
5
5
  "main": "dist/mercury.scss",
6
6
  "module": "dist/assets-manager.js",
@@ -11,9 +11,9 @@
11
11
  "type": "module",
12
12
  "scripts": {
13
13
  "build": "tsc && npm run icons-svg && npm run icons-sass && npm run build.scss && npm run copy-tasks && npm run showcase.scss",
14
- "build-no-svg": "npm run build.scss && npm run copy-tasks",
14
+ "build-no-svg": "tsc && npm run build.scss && npm run copy-tasks",
15
15
  "build.scss": "scss-bundle -e ./src/mercury.scss -o dist/mercury.scss && sass test/test.scss dist/css/mercury.css",
16
- "test": "sass test/test.scss test/mercury.css",
16
+ "test": "sass test/test.scss test/mercury.css",
17
17
  "validate.ci": "npm run build-no-svg",
18
18
  "icons-svg": "ssg-svg --srcDir=src/icons/svg-source --outDir=src/assets/icons/_generated/ --configFilePath=src/icons/svg-source/.config/color-states.json --showcaseDir=showcase/icons/ --showcaseBaseHref=../assets/icons/ --logDir=./log --objectFilePath=src/assets/MERCURY_ASSETS.ts",
19
19
  "icons-sass": "ssg-sass src/assets/icons/ src/icons/_generated/ src/icons/svg-source/.config/color-states.json",
@@ -24,7 +24,7 @@
24
24
  "license": "MIT",
25
25
  "devDependencies": {
26
26
  "@atao60/fse-cli": "^0.1.9",
27
- "@genexus/svg-sass-generator": "1.1.15",
27
+ "@genexus/svg-sass-generator": "1.1.16",
28
28
  "chokidar": "^3.6.0",
29
29
  "chokidar-cli": "^3.0.0",
30
30
  "sass": "~1.72.0",