Skip to content

Commit

Permalink
fix(currentLineMenu): undefined currentParagraph handling
Browse files Browse the repository at this point in the history
Also improve some type annotations.

Signed-off-by: Max <[email protected]>
  • Loading branch information
max-nextcloud committed Jan 16, 2025
1 parent 1d95953 commit d289b67
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions src/plugins/currentLineMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import { Plugin, PluginKey, EditorState } from '@tiptap/pm/state'
import { EditorState, Plugin, PluginKey } from '@tiptap/pm/state'
import { Editor } from '@tiptap/core'
import { Decoration, DecorationSet } from '@tiptap/pm/view'
import Vue from 'vue'
import SmartPickerMenu from '../components/Editor/SmartPickerMenu.vue'
Expand All @@ -15,7 +16,7 @@ export const currentLineMenuKey = new PluginKey('currentLineMenu')
* ProseMirror plugin providing a single decoration for the current line.
*
* @param {object} options - options for the plugin
* @param {object} options.editor - the tiptap editor
* @param {Editor} options.editor - the tiptap editor
*
* @return {Plugin<DecorationSet>}
*/
Expand All @@ -39,9 +40,6 @@ export default function currentLineMenu({ editor }) {
return { decorations: DecorationSet.empty }
}
const currentParagraph = getCurrentParagraph(newState)
if (!currentParagraph) {
return { decorations: DecorationSet.empty }
}
const decorations = mapDecorations(value, tr, currentParagraph)
|| currentParagraphDecorations(newState.doc, currentParagraph, editor)
return { currentParagraph, decorations }
Expand All @@ -64,12 +62,12 @@ export default function currentLineMenu({ editor }) {
*
* @param {object} value - previous plugin state
* @param {object} tr - current transaction
* @param {object} currentParagraph - attributes of the current paragraph
* @param {object|undefined} currentParagraph - attributes of the current paragraph
*
* @return {false|DecorationSet}
*/
function mapDecorations(value, tr, currentParagraph) {
if (value.currentParagraph?.pos !== currentParagraph.pos) {
if (!currentParagraph || currentParagraph.pos !== value.currentParagraph?.pos) {
return false
}
let removedDecorations = false
Expand All @@ -87,7 +85,7 @@ function mapDecorations(value, tr, currentParagraph) {
* Get the paragraph node the cursor is on.
*
* @param {EditorState} state - the prosemirror state
* @return {Node|undefined} - the current paragraph if the cursor is in one
* @return {object|undefined} - the current paragraph if the cursor is in one
*/
function getCurrentParagraph({ selection }) {
const { parent, depth } = selection.$anchor
Expand All @@ -111,21 +109,24 @@ function getCurrentParagraph({ selection }) {
* Create a menu decorations for the given paragraph
*
* @param {Document} doc - prosemirror doc
* @param {Node} currentParagraph - paragraph to decorate
* @param {object} editor - tiptap editor
* @param {object|undefined} currentParagraph - paragraph to decorate
* @param {Editor} editor - tiptap editor
*
* @return {DecorationSet}
*/
function currentParagraphDecorations(doc, currentParagraph, editor) {
if (!currentParagraph) {
return { decorations: DecorationSet.empty }
}
const decorations = [decorationForCurrentParagraph(currentParagraph, editor)]
return DecorationSet.create(doc, decorations)
}

/**
* Create a decoration for the currentParagraph
*
* @param {object} currentParagraph to decorate
* @param {object} editor - tiptap editor
* @param {Node} currentParagraph to decorate
* @param {Editor} editor - tiptap editor
*
* @return {Decoration}
*/
Expand All @@ -140,7 +141,7 @@ function decorationForCurrentParagraph(currentParagraph, editor) {
/**
* Create a menu element for the given currentParagraph
*
* @param {object} editor - tiptap editor
* @param {Editor} editor - tiptap editor
*
* @return {Element}
*/
Expand Down

0 comments on commit d289b67

Please sign in to comment.