@edifice.io/tiptap-extensions
Version:
Edifice Rich Text Editor Extensions
3 lines (2 loc) • 1.92 kB
JavaScript
;Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const core=require("@tiptap/core"),Attachment=core.Node.create({name:"attachments",content:"",marks:"",group:"block",selectable:!0,atom:!0,draggable:!0,addOptions(){return{HTMLAttributes:{class:"attachments"}}},parseHTML(){return[{tag:"div[class=attachments]"}]},renderHTML({HTMLAttributes}){const renderedLinks=HTMLAttributes.links.map(el=>["a",{name:el.name,href:el.href,documentId:el.documentId,dataContentType:el.dataContentType},el.name]);return["div",this.options.HTMLAttributes,...renderedLinks]},addAttributes(){return{links:{default:[],parseHTML:element=>{const links=element.getElementsByTagName("a"),parsedLinks=[];for(let i=0;i<links.length;i++){const link=links[i],href=link.getAttribute("href"),name=link.textContent,regexResult=href.match(/([^/]+$)/),documentId=link.getAttribute("data-document-id")||regexResult&®exResult[0],dataContentType=link.getAttribute("data-content-type");parsedLinks.push({href,name,documentId,dataContentType})}return parsedLinks},renderHTML:attributes=>({links:attributes.links.map(link=>({href:link.href,name:link.name,documentId:link.documentId,dataContentType:link.dataContentType}))})}}},addCommands(){return{setAttachment:(attrs={dataContentType:"",name:"",documentId:"",href:""})=>({chain})=>chain().insertContent({type:this.name,attrs}).run(),unsetAttachment:documentId=>({state,dispatch})=>{const{selection}=state,{from,to}=selection;return state.doc.nodesBetween(from,to,(node,pos)=>{if(node.type.name===this.name&&node.attrs.links.length>1){const newLinks=node.attrs.links.filter(link=>link.documentId!==documentId);if(newLinks.length!==node.attrs.links.length){const newAttrs={...node.attrs,links:newLinks};dispatch(state.tr.setNodeMarkup(pos,void 0,newAttrs))}}else dispatch(state.tr.delete(from,to))}),!0}}}});exports.Attachment=Attachment;
//# sourceMappingURL=attachment.cjs.map