refactor(editor): document updateSettingsSortSafe's exceptions and dedupe sort-config normalization
Build / Build-and-ng-test (pull_request) Failing after 2m7s
Build / Build-and-test-development (pull_request) Skipped
Lighthouse Checks / lighthouse (pull_request) Successful in 21m46s

This commit is contained in:
YuryShkoda committed 2026-09-03 08:57:35 +03:00
1 parent d3b881fa85
commit cc68c218d8
4 files changed
+41 -8

No files matched your search

+9 -2
View File
@@ -86,6 +86,7 @@ import { parseTableColumns } from './utils/grid.utils'
import { classifyRow } from './utils/classifyRow'
import { withoutEditStatus } from './utils/withoutEditStatus'
import { getEditStatusSymbol } from './utils/getEditStatusSymbol'
import { normalizeSortConfig } from './utils/normalizeSortConfig'
import { EDIT_STATUS_COLUMN_NAME } from '../shared/dc-validator/utils/editStatusColumnRule'
import {
errorRenderer,
@@ -1204,8 +1205,7 @@ export class EditorComponent implements OnInit, AfterViewInit, OnDestroy {
if (!hot) return []
try {
const plugin = hot.getPlugin('multiColumnSorting')
const cfg = plugin?.getSortConfig()
return Array.isArray(cfg) ? cfg : cfg ? [cfg] : []
return normalizeSortConfig(plugin?.getSortConfig())
} catch {
return []
}
@@ -1221,6 +1221,13 @@ export class EditorComponent implements OnInit, AfterViewInit, OnDestroy {
* sort first and restoring it immediately afterward avoids the
* corruption entirely. Not needed for the very first updateSettings()
* call at initial table load, since nothing can be sorted yet then.
*
* editTable()/cancelEdit() deliberately do NOT call this wrapper - they
* manage their own clear/restore inline, because their restore point is
* later in the method (after other logic like syncOverwrittenComments()),
* not immediately after their own updateSettings() call the way this
* wrapper restores it. Don't "simplify" those two to use this instead
* without preserving that later restore point.
*/
private updateSettingsSortSafe(
settings: Handsontable.GridSettings,
@@ -0,0 +1,20 @@
import { normalizeSortConfig } from './normalizeSortConfig'
describe('normalizeSortConfig', () => {
it('wraps a single config in an array', () => {
const cfg = { column: 0, sortOrder: 'asc' as const }
expect(normalizeSortConfig(cfg)).toEqual([cfg])
})
it('passes an array of configs through unchanged', () => {
const cfgs = [
{ column: 0, sortOrder: 'asc' as const },
{ column: 1, sortOrder: 'desc' as const }
]
expect(normalizeSortConfig(cfgs)).toEqual(cfgs)
})
it('returns an empty array for undefined (nothing currently sorted)', () => {
expect(normalizeSortConfig(undefined)).toEqual([])
})
})
@@ -0,0 +1,10 @@
/**
* multiColumnSorting's getSortConfig() returns a single config, an array of
* them, or undefined depending on how many columns are currently sorted -
* normalizes all three shapes into one array so callers never need three
* branches to handle it. `SortConfig` isn't part of Handsontable's public
* API surface in this version (only re-exported internally), hence `any`
* rather than importing it from a deep, non-exported path.
*/
export const normalizeSortConfig = (cfg: any): any[] =>
Array.isArray(cfg) ? cfg : cfg ? [cfg] : []
@@ -1,6 +1,7 @@
import Handsontable from 'handsontable'
import { HyperFormula } from 'hyperformula'
import { classifyRow } from './classifyRow'
import { normalizeSortConfig } from './normalizeSortConfig'
import { EDIT_STATUS_COLUMN_NAME } from '../../shared/dc-validator/utils/editStatusColumnRule'
import { parseFormulaRule } from '../../shared/dc-validator/utils/parseFormulaRule'
@@ -352,12 +353,7 @@ describe('updateSettings must not run while a sort is active - it corrupts formu
it('clearing the sort before updateSettings and restoring it after avoids the corruption', () => {
const hot = buildSortedGrid()
const sortPlugin: any = hot.getPlugin('multiColumnSorting')
const sortConfig = sortPlugin.getSortConfig()
const sortConfigs = Array.isArray(sortConfig)
? sortConfig
: sortConfig
? [sortConfig]
: []
const sortConfigs = normalizeSortConfig(sortPlugin.getSortConfig())
sortPlugin.clearSort()
hot.updateSettings({}, false)