fix: recover stale editor block references
This commit is contained in:
@@ -38,6 +38,7 @@ interface RepairableBlockNoteEditor {
|
||||
type RecoveryReason =
|
||||
| 'invalid_insertion_depth'
|
||||
| 'mismatched_transaction'
|
||||
| 'stale_block_reference'
|
||||
| 'stale_transaction'
|
||||
| 'table_position_out_of_range'
|
||||
| 'transform_error'
|
||||
@@ -80,6 +81,10 @@ function isTablePositionOutOfRangeError(error: unknown): boolean {
|
||||
return error instanceof RangeError && /^Index \d+ out of range for <tableRow\(/.test(error.message)
|
||||
}
|
||||
|
||||
export function isStaleBlockReferenceError(error: unknown): boolean {
|
||||
return error instanceof Error && /^Block with ID .+ not found$/.test(error.message)
|
||||
}
|
||||
|
||||
function isTransformError(error: unknown): boolean {
|
||||
return error instanceof Error && error.name === 'TransformError'
|
||||
}
|
||||
@@ -90,8 +95,15 @@ function isRecoverableRangeError(error: unknown): boolean {
|
||||
|| isTablePositionOutOfRangeError(error)
|
||||
}
|
||||
|
||||
const RECOVERABLE_EDITOR_ERROR_PREDICATES = [
|
||||
isTransformError,
|
||||
isMismatchedTransactionError,
|
||||
isRecoverableRangeError,
|
||||
isStaleBlockReferenceError,
|
||||
]
|
||||
|
||||
export function isRecoverableEditorTransformError(error: unknown): boolean {
|
||||
return isTransformError(error) || isMismatchedTransactionError(error) || isRecoverableRangeError(error)
|
||||
return RECOVERABLE_EDITOR_ERROR_PREDICATES.some((predicate) => predicate(error))
|
||||
}
|
||||
|
||||
function recoveryReason(
|
||||
@@ -101,6 +113,7 @@ function recoveryReason(
|
||||
): RecoveryReason {
|
||||
if (transactionDocIsStale(transaction, view)) return 'stale_transaction'
|
||||
if (isMismatchedTransactionError(error)) return 'mismatched_transaction'
|
||||
if (isStaleBlockReferenceError(error)) return 'stale_block_reference'
|
||||
if (isInvalidInsertionDepthError(error)) return 'invalid_insertion_depth'
|
||||
if (isTablePositionOutOfRangeError(error)) return 'table_position_out_of_range'
|
||||
return 'transform_error'
|
||||
|
||||
Reference in New Issue
Block a user