diff --git a/.gitignore b/.gitignore index ab4b17f..6ee31b1 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,6 @@ node_modules package-lock.json dist -docs/.vuepress/dist \ No newline at end of file +docs/.vuepress/dist + +.idea diff --git a/src/global/api.js b/src/global/api.js index 3d586b6..5461e7d 100644 --- a/src/global/api.js +++ b/src/global/api.js @@ -1332,7 +1332,7 @@ export function showColumn(startIndex, endIndex, options = {}) { /** - * 设置指定行的高度 + * 设置指定行的高度。优先级最高,高于默认行高和用户自定义行高。 * @param {Object} rowInfo 行数和高度对应关系 * @param {Object} options 可选参数 * @param {Number} options.order 工作表索引;默认值为当前工作表索引 @@ -1363,8 +1363,12 @@ export function setRowHeight(rowInfo, options = {}) { if(parseInt(r) >= 0){ let len = rowInfo[r]; - if(Number(len) >= 0){ - cfg['rowlen'][parseInt(r)] = Number(len); + if (len === 'auto') { + cfg['rowlen'][parseInt(r)] = len + } else { + if(Number(len) >= 0){ + cfg['rowlen'][parseInt(r)] = Number(len); + } } } } @@ -6652,7 +6656,7 @@ export function refreshFormula (success) { * @param {Array} data 工作簿配置,可以包含多个表 * @param {Object} options 可选参数 * @param {Function} options.success 操作结束的回调函数 - * + * */ export function updataSheet (options = {}) { let {data, success} = options @@ -6728,7 +6732,7 @@ export function refreshMenuButtonFocus(data ,r,c , success){ export function checkTheStatusOfTheSelectedCells(type,status){ /* 获取选区内所有的单元格-扁平后的处理 */ - let cells = getRangeWithFlatten(); + let cells = getRangeWithFlatten(); let flag = cells.every(({r,c})=>{ let cell = Store.flowdata[r][c]; @@ -6739,4 +6743,4 @@ export function checkTheStatusOfTheSelectedCells(type,status){ }) return flag; -} \ No newline at end of file +} diff --git a/src/global/getRowlen.js b/src/global/getRowlen.js index 3f7cc4a..92b1ea0 100644 --- a/src/global/getRowlen.js +++ b/src/global/getRowlen.js @@ -6,7 +6,15 @@ import { hasChinaword, isRealNull,checkWordByteLength } from './validate'; import { isInlineStringCell } from '../controllers/inlineString'; import Store from '../store'; -//计算范围行高 +/** + * 计算范围行高 + * + * @param d 原始数据 + * @param r1 起始行 + * @param r2 截至行 + * @param cfg 配置 + * @returns 计算后的配置 + */ function rowlenByRange(d, r1, r2, cfg) { let cfg_clone = $.extend(true, {}, cfg); if(cfg_clone["rowlen"] == null){ @@ -72,6 +80,46 @@ function rowlenByRange(d, r1, r2, cfg) { return cfg_clone; } +//根据内容计算行高 +function computeRowlenByContent(d, r) { + let currentRowLen = 0; + + let canvas = $("#luckysheetTableContent").get(0).getContext("2d"); + canvas.textBaseline = 'top'; //textBaseline以top计算 + + for(let c = 0; c < d[r].length; c++){ + let cell = d[r][c]; + + if(cell == null || cell.mc != null){ + continue; + } + + if(cell != null && (cell.v != null || isInlineStringCell(cell)) ){ + let cellWidth = colLocationByIndex(c)[1] - colLocationByIndex(c)[0] - 2; + + let textInfo = getCellTextInfo(cell, canvas,{ + r:r, + c:c, + cellWidth:cellWidth + }); + + let computeRowlen = 0; + + if(textInfo!=null){ + computeRowlen = textInfo.textHeightAll+2; + } + + //比较计算高度和当前高度取最大高度 + if(computeRowlen > currentRowLen){ + currentRowLen = computeRowlen; + } + } + } + + return currentRowLen; +} + + //计算表格行高数组 function computeRowlenArr(rowHeight, cfg) { let rowlenArr = []; @@ -1614,8 +1662,9 @@ function drawLineInfo(wordGroup, cancelLine,underLine,option){ export { rowlenByRange, + computeRowlenByContent, computeRowlenArr, getCellTextSplitArr, getMeasureText, getCellTextInfo -} \ No newline at end of file +} diff --git a/src/global/rhchInit.js b/src/global/rhchInit.js index 35fb1bf..c121850 100644 --- a/src/global/rhchInit.js +++ b/src/global/rhchInit.js @@ -1,4 +1,5 @@ import Store from '../store'; +import { computeRowlenByContent } from './getRowlen'; import luckysheetConfigsetting from '../controllers/luckysheetConfigsetting'; export default function rhchInit(rowheight, colwidth) { @@ -19,19 +20,23 @@ export default function rhchInit(rowheight, colwidth) { Store.visibledatarow.push(Store.rh_height); continue; } - + + // 自动行高计算 + if (rowlen === 'auto') { + rowlen = computeRowlenByContent(Store.flowdata, r); + } Store.rh_height += Math.round((rowlen + 1) * Store.zoomRatio); Store.visibledatarow.push(Store.rh_height); //行的临时长度分布 } // 如果增加行和回到顶部按钮隐藏,则减少底部空白区域,但是预留足够空间给单元格下拉按钮 - if(!luckysheetConfigsetting.enableAddRow && !luckysheetConfigsetting.enableAddBackTop){ + if (!luckysheetConfigsetting.enableAddRow && !luckysheetConfigsetting.enableAddBackTop) { Store.rh_height += 29; - }else{ + } else { Store.rh_height += 80; //最底部增加空白 } - + } //列宽 @@ -79,7 +84,7 @@ export default function rhchInit(rowheight, colwidth) { // maxColumnlen = firstcolumnlen + 1; // } } - + // Store.ch_width += 120; Store.ch_width += maxColumnlen; } @@ -93,4 +98,4 @@ export function zoomSetting(){ $("#luckysheet-rows-h").width((Store.rowHeaderWidth-1.5)); $("#luckysheet-cols-h-c").height((Store.columnHeaderHeight-1.5)); $("#luckysheet-left-top").css({width:Store.rowHeaderWidth-1.5, height:Store.columnHeaderHeight-1.5}); -} \ No newline at end of file +}