UNPKG

@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`

150 lines (146 loc) 8.78 kB
import _extends from "@babel/runtime/helpers/extends"; import $i18n from "../../../../locales/i18n"; import { createCellSnippet, createRowColSnippet } from "../../../../common/manager/setter-snippet"; import { updateSpan } from "./auto-block"; import { CELL, ROW, COL, BLOCK } from "../../../../common/util/const"; export var splitNodeByDimension = function splitNodeByDimension(dimension, node, preAppend) { if (preAppend === void 0) { preAppend = false; } var currentDocument = window.parent.AliLowCodeEngine.project.currentDocument; if (!currentDocument) { return; } var selectedIds = currentDocument.selection.selected; var nodeList = [node]; if (selectedIds.length > 1) { nodeList = selectedIds.map(function (id) { return currentDocument.nodesMap.get(id); }).filter(function (_node) { return !!_node; }); // 1. 类型必须完全相等,并且在同一个父元素中 if (nodeList.some(function (n) { var _n$parent, _node$parent; return [CELL, ROW, COL].indexOf(n.componentName) === -1 || ((_n$parent = n.parent) === null || _n$parent === void 0 ? void 0 : _n$parent.id) !== ((_node$parent = node.parent) === null || _node$parent === void 0 ? void 0 : _node$parent.id); })) { var _window, _window$parent, _window$parent$Next, _window$parent$Next$M; // @ts-ignore (_window = window) === null || _window === void 0 ? void 0 : (_window$parent = _window.parent) === null || _window$parent === void 0 ? void 0 : (_window$parent$Next = _window$parent.Next) === null || _window$parent$Next === void 0 ? void 0 : (_window$parent$Next$M = _window$parent$Next.Message) === null || _window$parent$Next$M === void 0 ? void 0 : _window$parent$Next$M.error($i18n.get({ id: 'NeighboringComponentsAreRequired_320275093', dm: '必须相邻组件才支持快捷操作' })); return; } // 排序, nodeList是按照选中顺序展示的 nodeList = nodeList.sort(function (a, b) { return a.index - b.index; }); // 2. 必须连续 var prevIndex = nodeList[0].index - 1; var lastIndex = nodeList[nodeList.length - 1].index; // 利用连续整数 n(n+1)/2 求和对比 if (nodeList.reduce(function (total, n) { return n.index + total; }, 0) !== lastIndex * (lastIndex + 1) / 2 - prevIndex * (prevIndex + 1) / 2) { var _window$parent2, _window$parent2$Next, _window$parent2$Next$; // @ts-ignore (_window$parent2 = window.parent) === null || _window$parent2 === void 0 ? void 0 : (_window$parent2$Next = _window$parent2.Next) === null || _window$parent2$Next === void 0 ? void 0 : (_window$parent2$Next$ = _window$parent2$Next.Message) === null || _window$parent2$Next$ === void 0 ? void 0 : _window$parent2$Next$.error($i18n.get({ id: 'NeighboringComponentsAreRequired_320275093', dm: '必须相邻组件才支持快捷操作' })); return; } } var parentNode = node.parent; var insertPoint = node; switch (node.componentName) { case CELL: case ROW: case COL: { var _parentNode, _parentNode2, _parentNode2$children; var pComponentName = (_parentNode = parentNode) === null || _parentNode === void 0 ? void 0 : _parentNode.componentName; var isSameDirection = pComponentName === ROW && dimension === 'v' || pComponentName === COL && dimension === 'h'; if (isSameDirection) { insertPoint = preAppend ? nodeList[0] : nodeList[nodeList.length - 1]; } // 需要判断下是不是全选了 if (!isSameDirection && nodeList.length > 1 && ((_parentNode2 = parentNode) === null || _parentNode2 === void 0 ? void 0 : (_parentNode2$children = _parentNode2.children) === null || _parentNode2$children === void 0 ? void 0 : _parentNode2$children.size) === nodeList.length) { var _parentNode$parent; var ppComponentName = (_parentNode$parent = parentNode.parent) === null || _parentNode$parent === void 0 ? void 0 : _parentNode$parent.componentName; isSameDirection = ppComponentName === ROW && dimension === 'v' || ppComponentName === COL && dimension === 'h'; if (isSameDirection) { insertPoint = parentNode; parentNode = parentNode.parent; } } if (isSameDirection) { var _node$document, _cellSnippet$props, _parentNode3, _parentNode4; // 切割手法和方向一致,直接 append 节点 var cellSnippet = createCellSnippet(); var newNode = (_node$document = node.document) === null || _node$document === void 0 ? void 0 : _node$document.createNode(_extends({}, cellSnippet, { props: _extends({}, cellSnippet.props, insertPoint.schema.props, { style: (_cellSnippet$props = cellSnippet.props) === null || _cellSnippet$props === void 0 ? void 0 : _cellSnippet$props.style }) })); preAppend ? newNode && ((_parentNode3 = parentNode) === null || _parentNode3 === void 0 ? void 0 : _parentNode3.insertBefore(newNode, insertPoint, false)) : newNode && ((_parentNode4 = parentNode) === null || _parentNode4 === void 0 ? void 0 : _parentNode4.insertAfter(newNode, insertPoint, false)); newNode === null || newNode === void 0 ? void 0 : newNode.select(); } else { var _node$schema$props, _node$schema$props2, _node$schema$props3, _node$document2, _parentNode5, _node$document4, _cellSnippet$props2; // 切割手法不一致,一变三节点 <ROW><CELL/><CELL/></ROW> var rowcolSnippet = createRowColSnippet(dimension === 'v' ? ROW : COL); var wrapSnippet = _extends({}, rowcolSnippet, { props: _extends({}, rowcolSnippet.props, { style: (_node$schema$props = node.schema.props) === null || _node$schema$props === void 0 ? void 0 : _node$schema$props.style, width: (_node$schema$props2 = node.schema.props) === null || _node$schema$props2 === void 0 ? void 0 : _node$schema$props2.width, height: (_node$schema$props3 = node.schema.props) === null || _node$schema$props3 === void 0 ? void 0 : _node$schema$props3.height }) }); var wrapNode = (_node$document2 = node.document) === null || _node$document2 === void 0 ? void 0 : _node$document2.createNode(wrapSnippet); wrapNode && ((_parentNode5 = parentNode) === null || _parentNode5 === void 0 ? void 0 : _parentNode5.insertAfter(wrapNode, node, false)); if (nodeList.length > 1) { var _node$document3, _parentNode6; // 多个子节点选中,需要先合并 var rowcolSnippet2 = createRowColSnippet(dimension === 'v' ? COL : ROW); var wrapNode2 = (_node$document3 = node.document) === null || _node$document3 === void 0 ? void 0 : _node$document3.createNode(rowcolSnippet2); wrapNode2 && ((_parentNode6 = parentNode) === null || _parentNode6 === void 0 ? void 0 : _parentNode6.insertAfter(wrapNode2, node, false)); nodeList.forEach(function (n) { return wrapNode2 === null || wrapNode2 === void 0 ? void 0 : wrapNode2.insertAfter(n); }); wrapNode2 && (wrapNode === null || wrapNode === void 0 ? void 0 : wrapNode.insertAfter(wrapNode2)); if (wrapNode2) { insertPoint = wrapNode2; } } else { // node 的宽度要去掉 node.setPropValue('style', undefined); wrapNode === null || wrapNode === void 0 ? void 0 : wrapNode.insertAfter(node, wrapNode, false); } var _cellSnippet = createCellSnippet(); var _newNode = (_node$document4 = node.document) === null || _node$document4 === void 0 ? void 0 : _node$document4.createNode(_extends({}, _cellSnippet, { props: _extends({}, _cellSnippet.props, insertPoint.schema.props, { style: (_cellSnippet$props2 = _cellSnippet.props) === null || _cellSnippet$props2 === void 0 ? void 0 : _cellSnippet$props2.style, width: undefined, height: undefined }) })); preAppend ? _newNode && (wrapNode === null || wrapNode === void 0 ? void 0 : wrapNode.insertBefore(_newNode, insertPoint, false)) : _newNode && (wrapNode === null || wrapNode === void 0 ? void 0 : wrapNode.insertAfter(_newNode, insertPoint, false)); _newNode === null || _newNode === void 0 ? void 0 : _newNode.select(); } } break; case BLOCK: if (dimension === 'v') { updateSpan({ parent: node.parent, child: node, type: 'split' }); } node.select(); break; default: break; } };