-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix footnote reference insertions (#4)
* fix footnote reference insertions * add tests * add jsdom dep * update deletion test case * update version - 2.0.1
- Loading branch information
Showing
7 changed files
with
802 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { expect, test } from "vitest"; | ||
import { Footnotes, getFootnotes, newEditor } from "./utils"; | ||
|
||
function testMatchingFootnotes(footnotes: Footnotes) { | ||
expect(footnotes.refs.length).toEqual(footnotes.footnotes.length); | ||
|
||
for (let i = 0; i < footnotes.refs.length; i++) { | ||
const ref = footnotes.refs[i]; | ||
const footnote = footnotes.footnotes[i]; | ||
let refNumber = String(i + 1); | ||
|
||
expect(ref.attrs!["data-id"]).toEqual(footnote.attrs!["data-id"]); | ||
expect(ref.attrs!["referenceNumber"]).toEqual(refNumber); | ||
expect(footnote.attrs!["id"]).toEqual(`fn:${refNumber}`); | ||
} | ||
} | ||
|
||
test("basic footnote reference insertion", () => { | ||
const editor = newEditor(); | ||
editor.commands.addFootnote(); | ||
const footnotes = getFootnotes(editor); | ||
testMatchingFootnotes(footnotes); | ||
}); | ||
|
||
test("footnote insertion between two refs ", () => { | ||
const editor = newEditor(); | ||
editor.commands.addFootnote(); | ||
editor.commands.insertContent("-----------"); | ||
editor.commands.addFootnote(); | ||
|
||
// add footnote betwween refs 1 & 2 | ||
editor.commands.setTextSelection(10); | ||
editor.commands.addFootnote(); | ||
|
||
const footnotes = getFootnotes(editor); | ||
|
||
testMatchingFootnotes(footnotes); | ||
}); | ||
|
||
test("footnote insertion between two refs (with chained command)", () => { | ||
const editor = newEditor(); | ||
editor.commands.addFootnote(); | ||
editor.commands.insertContent("-----------"); | ||
editor.commands.addFootnote(); | ||
|
||
// add footnote betwween refs 1 & 2 | ||
editor.commands.setTextSelection(10); | ||
|
||
editor.chain().deleteRange({ from: 2, to: 5 }).addFootnote().run(); | ||
|
||
const footnotes = getFootnotes(editor); | ||
|
||
testMatchingFootnotes(footnotes); | ||
}); | ||
|
||
test("test footnote reference deletion", () => { | ||
const editor = newEditor(); | ||
editor.commands.addFootnote(); | ||
editor.commands.addFootnote(); | ||
editor.commands.addFootnote(); | ||
|
||
const prevFootnotes = getFootnotes(editor); | ||
|
||
// delete the first footnote ref | ||
editor.commands.deleteRange({ from: 1, to: 3 }); | ||
|
||
const footnotes = getFootnotes(editor); | ||
|
||
expect(footnotes.refs.length).toEqual(2); | ||
|
||
expect(footnotes.refs[0].attrs["data-id"]).toEqual( | ||
prevFootnotes.refs[1].attrs["data-id"], | ||
); | ||
expect(footnotes.refs[1].attrs["data-id"]).toEqual( | ||
prevFootnotes.refs[2].attrs["data-id"], | ||
); | ||
|
||
testMatchingFootnotes(footnotes); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { Editor } from "@tiptap/core"; | ||
import { Node } from "@tiptap/pm/model"; | ||
import Document from "@tiptap/extension-document"; | ||
import Paragraph from "@tiptap/extension-paragraph"; | ||
import Text from "@tiptap/extension-text"; | ||
import { Footnote, Footnotes, FootnoteReference } from "../src/index"; | ||
|
||
type Footnotes = { | ||
refs: Node[]; | ||
footnotes: Node[]; | ||
}; | ||
export function getFootnotes(editor: Editor) { | ||
const footnotes: Footnotes = { refs: [], footnotes: [] }; | ||
|
||
editor.state.doc.descendants((node) => { | ||
if (node.type.name == "footnoteReference") { | ||
footnotes.refs.push(node); | ||
} else if (node.type.name == "footnote") { | ||
footnotes.footnotes.push(node); | ||
} | ||
}); | ||
return footnotes; | ||
} | ||
|
||
const createEditorEl = () => { | ||
const editorEl = document.createElement("div"); | ||
|
||
editorEl.classList.add("tiptap"); | ||
document.body.appendChild(editorEl); | ||
|
||
return editorEl; | ||
}; | ||
export function newEditor() { | ||
return new Editor({ | ||
element: createEditorEl(), | ||
extensions: [ | ||
Document.extend({ | ||
content: "block+ footnotes?", | ||
}), | ||
Text, | ||
Paragraph, | ||
Footnotes, | ||
Footnote, | ||
FootnoteReference, | ||
], | ||
}); | ||
} |
Oops, something went wrong.