forked from CoreBunch/Instatic
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscaleModule.ts
More file actions
404 lines (364 loc) · 13.8 KB
/
Copy pathscaleModule.ts
File metadata and controls
404 lines (364 loc) · 13.8 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/**
* Shared engine that turns a "framework scale" configuration (typography OR
* spacing) into:
* - the flat list of CSS custom properties at `:root`,
* - the rendered `:root { … }` block,
* - the per-step utility classes (e.g. `.text-xs`, `.padding-m`, …).
*
* Typography and spacing are sister modules: the math, the variable-naming
* convention, the `mode === 'fluid_manual'` branch, the class-pattern
* expansion (`*` and `{step}`) — all identical. The only differences are:
*
* - the per-group base size lives at `group.min.size` for spacing and
* `group.min.fontSize` for typography (`getMinBaseSize` / `getMaxBaseSize`),
* - the CSS property whitelist (`propertyKeymap`),
* - the class id prefix and the `family` discriminator in
* `GeneratedClassMetadata`,
* - the `tags` array on the emitted `StyleRule`.
*
* Both `framework/typography.ts` and `framework/spacing.ts` are now thin
* adapters that call this factory with their type-specific extractors.
*/
import type { StyleRule, CSSPropertyBag } from '@core/page-tree'
import { classKindSelector } from '@core/page-tree'
import type { FrameworkScaleManualSize, FrameworkScaleMode } from '@core/framework-schema'
import {
computeFluidScale,
convertToVariableDeclarationName,
declarationFromStep,
effectiveScaleRatio,
type FrameworkPreferences,
getVariableName,
manualSizeVariableName,
} from './scale'
// ---------------------------------------------------------------------------
// Public types — shared between typography and spacing
// ---------------------------------------------------------------------------
type FrameworkScaleFamily = 'spacing' | 'typography'
/** Output shape — same for typography and spacing. */
export interface FrameworkScaleVariable {
name: string
value: string
groupId: string
groupName: string
/** Step suffix as it appears in the user-defined `steps` string ("xs", "m"…). */
step: string
/** Manual sizes have an arbitrary CSS-safe name instead of a step. */
manualName?: string
}
type FrameworkStyleTarget = keyof CSSPropertyBag | readonly (keyof CSSPropertyBag)[]
/** A group paired with its parsed step labels — computed once per generation. */
interface FrameworkScaleGroupEntry<TGroup> {
group: TGroup
stepLabels: string[]
}
/**
* The ordered group enumeration shared between the variable pass and the
* utility-class pass. Building it once (sort + step-label parse) means a single
* traversal per family feeds both outputs instead of two independent ones.
*/
interface FrameworkScalePlan<
TGroup extends FrameworkScaleGroupCommon,
TGenerator extends FrameworkClassGeneratorCommon,
> {
/** Ordered, non-disabled groups with their parsed step labels. */
groups: FrameworkScaleGroupEntry<TGroup>[]
groupsById: Map<string, FrameworkScaleGroupEntry<TGroup>>
generators: TGenerator[]
}
/** Both scale outputs derived from one shared plan. */
interface FrameworkScalePlanResult {
variables: FrameworkScaleVariable[]
utilityClasses: Record<string, StyleRule>
}
// ---------------------------------------------------------------------------
// Generic shape of a framework scale group / settings — what the factory
// actually needs to do its work, irrespective of family.
// ---------------------------------------------------------------------------
interface FrameworkScaleGroupCommon {
id: string
name: string
namingConvention: string
steps: string
baseScaleIndex: number
mode: FrameworkScaleMode
manualSizes?: FrameworkScaleManualSize[]
isDisabled?: boolean
order: number
updatedAt: number
}
interface FrameworkScaleSettingsCommon<TGroup, TGenerator> {
groups: TGroup[]
classes?: TGenerator[]
isDisabled?: boolean
}
interface FrameworkClassGeneratorCommon {
id: string
name: string
property: string[]
tabId: string
isDisabled?: boolean
}
// ---------------------------------------------------------------------------
// Factory
// ---------------------------------------------------------------------------
interface FrameworkScaleModuleConfig<TGroup extends FrameworkScaleGroupCommon> {
family: FrameworkScaleFamily
/** Reads the per-breakpoint base size off a group (`size` for spacing, `fontSize` for typography). */
getMinBaseSize: (group: TGroup) => number
getMaxBaseSize: (group: TGroup) => number
/** Per-breakpoint extractor for the scale ratio (lives on `min`/`max` for both families). */
getMinScaleConfig: (group: TGroup) => {
scaleRatio: number | string
isCustomScaleRatio?: boolean
scaleRatioInputValue?: number
}
getMaxScaleConfig: (group: TGroup) => {
scaleRatio: number | string
isCustomScaleRatio?: boolean
scaleRatioInputValue?: number
}
/** Maps CSS property tokens (`font-size`, `padding`, …) to `CSSPropertyBag` keys. */
propertyKeymap: Record<string, FrameworkStyleTarget>
/** Tags applied to every generated `StyleRule` — e.g. `['framework', 'utility', 'spacing']`. */
classTags: string[]
}
interface FrameworkScaleModule<
TGroup extends FrameworkScaleGroupCommon,
TGenerator extends FrameworkClassGeneratorCommon,
> {
generateVariables(
settings: FrameworkScaleSettingsCommon<TGroup, TGenerator> | null | undefined,
preferences: FrameworkPreferences,
): FrameworkScaleVariable[]
generateUtilityClasses(
settings: FrameworkScaleSettingsCommon<TGroup, TGenerator> | null | undefined,
): Record<string, StyleRule>
/** Variables + utility classes from a single shared group enumeration. */
generatePlan(
settings: FrameworkScaleSettingsCommon<TGroup, TGenerator> | null | undefined,
preferences: FrameworkPreferences,
): FrameworkScalePlanResult
}
export function createFrameworkScaleModule<
TGroup extends FrameworkScaleGroupCommon,
TGenerator extends FrameworkClassGeneratorCommon,
>(config: FrameworkScaleModuleConfig<TGroup>): FrameworkScaleModule<TGroup, TGenerator> {
const {
family,
getMinBaseSize,
getMaxBaseSize,
getMinScaleConfig,
getMaxScaleConfig,
propertyKeymap,
classTags,
} = config
function fluidVariables(
group: TGroup,
stepLabels: string[],
preferences: FrameworkPreferences,
targetUnit: 'px' | 'rem',
): FrameworkScaleVariable[] {
const min = getMinScaleConfig(group)
const max = getMaxScaleConfig(group)
const minRatio = effectiveScaleRatio(min.scaleRatio, min.isCustomScaleRatio, min.scaleRatioInputValue)
const maxRatio = effectiveScaleRatio(max.scaleRatio, max.isCustomScaleRatio, max.scaleRatioInputValue)
const fluidSteps = computeFluidScale({
minBaseSize: Number(getMinBaseSize(group)),
maxBaseSize: Number(getMaxBaseSize(group)),
minScaleRatio: minRatio,
maxScaleRatio: maxRatio,
steps: stepLabels.length,
baseScaleIndex: group.baseScaleIndex,
minScreenWidth: preferences.minScreenWidth,
maxScreenWidth: preferences.maxScreenWidth,
})
return fluidSteps.map((step, idx) => {
const stepLabel = stepLabels[idx]
return {
name: getVariableName(group.namingConvention, stepLabel),
value: declarationFromStep(step, targetUnit, preferences.rootFontSize),
groupId: group.id,
groupName: group.name,
step: stepLabel,
}
})
}
function manualVariables(
group: TGroup,
stepLabels: string[],
preferences: FrameworkPreferences,
targetUnit: 'px' | 'rem',
): FrameworkScaleVariable[] {
const items = group.manualSizes ?? []
return items.map((size, idx) => {
const fluid = computeFluidScale({
minBaseSize: Number(size.min),
maxBaseSize: Number(size.max),
minScaleRatio: 1,
maxScaleRatio: 1,
steps: 1,
baseScaleIndex: 0,
minScreenWidth: preferences.minScreenWidth,
maxScreenWidth: preferences.maxScreenWidth,
})[0]
return {
name: convertToVariableDeclarationName(manualSizeVariableName(size.name)),
value: declarationFromStep(fluid, targetUnit, preferences.rootFontSize),
groupId: group.id,
groupName: group.name,
step: stepLabels[idx] ?? size.name,
manualName: size.name,
}
})
}
/**
* Build the ordered group enumeration once: sort groups, drop disabled ones,
* and parse each surviving group's step labels. Both passes consume this so
* the sort + step parse is not repeated. Returns null when the family is
* absent or disabled (no groups to plan).
*/
function planGroups(
settings: FrameworkScaleSettingsCommon<TGroup, TGenerator> | null | undefined,
): FrameworkScalePlan<TGroup, TGenerator> | null {
if (!settings || settings.isDisabled) return null
const groups: FrameworkScaleGroupEntry<TGroup>[] = []
for (const group of orderedGroups(settings.groups)) {
if (group.isDisabled) continue
groups.push({ group, stepLabels: stepLabelsForGroup(group) })
}
const groupsById = new Map(groups.map((entry) => [entry.group.id, entry]))
return { groups, groupsById, generators: settings.classes ?? [] }
}
function variablesFromPlan(
plan: FrameworkScalePlan<TGroup, TGenerator> | null,
preferences: FrameworkPreferences,
): FrameworkScaleVariable[] {
if (!plan) return []
const targetUnit = preferences.isRem ? 'rem' : 'px'
const variables: FrameworkScaleVariable[] = []
for (const { group, stepLabels } of plan.groups) {
if (group.mode === 'fluid_manual') {
variables.push(...manualVariables(group, stepLabels, preferences, targetUnit))
} else {
variables.push(...fluidVariables(group, stepLabels, preferences, targetUnit))
}
}
return variables
}
function generateVariables(
settings: FrameworkScaleSettingsCommon<TGroup, TGenerator> | null | undefined,
preferences: FrameworkPreferences,
): FrameworkScaleVariable[] {
return variablesFromPlan(planGroups(settings), preferences)
}
function utilityClassesFromPlan(
plan: FrameworkScalePlan<TGroup, TGenerator> | null,
): Record<string, StyleRule> {
const classes: Record<string, StyleRule> = {}
if (!plan) return classes
for (const generator of plan.generators) {
if (generator.isDisabled) continue
if (!generator.tabId) continue
const entry = plan.groupsById.get(generator.tabId)
if (!entry) continue
const { group, stepLabels } = entry
for (let stepIdx = 0; stepIdx < stepLabels.length; stepIdx += 1) {
const step = stepLabels[stepIdx]
const className = expandClassPattern(generator.name, step)
if (!className) continue
const variableName =
group.mode === 'fluid_manual'
? convertToVariableDeclarationName(
manualSizeVariableName(
group.manualSizes?.[stepIdx]?.name ?? `${group.namingConvention}-${step}`,
),
)
: getVariableName(group.namingConvention, step)
const styles = buildUtilityStyles(propertyKeymap, generator.property, `var(${variableName})`)
const id = `framework:${family}:${group.id}:${generator.id}:${step}`
// Static-0 contract: generated utility classes must be a pure function of
// settings. `updatedAt` defaults to 0 in the schema; the reconciler
// preserves real timestamps, so falling back to 0 (not Date.now()) keeps
// generateUtilityClasses deterministic.
const now = group.updatedAt || 0
classes[id] = {
id,
name: className,
kind: 'class',
selector: classKindSelector(className),
order: 0,
styles,
contextStyles: {},
generated: {
origin: 'framework',
family,
sourceId: group.id,
generatorId: generator.id,
tokenName: group.namingConvention,
step,
locked: true,
},
tags: classTags,
createdAt: now,
updatedAt: now,
}
}
}
return classes
}
function generateUtilityClasses(
settings: FrameworkScaleSettingsCommon<TGroup, TGenerator> | null | undefined,
): Record<string, StyleRule> {
return utilityClassesFromPlan(planGroups(settings))
}
function generatePlan(
settings: FrameworkScaleSettingsCommon<TGroup, TGenerator> | null | undefined,
preferences: FrameworkPreferences,
): FrameworkScalePlanResult {
const plan = planGroups(settings)
return {
variables: variablesFromPlan(plan, preferences),
utilityClasses: utilityClassesFromPlan(plan),
}
}
return { generateVariables, generateUtilityClasses, generatePlan }
}
// ---------------------------------------------------------------------------
// Internals — independent of family
// ---------------------------------------------------------------------------
function orderedGroups<T extends { order: number }>(groups: T[]): T[] {
return [...groups].sort((a, b) => (a.order ?? 0) - (b.order ?? 0))
}
function stepLabelsForGroup(group: { steps: string }): string[] {
return group.steps
.split(',')
.map((s) => s.trim())
.filter((s) => s.length > 0)
}
function expandClassPattern(pattern: string, step: string): string {
const trimmed = pattern.trim().replace(/^\./, '')
if (!trimmed) return ''
if (trimmed.includes('*')) return trimmed.replaceAll('*', step)
if (trimmed.includes('{step}')) return trimmed.replaceAll('{step}', step)
return `${trimmed}-${step}`
}
function buildUtilityStyles(
keymap: Record<string, FrameworkStyleTarget>,
properties: string[],
value: string,
): Partial<CSSPropertyBag> {
const styles: Partial<CSSPropertyBag> = {}
for (const property of properties) {
const target = keymap[property] ?? toCamelCase(property)
const keys = Array.isArray(target) ? target : [target]
for (const key of keys) {
if (!key) continue
;(styles as Record<string, string>)[key] = value
}
}
return styles
}
function toCamelCase(value: string): string {
return value.replace(/-([a-z])/g, (_, letter: string) => letter.toUpperCase())
}