|
1 | | -/* eslint-disable @typescript-eslint/no-non-null-assertion */ |
2 | | -/* |
| 1 | +import memoize from "memoizee"; |
3 | 2 | import { assert } from "tsafe/assert"; |
4 | | -import type { Equals } from "tsafe"; |
5 | | -import { objectKeys } from "tsafe/objectKeys"; |
6 | 3 | import { exclude } from "tsafe/exclude"; |
7 | | -import { parseCss } from "./parseCss"; |
8 | | -*/ |
9 | | -import memoize from "memoizee"; |
| 4 | +import { getRulesByBreakpoint } from "./breakpoints"; |
| 5 | +import { objectKeys } from "tsafe/objectKeys"; |
10 | 6 |
|
11 | 7 | export type SpacingTokenAndValue = { token: string; value: string }; |
12 | 8 |
|
13 | 9 | export const parseSpacing = memoize((rawCssCode: string): SpacingTokenAndValue[] => { |
14 | | - //const parsedCss = parseCss(rawCssCode); |
15 | | - console.log(rawCssCode); |
| 10 | + const spacingTokenAndValues: SpacingTokenAndValue[] = []; |
| 11 | + |
| 12 | + const rulesByBreakpoint = getRulesByBreakpoint(rawCssCode); |
| 13 | + |
| 14 | + objectKeys(rulesByBreakpoint).forEach(breakpoint => { |
| 15 | + rulesByBreakpoint[breakpoint].forEach(({ selectors, declarations }) => { |
| 16 | + let value: string | undefined = undefined; |
| 17 | + |
| 18 | + selectors |
| 19 | + .map((selector: string) => { |
| 20 | + const matchArr = selector.match(/^.fr-m-([0-9]+[a-zA-Z])+$/); |
| 21 | + return matchArr === null ? undefined : matchArr[1]; |
| 22 | + }) |
| 23 | + .filter(exclude(undefined)) |
| 24 | + .forEach(token => { |
| 25 | + if (value === undefined) { |
| 26 | + const declaration = declarations.find( |
| 27 | + (declaration: any) => |
| 28 | + declaration.type === "declaration" && |
| 29 | + declaration.property === "margin" |
| 30 | + ); |
| 31 | + assert(declaration !== undefined); |
| 32 | + value = (declaration as any).value.replace( |
| 33 | + /\s*!important\s*$/, |
| 34 | + "" |
| 35 | + ) as string; |
| 36 | + } |
| 37 | + |
| 38 | + const preExistingSpacingTokenAndValues = spacingTokenAndValues.find( |
| 39 | + o => o.token === token |
| 40 | + ); |
| 41 | + |
| 42 | + if (breakpoint !== "root") { |
| 43 | + assert( |
| 44 | + preExistingSpacingTokenAndValues !== undefined && |
| 45 | + preExistingSpacingTokenAndValues.value === value, |
| 46 | + "Need tu update the module so that we support spacing that varies depending on screen sizes, so far we assumed they didn't" |
| 47 | + ); |
| 48 | + |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + assert(spacingTokenAndValues.find(o => o.token === token) === undefined); |
| 53 | + |
| 54 | + spacingTokenAndValues.push({ |
| 55 | + token, |
| 56 | + value |
| 57 | + }); |
| 58 | + }); |
| 59 | + }); |
| 60 | + }); |
| 61 | + |
| 62 | + { |
| 63 | + const getValueWeight = (value: string) => { |
| 64 | + const match = value.match(/^([0-9.]+)/); |
| 65 | + assert(match !== null); |
| 66 | + const n = parseFloat(match[1]); |
| 67 | + assert(!isNaN(n)); |
| 68 | + return n; |
| 69 | + }; |
| 70 | + |
| 71 | + spacingTokenAndValues.sort((a, b) => { |
| 72 | + const w1 = getValueWeight(a.value); |
| 73 | + const w2 = getValueWeight(b.value); |
| 74 | + |
| 75 | + if (w1 === w2) { |
| 76 | + if (a.token < b.token) { |
| 77 | + return -1; |
| 78 | + } |
| 79 | + if (a.token > b.token) { |
| 80 | + return 1; |
| 81 | + } |
| 82 | + return 0; |
| 83 | + } |
| 84 | + |
| 85 | + return w1 - w2; |
| 86 | + }); |
| 87 | + } |
16 | 88 |
|
17 | | - return null as any; |
| 89 | + return spacingTokenAndValues; |
18 | 90 | }); |
0 commit comments