overleaf/services/web/frontend/js/features/review-panel-new/utils/changes-in-selection.ts
Domagoj Kriskovic 8a90ffa3fb Add confirm modal on accept/reject selected changes (#21540)
* Add showGenericConfirmModal in ModalsContext

* Add confirm modal on accept/reject selected changes

* plural in translations

* change tooltip to include selected changes

* add _plural to all translated languages

* lowercase title/tooltip

* count replacements as single change

* use new translation key

GitOrigin-RevId: afadbe1eeb2a290688b96f2b5388485f40c958d0
2024-11-12 09:06:01 +00:00

44 lines
1.1 KiB
TypeScript

import { SelectionRange } from '@codemirror/state'
import { Ranges } from '@/features/review-panel-new/context/ranges-context'
import { isDeleteChange, isInsertChange } from '@/utils/operations'
import { canAggregate } from './can-aggregate'
import { Change, EditOperation } from '../../../../../types/change'
export function numberOfChangesInSelection(
ranges: Ranges | undefined,
selection: SelectionRange
) {
if (!ranges) {
return 0
}
let count = 0
let precedingChange: Change<EditOperation> | null = null
for (const change of ranges.changes) {
if (
precedingChange &&
isInsertChange(precedingChange) &&
isDeleteChange(change) &&
canAggregate(change, precedingChange)
) {
// only count once for the aggregated change
continue
} else if (
isInsertChange(change) &&
change.op.p >= selection.from &&
change.op.p + change.op.i.length <= selection.to
) {
count++
} else if (
isDeleteChange(change) &&
selection.from <= change.op.p &&
change.op.p <= selection.to
) {
count++
}
precedingChange = change
}
return count
}