diff --git a/playwright/e2e/footnotes.spec.ts b/playwright/e2e/footnotes.spec.ts index cfbe9d07113..63509ea5bb8 100644 --- a/playwright/e2e/footnotes.spec.ts +++ b/playwright/e2e/footnotes.spec.ts @@ -17,7 +17,7 @@ test.describe('renders footnotes', () => { test('shows reference and footnote from Markdown', async ({ editor, open }) => { await open() await expect(editor.getFootnoteReference('bar')).toBeVisible() - await expect(editor.getFootnoteReference('bar').locator('a')).toHaveText('1') + await expect(editor.getFootnoteReference('bar').locator('a')).toHaveCSS('--footnote-ordinal', '1') await expect(editor.getFootnote('bar')).toContainText('footnote') }) }) diff --git a/src/css/prosemirror.scss b/src/css/prosemirror.scss index 9dc743e1ad2..26c95417753 100644 --- a/src/css/prosemirror.scss +++ b/src/css/prosemirror.scss @@ -490,6 +490,11 @@ div.ProseMirror { } } + a.footnote-ref::after, .footnote-label::before { + counter-reset: footnote-ordinal var(--footnote-ordinal); + content: counter(footnote-ordinal); + } + section[data-type="footnotes"] { display: flex; flex-direction: column; diff --git a/src/nodes/Footnote.ts b/src/nodes/Footnote.ts index 364fcc793dc..dba01bb9f5e 100644 --- a/src/nodes/Footnote.ts +++ b/src/nodes/Footnote.ts @@ -20,10 +20,6 @@ const Footnote = Node.create({ 'data-reference-id': attrs.referenceId, }), }, - ordinal: { - default: 0, - rendered: false, - }, } }, @@ -39,15 +35,13 @@ const Footnote = Node.create({ renderHTML({ node, HTMLAttributes }) { const id = node.attrs.referenceId - const ordinal = node.attrs.ordinal return [ 'div', mergeAttributes(HTMLAttributes, { 'data-type': 'footnote', id: `fn-${id}`, - style: `order: ${ordinal || 0}`, }), - ['span', { class: 'footnote-label', contenteditable: 'false', title: id }, String(ordinal || '')], + ['span', { class: 'footnote-label', contenteditable: 'false', title: id }], [ 'div', { class: 'footnote-body-wrapper' }, diff --git a/src/nodes/FootnoteReference.ts b/src/nodes/FootnoteReference.ts index 915c1c769b3..ee460707225 100644 --- a/src/nodes/FootnoteReference.ts +++ b/src/nodes/FootnoteReference.ts @@ -53,10 +53,6 @@ const FootnoteReference = Node.create({ 'data-reference-id': attrs.referenceId, }), }, - ordinal: { - default: 0, - rendered: false, - }, } }, @@ -66,7 +62,6 @@ const FootnoteReference = Node.create({ renderHTML({ node, HTMLAttributes }) { const id = node.attrs.referenceId - const ordinal = node.attrs.ordinal return [ 'sup', mergeAttributes(HTMLAttributes, { @@ -76,7 +71,6 @@ const FootnoteReference = Node.create({ [ 'a', { href: `#fn-${id}`, class: 'footnote-ref', role: 'doc-noteref', title: id }, - String(ordinal || ''), ], ] }, diff --git a/src/nodes/Footnotes.ts b/src/nodes/Footnotes.ts index 32d34309302..896f7751436 100644 --- a/src/nodes/Footnotes.ts +++ b/src/nodes/Footnotes.ts @@ -3,8 +3,11 @@ * SPDX-License-Identifier: AGPL-3.0-or-later */ +import type { Node as PMNode } from '@tiptap/pm/model' + import { mergeAttributes, Node } from '@tiptap/core' import { Plugin, PluginKey } from '@tiptap/pm/state' +import { Decoration, DecorationSet } from '@tiptap/pm/view' import Footnote from './Footnote.ts' import FootnoteReference from './FootnoteReference.ts' @@ -39,40 +42,14 @@ const Footnotes = Node.create({ return [ new Plugin({ key: new PluginKey('footnoteOrdinals'), - appendTransaction(transactions, _oldState, newState) { - if (!transactions.some((tr) => tr.docChanged)) { - return null - } - - // Compute ordinals - const ordinals = new Map() - newState.doc.descendants((node) => { - if (node.type.name === 'footnoteReference') { - const id = node.attrs.referenceId - if (id && !ordinals.has(id)) { - ordinals.set(id, ordinals.size + 1) - } - } - }) - - const tr = newState.tr - let hasChange = false - newState.doc.descendants((node, pos) => { - if (node.type.name !== 'footnoteReference' && node.type.name !== 'footnote') { - return - } - - const targetOrdinal = ordinals.get(node.attrs.referenceId) ?? 0 - if (node.attrs.ordinal !== targetOrdinal) { - tr.setNodeAttribute(pos, 'ordinal', targetOrdinal) - hasChange = true - } - }) - - if (!hasChange) { - return null - } - return tr + state: { + init: (_, { doc }) => footnoteOrdinals(doc), + apply: (tr, value) => (tr.docChanged ? footnoteOrdinals(tr.doc) : value), + }, + props: { + decorations(state) { + return this.getState(state) + }, }, }), new Plugin({ @@ -144,4 +121,38 @@ const Footnotes = Node.create({ }, }) +/** + * Number footnotes by first reference and expose the number as a CSS variable + * + * @param doc the document node + */ +function footnoteOrdinals(doc: PMNode): DecorationSet { + const ordinals = new Map() + const nodes: { node: PMNode, pos: number }[] = [] + doc.descendants((node, pos) => { + if (node.type.name === 'footnoteReference') { + const id = node.attrs.referenceId + if (id && !ordinals.has(id)) { + ordinals.set(id, ordinals.size + 1) + } + nodes.push({ node, pos }) + } else if (node.type.name === 'footnote') { + nodes.push({ node, pos }) + } + }) + + const decorations = [] + for (const { node, pos } of nodes) { + const ordinal = ordinals.get(node.attrs.referenceId) + if (!ordinal) { + continue + } + const style = node.type.name === 'footnote' + ? `order: ${ordinal}; --footnote-ordinal: ${ordinal}` + : `--footnote-ordinal: ${ordinal}` + decorations.push(Decoration.node(pos, pos + node.nodeSize, { style })) + } + return DecorationSet.create(doc, decorations) +} + export default Footnotes diff --git a/src/tests/nodes/Footnotes.spec.ts b/src/tests/nodes/Footnotes.spec.ts index 267071b54c8..8ecf5167fd5 100644 --- a/src/tests/nodes/Footnotes.spec.ts +++ b/src/tests/nodes/Footnotes.spec.ts @@ -172,6 +172,50 @@ describe('insertFootnote command', () => { }) }) +describe('Footnote ordinals', () => { + function ordinals(editor: Editor, selector: string): string[] { + return [...editor.view.dom.querySelectorAll(selector)] + .map((el) => el.style.getPropertyValue('--footnote-ordinal')) + } + + test('numbers footnotes by first reference and reuses the number for repeated references', ({ editor }) => { + editor.commands.setContent('

A' + + ' B' + + ' C

' + + '
' + + '

a

' + + '

b

' + + '
') + + expect(ordinals(editor, 'sup[data-type="footnote-reference"]')).toEqual(['1', '2', '1']) + expect(ordinals(editor, 'div[data-type="footnote"]')).toEqual(['2', '1']) + expect(editor.view.dom.querySelector('div[data-type="footnote"]')!.style.order).toBe('2') + }) + + test('renumbers when a reference is inserted before existing ones', ({ editor }) => { + editor.commands.setContent('

Foo

' + + '
' + + '

x

' + + '
') + editor.commands.setTextSelection(1) + editor.commands.insertFootnote() + + expect(ordinals(editor, 'sup[data-type="footnote-reference"]')).toEqual(['1', '2']) + expect(ordinals(editor, 'div[data-type="footnote"]')).toEqual(['2', '1']) + }) + + test('does not store ordinals in the document', ({ editor }) => { + editor.commands.setContent('

Foo

' + + '
' + + '

x

' + + '
') + + editor.state.doc.descendants((node) => { + expect(node.attrs).not.toHaveProperty('ordinal') + }) + }) +}) + describe('Footnotes cleanup', () => { function deleteFirstReference(editor: Editor) { // Delete reference node