Skip to content

fix: keep caret in place when editing empty table cells - #5

Open
lastday97 wants to merge 1 commit into
VictorVow:mainfrom
lastday97:fix/caret-jump-empty-cells
Open

lastday97 wants to merge 1 commit into
VictorVow:mainfrom
lastday97:fix/caret-jump-empty-cells

Conversation

@lastday97

Copy link
Copy Markdown

Why

While editing an inline-rendered table cell, the caret snaps back to the
start of the cell whenever the debounced updateBlock re-renders the block
renderer — worst when the cell is empty. (Closes #3)

Root cause

Cells were rendered with a React children prop. Every updateBlock echo
re-run reconciled the tree against freshly-written content; React's direct
text-child fast path (setTextContent) rewrote the live contenteditable
text (empty cells rebuild the text child via node.textContent = ''),
resetting the caret. The old "identical text is a no-op" assumption broke
for empty cells.

Fix

Render <th>/<td> WITHOUT a children prop so React never owns cell text
(shouldSetTextContent stays false — no more setTextContent during
re-render). Initial text is applied once via a cell ref (syncCellText),
guarded by a textContent comparison so the normal echo re-render is a
no-op and the caret is left untouched. Structural ops still overwrite
genuinely different text; resumePinnedToolbar re-anchors the caret.

Verification

  • New unit tests for syncCellText (no-op on equal text, write/clear/overwrite,
    null-safety) — npm test 10/10 pass.
  • npm run build succeeds.
  • Manually verified in Logseq: caret stays put while typing mid-cell and
    while clearing a cell.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cursor jumps to beginning of cell

1 participant