fix: keep editor selection stable around media blocks
This commit is contained in:
45
src/components/blockNoteCursorTarget.ts
Normal file
45
src/components/blockNoteCursorTarget.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
interface CursorTargetBlockLike {
|
||||
id: string
|
||||
content?: unknown
|
||||
}
|
||||
|
||||
function blockSupportsTextCursor(block: CursorTargetBlockLike | undefined): block is CursorTargetBlockLike {
|
||||
return Array.isArray(block?.content)
|
||||
}
|
||||
|
||||
export function findNearestTextCursorBlock(
|
||||
blocks: CursorTargetBlockLike[],
|
||||
targetIndex: number,
|
||||
): CursorTargetBlockLike | null {
|
||||
if (blocks.length === 0) return null
|
||||
|
||||
const clampedTargetIndex = Math.min(Math.max(targetIndex, 0), blocks.length - 1)
|
||||
const targetBlock = blocks[clampedTargetIndex]
|
||||
if (blockSupportsTextCursor(targetBlock)) {
|
||||
return targetBlock
|
||||
}
|
||||
|
||||
for (let distance = 1; distance < blocks.length; distance += 1) {
|
||||
const forwardBlock = blocks[clampedTargetIndex + distance]
|
||||
if (blockSupportsTextCursor(forwardBlock)) {
|
||||
return forwardBlock
|
||||
}
|
||||
|
||||
const backwardBlock = blocks[clampedTargetIndex - distance]
|
||||
if (blockSupportsTextCursor(backwardBlock)) {
|
||||
return backwardBlock
|
||||
}
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
export function findNearestTextCursorBlockById(
|
||||
blocks: CursorTargetBlockLike[],
|
||||
targetBlockId: string,
|
||||
): CursorTargetBlockLike | null {
|
||||
const targetIndex = blocks.findIndex((block) => block.id === targetBlockId)
|
||||
if (targetIndex === -1) return null
|
||||
|
||||
return findNearestTextCursorBlock(blocks, targetIndex)
|
||||
}
|
||||
Reference in New Issue
Block a user