|
| 1 | +import { downloadFile } from '../../utils/download-file' |
| 2 | +import { isVariableAlias } from '../../utils/is-variable-alias' |
| 3 | +import { optimizeHex } from '../../utils/optimize-hex' |
| 4 | +import { rgbaToHex } from '../../utils/rgba-to-hex' |
| 5 | +import { styleNameToTypography } from '../../utils/style-name-to-typography' |
| 6 | +import { textSegmentToTypography } from '../../utils/text-segment-to-typography' |
| 7 | +import { textStyleToTypography } from '../../utils/text-style-to-typography' |
| 8 | +import { toCamel } from '../../utils/to-camel' |
| 9 | +import { variableAliasToValue } from '../../utils/variable-alias-to-value' |
| 10 | +import type { Devup, DevupTypography } from './types' |
| 11 | +import { downloadDevupXlsx } from './utils/download-devup-xlsx' |
| 12 | +import { getDevupColorCollection } from './utils/get-devup-color-collection' |
| 13 | + |
| 14 | +export async function exportDevup( |
| 15 | + output: 'json' | 'excel', |
| 16 | + treeshaking: boolean = true, |
| 17 | +) { |
| 18 | + const devup: Devup = {} |
| 19 | + |
| 20 | + const collection = await getDevupColorCollection() |
| 21 | + if (collection) { |
| 22 | + for (const mode of collection.modes) { |
| 23 | + devup.theme ??= {} |
| 24 | + devup.theme.colors ??= {} |
| 25 | + const colors: Record<string, string> = {} |
| 26 | + devup.theme.colors[mode.name.toLowerCase()] = colors |
| 27 | + await Promise.all( |
| 28 | + collection.variableIds.map(async (varId) => { |
| 29 | + const variable = await figma.variables.getVariableByIdAsync(varId) |
| 30 | + if (variable === null) return |
| 31 | + const value = variable.valuesByMode[mode.modeId] |
| 32 | + if (typeof value === 'boolean' || typeof value === 'number') return |
| 33 | + if (isVariableAlias(value)) { |
| 34 | + const nextValue = await variableAliasToValue(value, mode.modeId) |
| 35 | + if (nextValue === null) return |
| 36 | + if (typeof nextValue === 'boolean' || typeof nextValue === 'number') |
| 37 | + return |
| 38 | + colors[toCamel(variable.name)] = optimizeHex( |
| 39 | + rgbaToHex(figma.util.rgba(nextValue)), |
| 40 | + ) |
| 41 | + } else { |
| 42 | + colors[toCamel(variable.name)] = optimizeHex( |
| 43 | + rgbaToHex(figma.util.rgba(value)), |
| 44 | + ) |
| 45 | + } |
| 46 | + }), |
| 47 | + ) |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + await figma.loadAllPagesAsync() |
| 52 | + |
| 53 | + const textStyles = await figma.getLocalTextStylesAsync() |
| 54 | + const ids = new Set() |
| 55 | + const styles: Record<string, TextStyle> = {} |
| 56 | + for (const style of textStyles) { |
| 57 | + ids.add(style.id) |
| 58 | + styles[style.name] = style |
| 59 | + } |
| 60 | + |
| 61 | + const typography: Record<string, (null | DevupTypography)[]> = {} |
| 62 | + if (treeshaking) { |
| 63 | + const texts = figma.root.findAllWithCriteria({ types: ['TEXT'] }) |
| 64 | + await Promise.all( |
| 65 | + texts |
| 66 | + .filter( |
| 67 | + (text) => |
| 68 | + (typeof text.textStyleId === 'string' && text.textStyleId) || |
| 69 | + text.textStyleId === figma.mixed, |
| 70 | + ) |
| 71 | + .map(async (text) => { |
| 72 | + for (const seg of text.getStyledTextSegments([ |
| 73 | + 'fontName', |
| 74 | + 'fontWeight', |
| 75 | + 'fontSize', |
| 76 | + 'textDecoration', |
| 77 | + 'textCase', |
| 78 | + 'lineHeight', |
| 79 | + 'letterSpacing', |
| 80 | + 'fills', |
| 81 | + 'textStyleId', |
| 82 | + 'fillStyleId', |
| 83 | + 'listOptions', |
| 84 | + 'indentation', |
| 85 | + 'hyperlink', |
| 86 | + ])) { |
| 87 | + if (seg?.textStyleId) { |
| 88 | + const style = await figma.getStyleByIdAsync(seg.textStyleId) |
| 89 | + |
| 90 | + if (!(style && ids.has(style.id))) continue |
| 91 | + const { level, name } = styleNameToTypography(style.name) |
| 92 | + const typo = textSegmentToTypography(seg) |
| 93 | + if (typography[name]?.[level]) continue |
| 94 | + typography[name] ??= [null, null, null, null, null, null] |
| 95 | + typography[name][level] = typo |
| 96 | + } |
| 97 | + } |
| 98 | + }), |
| 99 | + ) |
| 100 | + } else { |
| 101 | + for (const [styleName, style] of Object.entries(styles)) { |
| 102 | + const { level, name } = styleNameToTypography(styleName) |
| 103 | + const typo = textStyleToTypography(style) |
| 104 | + if (typography[name]?.[level]) continue |
| 105 | + typography[name] ??= [null, null, null, null, null, null] |
| 106 | + typography[name][level] = typo |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + for (const [name, style] of Object.entries(styles)) { |
| 111 | + const { level, name: styleName } = styleNameToTypography(name) |
| 112 | + if (typography[styleName] && !typography[styleName][level]) { |
| 113 | + typography[styleName][level] = textStyleToTypography(style) |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + if (Object.keys(typography).length > 0) { |
| 118 | + devup.theme ??= {} |
| 119 | + devup.theme.typography = Object.entries(typography).reduce( |
| 120 | + (acc, [key, value]) => { |
| 121 | + const filtered = value.filter((v) => v !== null) |
| 122 | + if (filtered.length === 0) { |
| 123 | + return acc |
| 124 | + } |
| 125 | + if (filtered.length === 1) { |
| 126 | + acc[key] = filtered[0] |
| 127 | + return acc |
| 128 | + } |
| 129 | + if (value[0] === null) { |
| 130 | + acc[key] = [filtered[0]] |
| 131 | + for (let i = 1; i < value.length; i += 1) { |
| 132 | + acc[key].push(value[i]) |
| 133 | + } |
| 134 | + while (acc[key][acc[key].length - 1] === null) { |
| 135 | + acc[key].pop() |
| 136 | + } |
| 137 | + return acc |
| 138 | + } |
| 139 | + acc[key] = value |
| 140 | + return acc |
| 141 | + }, |
| 142 | + {} as Record<string, DevupTypography | (null | DevupTypography)[]>, |
| 143 | + ) |
| 144 | + } |
| 145 | + |
| 146 | + switch (output) { |
| 147 | + case 'json': |
| 148 | + return downloadFile('devup.json', JSON.stringify(devup)) |
| 149 | + case 'excel': |
| 150 | + return downloadDevupXlsx('devup.xlsx', JSON.stringify(devup)) |
| 151 | + } |
| 152 | +} |
0 commit comments