Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/extensions/RichText.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Holy moly. Gapcursor is loaded in the line before. Does the capital c make all the difference? 😅

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hehe, yes. One is the ProseMirror plugin GapCursor (tiptap/pm is a repackaged version of ProseMirror) and one is the Tiptap extension Gapcursor which builds on top of the ProseMirror plugin.

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'
Expand Down Expand Up @@ -95,6 +97,31 @@ export default Extension.create<RichTextOptions>({
// 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))
Comment on lines +119 to +120

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With dir = -1 this moves the selection into comments, but the editor always renders that section hidden (section[data-type="comments"] { display: none; } in src/css/prosemirror.scss), and CommentReference.insertComment explicitly moves the cursor out of "the hidden comments container" for that reason. The new test is skipped between comments and footnotes asserts the selection lands in Comment.

Checked in Chrome with this change on stable35 (document: one paragraph with a comment and a footnote reference, one comment, one footnote; caret at the start of the footnote):

  • ArrowLeft or ArrowUp → TextSelection in doc > comments > comment > commentItem > paragraph, no visible caret. Without this change the gap cursor between the sections is shown.
  • then Backspace → the whole comment thread is removed and the footnote text is merged into the body paragraph. Saved file: Text with a comment[^comment-1] and a footnote[^1].Footnote body followed by an empty [^1]:. Without this change, ArrowLeft + Backspace only node-selects the hidden comments section.
  • then Enter instead → an empty paragraph is added inside the hidden comment.

Suggestion: never target comments; when the neighbour in direction dir is comments, continue before it (e.g. Selection.near(doc.resolve(<position before the comments node>), -1)) and adapt the test.

},
}),
]
},
}),
Text,
Paragraph,
Expand Down
63 changes: 63 additions & 0 deletions src/tests/extensions/Gapcursor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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'))
Expand All @@ -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('<p>Text'
+ '<sup data-type="comment-reference" data-reference-id="comment-1"></sup>'
+ '<sup data-type="footnote-reference" data-reference-id="1"></sup></p>'
+ '<section data-type="comments"><div data-type="comment" data-reference-id="comment-1">'
+ '<div data-type="comment-item" data-author="jane"><p>Comment</p></div>'
+ '</div></section>'
+ '<section data-type="footnotes"><div data-type="footnote" data-reference-id="1">'
+ '<p>Note</p>'
+ '</div></section>')
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')
})
})
Loading