Skip to content

Commit ca10a24

Browse files
committed
Merge branch 'new_color_picker'
2 parents a61ce58 + 0b3c72a commit ca10a24

15 files changed

+329
-270
lines changed

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ node_modules/
44
/.yarn_home/
55
/test/integration/
66
/dsfr/
7+
/src/fr/generatedFromCss/

README.fr.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,7 @@ DSFR en pur JavaScript/CSS.
3535

3636
[Lien vers la vidéo démo](https://youtu.be/5q88JgXUAY4)
3737

38-
> Remarque pour les agents non familiers avec TypeScript: Cette librairie, à l'instar de toute librairie développez-en TypeScript
39-
> **peut** être utilisé dans des projets n'utilisant pas TypeScript!
40-
> La distribution publiée sur NPM est constituée de fichier JavaScript couplé à des fichiers de déclaration de types. Ces derniers
41-
> on pour objet d'aider au développement, mais n'intervienne pas dans le fonctionnement de la libraire.
38+
> Bien que cette bibliothèque soit écrit en TypeScript, l'utilisation de TypeScript dans votre application est facultative (mais recommandée car elle présente des avantages exceptionnels pour vous et votre base de code).
4239
4340
- [x] une interface de programmation strictement typée et bien documentée.
4441
- [x] Garantie d'être toujours à jour avec les [dernières évolutions du DSFR](https://www.systeme-de-design.gouv.fr/).

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ This module is a wrapper/compatibility layer for [@gouvfr/dsfr](https://github.c
3636

3737
[Link to the demo video](https://youtu.be/5q88JgXUAY4)
3838

39-
> For TypeScript and JavaScript projects.
39+
> While this module is written in TypeScript, using TypeScript in your application is optional (but recommended as it comes with outstanding benefits to both you and your codebase).
4040
4141
- [x] Fully TypeSafe, well documented API.
4242
- [x] Always in up to date with latest the DSFR evolutions.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,14 @@
9797
"eslint-config-prettier": "^8.3.0",
9898
"eslint-plugin-storybook": "^0.6.7",
9999
"evt": "^2.4.2",
100+
"fzf": "^0.5.1",
100101
"husky": "^4.3.8",
101102
"lint-staged": "^11.0.0",
102103
"memoizee": "^0.4.15",
103104
"next": "12.3.1",
104105
"oppa": "^0.4.0",
105106
"parse-numeric-range": "^1.3.0",
107+
"powerhooks": "^0.21.0",
106108
"prettier": "^2.3.0",
107109
"react": "18.2.0",
108110
"react-dom": "18.2.0",

src/fr/colorResolver.ts

Lines changed: 0 additions & 55 deletions
This file was deleted.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { parseColorOptions } from "./colorOptions";
2+
import { parseColorDecision } from "./colorDecisions";
3+
import { exclude } from "tsafe/exclude";
4+
import type { ColorDecision } from "./colorDecisions";
5+
import type { ColorOption } from "./colorOptions";
6+
import { symToStr } from "tsafe/symToStr";
7+
8+
export type ColorDecisionAndCorrespondingOption = Omit<ColorDecision, "optionThemePath"> & {
9+
colorOption: ColorOption;
10+
};
11+
12+
export function generateColorDecisionAndCorrespondingOptionsTsCode(rawCssCode: string): string {
13+
const colorOptions = parseColorOptions(rawCssCode);
14+
15+
const colorDecisionAndCorrespondingOption = parseColorDecision(rawCssCode)
16+
.map(colorDecision => {
17+
const colorOption = colorOptions.find(
18+
colorOption =>
19+
colorOption.themePath.join(".") === colorDecision.optionThemePath.join(".")
20+
);
21+
return colorOption === undefined ? undefined : ([colorDecision, colorOption] as const);
22+
})
23+
.filter(exclude(undefined))
24+
.map(
25+
([
26+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
27+
{ optionThemePath, ...colorDecision },
28+
colorOption
29+
]): ColorDecisionAndCorrespondingOption => ({
30+
...colorDecision,
31+
colorOption
32+
})
33+
);
34+
35+
return [
36+
``,
37+
`export const ${symToStr({ colorDecisionAndCorrespondingOption })}= ${JSON.stringify(
38+
colorDecisionAndCorrespondingOption,
39+
null,
40+
4
41+
)} as const;`,
42+
``
43+
].join("\n");
44+
}

src/scripts/build/cssToTs/colorDecisions.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,8 @@ export function getThemePath(parsedColorDecisionName: ParsedColorDecisionName) {
161161

162162
export type ColorDecision = {
163163
colorDecisionName: `--${string}`;
164-
themePath: string[];
165-
optionThemePath: string[];
164+
themePath: readonly string[];
165+
optionThemePath: readonly string[];
166166
};
167167

168168
export const parseColorDecision = memoize((rawCssCode: string): ColorDecision[] => {
@@ -230,7 +230,7 @@ export function generateGetColorDecisionsTsCode(rawCssCode: string): string {
230230
return hash;
231231
})();
232232

233-
function req(obj: any, path: string[]): void {
233+
function req(obj: any, path: readonly string[]): void {
234234
const [propertyName, ...pathRest] = path;
235235

236236
if (pathRest.length === 0) {

src/scripts/build/cssToTs/colorOptions.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ export function getThemePath(parsedColorOptionName: ParsedColorOptionName): stri
320320

321321
export type ColorOption = {
322322
colorOptionName: `--${string}`;
323-
themePath: string[];
323+
themePath: readonly string[];
324324
color:
325325
| `#${string}`
326326
| {
@@ -404,7 +404,7 @@ export function generateGetColorOptionsTsCode(rawCssCode: string) {
404404
return hash;
405405
})();
406406

407-
function req(obj: any, path: string[]): void {
407+
function req(obj: any, path: readonly string[]): void {
408408
const [propertyName, ...pathRest] = path;
409409

410410
if (pathRest.length === 0) {

src/scripts/build/cssToTs/colorResolution.ts

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/scripts/build/cssToTs/cssToTs.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { getProjectRoot } from "../../../bin/tools/getProjectRoot";
55
import { generateTypographyTsCode } from "./typography";
66
import { generateSpacingTsCode } from "./spacing";
77
import { generateClassNamesTsCode } from "./classNames";
8-
import { generateColorResolutionTsCode } from "./colorResolution";
8+
import { generateColorDecisionAndCorrespondingOptionsTsCode } from "./colorDecisionAndCorrespondingOptions";
99
import * as fs from "fs";
1010
import { join as pathJoin, basename as pathBasename, relative as pathRelative } from "path";
1111
import type { Icon } from "../../../bin/only-include-used-icons";
@@ -114,9 +114,14 @@ export function cssToTs(params: {
114114
);
115115

116116
fs.writeFileSync(
117-
pathJoin(generatedDirPath, "colorResolution.ts"),
117+
pathJoin(generatedDirPath, "colorDecisionAndCorrespondingOptions.ts"),
118118
Buffer.from(
119-
[warningMessage, ``, generateColorResolutionTsCode(rawDsfrCssCode), ``].join("\n"),
119+
[
120+
warningMessage,
121+
``,
122+
generateColorDecisionAndCorrespondingOptionsTsCode(rawDsfrCssCode),
123+
``
124+
].join("\n"),
120125
"utf8"
121126
)
122127
);

0 commit comments

Comments
 (0)