antd-color-editor
Version:
An open-source color editor for designing color system
124 lines (117 loc) • 4.7 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/ColorStudio/components/TokenView.tsx
var TokenView_exports = {};
__export(TokenView_exports, {
default: () => TokenView_default
});
module.exports = __toCommonJS(TokenView_exports);
var import_dumi_theme_antd_style = require("dumi-theme-antd-style");
var import_lodash_es = require("lodash-es");
var import_react = require("react");
var import__ = require("../../index");
var import_styles = require("../../styles");
var TokenView = (0, import_react.memo)(({ data, config }) => {
const { colorType, codeType, isolateDarkToken } = config;
const tokenData = data.map((item) => ({
dark: item.scales.dark.map((s) => (0, import__.colorTypeFormat)(s, colorType)),
darkA: item.scales.dark.map((s) => (0, import__.colorTypeFormat)((0, import__.getAlphaColor)(s, "#000"), colorType)),
light: item.scales.light.map((s) => (0, import__.colorTypeFormat)(s, colorType)),
lightA: item.scales.light.map((s) => (0, import__.colorTypeFormat)((0, import__.getAlphaColor)(s, "#fff"), colorType)),
name: item.name
}));
let content = null;
if (codeType === "js") {
if (isolateDarkToken) {
const objLight = {};
const objDark = {};
for (const item of tokenData) {
objLight[(0, import_lodash_es.camelCase)(item.name)] = {
alpha: item.lightA,
solid: item.light
};
objDark[(0, import_lodash_es.camelCase)(item.name)] = {
alpha: item.darkA,
solid: item.dark
};
}
content = `
export interface ColorScaleItem {
solid: string[],
alpha: string[],
}
export interface ColorScales {
${Object.keys(objLight).map((key) => `${key}: ColorScaleItem;`).join("\n")}
}
export const LightTheme:ColorScales = ${JSON.stringify(objLight, null, 2)}
export const DarkTheme:ColorScales = ${JSON.stringify(objDark, null, 2)}
`;
} else {
const obj = {};
for (const item of tokenData) {
obj[(0, import_lodash_es.camelCase)(item.name)] = {
dark: item.dark,
darkA: item.darkA,
light: item.light,
lightA: item.lightA
};
}
content = `
export interface ColorScaleItem {
light: string[],
lightA: string[],
dark: string[],
darkA: string[],
}
export interface ColorScales {
${Object.keys(obj).map((key) => `${key}: ColorScaleItem;`).join("\n")}
}
export const Theme:ColorScales = ${JSON.stringify(obj, null, 2)}
`;
}
} else {
let tokenLightList = isolateDarkToken ? [`/* light.${codeType} */`] : [];
let tokenDarkList = isolateDarkToken ? ["\n", `/* dark.${codeType} */`] : [];
for (const item of tokenData) {
let lightName = (0, import_lodash_es.kebabCase)(item.name);
let darkName = (0, import_lodash_es.kebabCase)(item.name);
if (!isolateDarkToken) {
lightName = "light-" + lightName;
darkName = "dark-" + darkName;
}
let prefix = "--";
if (codeType === "less")
prefix = "@";
if (codeType === "scss")
prefix = "$";
const light = item.light.map((c, index) => `${prefix}${lightName}-color-${index + 1}: ${c};`);
const lightA = item.lightA.map(
(c, index) => `${prefix}${lightName}-color-${index + 1}-alpha: ${c};`
);
const dark = item.dark.map((c, index) => `${prefix}${darkName}-color-${index + 1}: ${c};`);
const darkA = item.darkA.map(
(c, index) => `${prefix}${darkName}-color-${index + 1}-alpha: ${c};`
);
tokenLightList = [...tokenLightList, ...light, ...lightA];
tokenDarkList = [...tokenDarkList, ...dark, ...darkA];
}
content = [...tokenLightList, ...tokenDarkList].join("\n");
}
return /* @__PURE__ */ React.createElement(import_styles.CodeView, null, /* @__PURE__ */ React.createElement(import_dumi_theme_antd_style.Highlighter, { background: false, language: codeType }, String(content)));
});
var TokenView_default = TokenView;