-
Notifications
You must be signed in to change notification settings - Fork 11
Adding pasteHandler to ensure new UUID on copying a footnote Reference #25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| import { mergeAttributes, Node, nodeInputRule } from "@tiptap/core"; | ||
| import { mergeAttributes, Node, } from "@tiptap/core"; | ||
| import { Node as PMNode, Fragment as PMFragment, Slice } from "@tiptap/pm/model"; | ||
| import { NodeSelection, Plugin, PluginKey } from "@tiptap/pm/state"; | ||
|
|
||
| import { v4 as uuid } from "uuid"; | ||
|
|
@@ -93,7 +94,50 @@ const FootnoteReference = Node.create({ | |
|
|
||
| addProseMirrorPlugins() { | ||
| const { editor } = this; | ||
|
|
||
| /** | ||
| * A recursive function to traverse a node and its children. | ||
| * It rebuilds the node tree, giving any found `footnoteReference` | ||
| * a new, unique data-id. | ||
| */ | ||
| const mapNode = (node: PMNode): PMNode => { | ||
| // 1. If this is a footnoteReference, create a new one with a new ID. | ||
| if (node.type.name === this.name) { | ||
| const newAttrs = { ...node.attrs, "data-id": uuid() }; | ||
| return node.type.create(newAttrs, node.content, node.marks); | ||
| } | ||
|
|
||
| // 2. If this node has children, we must recurse by mapping over its content. | ||
| if (node.content && node.content.size > 0) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if node.type.name === this.name is set, the node can't have children?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. footnoteReference cannot have child nodes. It can only have text within it, according to the schema defined : |
||
| const newChildren: PMNode[] = []; | ||
| node.content.forEach(childNode => { | ||
| newChildren.push(mapNode(childNode)); | ||
| }); | ||
| const newFragment = PMFragment.from(newChildren); | ||
| return node.copy(newFragment); | ||
| } | ||
|
|
||
| // 3. If it's a plain node with no content, return it as-is. | ||
| return node; | ||
| }; | ||
|
|
||
|
|
||
| return [ | ||
| new Plugin({ | ||
| key: new PluginKey("footnotePasteHandler"), | ||
| props: { | ||
| transformPasted(slice) { | ||
| const newTopLevelNodes: PMNode[] = []; | ||
| slice.content.forEach(topNode => { | ||
| newTopLevelNodes.push(mapNode(topNode)); | ||
| }); | ||
|
Comment on lines
+129
to
+133
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can this not use map? feels like its just mapping each node in slice.content
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. tiptap fragment (which is what slice.content is) does not have a map function. It only has a forEach function to interate through the nodes in the fragment. That's why I used a forEach |
||
|
|
||
| // Build a new Fragment and a new Slice | ||
| const newFragment = PMFragment.from(newTopLevelNodes); | ||
| return new Slice(newFragment, slice.openStart, slice.openEnd); | ||
| }, | ||
| }, | ||
| }), | ||
| new Plugin({ | ||
| key: new PluginKey("footnoteRefClick"), | ||
|
|
||
|
|
@@ -154,6 +198,7 @@ const FootnoteReference = Node.create({ | |
| }, | ||
| ]; | ||
| }, | ||
|
|
||
| }); | ||
|
|
||
| export default FootnoteReference; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
where does this.name get set to
footnoteReference?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this.name is accessible because we are creating a TipTap Node.
It gets set here
https://github.com/Some1Somewhere/tiptap-footnotes/blob/2984459a4eb67167ad65c17fd2647a018fd6c9be/package/src/footnotes/reference.ts#L23-L24