From 6a151397ddeca9797fb9f07d601b26248f9ce472 Mon Sep 17 00:00:00 2001 From: Jonas Date: Thu, 5 Nov 2026 16:13:54 +0100 Subject: [PATCH] refactor(links): derive link bubble state in plugin state apply The link bubble plugin used appendTransaction to work out the active link from the selection, which dispatched an extra transaction on every selection change. Do this in the plugin state's apply() instead, which gets the old and new state as arguments. This also replaces the full document comparison (doc.eq) with tr.docChanged, and adds tests for the derived bubble state. Signed-off-by: Jonas Assisted-by: ClaudeCode:claude-opus-5-5 --- src/plugins/links.ts | 41 +++++++----------- src/tests/plugins/linkBubble.spec.js | 64 +++++++++++++++++++++++++++- 2 files changed, 78 insertions(+), 27 deletions(-) diff --git a/src/plugins/links.ts b/src/plugins/links.ts index 17ab16e05b8..5dd663b83af 100644 --- a/src/plugins/links.ts +++ b/src/plugins/links.ts @@ -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 } }, }, @@ -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()), diff --git a/src/tests/plugins/linkBubble.spec.js b/src/tests/plugins/linkBubble.spec.js index b9e2240cac8..c7a9230cee0 100644 --- a/src/tests/plugins/linkBubble.spec.js +++ b/src/tests/plugins/linkBubble.spec.js @@ -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', () => { @@ -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