Skip to content
Merged
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
41 changes: 16 additions & 25 deletions src/plugins/links.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,26 @@ export function linkBubble(options: { editor: Editor }) {
key: linkBubbleKey,
state: {
init: () => ({ active: null, focusInput: false }),
apply: (tr, cur) => {
apply: (tr, cur, oldState, newState) => {
const meta = tr.getMeta(linkBubbleKey)
// Explicit updates of the bubble state take precedence
if (meta) {
return { active: meta.active, focusInput: !!meta.focusInput }
}
return cur

// Don't open bubble at editor initialisation
if (oldState.doc.content.size === 2) {
return cur
}

// Don't open bubble if neither selection nor doc changed
const sameSelection = oldState.selection.eq(newState.selection)
// Don't open bubble on changes by other session members
const noHistory = tr.getMeta('addToHistory') === false
if (sameSelection && (noHistory || !tr.docChanged)) {
return cur
}
return { active: activeLinkFromSelection(newState), focusInput: false }
},
},

Expand All @@ -138,29 +152,6 @@ export function linkBubble(options: { editor: Editor }) {
plugin: linkBubblePlugin,
}),

appendTransaction: (transactions, oldState, state) => {
// Don't open bubble at editor initialisation
if (oldState?.doc.content.size === 2) {
return
}

// Explicit updates of the bubble state take precedence
if (transactions.some((tr) => tr.getMeta(linkBubbleKey))) {
return
}

// Don't open bubble if neither selection nor doc changed
const sameSelection = oldState?.selection.eq(state.selection)
const sameDoc = oldState?.doc.eq(state.doc)
// Don't open bubble on changes by other session members
const noHistory = transactions.every((tr) => tr.getMeta('addToHistory') === false)
if (sameSelection && (noHistory || sameDoc)) {
return
}
const active = activeLinkFromSelection(state)
return state.tr.setMeta(linkBubbleKey, { active })
},

props: {
handleDOMEvents: {
...(isMobileDevice ? {} : linkHoverHandlers()),
Expand Down
64 changes: 62 additions & 2 deletions src/tests/plugins/linkBubble.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import { EditorState, Plugin } from '@tiptap/pm/state'
import { EditorState, Plugin, TextSelection } from '@tiptap/pm/state'
import { schema } from 'prosemirror-schema-basic'
import { hideLinkBubble, linkBubble, setActiveLink } from '../../plugins/links.ts'
import { hideLinkBubble, linkBubble, linkBubbleKey, setActiveLink } from '../../plugins/links.ts'

describe('linkBubble prosemirror plugin', () => {
test('signature', () => {
Expand Down Expand Up @@ -66,6 +66,66 @@ describe('linkBubble prosemirror plugin', () => {
})
})

describe('linkBubble state derived from selection', () => {
const linkMark = schema.marks.link.create({ href: 'https://example.org' })
const doc = schema.node('doc', null, [
schema.node('paragraph', null, [
schema.text('foo '),
schema.text('link', [linkMark]),
]),
])

const createLinkState = () => createState({ doc, plugins: [linkBubble()] })
const select = (state, pos) => state.apply(state.tr.setSelection(TextSelection.create(state.doc, pos)))
const active = (state) => linkBubbleKey.getState(state).active

test('sets active link when selection moves into a link', () => {
const state = select(createLinkState(), 7)
expect(active(state)).toEqual({ mark: linkMark, nodeStart: 5 })
})

test('clears active link when selection moves out of a link', () => {
const state = select(select(createLinkState(), 7), 2)
expect(active(state)).toBe(null)
})

test('keeps explicit state if selection and doc are unchanged', () => {
const state = select(createLinkState(), 7)
const hidden = state.apply(state.tr.setMeta(linkBubbleKey, { active: null }))
const after = hidden.apply(hidden.tr.setMeta('foo', 'bar'))
expect(active(after)).toBe(null)
})

test('keeps explicit state on remote changes', () => {
const state = select(createLinkState(), 7)
const hidden = state.apply(state.tr.setMeta(linkBubbleKey, { active: null }))
const remote = hidden.tr.insertText('!', hidden.doc.content.size - 1)
.setMeta('addToHistory', false)
expect(active(hidden.apply(remote))).toBe(null)
})

test('updates active link on local changes', () => {
const state = select(createLinkState(), 7)
const hidden = state.apply(state.tr.setMeta(linkBubbleKey, { active: null }))
const after = hidden.apply(hidden.tr.insertText('x', 7))
expect(active(after)?.mark.eq(linkMark)).toBe(true)
})

test('explicit state takes precedence over selection', () => {
const state = createLinkState()
const tr = state.tr.setSelection(TextSelection.create(state.doc, 7))
.setMeta(linkBubbleKey, { active: null, focusInput: true })
expect(linkBubbleKey.getState(state.apply(tr))).toEqual({ active: null, focusInput: true })
})

test('does not open bubble on editor initialisation', () => {
const state = createState({ plugins: [linkBubble()] })
const tr = state.tr.replaceWith(0, state.doc.content.size, doc.content)
tr.setSelection(TextSelection.create(tr.doc, 7))
expect(active(state.apply(tr))).toBe(null)
})
})

// simulate the data flow in prosemirror
function createFlow(initialState) {
let state = initialState
Expand Down
Loading