Advertisement
dhniceday

Untitled

Nov 27th, 2022
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <%*
  2. const oldPlannerHeader = "## Day Planner"
  3. const newPlannerHeader = "# Day Planner"
  4. const file = tp.file.find_tfile("Day Planner-" + tp.date.now("YYYYMMDD"))
  5.  
  6. var rxLogItem = /^([0-2][0-9]:[0-5][0-9] [a-zA-Z0-9]).*/
  7. var rxTaskItem = /^- \[[ |x]\] [0-2][0-9]:[0-5][0-9] [a-zA-Z0-9].*/
  8. var rxTime = /[0-2][0-9]:[0-5][0-9]/
  9. var insertPosition = -1
  10. var positionFound = -1
  11. var index = 0
  12.  
  13. function isEarlier(t1, t2) {
  14.     const r = t1 < t2 ? true : false
  15.     return r
  16. }
  17.  
  18. function isSame(t1, t2) {
  19.     const r = t1.toString() == t2.toString() ? true : false;
  20.     return r;
  21. }
  22.  
  23. async function initFirstUse() {
  24.     await app.vault.modify(file, newPlannerHeader)
  25.     //await executeCommand("ICS: import events")
  26. }
  27.  
  28. async function executeCommand(name) {
  29. // ICS plugin only works when file.name == YYYY-MM-DD
  30. // I will keep it for later
  31.  
  32.     const commandName = name;
  33.     const commands = app.commands.listCommands();
  34.     const collatorCompare = new Intl.Collator(["en"], {sensitivity: "base", usage: "search", ignorePunctuation: true}).compare;
  35.     const command = commands.find(c => collatorCompare(c.name, commandName) === 0);
  36.     await  app.commands.executeCommandById(command.id);
  37. }
  38.  
  39. if (file) {
  40.     logItem = await tp.system.prompt("Event for Day Planner: (HH:mm event)")
  41.  
  42.     if (rxLogItem.test(logItem)) {
  43.         const content = (await app.vault.read(file)).split("\n")
  44.         const firstUse = content.indexOf(oldPlannerHeader)
  45.  
  46.         if (firstUse >= 0) {
  47.             initFirstUse();
  48.             index = 0
  49.         } else {
  50.             const index = await  content.indexOf(newPlannerHeader)
  51.         }
  52.        
  53.         // Find positionItem
  54.         let timeLogItem = rxTime.exec(logItem)
  55.         do {
  56.             positionFound = -1;
  57.             index++
  58.             let row = content[index]
  59.            
  60.             if (rxTaskItem.test(row)) {
  61.                 let timeTask = rxTime.exec(row)
  62.                 if (isSame(timeLogItem, timeTask)) {
  63.                     new Notice("Entry already exists.")
  64.                     break;
  65.                 } else {
  66.                     if (isEarlier(timeLogItem, timeTask)) {
  67.                         positionFound = index
  68.                     }
  69.                 }
  70.             } else {
  71.                 positionFound = index
  72.                 break;
  73.             }
  74.  
  75.         }
  76.         while (positionFound < 0)
  77.  
  78.         console.log('positionFound: ' + positionFound)
  79.         if (positionFound > 0) {
  80.             // insert entry
  81.             let addContent = "- [ ] " + logItem
  82.             content.splice(positionFound, 0 , addContent)
  83.             await app.vault.modify(file, content.join("\n"))
  84.             console.log('Task ' + logItem + ' added ' + ' in file ' + file + ' at position ' + positionFound)
  85.         }
  86.     } else {
  87.         new Notice("Wrong task format (HH:MM event)")
  88.     }
  89. } else {
  90.     new Notice("No Day Planner note found.")
  91. }  
  92. -%>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement