@@ -29,7 +29,7 @@ import {
29
29
RemoveColumnsRowsCommand ,
30
30
isMatrix ,
31
31
} from "../../../types" ;
32
- import { AbstractComposerStore } from "./abstract_composer_store" ;
32
+ import { AbstractComposerStore , ComposerSelection } from "./abstract_composer_store" ;
33
33
34
34
const CELL_DELETED_MESSAGE = _t ( "The cell you are trying to edit has been deleted." ) ;
35
35
@@ -201,39 +201,66 @@ export class CellComposerStore extends AbstractComposerStore {
201
201
this . setContent ( "" ) ;
202
202
}
203
203
204
- protected getComposerContent ( position : CellPosition ) : string {
204
+ protected getComposerContent (
205
+ position : CellPosition ,
206
+ selection ?: ComposerSelection
207
+ ) : { content : string ; adjustedSelection ?: ComposerSelection } {
205
208
const locale = this . getters . getLocale ( ) ;
206
209
const cell = this . getters . getCell ( position ) ;
207
210
if ( cell ?. isFormula ) {
208
211
const prettifiedContent = this . getPrettifiedFormula ( cell ) ;
209
- return localizeFormula ( prettifiedContent , locale ) ;
212
+ // when a formula is prettified (multi lines, indented), adapt the cursor position
213
+ // to take into account line breaks and tabs
214
+ function adjustCursorIndex ( targetIndex : number ) : number {
215
+ let adjustedIndex = 0 ;
216
+ let originalIndex = 0 ;
217
+ while ( originalIndex < targetIndex ) {
218
+ adjustedIndex ++ ;
219
+ const char = prettifiedContent [ adjustedIndex ] ;
220
+ if ( char !== "\n" && char !== "\t" ) {
221
+ originalIndex ++ ;
222
+ }
223
+ }
224
+ return adjustedIndex ;
225
+ }
226
+ let adjustedSelection = selection ;
227
+ if ( selection ) {
228
+ adjustedSelection = {
229
+ start : adjustCursorIndex ( selection . start ) ,
230
+ end : adjustCursorIndex ( selection . end ) ,
231
+ } ;
232
+ }
233
+ return {
234
+ content : localizeFormula ( prettifiedContent , locale ) ,
235
+ adjustedSelection,
236
+ } ;
210
237
}
211
238
const spreader = this . model . getters . getArrayFormulaSpreadingOn ( position ) ;
212
239
if ( spreader ) {
213
- return "" ;
240
+ return { content : "" } ;
214
241
}
215
242
const { format, value, type, formattedValue } = this . getters . getEvaluatedCell ( position ) ;
216
243
switch ( type ) {
217
244
case CellValueType . empty :
218
- return "" ;
245
+ return { content : "" } ;
219
246
case CellValueType . text :
220
247
case CellValueType . error :
221
- return value ;
248
+ return { content : value } ;
222
249
case CellValueType . boolean :
223
- return formattedValue ;
250
+ return { content : formattedValue } ;
224
251
case CellValueType . number :
225
252
if ( format && isDateTimeFormat ( format ) ) {
226
253
if ( parseDateTime ( formattedValue , locale ) !== null ) {
227
254
// formatted string can be parsed again
228
- return formattedValue ;
255
+ return { content : formattedValue } ;
229
256
}
230
257
// display a simplified and parsable string otherwise
231
258
const timeFormat = Number . isInteger ( value )
232
259
? locale . dateFormat
233
260
: getDateTimeFormat ( locale ) ;
234
- return formatValue ( value , { locale, format : timeFormat } ) ;
261
+ return { content : formatValue ( value , { locale, format : timeFormat } ) } ;
235
262
}
236
- return this . numberComposerContent ( value , format , locale ) ;
263
+ return { content : this . numberComposerContent ( value , format , locale ) } ;
237
264
}
238
265
}
239
266
0 commit comments