@xrenders/xflow
Version:
一款功能强大、易用灵活的流程编辑器框架,帮助你轻松构建复杂的工作流和流程产品
406 lines (404 loc) • 13.7 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var _exportNames = {
uuid: true,
uuid4: true,
_set: true,
_get: true,
_cloneDeep: true,
_merge: true,
_mergeWith: true,
_isUndefined: true,
_omitBy: true,
_some: true,
_isMatch: true,
isObject: true,
isArray: true,
isFunction: true,
isUrl: true,
isNumber: true,
getArray: true,
getFormat: true,
isObjType: true,
isListType: true,
isCheckBoxType: true,
translation: true,
hasFuncProperty: true,
safeGet: true,
isMac: true,
getKeyboardKeyNameBySystem: true,
capitalize: true,
transformNodes: true,
transformSwitchNodes: true,
safeJsonStringify: true,
NODE_STATUS: true,
transformNodeStatus: true,
getTransparentColor: true,
isTruthy: true,
hexToRgba: true,
getColorfulModeBackground: true,
_has: true
};
exports._get = exports._cloneDeep = exports.NODE_STATUS = void 0;
Object.defineProperty(exports, "_has", {
enumerable: true,
get: function get() {
return _lodashEs.has;
}
});
exports.getArray = exports.capitalize = exports._some = exports._set = exports._omitBy = exports._mergeWith = exports._merge = exports._isUndefined = exports._isMatch = void 0;
exports.getColorfulModeBackground = getColorfulModeBackground;
exports.getFormat = getFormat;
exports.getKeyboardKeyNameBySystem = void 0;
exports.getTransparentColor = getTransparentColor;
exports.hasFuncProperty = void 0;
exports.hexToRgba = hexToRgba;
exports.isArray = void 0;
exports.isCheckBoxType = isCheckBoxType;
exports.isFunction = void 0;
exports.isListType = isListType;
exports.isNumber = exports.isMac = void 0;
exports.isObjType = isObjType;
exports.isObject = void 0;
exports.isTruthy = isTruthy;
exports.isUrl = isUrl;
exports.safeGet = void 0;
exports.safeJsonStringify = safeJsonStringify;
exports.uuid4 = exports.uuid = exports.translation = exports.transformSwitchNodes = exports.transformNodes = exports.transformNodeStatus = void 0;
var _tslib = require("tslib");
var _nanoid = require("nanoid");
var _tinycolor = _interopRequireDefault(require("tinycolor2"));
var _lodashEs = require("lodash-es");
var _flow = require("./flow");
Object.keys(_flow).forEach(function (key) {
if (key === "default" || key === "__esModule") return;
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
if (key in exports && exports[key] === _flow[key]) return;
Object.defineProperty(exports, key, {
enumerable: true,
get: function get() {
return _flow[key];
}
});
});
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
var uuid = exports.uuid = (0, _nanoid.customAlphabet)('0123456789abcdefghijklmnopqrstuvwxyz', 16);
var uuid4 = exports.uuid4 = (0, _nanoid.customAlphabet)('0123456789abcdefghijklmnopqrstuvwxyz', 4);
var _set = exports._set = _lodashEs.set;
var _get = exports._get = _lodashEs.get;
var _cloneDeep = exports._cloneDeep = _lodashEs.cloneDeep;
// export const _has = has;
var _merge = exports._merge = _lodashEs.merge;
var _mergeWith = exports._mergeWith = _lodashEs.mergeWith;
var _isUndefined = exports._isUndefined = _lodashEs.isUndefined;
var _omitBy = exports._omitBy = _lodashEs.omitBy;
var _some = exports._some = _lodashEs.some;
var _isMatch = exports._isMatch = _lodashEs.isMatch;
var isObject = exports.isObject = function isObject(data) {
var str = Object.prototype.toString.call(data);
return str.indexOf('Object') > -1;
};
var isArray = exports.isArray = function isArray(data) {
var str = Object.prototype.toString.call(data);
return str.indexOf('Array') > -1;
};
var isFunction = exports.isFunction = function isFunction(data) {
return typeof data === 'function';
};
function isUrl(string) {
var protocolRE = /^(?:\w+:)?\/\/(\S+)$/;
// const domainRE = /^[^\s\.]+\.\S{2,}$/;
if (typeof string !== 'string') return false;
return protocolRE.test(string);
}
var isNumber = exports.isNumber = function isNumber(str) {
return !isNaN(Number(str));
};
var getArray = exports.getArray = function getArray(arr) {
var defaultValue = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
if (Array.isArray(arr)) return arr;
return defaultValue;
};
function getFormat(format) {
var dateFormat;
switch (format) {
case 'date':
dateFormat = 'YYYY-MM-DD';
break;
case 'time':
dateFormat = 'HH:mm:ss';
break;
case 'dateTime':
dateFormat = 'YYYY-MM-DD HH:mm:ss';
break;
case 'week':
dateFormat = 'YYYY-w';
break;
case 'year':
dateFormat = 'YYYY';
break;
case 'quarter':
dateFormat = 'YYYY-Q';
break;
case 'month':
dateFormat = 'YYYY-MM';
break;
default:
// dateTime
if (typeof format === 'string') {
dateFormat = format;
} else {
dateFormat = 'YYYY-MM-DD';
}
}
return dateFormat;
}
// TODO: to support case that item is not an object
function isObjType(schema) {
//return schema?.type === 'object' && schema.properties && !schema.widget;
return (schema === null || schema === void 0 ? void 0 : schema.type) === 'object' && (schema === null || schema === void 0 ? void 0 : schema.properties) && (schema === null || schema === void 0 ? void 0 : schema.widgetType) !== 'field';
}
function isListType(schema) {
return (schema === null || schema === void 0 ? void 0 : schema.type) === 'array' && isObjType(schema === null || schema === void 0 ? void 0 : schema.items) && (schema === null || schema === void 0 ? void 0 : schema.enum) === undefined;
}
function isCheckBoxType(schema, readOnly) {
if (readOnly) return false;
if (schema.widget === 'checkbox') return true;
if (schema && schema.type === 'boolean') {
if (schema.enum) return false;
if (schema.widget === undefined) return true;
return false;
}
}
var translation = exports.translation = function translation(configCtx) {
return function (key) {
var locale = configCtx === null || configCtx === void 0 ? void 0 : configCtx.locale.FormRender;
return locale[key];
};
};
var _hasFuncProperty = exports.hasFuncProperty = function hasFuncProperty(obj) {
return _some(obj, function (value) {
if (isFunction(value)) {
return true;
}
if (isObject(value)) {
return _hasFuncProperty(value);
}
return false;
});
};
/**
* 安全地获取对象的值,如果值为 null 或 undefined,则返回 defaultValue。
*
* @param {Object} object - 要获取值的对象。
* @param {string|Array} path - 要获取的路径,可以是字符串或数组。
* @param {*} [defaultValue] - 如果值为 null 或 undefined,则返回 defaultValue。
* @returns {*} - 返回获取的值,或者默认值。
*/
var safeGet = exports.safeGet = function safeGet(object, path, defaultValue) {
var _a;
return (_a = (0, _lodashEs.get)(object, path, defaultValue)) !== null && _a !== void 0 ? _a : defaultValue;
};
var isMac = exports.isMac = function isMac() {
return navigator.userAgent.toUpperCase().includes('MAC');
};
var specialKeysNameMap = {
ctrl: '⌘',
alt: '⌥'
};
var getKeyboardKeyNameBySystem = exports.getKeyboardKeyNameBySystem = function getKeyboardKeyNameBySystem(key) {
if (isMac()) return specialKeysNameMap[key] || key;
return key;
};
var capitalize = exports.capitalize = function capitalize(string) {
if (typeof string !== 'string' || string.length === 0) {
return string;
}
return "".concat(string.charAt(0).toUpperCase()).concat(string.slice(1));
};
var transformNodes = exports.transformNodes = function transformNodes(nodes) {
return nodes === null || nodes === void 0 ? void 0 : nodes.map(function (item) {
var _a, _b;
var type = item.type,
data = item.data,
rest = (0, _tslib.__rest)(item, ["type", "data"]);
if (type === 'custom') {
return item;
}
if (type === 'Switch' || type === 'Parallel') {
return Object.assign({
type: 'custom',
data: Object.assign(Object.assign(Object.assign({}, data), {
_nodeType: type
}), ((_a = data === null || data === void 0 ? void 0 : data.list) === null || _a === void 0 ? void 0 : _a.length) && {
list: (_b = (data === null || data === void 0 ? void 0 : data.list) || []) === null || _b === void 0 ? void 0 : _b.map(function (n) {
if (n === null || n === void 0 ? void 0 : n._id) {
return n;
} else {
return Object.assign(Object.assign({}, n), {
_id: "id_".concat(uuid())
});
}
})
})
}, rest);
} else {
return Object.assign({
type: 'custom',
data: Object.assign(Object.assign({}, data), {
_nodeType: type
})
}, rest);
}
});
};
var transformSwitchNodes = exports.transformSwitchNodes = function transformSwitchNodes(nodes) {
var _a;
return (_a = nodes || []) === null || _a === void 0 ? void 0 : _a.map(function (item) {
var _a;
if ((item === null || item === void 0 ? void 0 : item.type) === 'Switch' || (item === null || item === void 0 ? void 0 : item.type) === 'Parallel') {
var _b = item === null || item === void 0 ? void 0 : item.data,
list = _b.list,
rest = (0, _tslib.__rest)(_b, ["list"]);
return Object.assign(Object.assign({}, item), {
data: Object.assign(Object.assign({}, rest), {
list: (_a = list || []) === null || _a === void 0 ? void 0 : _a.map(function (item) {
if (item === null || item === void 0 ? void 0 : item._id) {
return item;
} else {
return Object.assign(Object.assign({}, item), {
_id: "id_".concat(uuid())
});
}
})
})
});
} else {
return item;
}
});
};
// 废弃:
// export const getAntdVersion = () => {
// const majorVersion = parseInt(antdVersion?.split('.')?.[0], 10);
// if (majorVersion >= 5) {
// return 'V5';
// } else if (majorVersion === 4) {
// return 'V4';
// } else {
// return 'V4';
// }
// };
// 安全的JSON.stringify
function safeJsonStringify(obj) {
var seen = new WeakSet();
function replacer(key, value) {
// 循环引用
if (_typeof(value) === 'object' && value !== null) {
if (seen.has(value)) {
return '[Circular]';
}
seen.add(value);
}
// React
if (value && _typeof(value) === 'object' && value._owner) {
return '[React Element]';
}
// BigInt
if (typeof value === 'bigint') {
return value.toString();
}
// 其他无法序列化的类型
if (typeof value === 'function') {
return "[Function: ".concat(value.name || 'anonymous', "]");
}
return value;
}
try {
return JSON.stringify(obj, replacer, 2);
} catch (error) {
console.error('json.stringify error', error);
return null;
}
}
// 内置节点状态
var NODE_STATUS = exports.NODE_STATUS = {
success: {
color: '#52c41a',
name: '成功'
},
error: {
color: '#ff4d4f',
name: '失败'
},
warning: {
color: '#faad14',
name: '告警'
}
};
var transformNodeStatus = exports.transformNodeStatus = function transformNodeStatus() {
var statusList = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
var obj = {};
statusList === null || statusList === void 0 ? void 0 : statusList.forEach(function (status) {
if (isTruthy(status === null || status === void 0 ? void 0 : status.value) && (status === null || status === void 0 ? void 0 : status.color)) obj[status.value] = {
color: status.color,
name: status === null || status === void 0 ? void 0 : status.name
};
});
return Object.assign(Object.assign({}, NODE_STATUS), obj);
};
// 处理颜色值
function getTransparentColor(colorInput, alpha) {
var color = (0, _tinycolor.default)(colorInput);
var alphaNum = Number(alpha);
if (!color.isValid()) {
return;
}
color.setAlpha(alphaNum);
// 返回 RGBA 格式的颜色字符串
return color.toRgbString();
}
function isTruthy(value) {
if ((0, _lodashEs.isNil)(value)) {
return false;
}
if (isNumber(value) && value === 0) {
return true;
}
return Boolean(value);
}
function hexToRgba(hex) {
var alpha = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0.25;
// 处理缺少 # 前缀的情况
if (hex.charAt(0) !== '#') {
hex = '#' + hex;
}
// 处理3位十六进制颜色值 (#RGB)
if (/^#([0-9A-Fa-f]{3})$/.test(hex)) {
var _r = hex.charAt(1);
var _g = hex.charAt(2);
var _b2 = hex.charAt(3);
hex = "#".concat(_r).concat(_r).concat(_g).concat(_g).concat(_b2).concat(_b2);
}
// 如果不是合法的六位十六进制颜色,返回默认颜色
if (!/^#([0-9A-Fa-f]{6})$/.test(hex)) {
hex = '#F79009'; // 使用默认颜色
}
// 提取 R、G 和 B 值
var r = parseInt(hex.slice(1, 3), 16);
var g = parseInt(hex.slice(3, 5), 16);
var b = parseInt(hex.slice(5, 7), 16);
// 返回 `rgba` 格式字符串
return "rgba(".concat(r, ", ").concat(g, ", ").concat(b, ", ").concat(alpha, ")");
}
function getColorfulModeBackground(color, openColorfulMode) {
if (!openColorfulMode) {
return {};
}
return {
background: "linear-gradient(to bottom, ".concat(hexToRgba(color || '#F79009'), ", #fff)")
};
}