From 5c046ad894487a1c7440c1f1bc0a38af82629933 Mon Sep 17 00:00:00 2001 From: Smillence Date: Fri, 16 Apr 2021 10:16:21 +0800 Subject: [PATCH 1/5] API setauto rowheight --- .gitignore | 4 +++- src/global/api.js | 16 ++++++++----- src/global/getRowlen.js | 53 +++++++++++++++++++++++++++++++++++++++-- src/global/rhchInit.js | 17 ++++++++----- 4 files changed, 75 insertions(+), 15 deletions(-) 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 +} From 3cf423604562c48da01733aeb0249352bb89651d Mon Sep 17 00:00:00 2001 From: Smillence Date: Fri, 16 Apr 2021 10:16:21 +0800 Subject: [PATCH 2/5] API setauto rowheight --- .gitignore | 4 +++- src/global/api.js | 12 ++++++---- src/global/getRowlen.js | 53 +++++++++++++++++++++++++++++++++++++++-- src/global/rhchInit.js | 17 ++++++++----- 4 files changed, 73 insertions(+), 13 deletions(-) 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 4c84258..1082f0b 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); + } } } } @@ -6741,4 +6745,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 +} From 717ccaf6df545f010ca856fc0efd5c0dfdbd6f67 Mon Sep 17 00:00:00 2001 From: Smillence Date: Fri, 16 Apr 2021 16:39:44 +0800 Subject: [PATCH 3/5] API setauto rowheight cs --- src/global/getRowlen.js | 36 ++++++++++++++++++++++++++---------- src/global/location.js | 23 +++++++++++++++++++---- 2 files changed, 45 insertions(+), 14 deletions(-) diff --git a/src/global/getRowlen.js b/src/global/getRowlen.js index 92b1ea0..19c1a2a 100644 --- a/src/global/getRowlen.js +++ b/src/global/getRowlen.js @@ -1,9 +1,9 @@ -import { luckysheetfontformat } from '../utils/util'; +import {luckysheetfontformat} from '../utils/util'; import menuButton from '../controllers/menuButton'; -import { getcellvalue,checkstatusByCell } from './getdata'; -import { colLocationByIndex } from './location'; -import { hasChinaword, isRealNull,checkWordByteLength } from './validate'; -import { isInlineStringCell } from '../controllers/inlineString'; +import {checkstatusByCell} from './getdata'; +import {colLocationByIndex,colSpanLocationByIndex} from './location'; +import {checkWordByteLength, hasChinaword, isRealNull} from './validate'; +import {isInlineStringCell} from '../controllers/inlineString'; import Store from '../store'; /** @@ -90,12 +90,19 @@ function computeRowlenByContent(d, r) { for(let c = 0; c < d[r].length; c++){ let cell = d[r][c]; - if(cell == null || cell.mc != null){ + if (cell == null) { continue; } + if (cell.mc != null) { + if (1 !== cell.mc.rs) { + continue; + } + } + + if(cell != null && (cell.v != null || isInlineStringCell(cell)) ){ - let cellWidth = colLocationByIndex(c)[1] - colLocationByIndex(c)[0] - 2; + let cellWidth = computeCellWidth(cell, c); let textInfo = getCellTextInfo(cell, canvas,{ r:r, @@ -105,12 +112,12 @@ function computeRowlenByContent(d, r) { let computeRowlen = 0; - if(textInfo!=null){ - computeRowlen = textInfo.textHeightAll+2; + if (textInfo != null) { + computeRowlen = textInfo.textHeightAll + 2; } //比较计算高度和当前高度取最大高度 - if(computeRowlen > currentRowLen){ + if (computeRowlen > currentRowLen) { currentRowLen = computeRowlen; } } @@ -119,6 +126,15 @@ function computeRowlenByContent(d, r) { return currentRowLen; } +function computeCellWidth(cell, col_index) { + let colLocationArr = colLocationByIndex(col_index); + if (cell.mc && cell.mc.c !== cell.mc.cs) { + colLocationArr = colSpanLocationByIndex(col_index, cell.mc.cs); + } + + return colLocationArr[1] - colLocationArr[0] - 2; +} + //计算表格行高数组 function computeRowlenArr(rowHeight, cfg) { diff --git a/src/global/location.js b/src/global/location.js index 893cbc3..b4c130f 100644 --- a/src/global/location.js +++ b/src/global/location.js @@ -24,12 +24,12 @@ function rowLocation(y) { else if (row_index == -1 && y <= 0) { row_index = 0; } - + return rowLocationByIndex(row_index); } function colLocationByIndex(col_index){ - let col = 0, col_pre = 0; + let col = 0, col_pre = 0; col = Store.visibledatacolumn[col_index]; if (col_index == 0) { @@ -42,6 +42,20 @@ function colLocationByIndex(col_index){ return [col_pre, col, col_index]; } +function colSpanLocationByIndex(col_index, span){ + let col = 0, col_pre = 0; + col = Store.visibledatacolumn[col_index + span - 1]; + + if (col_index == 0) { + col_pre = 0; + } + else { + col_pre = Store.visibledatacolumn[col_index - 1]; + } + + return [col_pre, col, col_index]; +} + function colLocation(x) { let col_index = luckysheet_searcharray(Store.visibledatacolumn, x); @@ -57,7 +71,7 @@ function colLocation(x) { function mouseposition(x, y) { let container_offset = $("#" + Store.container).offset(); - + let newX = x - container_offset.left - Store.rowHeaderWidth, newY = y - container_offset.top - Store.infobarHeight - Store.toolbarHeight - Store.calculatebarHeight - Store.columnHeaderHeight; @@ -68,6 +82,7 @@ export { rowLocationByIndex, rowLocation, colLocationByIndex, + colSpanLocationByIndex, colLocation, mouseposition, -} \ No newline at end of file +} From 3d6ed4956d107ab6c7953684f8306f7c4a4203fe Mon Sep 17 00:00:00 2001 From: Smillence Date: Thu, 22 Apr 2021 19:03:42 +0800 Subject: [PATCH 4/5] auto collen --- src/global/api.js | 8 ++++++-- src/global/getRowlen.js | 34 ++++++++++++++++++++++++++++++++++ src/global/rhchInit.js | 6 +++++- 3 files changed, 45 insertions(+), 3 deletions(-) diff --git a/src/global/api.js b/src/global/api.js index 1082f0b..9004f3e 100644 --- a/src/global/api.js +++ b/src/global/api.js @@ -1420,8 +1420,12 @@ export function setColumnWidth(columnInfo, options = {}) { if(parseInt(c) >= 0){ let len = columnInfo[c]; - if(Number(len) >= 0){ - cfg['columnlen'][parseInt(c)] = Number(len); + if (len === 'auto') { + cfg['columnlen'][parseInt(c)] = len + } else { + if(Number(len) >= 0){ + cfg['columnlen'][parseInt(c)] = Number(len); + } } } } diff --git a/src/global/getRowlen.js b/src/global/getRowlen.js index 19c1a2a..653afbf 100644 --- a/src/global/getRowlen.js +++ b/src/global/getRowlen.js @@ -135,6 +135,39 @@ function computeCellWidth(cell, col_index) { return colLocationArr[1] - colLocationArr[0] - 2; } +function computeColWidthByContent(d, c, rh) { + let currentColLen = 0; + let rowlenArr = computeRowlenArr(rh, c) + + let canvas = $("#luckysheetTableContent").get(0).getContext("2d"); + canvas.textBaseline = 'top'; //textBaseline以top计算 + + for (var i = 0; i < d.length; i++) { + var cell = d[i][c] + + if (cell != null && (cell.v != null || isInlineStringCell(cell))) { + let cellHeight = rowlenArr[c]; + let textInfo = getCellTextInfo(cell, canvas, { + r: i, + c: c, + cellHeight: cellHeight + }); + + let computeCollen = 0; + + if (textInfo != null) { + computeCollen = textInfo.textWidthAll + 2; + } + + //比较计算高度和当前高度取最大高度 + if (computeCollen > currentColLen) { + currentColLen = computeCollen; + } + } + } + + return currentColLen; +} //计算表格行高数组 function computeRowlenArr(rowHeight, cfg) { @@ -1677,6 +1710,7 @@ function drawLineInfo(wordGroup, cancelLine,underLine,option){ export { + computeColWidthByContent, rowlenByRange, computeRowlenByContent, computeRowlenArr, diff --git a/src/global/rhchInit.js b/src/global/rhchInit.js index c121850..6be35bb 100644 --- a/src/global/rhchInit.js +++ b/src/global/rhchInit.js @@ -1,5 +1,5 @@ import Store from '../store'; -import { computeRowlenByContent } from './getRowlen'; +import { computeRowlenByContent,computeColWidthByContent } from './getRowlen'; import luckysheetConfigsetting from '../controllers/luckysheetConfigsetting'; export default function rhchInit(rowheight, colwidth) { @@ -76,6 +76,10 @@ export default function rhchInit(rowheight, colwidth) { continue; } + // 自动行高计算 + if (firstcolumnlen === 'auto') { + firstcolumnlen = computeColWidthByContent(Store.flowdata, c, rowheight); + } Store.ch_width += Math.round((firstcolumnlen + 1)*Store.zoomRatio); Store.visibledatacolumn.push(Store.ch_width);//列的临时长度分布 From d610678c9bd68f6b79a060cd3db535b6dcd35d70 Mon Sep 17 00:00:00 2001 From: Smillence Date: Mon, 31 May 2021 14:30:06 +0800 Subject: [PATCH 5/5] =?UTF-8?q?=E4=B8=AD=E8=8B=B1=E6=96=87=E8=87=AA?= =?UTF-8?q?=E5=8A=A8=E6=8D=A2=E8=A1=8C=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/global/getRowlen.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/global/getRowlen.js b/src/global/getRowlen.js index 653afbf..4956659 100644 --- a/src/global/getRowlen.js +++ b/src/global/getRowlen.js @@ -1002,7 +1002,7 @@ function getCellTextInfo(cell , ctx, option){ if(preMeasureText!=null){ spaceOrTwoByte = { index:i, - str:preStr, + str:preStr + lastWord, width:preTextWidth, height:preTextHeight, asc:preMeasureText.actualBoundingBoxAscent,