From d4581a2513d00c200009a9fdaffd71f2441daa77 Mon Sep 17 00:00:00 2001 From: Rashesh Date: Mon, 26 Aug 2024 20:17:44 +0530 Subject: [PATCH] calc: fix: named-range can't be used in function like SUM Signed-off-by: Rashesh Change-Id: Ia07c579b3bc81ce73f40e86e6e07a02098e7bf4c wip Signed-off-by: Rashesh Change-Id: I2c2ecc74ed836f3b6d11c20990c8c8b29950abfe --- .../Control.FormulaAutoCompletePopup.ts | 77 +++++++++++++++++-- browser/src/layer/tile/CanvasTileLayer.js | 20 ++++- 2 files changed, 89 insertions(+), 8 deletions(-) diff --git a/browser/src/control/Control.FormulaAutoCompletePopup.ts b/browser/src/control/Control.FormulaAutoCompletePopup.ts index fe1263f45d913..6cf88eaf481a9 100644 --- a/browser/src/control/Control.FormulaAutoCompletePopup.ts +++ b/browser/src/control/Control.FormulaAutoCompletePopup.ts @@ -60,15 +60,82 @@ class FormulaAutoCompletePopup extends L.Control.AutoCompletePopup { return entries; } + getAutocompleteText( + currentCellFormula: string, + functionName: string, + endIndex: number, + ): string { + const openBracketIndex: number[] = []; + const semicolonIndex: number[] = []; + const equalIndex: number = 0; + + for (let i = 0; i < currentCellFormula.length; i++) { + if (currentCellFormula.charAt(i) === '(') openBracketIndex.push(i); + else if (currentCellFormula.charAt(i) === ';') semicolonIndex.push(i); + } + + let minDiff: number = Number.MAX_VALUE; + let startIndex: number; + + const tmp = endIndex - equalIndex; + if (tmp >= 0 && tmp < minDiff) { + minDiff = tmp; + startIndex = equalIndex + 1; + } + + openBracketIndex.forEach((index) => { + const tmp = endIndex - index; + if (tmp >= 0 && tmp < minDiff) { + minDiff = tmp; + startIndex = index + 1; + } + }); + + semicolonIndex.forEach((index) => { + const tmp = endIndex - index; + if (tmp >= 0 && tmp < minDiff) { + minDiff = tmp; + startIndex = index + 1; + } + }); + + const partialText: string = currentCellFormula + .substring(startIndex, endIndex + 1) + .trim(); + + let autoCompleteFunctionName: string = ''; + for ( + let i = 0; + i < Math.max(partialText.length, functionName.length); + i++ + ) { + if ( + partialText.charAt(i).toLowerCase() != + functionName.charAt(i).toLowerCase() + ) { + autoCompleteFunctionName = functionName.substring(i); + break; + } + } + + return autoCompleteFunctionName; + } + callback(objectType: any, eventType: any, object: any, index: number) { if (eventType === 'close') { this.closePopup(); } else if (eventType === 'select' || eventType === 'activate') { - var currentText = this.map._docLayer._lastFormula; - var chIndex = currentText.length - 1; - var functionName = this.functionList[index].name; - var namedRange = this.functionList[index].namedRange; - functionName = functionName.substring(chIndex); + const namedRange: string = this.functionList[index].namedRange; + const currentText: string = this.map._docLayer._lastFormula; + const addedCharacterIndex: number = + this.map._docLayer._newFormulaDiffIndex; + + const functionName: string = this.getAutocompleteText( + currentText, + this.functionList[index].name, + addedCharacterIndex, + ); + if (namedRange) this.map._textInput._sendText(functionName); else this.map._textInput._sendText(functionName + '('); this.closePopup(); diff --git a/browser/src/layer/tile/CanvasTileLayer.js b/browser/src/layer/tile/CanvasTileLayer.js index f0ef3dc8acb21..5c6c58c5d667b 100644 --- a/browser/src/layer/tile/CanvasTileLayer.js +++ b/browser/src/layer/tile/CanvasTileLayer.js @@ -1794,9 +1794,23 @@ L.CanvasTileLayer = L.Layer.extend({ // This is done because coolwsd will send several 'cellformula' // messages during text composition, and resetting the contents // of the clipboard container mid-composition will easily break it. - var formula = textMsg.substring(13); - this._lastFormula = formula; - this._map.fire('cellformula', {formula: formula}); + + let newFormula = textMsg.substring(13); + if (this._lastFormula) { + let minLength = Math.min(newFormula.length, this._lastFormula.length); + let index = -1; + for (let i = 0; i < minLength; i++) { + if (newFormula.charAt(i) !== this._lastFormula.charAt(i)) { + index = i; + break; + } + } + if (index === -1) + index = minLength; + this._newFormulaDiffIndex = index; + } + this._lastFormula = newFormula; + this._map.fire('cellformula', {formula: newFormula}); }, _onCalcFunctionUsageMsg: function (textMsg) {