2024-01-18 12:35:25 +00:00
|
|
|
import Underlay from 'src/components/underlay.style.js'
|
|
|
|
import Panel from 'src/components/panel.style.js'
|
|
|
|
import Button from 'src/components/button.style.js'
|
|
|
|
import Text from 'src/components/text.style.js'
|
|
|
|
import Icon from 'src/components/icon.style.js'
|
|
|
|
|
|
|
|
const root = Underlay
|
|
|
|
const components = {
|
|
|
|
Underlay,
|
|
|
|
Panel,
|
|
|
|
Button,
|
|
|
|
Text,
|
|
|
|
Icon
|
|
|
|
}
|
|
|
|
|
|
|
|
// This gives you an array of arrays of all possible unique (i.e. order-insensitive) combinations
|
2024-01-22 22:43:46 +00:00
|
|
|
export const getAllPossibleCombinations = (array) => {
|
2024-01-18 12:35:25 +00:00
|
|
|
const combos = [array.map(x => [x])]
|
|
|
|
for (let comboSize = 2; comboSize <= array.length; comboSize++) {
|
|
|
|
const previous = combos[combos.length - 1]
|
|
|
|
const selfSet = new Set()
|
|
|
|
const newCombos = previous.map(self => {
|
|
|
|
self.forEach(x => selfSet.add(x))
|
|
|
|
const nonSelf = array.filter(x => !selfSet.has(x))
|
|
|
|
return nonSelf.map(x => [...self, x])
|
|
|
|
})
|
|
|
|
const flatCombos = newCombos.reduce((acc, x) => [...acc, ...x], [])
|
|
|
|
combos.push(flatCombos)
|
|
|
|
}
|
|
|
|
return combos.reduce((acc, x) => [...acc, ...x], [])
|
|
|
|
}
|
|
|
|
|
2024-01-22 22:43:46 +00:00
|
|
|
export const ruleToSelector = (rule) => {
|
|
|
|
const component = components[rule.component]
|
|
|
|
const { states, variants, selector } = component
|
|
|
|
|
|
|
|
const applicableStates = (rule.state.filter(x => x !== 'normal') || []).map(state => states[state])
|
|
|
|
|
|
|
|
const applicableVariantName = (rule.variant || 'normal')
|
|
|
|
let applicableVariant = ''
|
|
|
|
if (applicableVariantName !== 'normal') {
|
|
|
|
applicableVariant = variants[applicableVariantName]
|
|
|
|
}
|
|
|
|
|
|
|
|
const selectors = [selector, applicableVariant, ...applicableStates]
|
|
|
|
.toSorted((a, b) => {
|
|
|
|
if (a.startsWith(':')) return 1
|
|
|
|
else return -1
|
|
|
|
})
|
|
|
|
.join('')
|
|
|
|
|
|
|
|
if (rule.parent) {
|
|
|
|
return ruleToSelector(rule.parent) + ' ' + selectors
|
|
|
|
}
|
|
|
|
return selectors
|
|
|
|
}
|
|
|
|
|
|
|
|
export const init = (ruleset) => {
|
2024-01-18 12:35:25 +00:00
|
|
|
const rootName = root.name
|
|
|
|
const rules = []
|
2024-01-22 22:43:46 +00:00
|
|
|
const rulesByComponent = {}
|
|
|
|
|
|
|
|
const addRule = (rule) => {
|
|
|
|
rules.push(rule)
|
|
|
|
rulesByComponent[rule.component] = rulesByComponent[rule.component] || []
|
|
|
|
rulesByComponent.push(rule)
|
|
|
|
}
|
|
|
|
|
|
|
|
ruleset.forEach(rule => {
|
|
|
|
|
|
|
|
})
|
2024-01-18 12:35:25 +00:00
|
|
|
|
|
|
|
const processInnerComponent = (component, parent) => {
|
|
|
|
const {
|
2024-01-22 22:43:46 +00:00
|
|
|
validInnerComponents = [],
|
2024-01-18 12:35:25 +00:00
|
|
|
states: originalStates = {},
|
2024-01-22 22:43:46 +00:00
|
|
|
variants: originalVariants = {},
|
|
|
|
name
|
2024-01-18 12:35:25 +00:00
|
|
|
} = component
|
|
|
|
|
|
|
|
const states = { normal: '', ...originalStates }
|
|
|
|
const variants = { normal: '', ...originalVariants }
|
|
|
|
const innerComponents = validInnerComponents.map(name => components[name])
|
|
|
|
|
|
|
|
const stateCombinations = getAllPossibleCombinations(Object.keys(states))
|
|
|
|
const stateVariantCombination = Object.keys(variants).map(variant => {
|
|
|
|
return stateCombinations.map(state => ({ variant, state }))
|
|
|
|
}).reduce((acc, x) => [...acc, ...x], [])
|
|
|
|
|
|
|
|
stateVariantCombination.forEach(combination => {
|
2024-01-22 22:43:46 +00:00
|
|
|
// addRule(({
|
|
|
|
// parent,
|
|
|
|
// component: component.name,
|
|
|
|
// state: combination.state,
|
|
|
|
// variant: combination.variant
|
|
|
|
// }))
|
|
|
|
|
|
|
|
innerComponents.forEach(innerComponent => processInnerComponent(innerComponent, { parent, component: name, ...combination }))
|
2024-01-18 12:35:25 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
processInnerComponent(components[rootName])
|
2024-01-22 22:43:46 +00:00
|
|
|
return rules
|
2024-01-18 12:35:25 +00:00
|
|
|
}
|