overleaf-cep/services/web/frontend/js/utils/operations.ts
Alf Eaton a7e00e19e2 Only dispatch doc:changed event for insert or delete operations (#23779)
GitOrigin-RevId: b3425d4edffb045b2a15e5fc5cb69210d9e97203
2025-02-24 09:05:02 +00:00

43 lines
1.1 KiB
TypeScript

import {
AnyOperation,
Change,
CommentOperation,
DeleteOperation,
EditOperation,
InsertOperation,
Operation,
} from '../../../types/change'
export const isInsertOperation = (op: Operation): op is InsertOperation =>
'i' in op
export const isCommentOperation = (op: Operation): op is CommentOperation =>
'c' in op
export const isDeleteOperation = (op: Operation): op is DeleteOperation =>
'd' in op
export const isEditOperation = (op: Operation): op is EditOperation =>
isInsertOperation(op) || isDeleteOperation(op)
export const isInsertChange = (
change: Change<EditOperation>
): change is Change<InsertOperation> => isInsertOperation(change.op)
export const isCommentChange = (
change: Change<CommentOperation>
): change is Change<CommentOperation> => isCommentOperation(change.op)
export const isDeleteChange = (
change: Change<EditOperation>
): change is Change<DeleteOperation> => isDeleteOperation(change.op)
export const visibleTextLength = (op: AnyOperation) => {
if (isCommentOperation(op)) {
return op.c.length
}
if (isInsertOperation(op)) {
return op.i.length
}
return 0
}