diff --git a/src/controllers/inlineString.js b/src/controllers/inlineString.js index 50d83c89b..c81f84151 100644 --- a/src/controllers/inlineString.js +++ b/src/controllers/inlineString.js @@ -1,9 +1,10 @@ import {getFontStyleByCell, textTrim} from "../global/getdata"; +import {selectTextContent,selectTextContentCross,selectTextContentCollapse} from '../global/cursorPos'; import locale from '../locale/locale'; import Store from '../store'; export function isInlineStringCell(cell){ - let isIs = cell.ct!=null && cell.ct.t=="inlineStr" && cell.ct.s!=null && cell.ct.s.length>0; + let isIs = cell && cell.ct!=null && cell.ct.t=="inlineStr" && cell.ct.s!=null && cell.ct.s.length>0; return isIs; } @@ -13,26 +14,27 @@ export function isInlineStringCT(ct){ } export function updateInlineStringFormat(cell, attr, value, $input){ - - - - let s = Store.inlineStringEditCache; + // let s = Store.inlineStringEditCache; var w = window.getSelection(); - var range = w.getRangeAt(0); - if(range.collapsed===true){ - return; - } - - if(isInlineStringCell(cell)){ - if(Store.inlineStringEditCache==null){ - Store.inlineStringEditCache = JSON.parse(JSON.stringify(cell.ct.s)); - } + var range; + if(w.type=="None"){ + range = Store.inlineStringEditRange; } else{ - Store.inlineStringEditCache = [{ - v:cell.v - }]; - } + range = w.getRangeAt(0); + } + + + // if(isInlineStringCell(cell)){ + // if(Store.inlineStringEditCache==null){ + // Store.inlineStringEditCache = JSON.parse(JSON.stringify(cell.ct.s)); + // } + // } + // else{ + // Store.inlineStringEditCache = [{ + // v:cell.v + // }]; + // } let cac = range.commonAncestorContainer; let $textEditor; @@ -44,54 +46,240 @@ export function updateInlineStringFormat(cell, attr, value, $input){ } let $functionbox = $(cac).closest("#luckysheet-functionbox-cell"); + if($textEditor.length==0 && $functionbox.length==0 && Store.inlineStringEditRange!=null){ + range = Store.inlineStringEditRange; + cac = range.commonAncestorContainer; + if(cac.id=="luckysheet-rich-text-editor"){ + $textEditor = $(cac); + } + else{ + $textEditor = $(cac).closest("#luckysheet-rich-text-editor"); + } + $functionbox = $(cac).closest("#luckysheet-functionbox-cell"); + } + + if(range.collapsed===true){ + return; + } + let endContainer = range.endContainer, startContainer = range.startContainer; let endOffset = range.endOffset, startOffset = range.startOffset; if($textEditor.length>0){ if(startContainer===endContainer){ + let span = startContainer.parentNode, spanIndex; + + let content = span.innerHTML; + let left="" , mid="" , right=""; + let s1=0, s2=startOffset, s3 = endOffset, s4=content.length; + left = content.substring(s1, s2); + mid = content.substring(s2, s3); + right = content.substring(s3, s4); + + let cont = ""; + if(left!=""){ + cont += "" + left + ""; + } + + if(mid!=""){ + // let styleObj = {}; + // styleObj[attr] = value; + // let s = getFontStyleByCell(styleObj, undefined, undefined, false); + // let ukey = textTrim(s.substr(0, s.indexOf(':'))); + // let uvalue = textTrim(s.substr(s.indexOf(':')+1)); + // uvalue = uvalue.substr(0, uvalue.length-1); + // let cssText = span.style.cssText; + // cssText = removeClassWidthCss(cssText, attr); + + let cssText = getCssText(span.style.cssText, attr, value); + + cont += "" + mid + ""; + } + + if(right!=""){ + cont += "" + right + ""; + } + if(startContainer.parentNode.tagName=="SPAN"){ - let span = startContainer.parentNode; - let content = span.innerHTML; - let left="" , mid="" , right=""; - let s1=0, s2=startOffset, s3 = endOffset, s4=content.length; - left = content.substring(s1, s2); - mid = content.substring(s2, s3); - right = content.substring(s3, s4); + spanIndex = $textEditor.find("span").index(span); + $(span).replaceWith(cont); + } + else{ + spanIndex = 0; + $(span).html(cont); + } + + + let seletedNodeIndex = 0; + if(s1==s2){ + seletedNodeIndex = spanIndex; + } + else{ + seletedNodeIndex = spanIndex+1; + } + + selectTextContent($textEditor.find("span").get(seletedNodeIndex)); + } + else{ + if(startContainer.parentNode.tagName=="SPAN" && endContainer.parentNode.tagName=="SPAN"){ + let startSpan = startContainer.parentNode, startSpanIndex; + let endSpan = endContainer.parentNode, endSpanIndex; + + startSpanIndex = $textEditor.find("span").index(startSpan); + endSpanIndex = $textEditor.find("span").index(endSpan); + + let startContent = startSpan.innerHTML, endContent = endSpan.innerHTML; + let sleft="" , sright="", eleft="" , eright=""; + let s1=0, s2=startOffset, s3 = endOffset, s4=endContent.length; + sleft = startContent.substring(s1, s2); + sright = startContent.substring(s2, startContent.length); + + eleft = endContent.substring(0, s3); + eright = endContent.substring(s3, s4); + let spans = $textEditor.find("span"); + let replaceSpans = spans.slice(startSpanIndex, endSpanIndex+1); let cont = ""; - if(left!=""){ - cont += "" + left + ""; + for(let i=0;i" + content + ""; + } + if(sleft!=""){ + cont += "" + sleft + ""; } - if(mid!=""){ - let styleObj = {}; - styleObj[attr] = value; - let s = getFontStyleByCell(styleObj); - let ukey = textTrim(s.substr(0, s.indexOf(':'))); - let uvalue = textTrim(s.substr(s.indexOf(':')+1)); - uvalue = uvalue.substr(0, uvalue.length-1); - cont += "" + mid + ""; + if(sright!=""){ + let cssText = getCssText(startSpan.style.cssText, attr, value); + cont += "" + sright + ""; } - if(right!=""){ - cont += "" + right + ""; + if(startSpanIndex" + content + ""; + } } - $(span).replaceWith(cont); - } - else{ + if(eleft!=""){ + let cssText = getCssText(endSpan.style.cssText, attr, value); + cont += "" + eleft + ""; + } + + if(eright!=""){ + cont += "" + eright + ""; + } + + for(let i=endSpanIndex+1;i" + content + ""; + } + + $textEditor.html(cont); + + // console.log(replaceSpans, cont); + // replaceSpans.replaceWith(cont); + let startSeletedNodeIndex, endSeletedNodeIndex; + if(s1==s2){ + startSeletedNodeIndex = startSpanIndex; + endSeletedNodeIndex = endSpanIndex; + } + else{ + startSeletedNodeIndex = startSpanIndex+1; + endSeletedNodeIndex = endSpanIndex+1; + } + + spans = $textEditor.find("span"); + + selectTextContentCross(spans.get(startSeletedNodeIndex), spans.get(endSeletedNodeIndex)); } } - else{ - - } } else if($functionbox.length>0){ } } +export function enterKeyControll(){ + var w = window.getSelection(); + + if(w.type=="None"){ + return + } + var range = w.getRangeAt(0); + let cac = range.commonAncestorContainer; + let $textEditor; + if(cac.id=="luckysheet-rich-text-editor"){ + $textEditor = $(cac); + } + else{ + $textEditor = $(cac).closest("#luckysheet-rich-text-editor"); + } + let $functionbox = $(cac).closest("#luckysheet-functionbox-cell"); + + // if(range.collapsed===true){ + // return; + // } + + let endContainer = range.endContainer, startContainer = range.startContainer; + let endOffset = range.endOffset, startOffset = range.startOffset; + + if($textEditor.length>0){ + let startSpan = startContainer.parentNode; + let startSpanIndex = $textEditor.find("span").index(startSpan); + if(range.collapsed===false){ + range.deleteContents(); + } + + let startContent = startSpan.innerHTML; + let sleft="" , sright=""; + let s1=0, s2=startOffset; + + sleft = startContent.substring(s1, s2); + sright = startContent.substring(s2, startContent.length); + + let cont = "" + sleft + "\n" + sright + ""; + let spanIndex; + if(startContainer.parentNode.tagName=="SPAN"){ + spanIndex = $textEditor.find("span").index(startSpan); + $(startSpan).replaceWith(cont); + } + else{ + spanIndex = 0; + $(startSpan).html(cont); + } + + selectTextContentCollapse($textEditor.find("span").get(spanIndex), startOffset+1); + + } + else if($functionbox.length>0){ + + } +} + +export function convertSpanToShareString($dom){ + let styles = [], preStyleList, preStyleListString=null; + for(let i=0;i<$dom.length;i++){ + let span = $dom.get(i); + let styleList = convertCssToStyleList(span.style.cssText); + + let curStyleListString = JSON.stringify(styleList); + let v = span.innerHTML; + v = v.replace(/\n/g, "\r\n"); + + if(curStyleListString==preStyleListString){ + preStyleList.v += v; + } + else{ + styleList.v = v; + styles.push(styleList); + + preStyleListString = curStyleListString; + preStyleList = styleList; + } + } + return styles; +} export function convertCssToStyleList(cssText){ if(cssText==null || cssText.length==0){ @@ -179,47 +367,86 @@ export function convertCssToStyleList(cssText){ return styleList; } +const luckyToCssName = { + "bl":"font-weight", + "it":"font-style", + "ff":"font-family", + "fs":"font-size", + "fc":"color", + "cl":"text-decoration", + "un":"text-decoration", +} + function upsetClassWithCss(cssText, ukey, uvalue){ let cssTextArray = cssText.split(";"); let newCss = ""; + if(ukey==null || ukey.length==0){ + return cssText; + } if(cssText.indexOf(ukey)>-1){ for(let i=0;i0){ newCss += key + ":" + value + ";"; } } } - else{ + else if(ukey.length>0){ cssText += ukey + ":" + uvalue + ";"; + newCss = cssText; } - return cssText; + return newCss; } function removeClassWidthCss(cssText, ukey){ let cssTextArray = cssText.split(";"); let newCss = ""; + let oUkey = ukey; + if(ukey==null || ukey.length==0){ + return cssText; + } + if(ukey in luckyToCssName){ + ukey = luckyToCssName[ukey]; + } if(cssText.indexOf(ukey)>-1){ for(let i=0;i0){ newCss += key + ":" + value + ";"; } } } + else{ + newCss = cssText; + } + + return newCss; +} + +function getCssText(cssText, attr, value){ + let styleObj = {}; + styleObj[attr] = value; + let s = getFontStyleByCell(styleObj, undefined, undefined, false); + let ukey = textTrim(s.substr(0, s.indexOf(':'))); + let uvalue = textTrim(s.substr(s.indexOf(':')+1)); + uvalue = uvalue.substr(0, uvalue.length-1); + // let cssText = span.style.cssText; + cssText = removeClassWidthCss(cssText, attr); + + cssText = upsetClassWithCss(cssText, ukey, uvalue); return cssText; } diff --git a/src/controllers/keyboard.js b/src/controllers/keyboard.js index 4db773331..5356c0025 100644 --- a/src/controllers/keyboard.js +++ b/src/controllers/keyboard.js @@ -25,6 +25,7 @@ import formula from '../global/formula'; import cleargridelement from '../global/cleargridelement'; import tooltip from '../global/tooltip'; import locale from '../locale/locale'; +import {enterKeyControll} from './inlineString'; import Store from '../store'; @@ -300,10 +301,16 @@ export function keyboardInitial(){ return; } + + let $inputbox = $("#luckysheet-input-box"); - if (kcode == keycode.ENTER && parseInt($inputbox.css("top")) > 0) { + if((altKey || event.metaKey) && kcode == keycode.ENTER && parseInt($inputbox.css("top")) > 0){ + enterKeyControll(); + event.preventDefault(); + } + else if (kcode == keycode.ENTER && parseInt($inputbox.css("top")) > 0) { if ($("#luckysheet-formula-search-c").is(":visible") && formula.searchFunctionCell != null) { formula.searchFunctionEnter($("#luckysheet-formula-search-c").find(".luckysheet-formula-search-item-active")); } diff --git a/src/controllers/menuButton.js b/src/controllers/menuButton.js index 45468966b..939fadbc2 100644 --- a/src/controllers/menuButton.js +++ b/src/controllers/menuButton.js @@ -798,6 +798,15 @@ const menuButton = { //字体大小 let luckysheet_fs_setTimeout = null; $("#luckysheet-icon-font-size").mousedown(function(e){ + if (parseInt($("#luckysheet-input-box").css("top")) > 0){ + let w = window.getSelection(); + if(w.type!="None"){ + let range = w.getRangeAt(0); + if(!range.collapsed){ + Store.inlineStringEditRange = range.cloneRange(); + } + } + } hideMenuByCancel(e); e.stopPropagation(); }).click(function(){ @@ -861,6 +870,8 @@ const menuButton = { menuleft = menuleft - tlen + userlen; } mouseclickposition($menuButton, menuleft, $(this).offset().top + 25, "lefttop"); + + }) .find("input.luckysheet-toolbar-textinput").keydown(function(e){ hideMenuByCancel(e); @@ -2982,7 +2993,9 @@ const menuButton = { } } - cfg = rowlenByRange(d, row_st, row_ed, cfg); + if(attr == "tb" || attr == "tr" || attr == "fs"){ + cfg = rowlenByRange(d, row_st, row_ed, cfg); + } } if(attr == "tb" || attr == "tr" || attr == "fs"){ @@ -3352,6 +3365,7 @@ const menuButton = { var w = window.getSelection(); var range = w.getRangeAt(0); let startContainer = range.startContainer; + Store.inlineStringEditRange = null; const _locale = locale(); if(startContainer.parentNode.tagName=="SPAN"){ let cssText = startContainer.parentNode.style.cssText; diff --git a/src/controllers/sheetBar.js b/src/controllers/sheetBar.js index c048ee6d9..cfcb4cbc8 100644 --- a/src/controllers/sheetBar.js +++ b/src/controllers/sheetBar.js @@ -11,6 +11,8 @@ import { isEditMode } from '../global/validate'; import formula from '../global/formula'; import cleargridelement from '../global/cleargridelement'; import tooltip from '../global/tooltip'; + selectTextDom +import {selectTextDom} from '../global/cursorPos'; import locale from '../locale/locale'; import Store from '../store'; @@ -206,16 +208,7 @@ export function initialSheetBar(){ $t.attr("contenteditable", "true").addClass("luckysheet-mousedown-cancel").data("oldtxt", $t.text()); setTimeout(function () { - if (document.selection) { - let range = document.body.createTextRange(); - range.moveToElementText($t.get(0)); - range.select(); - } else if (window.getSelection) { - let range = document.createRange(); - range.selectNodeContents($t.get(0)); - window.getSelection().removeAllRanges(); - window.getSelection().addRange(range); - } + selectTextDom($t.get(0)); }, 1); } diff --git a/src/global/cursorPos.js b/src/global/cursorPos.js index b0eabd329..c01209e6d 100644 --- a/src/global/cursorPos.js +++ b/src/global/cursorPos.js @@ -60,8 +60,64 @@ function hideMenuByCancel(event){ } } +function selectTextDom(ele){ + if (window.getSelection) { + let range = document.createRange(); + range.selectNodeContents(ele); + window.getSelection().removeAllRanges(); + window.getSelection().addRange(range); + } + else if (document.selection) { + let range = document.body.createTextRange(); + range.moveToElementText(ele); + range.select(); + } +} + +function selectTextContent(ele){ + if (window.getSelection) { + let range = document.createRange(); + var content=ele.firstChild; + range.setStart(content,0); + range.setEnd(content,content.length); + window.getSelection().removeAllRanges(); + window.getSelection().addRange(range); + } + else if (document.selection) { + let range = document.body.createTextRange(); + range.moveToElementText(ele); + range.select(); + } +} + +function selectTextContentCross(sEle, eEle){ + if (window.getSelection) { + let range = document.createRange(); + var sContent=sEle.firstChild, eContent=eEle.firstChild; + range.setStart(sContent,0); + range.setEnd(eContent,eContent.length); + window.getSelection().removeAllRanges(); + window.getSelection().addRange(range); + } +} + +function selectTextContentCollapse(sEle, index){ + if (window.getSelection) { + let range = document.createRange(); + var sContent=sEle.firstChild; + range.setStart(sContent,index); + range.collapse(true); + window.getSelection().removeAllRanges(); + window.getSelection().addRange(range); + } +} + export { luckysheetRangeLast, getCursortPosition, hideMenuByCancel, + selectTextContent, + selectTextDom, + selectTextContentCross, + selectTextContentCollapse } \ No newline at end of file diff --git a/src/global/formula.js b/src/global/formula.js index 9fe88d659..74d71b5af 100644 --- a/src/global/formula.js +++ b/src/global/formula.js @@ -9,7 +9,7 @@ import luckysheetFreezen from '../controllers/freezen'; import { seletedHighlistByindex, luckysheet_count_show } from '../controllers/select'; import { isRealNum, isRealNull, valueIsError, isEditMode } from './validate'; import { isdatetime, isdatatype } from './datecontroll'; -import { getCellTextSplitArr } from '../global/getRowlen'; +import { getCellTextSplitArr,getCellTextInfo } from '../global/getRowlen'; import { getcellvalue,getcellFormula,getInlineStringNoStyle } from './getdata'; import { setcellvalue } from './setdata'; import { genarate, valueShowEs } from './format'; @@ -18,7 +18,7 @@ import tooltip from './tooltip'; import { rowLocation, colLocation, colLocationByIndex, mouseposition } from './location'; import { luckysheetRangeLast } from './cursorPos'; import { jfrefreshgrid } from './refresh'; -import { isInlineStringCell } from '../controllers/inlineString'; +import { isInlineStringCell,convertSpanToShareString } from '../controllers/inlineString'; // import luckysheet_function from '../function/luckysheet_function'; // import functionlist from '../function/functionlist'; import { luckysheet_compareWith, luckysheet_getarraydata, luckysheet_getcelldata, luckysheet_parseData, luckysheet_getValue, luckysheet_indirect_check, luckysheet_indirect_check_return, luckysheet_offset_check } from '../function/func'; @@ -1197,41 +1197,70 @@ const luckysheetformula = { let _this = this; let $input = $("#luckysheet-rich-text-editor"); - - // API, we get value from user - value = value || $input.text(); + let inputText = $input.text(), inputHtml = $input.html(); + + + if (_this.rangetosheet != null && _this.rangetosheet != Store.currentSheetIndex) { sheetmanage.changeSheetExec(_this.rangetosheet); } let curv = Store.flowdata[r][c]; - - if(isRealNull(value)){ - if(curv == null || (isRealNull(curv.v) && curv.spl == null)){ - _this.cancelNormalSelected(); - return; - } + let isPrevInline = isInlineStringCell(curv); + let isCurInline = (inputText.slice(0, 1) != "=" && inputHtml.indexOf("span")>-1); + if(!value && !isCurInline && isPrevInline){ + delete curv.ct.s; + curv.ct.t = "g"; + curv.ct.fa = "General"; } - else if(curv!=null && curv.qp != 1){ - if (getObjType(curv) == "object" && (value == curv.f || value == curv.v || value == curv.m)) { - _this.cancelNormalSelected(); - return; - } - else if (value == curv) { - _this.cancelNormalSelected(); - return; + else if(isCurInline){ + if (getObjType(curv) != "object") { + curv = {}; } - } + delete curv.f; + delete curv.v; + delete curv.m; - if (getObjType(value) == "string" && value.slice(0, 1) == "=" && value.length > 1) { + if(curv.ct==null){ + curv.ct = {}; + curv.ct.fa = "General"; + } + curv.ct.t = "inlineStr"; + curv.ct.s = convertSpanToShareString($input.find("span")); } - else if(getObjType(curv) == "object" && curv.ct != null && curv.ct.fa != null && curv.ct.fa != "@" && !isRealNull(value)){ - delete curv.m;//更新时间m处理 , 会实际删除单元格数据的参数(flowdata时已删除) - if(curv.f != null){ //如果原来是公式,而更新的数据不是公式,则把公式删除 - delete curv.f; - delete curv.spl; //删除单元格的sparklines的配置串 + + // API, we get value from user + value = value || $input.text(); + + if(!isCurInline){ + if(isRealNull(value)){ + if(curv == null || (isRealNull(curv.v) && curv.spl == null)){ + _this.cancelNormalSelected(); + return; + } + } + else if(curv!=null && curv.qp != 1){ + if (getObjType(curv) == "object" && (value == curv.f || value == curv.v || value == curv.m)) { + _this.cancelNormalSelected(); + return; + } + else if (value == curv) { + _this.cancelNormalSelected(); + return; + } + } + + if (getObjType(value) == "string" && value.slice(0, 1) == "=" && value.length > 1) { + + } + else if(getObjType(curv) == "object" && curv.ct != null && curv.ct.fa != null && curv.ct.fa != "@" && !isRealNull(value)){ + delete curv.m;//更新时间m处理 , 会实际删除单元格数据的参数(flowdata时已删除) + if(curv.f != null){ //如果原来是公式,而更新的数据不是公式,则把公式删除 + delete curv.f; + delete curv.spl; //删除单元格的sparklines的配置串 + } } } @@ -1242,53 +1271,20 @@ const luckysheetformula = { let d = editor.deepCopyFlowData(Store.flowdata); if (getObjType(curv) == "object") { - if(getObjType(value) == "string" && value.slice(0, 1) == "=" && value.length > 1){ - let v = _this.execfunction(value, r, c, true); - - curv = _this.execFunctionGroupData[r][c]; - curv.f = v[2]; - - //打进单元格的sparklines的配置串, 报错需要单独处理。 - if(v.length == 4 && v[3].type == "sparklines"){ - delete curv.m; - delete curv.v; - - let curCalv = v[3].data; - - if(getObjType(curCalv) == "array" && getObjType(curCalv[0]) != "object"){ - curv.v = curCalv[0]; - } - else{ - curv.spl = v[3].data; - } - } - } - // from API setCellValue,luckysheet.setCellValue(0, 0, {f: "=sum(D1)", bg:"#0188fb"}),value is an object, so get attribute f as value - else if(getObjType(value) == "object"){ - let valueFunction = value.f; - - if(getObjType(valueFunction) == "string" && valueFunction.slice(0, 1) == "=" && valueFunction.length > 1){ - let v = _this.execfunction(valueFunction, r, c, true); - - // get v/m/ct + if(!isCurInline){ + if(getObjType(value) == "string" && value.slice(0, 1) == "=" && value.length > 1){ + let v = _this.execfunction(value, r, c, true); + curv = _this.execFunctionGroupData[r][c]; - - // get f curv.f = v[2]; - - // get other cell style attribute - delete value.v; - delete value.m; - delete value.f; - Object.assign(curv,value); - + //打进单元格的sparklines的配置串, 报错需要单独处理。 if(v.length == 4 && v[3].type == "sparklines"){ delete curv.m; delete curv.v; - + let curCalv = v[3].data; - + if(getObjType(curCalv) == "array" && getObjType(curCalv[0]) != "object"){ curv.v = curCalv[0]; } @@ -1297,22 +1293,57 @@ const luckysheetformula = { } } } - - } - else{ - _this.delFunctionGroup(r, c); - _this.execFunctionGroup(r, c, value); - isRunExecFunction = false; - curv = _this.execFunctionGroupData[r][c]; - - delete curv.f; - delete curv.spl; - - if(curv.qp == 1 && ('' + value).substr(0,1)!="'"){//if quotePrefix is 1, cell is force string, cell clear quotePrefix when it is updated - curv.qp = 0; - if(curv.ct!=null){ - curv.ct.fa = "General"; - curv.ct.t = "n"; + // from API setCellValue,luckysheet.setCellValue(0, 0, {f: "=sum(D1)", bg:"#0188fb"}),value is an object, so get attribute f as value + else if(getObjType(value) == "object"){ + let valueFunction = value.f; + + if(getObjType(valueFunction) == "string" && valueFunction.slice(0, 1) == "=" && valueFunction.length > 1){ + let v = _this.execfunction(valueFunction, r, c, true); + + // get v/m/ct + curv = _this.execFunctionGroupData[r][c]; + + // get f + curv.f = v[2]; + + // get other cell style attribute + delete value.v; + delete value.m; + delete value.f; + Object.assign(curv,value); + + //打进单元格的sparklines的配置串, 报错需要单独处理。 + if(v.length == 4 && v[3].type == "sparklines"){ + delete curv.m; + delete curv.v; + + let curCalv = v[3].data; + + if(getObjType(curCalv) == "array" && getObjType(curCalv[0]) != "object"){ + curv.v = curCalv[0]; + } + else{ + curv.spl = v[3].data; + } + } + } + + } + else { + _this.delFunctionGroup(r, c); + _this.execFunctionGroup(r, c, value); + isRunExecFunction = false; + curv = _this.execFunctionGroupData[r][c]; + + delete curv.f; + delete curv.spl; + + if(curv.qp == 1 && ('' + value).substr(0,1)!="'"){//if quotePrefix is 1, cell is force string, cell clear quotePrefix when it is updated + curv.qp = 0; + if(curv.ct!=null){ + curv.ct.fa = "General"; + curv.ct.t = "n"; + } } } } @@ -1387,42 +1418,62 @@ const luckysheetformula = { cfg["rowlen"] = {}; } - if(d[r][c].tb == "2" && d[r][c].v != null){//自动换行 - let defaultrowlen = 19; + if((d[r][c].tb == "2" && d[r][c].v != null) || isInlineStringCell(d[r][c])){//自动换行 + let defaultrowlen = Store.defaultrowlen;; - let offlinecanvas = $("#luckysheetTableContentF").get(0).getContext("2d"); - offlinecanvas.textBaseline = 'top'; //textBaseline以top计算 + let canvas = $("#luckysheetTableContent").get(0).getContext("2d"); + // offlinecanvas.textBaseline = 'top'; //textBaseline以top计算 - let fontset = luckysheetfontformat(d[r][c]); - offlinecanvas.font = fontset; + // let fontset = luckysheetfontformat(d[r][c]); + // offlinecanvas.font = fontset; - let currentRowLen = defaultrowlen; - if(cfg["rowlen"][r] != null){ - currentRowLen = cfg["rowlen"][r]; + if(cfg["customHeight"] && cfg["customHeight"][r]==1){ + } - - let strValue = getcellvalue(r, c, d).toString(); - let measureText = offlinecanvas.measureText(strValue); - - let textMetrics = measureText.width; - let cellWidth = colLocationByIndex(c)[1] - colLocationByIndex(c)[0] - 4; - let oneLineTextHeight = measureText.actualBoundingBoxDescent - measureText.actualBoundingBoxAscent; - - if(textMetrics > cellWidth){ - let strArr = [];//文本截断数组 - strArr = getCellTextSplitArr(strValue, strArr, cellWidth, offlinecanvas); - - let computeRowlen = oneLineTextHeight * strArr.length + 4; - //比较计算高度和当前高度取最大高度 - if(computeRowlen > currentRowLen){ - currentRowLen = computeRowlen; + else{ + // let currentRowLen = defaultrowlen; + // if(cfg["rowlen"][r] != null){ + // currentRowLen = cfg["rowlen"][r]; + // } + + let cellWidth = colLocationByIndex(c)[1] - colLocationByIndex(c)[0] - 2; + + let textInfo = getCellTextInfo(d[r][c], canvas,{ + r:r, + c:c, + cellWidth:cellWidth + }); + + let currentRowLen = defaultrowlen; + // console.log("rowlen", textInfo); + if(textInfo!=null){ + currentRowLen = textInfo.textHeightAll+2; + } + + // let strValue = getcellvalue(r, c, d).toString(); + // let measureText = offlinecanvas.measureText(strValue); + + // let textMetrics = measureText.width; + // let cellWidth = colLocationByIndex(c)[1] - colLocationByIndex(c)[0] - 4; + // let oneLineTextHeight = measureText.actualBoundingBoxDescent - measureText.actualBoundingBoxAscent; + + // if(textMetrics > cellWidth){ + // let strArr = [];//文本截断数组 + // strArr = getCellTextSplitArr(strValue, strArr, cellWidth, offlinecanvas); + + // let computeRowlen = oneLineTextHeight * strArr.length + 4; + // //比较计算高度和当前高度取最大高度 + // if(computeRowlen > currentRowLen){ + // currentRowLen = computeRowlen; + // } + // } + + if(currentRowLen > defaultrowlen){ + cfg["rowlen"][r] = currentRowLen; + RowlChange = true; } } - if(currentRowLen != defaultrowlen){ - cfg["rowlen"][r] = currentRowLen; - RowlChange = true; - } } if(RowlChange){ diff --git a/src/global/getRowlen.js b/src/global/getRowlen.js index 8abc01084..15f494b34 100644 --- a/src/global/getRowlen.js +++ b/src/global/getRowlen.js @@ -43,7 +43,7 @@ function rowlenByRange(d, r1, r2, cfg) { continue; } - if(cell != null && cell.v != null){ + if(cell != null && (cell.v != null || isInlineStringCell(cell)) ){ let cellWidth = colLocationByIndex(c)[1] - colLocationByIndex(c)[0] - 2; let textInfo = getCellTextInfo(cell, canvas,{ @@ -55,7 +55,7 @@ function rowlenByRange(d, r1, r2, cfg) { let computeRowlen = 0; // console.log("rowlen", textInfo); if(textInfo!=null){ - computeRowlen = textInfo.textHeightAll; + computeRowlen = textInfo.textHeightAll+2; } // let fontset = luckysheetfontformat(cell); @@ -364,7 +364,7 @@ function getCellTextInfo(cell , ctx, option){ } - if(x!=splitArr.length-1 && preNewValue!=""){ + if(x!=splitArr.length-1 && !(preNewValue=="" && newValue=="") ){ inlineStringArr.push({ fontset:scfontset, fc:fc==null?"#000":fc, diff --git a/src/global/getdata.js b/src/global/getdata.js index 595247c05..2ab641eae 100644 --- a/src/global/getdata.js +++ b/src/global/getdata.js @@ -5,7 +5,7 @@ import formula from './formula'; import editor from './editor'; import { dynamicArrayCompute } from './dynamicArray'; import sheetmanage from '../controllers/sheetmanage'; -import { isInlineStringCT,isInlineStringCell } from '../controllers/inlineString'; +import { isInlineStringCT,isInlineStringCell,convertCssToStyleList } from '../controllers/inlineString'; import locale from '../locale/locale'; import Store from '../store'; @@ -317,7 +317,7 @@ export function getInlineStringStyle(r, c, data){ return ""; } -export function getFontStyleByCell(cell,checksAF,checksCF){ +export function getFontStyleByCell(cell,checksAF,checksCF, isCheck=true){ if(cell==null){ return; } @@ -325,7 +325,10 @@ export function getFontStyleByCell(cell,checksAF,checksCF){ const _locale = locale(); const locale_fontarray = _locale.fontarray; for(let key in cell){ - let value = checkstatusByCell(cell, key); + let value = cell[key]; + if(isCheck){ + value = checkstatusByCell(cell, key); + } if(key == "bl" && value != "0"){ style += "font-weight: bold;"; } @@ -382,13 +385,26 @@ export function checkstatusByCell(cell, a){ foucsStatus = "0"; } else{ - if(isInlineStringCell(cell)){ - foucsStatus = cell.ct.s[0][a]; - } - else{ - foucsStatus = foucsStatus[a]; + var w = window.getSelection(), isInlineEdit=false; + if(w.type!="None"){ + var range = w.getRangeAt(0); + let startContainer = range.startContainer; + if (parseInt($("#luckysheet-input-box").css("top")) > 0 && startContainer.parentNode.tagName=="SPAN" && !range.collapsed) { + let span = startContainer.parentNode; + let styleList = convertCssToStyleList(span.style.cssText); + foucsStatus = styleList[a]; + isInlineEdit = true; + } } + if(!isInlineEdit){ + if(isInlineStringCell(cell)){ + foucsStatus = cell.ct.s[0][a]; + } + else{ + foucsStatus = foucsStatus[a]; + } + } if(foucsStatus == null){ foucsStatus = "0"; diff --git a/src/store/index.js b/src/store/index.js index 5c69ca30d..2c779a1c3 100644 --- a/src/store/index.js +++ b/src/store/index.js @@ -127,6 +127,7 @@ const Store = { showGridLines:true, inlineStringEditCache:null, + inlineStringEditRange:null, }