1- import { mergeAttributes , Node } from "@tiptap/core" ;
1+ import { Attribute , mergeAttributes , Node } from "@tiptap/core" ;
22
33export default Node . create ( {
44 name : "tag" ,
@@ -23,30 +23,36 @@ export default Node.create({
2323 addNodeView : ( ) => ( { node, extension } ) => {
2424 const span = document . createElement ( 'span' ) ;
2525 span . className = 'mi-tag'
26- span . innerHTML = node . attrs . label
27- // span.setAttribute('contenteditable', 'false');
26+ const { label , id , class : classes , style , ... restAttrs } = node . attrs
27+ span . innerHTML = label
2828 span . setAttribute ( 'data-type' , 'tag' )
2929
30- if ( node . attrs . id ) {
31- span . setAttribute ( 'data-id' , node . attrs . id )
30+ if ( id ) {
31+ span . setAttribute ( 'data-id' , id )
3232 }
3333
3434 if ( extension . options . tagClassName ) {
3535 span . classList . add ( extension . options . tagClassName )
3636 }
3737
38- if ( node . attrs . class ) {
39- if ( Array . isArray ( node . attrs . class ) ) {
40- node . attrs . class . forEach ( ( c ) => {
38+ if ( classes ) {
39+ if ( Array . isArray ( classes ) ) {
40+ classes . forEach ( ( c ) => {
4141 span . classList . add ( c as string )
4242 } )
4343 } else {
44- span . classList . add ( node . attrs . class )
44+ span . classList . add ( classes )
4545 }
4646 }
4747
48- if ( node . attrs . style ) {
49- Object . assign ( span . style , node . attrs . style )
48+ if ( style ) {
49+ Object . assign ( span . style , style )
50+ }
51+
52+ if ( Object . keys ( restAttrs ) . length ) {
53+ Object . keys ( restAttrs ) . forEach ( ( key ) => {
54+ span . dataset [ key ] = restAttrs [ key ]
55+ } )
5056 }
5157
5258 const dom = document . createElement ( 'span' )
@@ -58,6 +64,10 @@ export default Node.create({
5864 } ,
5965
6066 addAttributes ( ) {
61- return { label : undefined , id : undefined , class : undefined , style : undefined } ;
67+ const extraAttrs : Record < string , Attribute > = { }
68+ for ( const key in this . options . attrs ) {
69+ extraAttrs [ key ] = { default : this . options . attrs [ key ] }
70+ }
71+ return extraAttrs
6272 }
6373} ) ;
0 commit comments