Files
obsidian/.obsidian/plugins/obsidian-wordnet-plugin/main.js

6 lines
8.0 KiB
JavaScript

var w=Object.defineProperty;var y=Object.getOwnPropertyDescriptor;var N=Object.getOwnPropertyNames;var D=Object.prototype.hasOwnProperty;var C=(a,s)=>{for(var t in s)w(a,t,{get:s[t],enumerable:!0})},v=(a,s,t,e)=>{if(s&&typeof s=="object"||typeof s=="function")for(let o of N(s))!D.call(a,o)&&o!==t&&w(a,o,{get:()=>s[o],enumerable:!(e=y(s,o))||e.enumerable});return a};var x=a=>v(w({},"__esModule",{value:!0}),a);var r=(a,s,t)=>new Promise((e,o)=>{var i=d=>{try{g(t.next(d))}catch(S){o(S)}},n=d=>{try{g(t.throw(d))}catch(S){o(S)}},g=d=>d.done?e(d.value):Promise.resolve(d.value).then(i,n);g((t=t.apply(a,s)).next())});var W={};C(W,{default:()=>T});module.exports=x(W);var E=require("obsidian");var u=require("obsidian"),c=class extends u.EditorSuggest{constructor(t){super(t.app);this.plugin=t,this.updatePattern()}updatePattern(){this.pattern=new RegExp(`.*${this.plugin.settings.slashCommandShortcut}(.*)$`)}onTrigger(t,e,o){if(this.plugin.settings.slashCommandEnabled===!1)return;let i=e.getRange({line:t.line,ch:0},{line:t.line,ch:t.ch}),n=this.pattern.exec(i);if(n){let g=n[1];return this.lastEditorSuggestTriggerInfo={start:{line:t.line,ch:t.ch-g.length-this.plugin.settings.slashCommandShortcut.length},end:{line:t.line,ch:t.ch},query:n[1]},this.lastEditorSuggestTriggerInfo}else return null}getSuggestions(t){return this.plugin.dictionarySuggestor.query(t.query)}renderSuggestion(t,e){e.createEl("b",{text:t.Term}),e.createEl("br"),e.appendText(t.Definition)}selectSuggestion(t,e){let o=this.plugin.app.workspace.getActiveViewOfType(u.MarkdownView);this.close(),e.ctrlKey?(new u.Notice(t.Term+`
`+t.Definition,6e4),o.editor.replaceRange("",this.lastEditorSuggestTriggerInfo.start,this.lastEditorSuggestTriggerInfo.end)):o.editor.replaceRange(this.plugin.renderDefinitionFromTemplate(t.Term,t.Definition),this.lastEditorSuggestTriggerInfo.start,this.lastEditorSuggestTriggerInfo.end)}};var l=require("obsidian");var p=class extends l.FuzzySuggestModal{constructor(t){super(t.app);this.plugin=t,this.setPlaceholder("type word to lookup in WordNet"),setTimeout(()=>r(this,null,function*(){let e=this.plugin.manifest.dir+"/dict-WordNet.json",o=this.app.vault.adapter;if(yield o.exists(e)){let i=yield o.read(e);this.wordNet=yield JSON.parse(i)}else if(navigator.onLine===!1)new l.Notice("You do not have an internet connection, and the WordNet dictionary cannot be downloaded. Please restore your interent connection and resteart Obsidian",3e4),this.plugin.unload();else{let i=new l.Notice("WordNet dictionary is being downloaded, this may take a few minutes. This message will disappear when the process is complete.",0);try{let n=yield(0,l.request)({url:"https://github.com/TfTHacker/Obsidian-WordNet/releases/download/WordNetJson/dict-WordNet.json"});i.hide(),n==="Not Found"||n==='{"error":"Not Found"}'?(new l.Notice("The WordNet dictionary file is not currently available for download. Please try again later or contact the developer on Twitter: @TfThacker for support.",3e4),this.plugin.unload()):(this.wordNet=yield JSON.parse(n),yield o.write(e,JSON.stringify(this.wordNet)))}catch(n){console.log(`Error in WordNet dictinary: ${n}`),new l.Notice(`An error has occured with the download, please try again later: ${n}`),this.plugin.unload()}}if(yield o.exists(this.plugin.manifest.dir+"/dict-MyDict.json")){let i=yield o.read(this.plugin.manifest.dir+"/dict-MyDict.json");this.customDict=yield JSON.parse(i)}else this.customDict=null}),10)}query(t){let e=[],o=t.toLocaleLowerCase(),i=0;if(this.customDict!=null)for(let n=0;n<this.customDict.length&&i<30;n++)this.customDict[n].SearchTerm.startsWith(o)&&(e.push(this.customDict[n]),i++);i=0;for(let n=0;n<this.wordNet.length&&i<20;n++)this.wordNet[n].SearchTerm.startsWith(o)&&(e.push(this.wordNet[n]),i++);return e}getItems(){let t="";if(this.inputEl.value.trim().length==0){let e=this.app.workspace.getActiveViewOfType(l.MarkdownView);e!=null&&e.getMode()!=null&&e.editor.somethingSelected()&&(t=e.editor.getSelection(),this.inputEl.value=t,this.inputEl.setSelectionRange(0,t.length))}else t=this.inputEl.value.trim();return t===""?[]:this.query(t)}getItemText(t){return t.SearchTerm}onChooseItem(t,e){}renderSuggestion(t,e){e.createEl("b",{text:t.item.Term}),e.createEl("br"),e.appendText(t.item.Definition)}onChooseSuggestion(t,e){let o=this.plugin.app.workspace.getActiveViewOfType(l.MarkdownView);o!=null&&o.getMode()==="source"?o.editor.replaceSelection(this.plugin.renderDefinitionFromTemplate(t.item.Term,t.item.Definition)):new l.Notice(t.item.Term+`
`+t.item.Definition,1e4)}};var m=require("obsidian"),b=require("obsidian");var h={enableRibbon:!0,slashCommandEnabled:!0,slashCommandShortcut:";;",insertTemplate:`**{term}**
{definition}
`},f=class extends b.PluginSettingTab{constructor(t,e){super(t,e);this.plugin=e}display(){let{containerEl:t}=this;t.empty(),t.createEl("h2",{text:"Obsidian42 - WordNet Dictionary Setting"}),t.createEl("b",{text:"Ribbon Support"}),new m.Setting(t).setName("Enable Ribbon Support").setDesc("Toggle on and off the WordNet dictionary button in the ribbon.").addToggle(i=>{i.setValue(this.plugin.settings.enableRibbon),i.onChange(n=>r(this,null,function*(){this.plugin.settings.enableRibbon=n,this.plugin.settings.enableRibbon===!1?this.plugin.ribbonIcon.remove():this.plugin.configureRibbonCommand(),yield this.plugin.saveSettings()}))}),t.createEl("b",{text:"Slash Command"}),new m.Setting(t).setName("Enable the Slash Command").setDesc("Toggle on and off the slash command.").addToggle(i=>{i.setValue(this.plugin.settings.slashCommandEnabled),i.onChange(n=>r(this,null,function*(){this.plugin.settings.slashCommandEnabled=n,yield this.plugin.saveSettings()}))});let e;new m.Setting(t).setName("Slash Command Characters").setDesc("The characters that will invoke the slash command. The command character cannot be a space.").addExtraButton(i=>{i.setIcon("reset").setTooltip("Reset to default").onClick(()=>r(this,null,function*(){this.plugin.settings.slashCommandShortcut=h.slashCommandShortcut,yield this.plugin.saveSettings(),this.plugin.editSuggester.updatePattern(),e.setValue(this.plugin.settings.slashCommandShortcut)}))}).addText(i=>{e=i,i.setValue(this.plugin.settings.slashCommandShortcut),i.onChange(n=>r(this,null,function*(){let g=n.trim().length===0?h.slashCommandShortcut:n;this.plugin.settings.slashCommandShortcut=g,yield this.plugin.saveSettings(),this.plugin.editSuggester.updatePattern()}))}),t.createEl("b",{text:"Template"});let o;new m.Setting(t).setName("Template for inserting a definition").setDesc("The template used for inserting a WordNet definition. Use {term} for the term looked up and {definition} for the defintion of that term.").addExtraButton(i=>{i.setIcon("reset").setTooltip("Reset to default").onClick(()=>r(this,null,function*(){this.plugin.settings.insertTemplate=h.insertTemplate,yield this.plugin.saveSettings(),o.setValue(this.plugin.settings.insertTemplate)}))}).addTextArea(i=>{o=i,i.setValue(this.plugin.settings.insertTemplate),i.onChange(n=>r(this,null,function*(){let g=n.trim().length===0?h.insertTemplate:n;this.plugin.settings.insertTemplate=g,yield this.plugin.saveSettings()})),i.inputEl.rows=2,i.inputEl.cols=40})}};var T=class extends E.Plugin{configureRibbonCommand(){this.ribbonIcon=this.addRibbonIcon("book-open-check","WordNet Dictionary",()=>r(this,null,function*(){this.dictionarySuggestor.open()}))}onload(){return r(this,null,function*(){console.log("loading WordNet plugin"),yield this.loadSettings(),this.addSettingTab(new f(this.app,this)),this.dictionarySuggestor=new p(this),this.settings.enableRibbon&&this.configureRibbonCommand(),this.addCommand({id:"open-wordnet-suggestor",name:"Look up a word",callback:()=>{this.dictionarySuggestor.open()}}),this.editSuggester=new c(this),this.registerEditorSuggest(this.editSuggester)})}onunload(){console.log("unloading WordNet plugin")}renderDefinitionFromTemplate(t,e){return this.settings.insertTemplate.replace("{term}",t).replace("{definition}",e)}loadSettings(){return r(this,null,function*(){this.settings=Object.assign({},h,yield this.loadData())})}saveSettings(){return r(this,null,function*(){yield this.saveData(this.settings)})}};