Skip to content

Instantly share code, notes, and snippets.

@mackmm145
Created January 16, 2022 22:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mackmm145/f100a62de841464872785e3d041be13f to your computer and use it in GitHub Desktop.
Save mackmm145/f100a62de841464872785e3d041be13f to your computer and use it in GitHub Desktop.
TipTap jump anchor example
import { Mark, mergeAttributes } from '@tiptap/core'
export const jumpAnchor = Mark.create( {
name: "jumpAnchor",
addAttributes() {
return {
id: {
default: null,
parseHTML: element => element.getAttribute( 'id' ),
renderHTML: attributes => {
if( !attributes.id ) {
return {}
}
return {
'id': attributes.id
}
}
},
}
},
parseHTML() {
return [
{
tag: 'span[data-type="jump-anchor"]',
}
]
},
renderHTML( { HTMLAttributes } ) {
return [ 'span', mergeAttributes( { 'data-type': 'jump-anchor' }, HTMLAttributes ), 0 ]
},
addCommands() {
return {
setJumpAnchor: ( attributes ) => ( { chain } ) => {
return chain().extendMarkRange( 'jumpAnchor' ).setMark( 'jumpAnchor', attributes ).run()
},
getJumpAnchor: () => ( { commands } ) => {
if ( this.editor.view.state.selection.$from.nodeAfter == null ) { return }
let node = this.editor.view.state.selection.$from.nodeAfter
let mark = node.marks.find( mark => mark.type && mark.type.name == "jumpAnchor" )
if ( mark ) { return mark.attrs.id }
},
unsetJumpAnchor: () => ( { commands } ) => {
console.log( "unsetJumpAnchor not written..." )
}
}
}
} )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment