UNPKG

@docs.plus/extension-indent

Version:

A Tiptap extension for managing text indentation in documents

4 lines 2.04 kB
"use strict";var u=Object.defineProperty;var x=Object.getOwnPropertyDescriptor;var T=Object.getOwnPropertyNames;var y=Object.prototype.hasOwnProperty;var S=(i,e)=>{for(var n in e)u(i,n,{get:e[n],enumerable:!0})},b=(i,e,n,o)=>{if(e&&typeof e=="object"||typeof e=="function")for(let t of T(e))!y.call(i,t)&&t!==n&&u(i,t,{get:()=>e[t],enumerable:!(o=x(e,t))||o.enumerable});return i};var C=i=>b(u({},"__esModule",{value:!0}),i);var B={};S(B,{Indent:()=>w,indentCSS:()=>$});module.exports=C(B);var m=require("@tiptap/core"),h=require("@tiptap/pm/state"),w=m.Extension.create({name:"indent",addOptions(){return{indentChars:" ",enabled:!0,allowedNodeTypes:["paragraph","listItem","orderedList"]}},addCommands(){let i=e=>{let{allowedNodeTypes:n}=this.options;if(!n?.length)return!0;let t=e.selection.$from.node();return n.includes(t.type.name)};return{indent:()=>({tr:e,state:n,dispatch:o})=>{if(!this.options.enabled||!i(n))return!1;let{selection:t}=n,{indentChars:r}=this.options;if(t.empty)return o&&(e.insertText(r,t.from,t.to),o(e)),!0;let d=n.doc.textBetween(t.from,t.to).split(` `),a=t.from;if(o){for(let f of d){let l=a,s=l+f.length;e.insertText(r,l,l),a=s+r.length+1}o(e)}return!0},outdent:()=>({tr:e,state:n,dispatch:o})=>{if(!this.options.enabled||!i(n))return!1;let{selection:t}=n,{indentChars:r}=this.options,d=!1;if(t.empty){let l=t.$cursor;if(!l)return!1;let s=l.pos,c=n.doc.resolve(s).start(),p=n.doc.textBetween(c,s),g=s===c;return p.endsWith(r)?o&&(e.delete(s-r.length,s),d=!0):g&&p.startsWith(r)&&o&&(e.delete(c,c+r.length),d=!0),o&&(d||e.setSelection(h.TextSelection.create(e.doc,s)),o(e)),!0}let a=n.doc.textBetween(t.from,t.to).split(` `),f=t.from;if(o){for(let l of a){let s=f;l.startsWith(r)?(e.delete(s,s+r.length),f=s+l.length-r.length+1):f=s+l.length+1}o(e)}return!0}}},addKeyboardShortcuts(){return{Tab:()=>this.options.enabled?this.editor.commands.indent():!1,"Shift-Tab":()=>this.options.enabled?this.editor.commands.outdent():!1}}}),$=null;0&&(module.exports={Indent,indentCSS}); //# sourceMappingURL=index.cjs.map