@cainiaofe/cn-ui-layout
Version:
- 安装 cone-cli | [Cone-CLI 使用指引](https://alidocs.dingtalk.com/i/nodes/xdqZp24KneBJzvGM6XezJvyb7RA0Nr1E) - `tnpm i -g @alife/cone-cli@latest` - 安装依赖包 - `cone install` - 启动研发 - `cone start`
233 lines (226 loc) • 11 kB
JavaScript
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.calculateAfterGroupList = calculateAfterGroupList;
exports.getCurrentBlockInfo = getCurrentBlockInfo;
exports.groupColumn = groupColumn;
exports.insertNewBlock = insertNewBlock;
exports.replaceNode = replaceNode;
var _i18n = _interopRequireDefault(require("../../locales/i18n"));
var _setterSnippet = require("../manager/setter-snippet");
var _isNumber = _interopRequireDefault(require("lodash/isNumber"));
var _util = require("./util");
var _window$CNUI;
function _createForOfIteratorHelperLoose(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (t) return (t = t.call(r)).next.bind(t); if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var o = 0; return function () { return o >= r.length ? { done: !0 } : { done: !1, value: r[o++] }; }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
var CnMessage = (_window$CNUI = window.CNUI) === null || _window$CNUI === void 0 ? void 0 : _window$CNUI.CnMessage;
/**
* @description 通过块,在画布中插入新栏
*/
function insertNewBlock(config) {
var _ref = config || {},
currentBlockInfo = _ref.currentBlockInfo,
_leaf = _ref._leaf;
var _ref2 = currentBlockInfo || {},
currentIndex = _ref2.currentIndex,
_ref2$groupList = _ref2.groupList,
groupList = _ref2$groupList === void 0 ? [] : _ref2$groupList,
currentLine = _ref2.currentLine,
currentLineBlockNum = _ref2.currentLineBlockNum;
if (!(0, _isNumber["default"])(currentLineBlockNum) || !(0, _isNumber["default"])(currentLine)) {
return;
}
if (currentLineBlockNum < 6) {
var sectionNode = _leaf === null || _leaf === void 0 ? void 0 : _leaf.parent;
if ((sectionNode === null || sectionNode === void 0 ? void 0 : sectionNode.componentName) === 'CnLayoutSection') {
var _sectionNode$schema;
var children = sectionNode === null || sectionNode === void 0 ? void 0 : (_sectionNode$schema = sectionNode.schema) === null || _sectionNode$schema === void 0 ? void 0 : _sectionNode$schema.children;
if ((0, _util.isArrayNotEmpty)(children)) {
var _groupList$currentLin, _groupList$currentLin2, _realChildren$forEach, _leaf$document;
var currentLineStartIndex = (_groupList$currentLin = groupList[currentLine]) === null || _groupList$currentLin === void 0 ? void 0 : (_groupList$currentLin2 = _groupList$currentLin[0]) === null || _groupList$currentLin2 === void 0 ? void 0 : _groupList$currentLin2.index;
var currentLineBlock = children.slice(currentLineStartIndex, currentLineStartIndex + currentLineBlockNum);
var realChildren = [];
currentLineBlock.forEach(function (item) {
var nodeId = item.id;
if (nodeId) {
realChildren.push({
nodeId: nodeId,
span: currentLineBlock.length === 4 ? 2 : 12 / (currentLineBlockNum + 1)
});
}
});
var document = _leaf === null || _leaf === void 0 ? void 0 : _leaf.document;
realChildren === null || realChildren === void 0 ? void 0 : (_realChildren$forEach = realChildren.forEach) === null || _realChildren$forEach === void 0 ? void 0 : _realChildren$forEach.call(realChildren, function (item) {
var _document$getNode, _node$setPropValue;
var node = document === null || document === void 0 ? void 0 : (_document$getNode = document.getNode) === null || _document$getNode === void 0 ? void 0 : _document$getNode.call(document, item.nodeId);
// @ts-ignore 忽略第3个参数,走默认值
node === null || node === void 0 ? void 0 : (_node$setPropValue = node.setPropValue) === null || _node$setPropValue === void 0 ? void 0 : _node$setPropValue.call(node, 'span', item.span);
});
var cardProps = {
title: _i18n["default"].get({
id: 'Title',
dm: '标题'
})
};
var parent = sectionNode === null || sectionNode === void 0 ? void 0 : sectionNode.parent;
if ((parent === null || parent === void 0 ? void 0 : parent.componentName) === 'CnCard') {
var _parent$propsData;
cardProps = {
noPadding: true,
noShadow: true,
title: _i18n["default"].get({
id: 'Title',
dm: '标题'
}),
shape: 'simple'
};
if ((parent === null || parent === void 0 ? void 0 : (_parent$propsData = parent.propsData) === null || _parent$propsData === void 0 ? void 0 : _parent$propsData.shape) === 'group') {
cardProps.noPadding = false;
cardProps.noShadow = false;
cardProps.shape = 'normal';
}
}
var blockSchema = (0, _setterSnippet.createBlockSnippet)({
cardProps: cardProps,
blockProps: {
span: currentLineBlock.length === 4 ? 4 : 12 / (currentLineBlockNum + 1)
}
});
var newNode = _leaf === null || _leaf === void 0 ? void 0 : (_leaf$document = _leaf.document) === null || _leaf$document === void 0 ? void 0 : _leaf$document.createNode(blockSchema);
// @ts-ignore ...
newNode && (sectionNode === null || sectionNode === void 0 ? void 0 : sectionNode.insertAfter(newNode, {
index: currentIndex
}));
}
}
} else {
var _CnMessage$error;
CnMessage === null || CnMessage === void 0 ? void 0 : (_CnMessage$error = CnMessage.error) === null || _CnMessage$error === void 0 ? void 0 : _CnMessage$error.call(CnMessage, _i18n["default"].get({
id: 'ASingleRowCannotExceed6Columns',
dm: '单行不能超过6栏'
}));
}
}
/**
* @description 获取当前块信息
*/
function getCurrentBlockInfo(_ref3) {
var _leaf$parent, _leaf$parent$schema;
var _nodeId = _ref3._nodeId,
_leaf = _ref3._leaf;
var children = _leaf === null || _leaf === void 0 ? void 0 : (_leaf$parent = _leaf.parent) === null || _leaf$parent === void 0 ? void 0 : (_leaf$parent$schema = _leaf$parent.schema) === null || _leaf$parent$schema === void 0 ? void 0 : _leaf$parent$schema.children;
var result = {};
if ((0, _util.isArrayNotEmpty)(children)) {
var currentIndex;
result.groupList = groupColumn(children.map(function (item, index) {
var nodeId = item.id;
var nodeProps = item.props;
if (nodeId === _nodeId) {
currentIndex = index;
}
return nodeProps === null || nodeProps === void 0 ? void 0 : nodeProps.span;
}));
if (currentIndex !== undefined) {
var _result$groupList;
result.currentIndex = currentIndex;
(_result$groupList = result.groupList) === null || _result$groupList === void 0 ? void 0 : _result$groupList.forEach(function (line, index) {
var currentLineIndex = line.findIndex(function (item2) {
return item2.index === currentIndex;
});
if (currentLineIndex !== -1) {
// 计算属于哪行
result.currentLine = index;
// 当前行的已有的块数
result.currentLineBlockNum = line.length;
// 当前行的已有的块数
result.currentLineIndex = currentLineIndex;
}
});
}
}
return result;
}
/**
* @description 对列进行分组
*/
function groupColumn(arr) {
var groups = [];
var currentGroup = [];
var currentSum = 0;
for (var _iterator = _createForOfIteratorHelperLoose(arr.entries()), _step; !(_step = _iterator()).done;) {
var _step$value = _step.value,
numberIndex = _step$value[0],
numberStr = _step$value[1];
// 将字符串数字转换为整数
var number = parseInt(numberStr, 10);
currentSum += number;
if (currentSum <= 12) {
currentGroup.push({
index: +numberIndex,
value: number
});
} else {
groups.push(currentGroup);
currentGroup = [{
index: +numberIndex,
value: number
}];
currentSum = number;
}
}
// 处理最后一组(如果不为空)
if (currentGroup.length > 0) {
groups.push(currentGroup);
}
return groups;
}
function calculateAfterGroupList(_ref4) {
var beforeGroupList = _ref4.beforeGroupList,
direction = _ref4.direction,
groupInnerIndex = _ref4.groupInnerIndex;
// 单行最后一个块不允许拖拽
if (groupInnerIndex === (beforeGroupList === null || beforeGroupList === void 0 ? void 0 : beforeGroupList.length) - 1) {
return [];
}
var afterGroupList = [];
// 2024.10更新afterGroupList计算逻辑,不再走BLOCK_RESIZE_MAP映射
for (var i = 0; i < beforeGroupList.length; i++) {
var span = beforeGroupList[i];
var numSpan = Number(span);
var offset = direction === 'r' ? 1 : -1;
var targetSpan = numSpan;
if (i === groupInnerIndex) {
targetSpan = numSpan + offset;
if (targetSpan > 12 && direction === 'r' || targetSpan < 2 && direction === 'l') {
return [];
}
} else if (i === groupInnerIndex + 1) {
targetSpan = numSpan - offset;
if (targetSpan > 12 && direction === 'l' || targetSpan < 2 && direction === 'r') {
return [];
}
}
afterGroupList.push("" + targetSpan);
}
return afterGroupList;
}
/**
* @description 根据Schema生成新Node替换原始Node(先删除再生成新Node,保证NodeId不变)
* @param preNode 原始Node
* @param newSchema 新Schema
*/
function replaceNode(preNode, newSchema) {
var _preNode$document, _preNode$parent2;
var _ref5 = preNode || {},
prevSibling = _ref5.prevSibling,
nextSibling = _ref5.nextSibling;
preNode === null || preNode === void 0 ? void 0 : preNode.remove();
var newNode = preNode === null || preNode === void 0 ? void 0 : (_preNode$document = preNode.document) === null || _preNode$document === void 0 ? void 0 : _preNode$document.createNode(newSchema);
if (prevSibling) {
var _preNode$parent;
(_preNode$parent = preNode.parent) === null || _preNode$parent === void 0 ? void 0 : _preNode$parent.insertAfter(newNode, prevSibling, false);
return;
}
(_preNode$parent2 = preNode.parent) === null || _preNode$parent2 === void 0 ? void 0 : _preNode$parent2.insertBefore(newNode, nextSibling || undefined, false);
}