Files
obsidian-notif/main.js
T

11 lines
28 KiB
JavaScript
Raw Normal View History

2026-06-27 15:05:31 +08:00
/*
THIS IS A GENERATED/BUNDLED FILE BY ESBUILD
*/
2026-06-27 15:12:40 +08:00
var q=Object.defineProperty;var ee=Object.getOwnPropertyDescriptor;var te=Object.getOwnPropertyNames;var ie=Object.prototype.hasOwnProperty;var ne=(o,t)=>{for(var e in t)q(o,e,{get:t[e],enumerable:!0})},re=(o,t,e,i)=>{if(t&&typeof t=="object"||typeof t=="function")for(let r of te(t))!ie.call(o,r)&&r!==e&&q(o,r,{get:()=>t[r],enumerable:!(i=ee(t,r))||i.enumerable});return o};var se=o=>re(q({},"__esModule",{value:!0}),o);var ce={};ne(ce,{default:()=>B});module.exports=se(ce);var h=require("obsidian");var v=require("obsidian"),S=class extends v.Modal{constructor(e,i,r,n="",s){var d,c,l,p,g,y;super(e);this.defaultChannels=i;this.onSubmit=r;this.existing=s;this.title="";this.body="";this.year="";this.month="";this.day="";this.hour="";this.minute="";this.channels="";this.repeatFrequency="none";this.repeatInterval="1";let a=s?new Date(s.dueAt):new Date(Date.now()+60*60*1e3);this.title=(d=s==null?void 0:s.title)!=null?d:n,this.body=(c=s==null?void 0:s.body)!=null?c:n,this.channels=s?s.channels.join(", "):i.join(", "),this.repeatFrequency=(p=(l=s==null?void 0:s.repeat)==null?void 0:l.frequency)!=null?p:"none",this.repeatInterval=String((y=(g=s==null?void 0:s.repeat)==null?void 0:g.interval)!=null?y:1),this.year=String(a.getFullYear()),this.month=String(a.getMonth()+1).padStart(2,"0"),this.day=String(a.getDate()).padStart(2,"0"),this.hour=String(a.getHours()).padStart(2,"0"),this.minute=String(Math.ceil(a.getMinutes()/5)*5).padStart(2,"0"),this.minute==="60"&&(this.minute="00",this.hour=String((a.getHours()+1)%24).padStart(2,"0"))}onOpen(){let{contentEl:e}=this;e.empty(),e.createEl("h2",{text:this.existing?"Edit reminder":"Create reminder"}),this.renderReminderRow(e),new v.Setting(e).setName("Details").setDesc("Optional longer message.").addTextArea(i=>{i.setValue(this.body),i.inputEl.rows=3,i.onChange(r=>{this.body=r})}),new v.Setting(e).setName("Channels").setDesc("Comma separated provider IDs or types. Leave empty to use all enabled providers.").addText(i=>i.setValue(this.channels).onChange(r=>{this.channels=r})),new v.Setting(e).setName("Repeat").addDropdown(i=>i.addOption("none","None").addOption("daily","Daily").addOption("weekly","Weekly").addOption("monthly","Monthly").setValue(this.repeatFrequency).onChange(r=>{this.repeatFrequency=r})).addText(i=>i.setPlaceholder("Interval").setValue(this.repeatInterval).onChange(r=>{this.repeatInterval=r})),new v.Setting(e).addButton(i=>i.setButtonText(this.existing?"Save":"Create").setCta().onClick(async()=>{let r=this.getSelectedDate();if(!this.title.trim()||!r){new v.Notice("Title and valid due date are required.");return}await this.onSubmit({title:this.title.trim(),body:this.body.trim(),dueAt:r.toISOString(),channels:this.channels.split(",").map(n=>n.trim()).filter(Boolean),repeat:{frequency:this.repeatFrequency,interval:Math.max(1,Number(this.repeatInterval)||1)}}),this.close()}))}renderReminderRow(e){let i=new v.Setting(e).setName("Reminder").setDesc("Text, date, and time.");i.addText(r=>{r.setPlaceholder("Reminder text"),r.setValue(this.title),r.onChange(n=>{this.title=n,(!this.body||this.body===this.title)&&(this.body=n)})}),i.addDropdown(r=>{for(let n of this.range(new Date().getFullYear(),new Date().getFullYear()+3))r.addOption(String(n),String(n));r.setValue(this.year),r.onChange(n=>{this.year=n,this.ensureValidDay()})}),i.addDropdown(r=>{for(let n of this.range(1,12)){let s=String(n).padStart(2,"0");r.addOption(s,s)}r.setValue(this.month),r.onChange(n=>{this.month=n,this.ensureValidDay()})}),i.addDropdown(r=>{for(let n of this.range(1,31)){let s=String(n).padStart(2,"0");r.addOption(s,s)}r.setValue(this.day),r.onChange(n=>{this.day=n})}),i.addDropdown(r=>{for(let n of this.range(0,23)){let s=String(n).padStart(2,"0");r.addOption(s,s)}r.setValue(this.hour),r.onChange(n=>{this.hour=n})}),i.addDropdown(r=>{for(let n of this.range(0,55,5)){let s=String(n).padStart(2,"0");r.addOption(s,s)}r.setValue(this.minute),r.onChange(n=>{this.minute=n})})}getSelectedDate(){let e=new Date(`${this.year}-${this.month}-${this.day}T${this.hour}:${this.minute}:00`);return Number.isNaN(e.getTime()
`)))}};function H(o){let t=o.join("|"),e=0;for(let i=0;i<t.length;i+=1)e=(e<<5)-e+t.charCodeAt(i)|0;return`rem_${Math.abs(e).toString(36)}`}var T=[{id:"local",type:"local",name:"Local notice",enabled:!0,config:{}},{id:"telegram",type:"telegram",name:"Telegram Bot",enabled:!1,config:{botToken:"123456:ABCDEF_your_bot_token",chatId:"123456789",parseMode:"Markdown"}},{id:"ntfy",type:"ntfy",name:"ntfy.sh",enabled:!1,config:{serverUrl:"https://ntfy.sh",topic:"my-obsidian-reminders",token:"",priority:3,tags:["calendar","obsidian"]}},{id:"discord",type:"discord",name:"Discord Webhook",enabled:!1,config:{webhookUrl:"https://discord.com/api/webhooks/...",username:"Obsidian Reminder"}},{id:"webhook",type:"webhook",name:"Generic Webhook",enabled:!1,config:{method:"POST",url:"https://example.com/reminder",headers:{"Content-Type":"application/json"},bodyTemplate:{title:"{{title}}",message:"{{message}}",dueAt:"{{dueAt}}",sourceFile:"{{sourceFile}}"}}}],oe={defaultChannels:[],scanVaultOnStartup:!0,scanOnFileChange:!0,checkIntervalSeconds:60,afterSend:"mark-sent",markTaskCompleteOnSend:!1,reminderSyntax:"@remind(YYYY-MM-DD HH:mm, provider)",notificationProviders:[T[0]]};function I(){return JSON.parse(JSON.stringify(oe))}function J(o){let t=T.find(i=>i.type===o);if(!t)return null;let e=Date.now().toString(36);return{...t,id:`${t.type}-${e}`,config:JSON.parse(JSON.stringify(t.config))}}var z=2,x=class{constructor(t){this.plugin=t;this.data={dataVersion:z,settings:I(),reminders:[]}}async load(){var i,r,n,s,a;let t=await this.plugin.loadData(),e=I();this.data=this.migrate({dataVersion:(i=t==null?void 0:t.dataVersion)!=null?i:1,settings:{...e,...(r=t==null?void 0:t.settings)!=null?r:{},notificationProviders:(s=(n=t==null?void 0:t.settings)==null?void 0:n.notificationProviders)!=null?s:e.notificationProviders},reminders:(a=t==null?void 0:t.reminders)!=null?a:[]}),await this.save()}async save(){await this.plugin.saveData(this.data)}getSettings(){return this.data.settings}async updateSettings(t){this.data.settings=t,await this.save()}getAll(){return[...this.data.reminders].sort((t,e)=>t.dueAt.localeCompare(e.dueAt))}getDue(t=new Date){return this.data.reminders.filter(e=>(e.status==="pending"||e.status==="snoozed"||e.status==="failed")&&new Date(e.dueAt).getTime()<=t.getTime())}async add(t,e){var s,a,d,c;let i=new Date().toISOString(),r={id:H([(s=t.sourceFile)!=null?s:"manual",String((a=t.sourceLine)!=null?a:""),t.title,t.dueAt]),title:t.title,body:t.body,dueAt:t.dueAt,sourceFile:t.sourceFile,sourceLine:t.sourceLine,completed:!1,channels:t.channels.length?t.channels:e,priority:(d=t.priority)!=null?d:"normal",status:"pending",repeat:(c=t.repeat)!=null&&c.frequency&&t.repeat.frequency!=="none"?t.repeat:void 0,createdAt:i,updatedAt:i},n=this.data.reminders.find(l=>l.id===r.id);return n?(Object.assign(n,{...r,status:n.status,completed:n.completed,createdAt:n.createdAt,updatedAt:i}),await this.save(),n):(this.data.reminders.push(r),await this.save(),r)}async update(t,e){var r,n;let i=this.find(t);i&&(i.title=e.title,i.body=e.body,i.dueAt=e.dueAt,i.channels=e.channels,i.priority=(r=e.priority)!=null?r:"normal",i.repeat=(n=e.repeat)!=null&&n.frequency&&e.repeat.frequency!=="none"?e.repeat:void 0,i.status="pending",i.completed=!1,i.updatedAt=new Date().toISOString(),i.lastError=void 0,await this.save())}async addMany(t){let e=0;for(let i of t)await this.add(i,[]),e+=1;return e}async markSent(t){let e=this.find(t);if(!e)return;let i=U(e.dueAt,e.repeat);i?(e.dueAt=i,e.status="pending",e.completed=!1):(e.status=this.data.settings.afterSend==="mark-done"?"done":"sent",e.completed=this.data.settings.afterSend==="mark-done"),e.updatedAt=new Date().toISOString(),e.lastError=void 0,await this.save()}async markFailed(t,e){let i=this.find(t);i&&(i.status="failed",i.lastError=e instanceof Error?e.message:String(e),i.updatedAt=new Date().toISOString(),await this.save())}async complete(t){let e=this.find(t);e&&(e.completed=!0,e.status="done",e.updatedAt=new Date().toISOString(),await this.save())}async snooze(t,e){let i=this.find(t);i&&(i.dueAt=new Date(Date.now()+
${t.message}`}),throw:!0})}async test(t){await this.send({title:"Obsidian Reminder Notifier",message:"Test notification",dueAt:new Date().toISOString()},t)}};var _=require("obsidian"),$=class{constructor(){this.id="local";this.name="Local Obsidian notice"}async send(t){new _.Notice(`${t.title}
${t.message}`,1e4)}async test(t){new _.Notice("Reminder Notifier test notification",5e3)}};var G=require("obsidian");var F=class{constructor(){this.id="ntfy";this.name="ntfy"}async send(t,e){var d;let i=f(e.config.serverUrl,"https://ntfy.sh").replace(/\/$/,""),r=f(e.config.topic);if(!r)throw new Error("ntfy topic is required.");let n={Title:t.title,Priority:String((d=e.config.priority)!=null?d:de(t.priority))},s=f(e.config.token);s&&(n.Authorization=`Bearer ${s}`);let a=e.config.tags;Array.isArray(a)&&(n.Tags=a.join(",")),await(0,G.requestUrl)({url:`${i}/${encodeURIComponent(r)}`,method:"POST",headers:n,body:t.message,throw:!0})}async test(t){await this.send({title:"Obsidian Reminder Notifier",message:"Test notification",dueAt:new Date().toISOString()},t)}};function de(o="normal"){return o==="high"?4:o==="low"?2:3}var X=require("obsidian");var M=class{constructor(){this.id="telegram";this.name="Telegram"}async send(t,e){let i=f(e.config.botToken),r=f(e.config.chatId);if(!i||!r)throw new Error("Telegram botToken and chatId are required.");let n=`*${t.title}*
2026-06-27 15:05:31 +08:00
2026-06-27 15:12:40 +08:00
${t.message}`;await(0,X.requestUrl)({url:`https://api.telegram.org/bot${i}/sendMessage`,method:"POST",contentType:"application/json",body:JSON.stringify({chat_id:r,text:n,parse_mode:f(e.config.parseMode,"Markdown"),disable_web_page_preview:!0}),throw:!0})}async test(t){await this.send({title:"Obsidian Reminder Notifier",message:"Test notification",dueAt:new Date().toISOString()},t)}};var Q=require("obsidian");var L=class{constructor(){this.id="webhook";this.name="Generic webhook"}async send(t,e){var a,d;let i=f(e.config.url);if(!i)throw new Error("Webhook URL is required.");let r=f(e.config.method,"POST").toUpperCase();if(!["POST","PUT","PATCH"].includes(r))throw new Error("Webhook method must be POST, PUT, or PATCH.");let n=C((a=e.config.headers)!=null?a:{"Content-Type":"application/json"},t),s=C((d=e.config.bodyTemplate)!=null?d:{title:"{{title}}",message:"{{message}}",dueAt:"{{dueAt}}",sourceFile:"{{sourceFile}}"},t);await(0,Q.requestUrl)({url:i,method:r,headers:n,body:typeof s=="string"?s:JSON.stringify(s),throw:!0})}async test(t){await this.send({title:"Obsidian Reminder Notifier",message:"Test notification",dueAt:new Date().toISOString()},t)}};function Z(){let o=[new $,new M,new F,new L,new V];return new Map(o.map(t=>[t.id,t]))}var B=class extends h.Plugin{constructor(){super(...arguments);this.providers=Z()}async onload(){if(this.store=new x(this),await this.store.load(),this.scheduler=new O(this.app.vault,this.store,this.providers,()=>this.refreshReminderViews()),this.registerView(b,e=>new k(e,this)),this.addSettingTab(new E(this.app,this)),this.registerCommands(),this.registerRibbonActions(),this.registerContextMenus(),this.registerFileWatcher(),this.store.getSettings().scanVaultOnStartup){let e=await this.scheduler.scanVault();e>0&&new h.Notice(`Reminder Notifier indexed ${e} reminders.`)}this.scheduler.start()}onunload(){var e;(e=this.scheduler)==null||e.stop()}restartScheduler(){this.scheduler.stop(),this.scheduler.start()}async testProvider(e){let i=this.providers.get(e.type);if(!i)throw new Error(`Unknown provider: ${e.type}`);await i.test(e)}async activateReminderView(){let e=this.app.workspace.getLeavesOfType(b)[0];if(e){this.app.workspace.revealLeaf(e);return}let i=this.app.workspace.getRightLeaf(!1);i&&(await i.setViewState({type:b,active:!0}),this.app.workspace.revealLeaf(i))}refreshReminderViews(){for(let e of this.app.workspace.getLeavesOfType(b)){let i=e.view;i instanceof k&&i.refresh()}}registerCommands(){this.addCommand({id:"create-reminder",name:"Create reminder",callback:()=>{new S(this.app,this.store.getSettings().defaultChannels,async e=>{await this.store.add(e,this.store.getSettings().defaultChannels),this.refreshReminderViews(),new h.Notice("Reminder created.")}).open()}}),this.addCommand({id:"create-reminder-from-selection",name:"Create reminder from selected text",editorCallback:e=>{let i=e.getSelection();new S(this.app,this.store.getSettings().defaultChannels,async r=>{await this.store.add(r,this.store.getSettings().defaultChannels),this.refreshReminderViews(),new h.Notice("Reminder created.")},i).open()}}),this.addCommand({id:"show-active-reminders",name:"Show active reminders",callback:()=>{this.activateReminderView()}}),this.addCommand({id:"show-all-reminders",name:"Show all reminders",callback:()=>{this.activateReminderView()}}),this.addCommand({id:"scan-current-note",name:"Scan current note for reminders",checkCallback:e=>{let i=this.app.workspace.getActiveFile();return i?(e||this.scanCurrentFile(i),!0):!1}}),this.addCommand({id:"scan-vault",name:"Scan vault for reminders",callback:async()=>{let e=await this.scheduler.scanVault();new h.Notice(`Indexed ${e} reminders.`)}}),this.addCommand({id:"test-notification-providers",name:"Test notification providers",callback:async()=>{let e=this.store.getSettings().notificationProviders.filter(i=>i.enabled);for(let i of e)await this.testProvider(i);new h.Notice(`Tested ${e.length} providers.`)}})}registerRibbonActions(){this.addRibbonIcon("bell-plus","Create reminder",()=>{new S(this.app,this.store.getSettings().defaultChannels,async e=>{await