2 lines
10 KiB
JavaScript
2 lines
10 KiB
JavaScript
var R=Object.create;var C=Object.defineProperty;var P=Object.getOwnPropertyDescriptor;var I=Object.getOwnPropertyNames;var B=Object.getPrototypeOf,N=Object.prototype.hasOwnProperty;var E=s=>C(s,"__esModule",{value:!0});var V=(s,t)=>{E(s);for(var e in t)C(s,e,{get:t[e],enumerable:!0})},O=(s,t,e)=>{if(t&&typeof t=="object"||typeof t=="function")for(let i of I(t))!N.call(s,i)&&i!=="default"&&C(s,i,{get:()=>t[i],enumerable:!(e=P(t,i))||e.enumerable});return s},x=s=>O(E(C(s!=null?R(B(s)):{},"default",s&&s.__esModule&&"default"in s?{get:()=>s.default,enumerable:!0}:{value:s,enumerable:!0})),s);var l=(s,t,e)=>new Promise((i,r)=>{var n=c=>{try{o(e.next(c))}catch(m){r(m)}},a=c=>{try{o(e.throw(c))}catch(m){r(m)}},o=c=>c.done?i(c.value):Promise.resolve(c.value).then(n,a);o((e=e.apply(s,t)).next())});V(exports,{default:()=>v});var u=x(require("obsidian"));var g=x(require("obsidian")),L=class extends g.PluginSettingTab{constructor(t,e){super(t,e);this.plugin=e}display(){let{containerEl:t}=this;t.empty(),t.createEl("h2",{text:"General Settings"}),new g.Setting(t).setName("Trim on Manual Save").setDesc("Trim the document during manual save (CTRL / CMD + S).").addToggle(e=>{e.setValue(this.plugin.settings.TrimOnSave).onChange(i=>l(this,null,function*(){this.plugin.settings.TrimOnSave=i,yield this.plugin.saveSettings(),this.plugin._toggleListenerEvent(i)}))}),new g.Setting(t).setName("Auto-Trim").setDesc("Automatically trim document when modified, according to the settings below.").addToggle(e=>{e.setValue(this.plugin.settings.AutoTrimDocument).onChange(i=>l(this,null,function*(){this.plugin.settings.AutoTrimDocument=i,yield this.plugin.saveSettings(),this.plugin._toggleListenerEvent(i)}))}),new g.Setting(t).setName("Auto-Trim Delay (seconds)").setDesc("Seconds to wait before auto-trimming.").addText(e=>{e.setValue(this.plugin.settings.AutoTrimTimeout.toString()).onChange(i=>l(this,null,function*(){let r=parseFloat(i);if(isNaN(r)){new g.Notice("Trim Whitespace: Enter a valid number!");return}this.plugin.settings.AutoTrimTimeout=r,yield this.plugin.saveSettings(),this.plugin._toggleListenerEvent(!1),this.plugin._initializeDebouncer(r),this.plugin._toggleListenerEvent(!0)}))}),new g.Setting(t).setName("Preserve Code Blocks").setDesc("Whether to preserve whitespace within code blocks.").addToggle(e=>{e.setValue(this.plugin.settings.PreserveCodeBlocks).onChange(i=>l(this,null,function*(){this.plugin.settings.PreserveCodeBlocks=i,yield this.plugin.saveSettings()}))}),t.createEl("h2",{text:"Trimming Rules"}),t.createEl("h3",{text:"Trailing Characters"}),new g.Setting(t).setName("Trim Trailing Spaces").setDesc("Trim spaces at the end of each line.").addToggle(e=>{e.setValue(this.plugin.settings.TrimTrailingSpaces).onChange(i=>l(this,null,function*(){this.plugin.settings.TrimTrailingSpaces=i,yield this.plugin.saveSettings()}))}),new g.Setting(t).setName("Trim Trailing Tabs").setDesc("Trim tabs at the end of each line.").addToggle(e=>{e.setValue(this.plugin.settings.TrimTrailingTabs).onChange(i=>l(this,null,function*(){this.plugin.settings.TrimTrailingTabs=i,yield this.plugin.saveSettings()}))}),new g.Setting(t).setName("Trim Trailing Lines").setDesc("Trim empty lines at the end of the document.").addToggle(e=>{e.setValue(this.plugin.settings.TrimTrailingLines).onChange(i=>l(this,null,function*(){this.plugin.settings.TrimTrailingLines=i,yield this.plugin.saveSettings()}))}),t.createEl("h3",{text:"Leading Characters"}),new g.Setting(t).setName("Preserve Indented Lists").setDesc("Preserve leading characters if they're used for list indentation.").addToggle(e=>{e.setValue(this.plugin.settings.PreserveIndentedLists).onChange(i=>l(this,null,function*(){this.plugin.settings.PreserveIndentedLists=i,yield this.plugin.saveSettings()}))}),new g.Setting(t).setName("Trim Leading Spaces").setDesc("Trim spaces at the start of each line.").addToggle(e=>{e.setValue(this.plugin.settings.TrimLeadingSpaces).onChange(i=>l(this,null,function*(){this.plugin.settings.TrimLeadingSpaces=i,yield this.plugin.saveSettings()}))}),new g.Setting(t).setName("Trim Leading Tabs").setDesc("Trim tabs at the start of each line.").addToggle(e=>{e.setValue(this.plugin.settings.TrimLeadingTabs).onChange(i=>l(this,null,function*(){this.plugin.settings.TrimLeadingTabs=i,yield this.plugin.saveSettings()}))}),new g.Setting(t).setName("Trim Leading Lines").setDesc("Trim lines at the start of the document.").addToggle(e=>{e.setValue(this.plugin.settings.TrimLeadingLines).onChange(i=>l(this,null,function*(){this.plugin.settings.TrimLeadingLines=i,yield this.plugin.saveSettings()}))}),t.createEl("h3",{text:"Multiple Characters"}),new g.Setting(t).setName("Trim Multiple Spaces").setDesc("Trim groups of multiple inline spaces.").addToggle(e=>{e.setValue(this.plugin.settings.TrimMultipleSpaces).onChange(i=>l(this,null,function*(){this.plugin.settings.TrimMultipleSpaces=i,yield this.plugin.saveSettings()}))}),new g.Setting(t).setName("Trim Multiple Tabs").setDesc("Trim groups of multiple inline tabs.").addToggle(e=>{e.setValue(this.plugin.settings.TrimMultipleTabs).onChange(i=>l(this,null,function*(){this.plugin.settings.TrimMultipleTabs=i,yield this.plugin.saveSettings()}))}),new g.Setting(t).setName("Trim Multiple Lines").setDesc("Trim groups of multiple blank lines.").addToggle(e=>{e.setValue(this.plugin.settings.TrimMultipleLines).onChange(i=>l(this,null,function*(){this.plugin.settings.TrimMultipleLines=i,yield this.plugin.saveSettings()}))})}};function D(s,t,e){let i={text:s.toString(),terms:[]};return e.forEach(r=>{let n=r.exec(i.text);if(!!n)for(;n;){let a=n[0];i.terms.push(a);let o=n.index,c=i.terms.length-1,m=`{{${t}${c.toString()}}}`;i.text=i.text.slice(0,o)+m+i.text.slice(o+a.length),r.lastIndex=0,n=r.exec(i.text)}}),i}function A(s,t,e){return e.forEach((i,r)=>{let n=`{{${t}${r.toString()}}}`,a=s.indexOf(n),o=s.substring(0,a),c=s.substring(a+n.length);s=o+i+c}),s}function W(s,t){let e=new RegExp(`(${t.join("|")})+$`,"gm");return s.replace(e,"")}function $(s){return s.trimEnd()}function F(s,t,e){let r=e?`(?!\\s*(${["\\*","\\-","\\+","\\d\\."].join("|")}))`:"",n=new RegExp(`^(${t.join("|")})+${r}`,"gm");return s.replace(n,"")}function H(s){return s.trimStart()}function G(s){for(;;){let t=s.replace(/([^|\n \t](?:[ \t]*\t)?) {2,}(?=(?:\t[ \t]*)?[^|\n \t])/gm,"$1 ");if(t==s)return s;s=t}}function j(s){for(;;){let t=s.replace(/([^|\n \t](?:[ \t]* )?)\t{2,}(?=(?: [ \t]*)?[^|\n \t])/gm,"$1 ");if(t==s)return s;s=t}}function z(s){return s.replace(/^\s+(?=(\n|\r|$))/gm,"")}function K(s,t){let e=s,i=" ",r=" ";if(t.TrimTrailingSpaces||t.TrimTrailingTabs){let n=[];t.TrimTrailingSpaces&&n.push(i),t.TrimTrailingTabs&&n.push(r),e=W(e,n)}if(t.TrimTrailingLines&&(e=$(e)),t.TrimLeadingSpaces||t.TrimLeadingTabs){let n=[];t.TrimLeadingSpaces&&n.push(i),t.TrimLeadingTabs&&n.push(r);let a=t.PreserveIndentedLists;e=F(e,n,a)}return t.TrimLeadingLines&&(e=H(e)),t.TrimMultipleSpaces&&(e=G(e)),t.TrimMultipleTabs&&(e=j(e)),t.TrimMultipleLines&&(e=z(e)),e}function h(s,t){let e=[],i=t.PreserveCodeBlocks,r="TRIM_WHITESPACE_REPLACE_",n=[new RegExp(/(`+)([\s\S]+?)\1/gm)];if(i){let o=D(s,r,n);s=o.text,e=o.terms}let a=K(s,t);return i&&(a=A(a,r,e)),a}function _(s,t,e){let i=[],r=e.exec(s);for(;r;)i.push(r),r=e.exec(s);if(i.length===0)return null;let n=i.find(a=>{let o=a.index,c=o+a[0].length;return t>=o&&t<=c});return n?{start:n.index,end:n.index+n[0].length}:null}function b(s,t,e){let i=/(?:\s?)```([\s\S]+?)```(?:\s?)/gm,r=/\s+/gm;if(e){let a=_(s,t,i);if(a)return{start:Math.max(a.start-1,0),end:a.end+1}}let n=_(s,t,r);return n?{start:n.start,end:n.end}:{start:t,end:t}}var X={TrimOnSave:!0,AutoTrimDocument:!0,AutoTrimTimeout:2.5,PreserveCodeBlocks:!0,PreserveIndentedLists:!0,TrimTrailingSpaces:!0,TrimLeadingSpaces:!1,TrimMultipleSpaces:!1,TrimTrailingTabs:!0,TrimLeadingTabs:!1,TrimMultipleTabs:!1,TrimTrailingLines:!0,TrimLeadingLines:!1,TrimMultipleLines:!1},d;(function(i){i[i.Command=0]="Command",i[i.Save=1]="Save",i[i.AutoTrim=2]="AutoTrim"})(d||(d={}));var v=class extends u.Plugin{onload(){return l(this,null,function*(){var i,r;yield this.loadSettings(),this._initializeDebouncer(this.settings.AutoTrimTimeout),this._toggleListenerEvent(this.settings.AutoTrimDocument),this.addRibbonIcon("unindent-glyph","Trim whitespace",n=>{n.shiftKey?this.trimSelection(0):this.trimDocument(0)}),this.addCommand({id:"trim-whitespace-selection",name:"Remove whitespace in selection",editorCallback:()=>this.trimSelection(0)}),this.addCommand({id:"trim-whitespace-document",name:"Remove whitespace in document",editorCallback:()=>this.trimDocument(0)}),this.addSettingTab(new L(this.app,this));let t=(r=(i=this.app.commands)==null?void 0:i.commands)==null?void 0:r["editor:save-file"],e=t==null?void 0:t.callback;typeof e=="function"&&(t.callback=()=>{this.settings.TrimOnSave&&this.trimDocument(1),e()})})}_getEditor(){let t=this.app.workspace.getActiveViewOfType(u.MarkdownView);return t?t.editor:null}_initializeDebouncer(t){this.debouncedTrim=(0,u.debounce)(()=>this.trimDocument(2),t*1e3,!0)}_toggleListenerEvent(t){if(!this.debouncedTrim){new u.Notice("Trim Whitespace: Can't start auto trimmer!");return}t?this.registerEvent(this.app.workspace.on("editor-change",this.debouncedTrim,this)):this.app.workspace.off("editor-change",this.debouncedTrim)}trimSelection(t){let e=this._getEditor();if(!e)return;let i=e.getSelection();if(i.length==0){new u.Notice("Select text to trim!");return}let r=h(i,this.settings);if(r==i)return;e.replaceSelection(r);let n=e.posToOffset(e.getCursor("to")),a=r.length,o=n-a;e.setSelection(e.offsetToPos(o),e.offsetToPos(n))}trimDocument(t){let e=this._getEditor();if(!e)return;let i=e.getValue(),r=e.getCursor("from"),n=e.posToOffset(r),a=e.getCursor("to"),o=e.posToOffset(a),c,m=n,w=o;if(t==2){let f=b(i,n,this.settings.PreserveCodeBlocks),S=b(i,o,this.settings.PreserveCodeBlocks),T=i.slice(0,f.start),p=h(T,this.settings),k=i.slice(f.start,S.end),y=i.slice(S.end),M=h(y,this.settings);c=p+k+M,m=n-T.length+p.length,w=o-T.length+p.length}else{c=h(i,this.settings);let f=i.slice(0,n),S=h(f,this.settings),T=i.slice(0,o),p=h(T,this.settings);m=S.length,w=p.length}c!=i&&(e.setValue(c),e.setSelection(e.offsetToPos(m),e.offsetToPos(w)))}loadSettings(){return l(this,null,function*(){this.settings=Object.assign({},X,yield this.loadData())})}saveSettings(){return l(this,null,function*(){yield this.saveData(this.settings)})}};0&&(module.exports={});
|