Created
January 16, 2022 22:14
-
-
Save mackmm145/f100a62de841464872785e3d041be13f to your computer and use it in GitHub Desktop.
TipTap jump anchor example
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
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