UNPKG

@docs.plus/extension-indent

Version:

A Tiptap extension for managing text indentation in documents

4 lines 1.55 kB
import{Extension as m}from"@tiptap/core";import{TextSelection as h}from"@tiptap/pm/state";var T=m.create({name:"indent",addOptions(){return{indentChars:" ",enabled:!0,allowedNodeTypes:["paragraph","listItem","orderedList"]}},addCommands(){let c=e=>{let{allowedNodeTypes:o}=this.options;if(!o?.length)return!0;let t=e.selection.$from.node();return o.includes(t.type.name)};return{indent:()=>({tr:e,state:o,dispatch:s})=>{if(!this.options.enabled||!c(o))return!1;let{selection:t}=o,{indentChars:r}=this.options;if(t.empty)return s&&(e.insertText(r,t.from,t.to),s(e)),!0;let l=o.doc.textBetween(t.from,t.to).split(` `),f=t.from;if(s){for(let d of l){let i=f,n=i+d.length;e.insertText(r,i,i),f=n+r.length+1}s(e)}return!0},outdent:()=>({tr:e,state:o,dispatch:s})=>{if(!this.options.enabled||!c(o))return!1;let{selection:t}=o,{indentChars:r}=this.options,l=!1;if(t.empty){let i=t.$cursor;if(!i)return!1;let n=i.pos,a=o.doc.resolve(n).start(),u=o.doc.textBetween(a,n),p=n===a;return u.endsWith(r)?s&&(e.delete(n-r.length,n),l=!0):p&&u.startsWith(r)&&s&&(e.delete(a,a+r.length),l=!0),s&&(l||e.setSelection(h.create(e.doc,n)),s(e)),!0}let f=o.doc.textBetween(t.from,t.to).split(` `),d=t.from;if(s){for(let i of f){let n=d;i.startsWith(r)?(e.delete(n,n+r.length),d=n+i.length-r.length+1):d=n+i.length+1}s(e)}return!0}}},addKeyboardShortcuts(){return{Tab:()=>this.options.enabled?this.editor.commands.indent():!1,"Shift-Tab":()=>this.options.enabled?this.editor.commands.outdent():!1}}}),y=null;export{T as Indent,y as indentCSS}; //# sourceMappingURL=index.js.map