From 2cf4c0b583da1def2f73740d708ff6e51c5bbc78 Mon Sep 17 00:00:00 2001 From: Jonas Date: Mon, 16 Nov 2026 16:36:18 +0100 Subject: [PATCH] fix(gapcursor): don't offer gap cursor after comments and footnotes The Document node sets `allowGapCursor: true` so that the gap cursor works between blocks, because its content match lists `footnotes` as the default type after the first block. The override also allowed a gap cursor at the top level after `comments` and `footnotes`, for example below the last footnote, where no paragraph can be inserted. Move such a gap cursor to the nearest text position. Prefer the direction the cursor was moving in, so arrow keys still move between comments and footnotes. Signed-off-by: Jonas Assisted-by: ClaudeCode:claude-opus-5-5 --- src/extensions/RichText.ts | 27 +++++++++++ src/tests/extensions/Gapcursor.spec.ts | 63 ++++++++++++++++++++++++++ 2 files changed, 90 insertions(+) diff --git a/src/extensions/RichText.ts b/src/extensions/RichText.ts index 58a64898dbb..b3135b055d4 100644 --- a/src/extensions/RichText.ts +++ b/src/extensions/RichText.ts @@ -13,6 +13,8 @@ import { ListItem } from '@tiptap/extension-list' import Placeholder from '@tiptap/extension-placeholder' import Text from '@tiptap/extension-text' import { CharacterCount, Dropcursor, Gapcursor } from '@tiptap/extensions' +import { GapCursor } from '@tiptap/pm/gapcursor' +import { Plugin, PluginKey, Selection } from '@tiptap/pm/state' import { common, createLowlight } from 'lowlight' import EmojiSuggestion from '../components/Suggestion/Emoji/suggestions.js' import MentionSuggestion from '../components/Suggestion/Mention/suggestions.js' @@ -95,6 +97,31 @@ export default Extension.create({ // type after the first block, so without this override the gap // cursor's textblock check rejects every position but the start. allowGapCursor: true, + + addProseMirrorPlugins() { + return [ + new Plugin({ + key: new PluginKey('gapCursorBeforeAnnotations'), + appendTransaction(_transactions, oldState, newState) { + const { selection, doc, schema } = newState + if (!(selection instanceof GapCursor) || selection.$head.depth > 0) { + return null + } + // A gap cursor marks where a paragraph can be inserted. With the + // override above it is also offered after `comments` and + // `footnotes`, where no paragraph is allowed. + const index = selection.$head.index() + if (doc.canReplaceWith(index, index, schema.nodes.paragraph)) { + return null + } + // Move into the neighbouring comments or footnotes, preferring + // the direction the cursor was moving in. + const dir = selection.head < oldState.selection.head ? -1 : 1 + return newState.tr.setSelection(Selection.near(selection.$head, dir)) + }, + }), + ] + }, }), Text, Paragraph, diff --git a/src/tests/extensions/Gapcursor.spec.ts b/src/tests/extensions/Gapcursor.spec.ts index f55d96b6bdf..6f78f8a0689 100644 --- a/src/tests/extensions/Gapcursor.spec.ts +++ b/src/tests/extensions/Gapcursor.spec.ts @@ -3,7 +3,10 @@ * SPDX-License-Identifier: AGPL-3.0-or-later */ +import type { Editor } from '@tiptap/core' + import { GapCursor } from '@tiptap/pm/gapcursor' +import { TextSelection } from '@tiptap/pm/state' import { describe, expect } from 'vitest' import RichText from '../../extensions/RichText.ts' import markdownit from '../../markdownit/index.ts' @@ -15,6 +18,11 @@ const test = testEditor.extend({ allExtensions: [RichText.configure({ editing: true })], }) +function pressKey(editor: Editor, key: string) { + const event = new KeyboardEvent('keydown', { key, bubbles: true, cancelable: true }) + editor.view.dom.dispatchEvent(event) +} + describe('gap cursor between block nodes', () => { test('is offered between two consecutive tables', ({ editor }) => { editor.commands.setContent(markdownit.render('| a |\n|---|\n| 1 |\n\n| b |\n|---|\n| 2 |\n')) @@ -30,4 +38,59 @@ describe('gap cursor between block nodes', () => { expect(editor.state.selection).toBeInstanceOf(GapCursor) expect(editor.state.selection.from).toBe(between) }) + + test('is not offered after the last footnote', ({ editor }) => { + editor.commands.setContent(markdownit.render('Text[^1]\n\n[^1]: Note\n')) + // end of the text in the footnote + const end = editor.state.doc.content.size - 3 + editor.commands.setTextSelection(end) + expect(editor.state.selection.$from.parent.textContent).toBe('Note') + pressKey(editor, 'ArrowRight') + expect(editor.state.selection).toBeInstanceOf(TextSelection) + expect(editor.state.selection.head).toBe(end) + }) + + test('moves a gap cursor after the footnotes into the last footnote', ({ editor }) => { + editor.commands.setContent(markdownit.render('Text[^1]\n\n[^1]: Note\n')) + const { doc } = editor.state + editor.view.dispatch(editor.state.tr.setSelection(new GapCursor(doc.resolve(doc.content.size)))) + expect(editor.state.selection).toBeInstanceOf(TextSelection) + expect(editor.state.selection.$head.parent.textContent).toBe('Note') + }) + + test('is offered between the last block and the footnotes', ({ editor }) => { + editor.commands.setContent(markdownit.render('Text[^1]\n\n| a |\n|---|\n| 1 |\n\n[^1]: Note\n')) + const { doc } = editor.state + const between = doc.content.size - doc.lastChild!.nodeSize + // end of the text in the last cell of the table + editor.commands.setTextSelection(between - 4) + expect(editor.state.selection.$from.parent.textContent).toBe('1') + pressKey(editor, 'ArrowRight') + expect(editor.state.selection).toBeInstanceOf(GapCursor) + expect(editor.state.selection.from).toBe(between) + }) + + test('is skipped between comments and footnotes', ({ editor }) => { + editor.commands.setContent('

Text' + + '' + + '

' + + '
' + + '

Comment

' + + '
' + + '
' + + '

Note

' + + '
') + const { doc } = editor.state + const between = doc.content.size - doc.lastChild!.nodeSize + expect(doc.childBefore(between).node!.type.name).toBe('comments') + // end of the text in the comment + editor.commands.setTextSelection(between - 4) + expect(editor.state.selection.$from.parent.textContent).toBe('Comment') + pressKey(editor, 'ArrowRight') + expect(editor.state.selection).toBeInstanceOf(TextSelection) + expect(editor.state.selection.$head.parent.textContent).toBe('Note') + pressKey(editor, 'ArrowLeft') + expect(editor.state.selection).toBeInstanceOf(TextSelection) + expect(editor.state.selection.$head.parent.textContent).toBe('Comment') + }) })