UNPKG

@tldraw/tlschema

Version:

tldraw infinite canvas SDK (schema).

8 lines (7 loc) 4.36 kB
{ "version": 3, "sources": ["../../src/styles/TLColorStyle.ts"], "sourcesContent": ["import { StyleProp } from './StyleProp'\nimport { TLDefaultColor, TLThemeDefaultColors, TLThemes } from './TLTheme'\n\n/**\n * The names of all available shape colors, derived from {@link TLThemeDefaultColors}.\n * Extend {@link TLThemeDefaultColors} to add custom color names.\n *\n * @public\n */\nexport type TLDefaultColorStyle = {\n\t[K in keyof TLThemeDefaultColors]: TLThemeDefaultColors[K] extends TLDefaultColor ? K : never\n}[keyof TLThemeDefaultColors] &\n\tstring\n\n/**\n * Used only for initial values of the color style; the source of truth has moved to TLTheme.\n *\n * @internal\n */\nconst defaultColorNames: TLDefaultColorStyle[] = [\n\t'black',\n\t'grey',\n\t'light-violet',\n\t'violet',\n\t'blue',\n\t'light-blue',\n\t'yellow',\n\t'orange',\n\t'green',\n\t'light-green',\n\t'light-red',\n\t'red',\n\t'white',\n] as const\n\n/**\n * @public\n */\nexport const DefaultColorStyle = StyleProp.defineEnum('tldraw:color', {\n\tdefaultValue: 'black',\n\tvalues: defaultColorNames,\n})\n\n/**\n * @public\n */\nexport const DefaultLabelColorStyle = StyleProp.defineEnum('tldraw:labelColor', {\n\tdefaultValue: 'black',\n\tvalues: defaultColorNames,\n})\n\n/**\n * Scan theme definitions and sync color registrations to match.\n * A color entry is any key in `TLThemeColors` whose value is an object\n * (i.e. a {@link TLDefaultColor}), as opposed to utility strings like\n * `background` or `text`.\n *\n * Colors present in themes but not yet registered will be added.\n * Colors currently registered but absent from all themes will be removed.\n *\n * @public\n */\nexport function registerColorsFromThemes(definitions: TLThemes): void {\n\tconst colorNames = new Set<TLDefaultColorStyle>()\n\tfor (const def of Object.values(definitions)) {\n\t\tfor (const colorPalette of [def.colors.light, def.colors.dark]) {\n\t\t\tfor (const [key, value] of Object.entries(colorPalette)) {\n\t\t\t\tif (typeof value === 'object' && value !== null) {\n\t\t\t\t\tcolorNames.add(key as TLDefaultColorStyle)\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tif (colorNames.size > 0) {\n\t\tDefaultColorStyle.addValues(...colorNames)\n\t\tDefaultLabelColorStyle.addValues(...colorNames)\n\t}\n\n\tconst toRemove = DefaultColorStyle.values.filter((v) => !colorNames.has(v as TLDefaultColorStyle))\n\tif (toRemove.length > 0) {\n\t\tDefaultColorStyle.removeValues(...toRemove)\n\t\tDefaultLabelColorStyle.removeValues(...toRemove)\n\t}\n\n\tif (process.env.NODE_ENV !== 'production') {\n\t\tfor (const def of Object.values(definitions)) {\n\t\t\tfor (const color of colorNames) {\n\t\t\t\tif (!(color in def.colors.light)) {\n\t\t\t\t\tconsole.warn(\n\t\t\t\t\t\t`Theme '${def.id}' light palette is missing color '${color}'. Shapes using this color won't render correctly.`\n\t\t\t\t\t)\n\t\t\t\t}\n\t\t\t\tif (!(color in def.colors.dark)) {\n\t\t\t\t\tconsole.warn(\n\t\t\t\t\t\t`Theme '${def.id}' dark palette is missing color '${color}'. Shapes using this color won't render correctly.`\n\t\t\t\t\t)\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}\n"], "mappings": "AAAA,SAAS,iBAAiB;AAmB1B,MAAM,oBAA2C;AAAA,EAChD;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACD;AAKO,MAAM,oBAAoB,UAAU,WAAW,gBAAgB;AAAA,EACrE,cAAc;AAAA,EACd,QAAQ;AACT,CAAC;AAKM,MAAM,yBAAyB,UAAU,WAAW,qBAAqB;AAAA,EAC/E,cAAc;AAAA,EACd,QAAQ;AACT,CAAC;AAaM,SAAS,yBAAyB,aAA6B;AACrE,QAAM,aAAa,oBAAI,IAAyB;AAChD,aAAW,OAAO,OAAO,OAAO,WAAW,GAAG;AAC7C,eAAW,gBAAgB,CAAC,IA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