UNPKG

antd-color-editor

Version:

An open-source color editor for designing color system

151 lines (147 loc) 6.71 kB
var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; 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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/ColorStudio/components/ExportPanel.tsx var ExportPanel_exports = {}; __export(ExportPanel_exports, { default: () => ExportPanel_default }); module.exports = __toCommonJS(ExportPanel_exports); var import_icons = require("@ant-design/icons"); var import_pro_editor = require("@ant-design/pro-editor"); var import_ahooks = require("ahooks"); var import_antd = require("antd"); var import_dayjs = __toESM(require("dayjs")); var import_leva = require("leva"); var import_react = require("react"); var import_styled_components = __toESM(require("styled-components")); var import__ = require("../../index"); var Btn = (0, import_styled_components.default)(import_antd.Button)` font-size: 12px !important; border-radius: 4px; `; var Flex = (0, import_styled_components.default)(import_antd.Space)` display: flex; width: 100%; padding: 0 12px; > div { flex: 1; } .ant-upload.ant-upload-select { width: 100%; } `; var ExportPanel = (0, import_react.memo)(({ config }) => { const [localData, setLocalData] = (0, import_ahooks.useLocalStorageState)("antd-color-config", { defaultValue: void 0 }); const exportStore = (0, import_leva.useCreateStore)(); const { setContent, startDownload } = (0, import__.useDownload)(); const { name } = (0, import_leva.useControls)( { name: { label: "配置名称", value: "未命名" } }, { store: exportStore } ); const genConfgFile = () => { return { data: config, name, time: Date.now() }; }; const handleSave = () => { const data = genConfgFile(); setLocalData(data); import_antd.message.success("保存成功"); }; const handleDownload = () => { const data = genConfgFile(); const fileName = name ? name + ".json" : "config.json"; setContent(JSON.stringify(data), fileName); startDownload(); }; const handleUpload = (0, import_react.useCallback)( (info) => { const reader = new FileReader(); reader.readAsText(info.file.originFileObj, "utf8"); reader.addEventListener("load", (evt) => { var _a; const fileString = (_a = evt.target) == null ? void 0 : _a.result; const fileJson = fileString; const uploadConfig = JSON.parse(fileJson); if ((uploadConfig == null ? void 0 : uploadConfig.time) && (uploadConfig == null ? void 0 : uploadConfig.data) && (uploadConfig == null ? void 0 : uploadConfig.name)) { setLocalData(uploadConfig); import_antd.message.success("上传并覆盖成功"); location.reload(); } else { import_antd.message.error("配置文件不正确"); } }); }, [setLocalData] ); const handleClean = () => { setLocalData(); location.reload(); }; const exportPanelGroup = [ { header: "配置存储", panel: /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement(import__.LevaPanel, { store: exportStore }), /* @__PURE__ */ React.createElement(Flex, { direction: "vertical" }, /* @__PURE__ */ React.createElement(Btn, { block: true, icon: /* @__PURE__ */ React.createElement(import_icons.SaveOutlined, null), onClick: handleSave, size: "small", type: "primary" }, "保存配置"), /* @__PURE__ */ React.createElement(Btn, { block: true, icon: /* @__PURE__ */ React.createElement(import_icons.DownloadOutlined, null), onClick: handleDownload, size: "small" }, "下载配置"))) }, { header: "读取/还原", panel: /* @__PURE__ */ React.createElement(Flex, { direction: "vertical" }, /* @__PURE__ */ React.createElement(import_antd.Upload, { listType: "picture", maxCount: 1, onChange: handleUpload }, /* @__PURE__ */ React.createElement(Btn, { block: true, icon: /* @__PURE__ */ React.createElement(import_icons.UploadOutlined, null), size: "small", type: "primary" }, "上传并读取配置文件")), /* @__PURE__ */ React.createElement( import_antd.Popconfirm, { cancelText: "取消", okText: "确认", onConfirm: () => location.reload(), title: "确定要恢复为保存配置?" }, /* @__PURE__ */ React.createElement(Btn, { block: true, icon: /* @__PURE__ */ React.createElement(import_icons.ReloadOutlined, null), size: "small" }, "恢复为保存配置") ), /* @__PURE__ */ React.createElement( import_antd.Popconfirm, { cancelText: "取消", okText: "确认", onConfirm: handleClean, title: "确定要清空并恢复初始化?" }, /* @__PURE__ */ React.createElement(Btn, { block: true, danger: true, icon: /* @__PURE__ */ React.createElement(import_icons.DeleteOutlined, null), size: "small" }, "清空历史配置并初始化") )) }, { header: localData ? ["本地配置:", localData.name, (0, import_dayjs.default)(localData == null ? void 0 : localData.time).format("YYYY-MM-DD HH:mm:ss")].join( " " ) : "无本地配置", panel: localData && /* @__PURE__ */ React.createElement(import_pro_editor.DataPreviewer, { data: [localData == null ? void 0 : localData.data] }) } ]; return /* @__PURE__ */ React.createElement(import__.PanelGroup, { panels: exportPanelGroup }); }); var ExportPanel_default = ExportPanel;