Skip to content

Commit b96b05b

Browse files
committed
fix: keep cursor position ast the start/end of the blocks in movement commands
1 parent e76ddb3 commit b96b05b

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

src/commands/index.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -208,9 +208,12 @@ export async function previousSiblingBlockCommand() {
208208
return
209209

210210
const cursorPosition = (await logseq.Editor.getEditingCursorPosition())?.pos
211+
const content = await logseq.Editor.getEditingBlockContent()
212+
const isPositionOnTheEnd = cursorPosition === undefined || cursorPosition === content.length
213+
211214
await logseq.Editor.editBlock(
212215
prevBlock.uuid,
213-
cursorPosition ? {pos: cursorPosition} : undefined
216+
!isPositionOnTheEnd ? {pos: cursorPosition} : undefined
214217
)
215218
}
216219

@@ -225,9 +228,12 @@ export async function nextSiblingBlockCommand() {
225228
return
226229

227230
const cursorPosition = (await logseq.Editor.getEditingCursorPosition())?.pos
231+
const content = await logseq.Editor.getEditingBlockContent()
232+
const isPositionOnTheEnd = cursorPosition === undefined || cursorPosition === content.length
233+
228234
await logseq.Editor.editBlock(
229235
nextBlock.uuid,
230-
cursorPosition ? {pos: cursorPosition} : undefined
236+
!isPositionOnTheEnd ? {pos: cursorPosition} : undefined
231237
)
232238
}
233239

@@ -264,9 +270,12 @@ export async function editPreviousBlockCommand() {
264270
}
265271

266272
const cursorPosition = (await logseq.Editor.getEditingCursorPosition())?.pos
273+
const content = await logseq.Editor.getEditingBlockContent()
274+
const isPositionOnTheEnd = cursorPosition === undefined || cursorPosition === content.length
275+
267276
await logseq.Editor.editBlock(
268277
(prevBlock as BlockEntity).uuid,
269-
cursorPosition ? {pos: cursorPosition} : undefined
278+
!isPositionOnTheEnd ? {pos: cursorPosition} : undefined
270279
)
271280
}
272281

@@ -311,9 +320,12 @@ export async function editNextBlockCommand() {
311320
}
312321

313322
const cursorPosition = (await logseq.Editor.getEditingCursorPosition())?.pos
323+
const content = await logseq.Editor.getEditingBlockContent()
324+
const isPositionOnTheEnd = cursorPosition === undefined || cursorPosition === content.length
325+
314326
await logseq.Editor.editBlock(
315327
(nextBlock as BlockEntity).uuid,
316-
cursorPosition ? {pos: cursorPosition} : undefined
328+
!isPositionOnTheEnd ? {pos: cursorPosition} : undefined
317329
)
318330
}
319331

@@ -892,7 +904,6 @@ export async function updateBlocksCommand(
892904
if (blocks.length === 0)
893905
return
894906

895-
let position = -1
896907
if (!isSelectedState)
897908
blocks[0]._selectPosition = getEditingCursorSelection()!
898909

0 commit comments

Comments
 (0)