SmartBlocks icon indicating copy to clipboard operation
SmartBlocks copied to clipboard

Defer TODOs or any other block for X days later

Open GitMurf opened this issue 4 years ago • 1 comments

✂️ Copy of your #42SmartBlock from Roam

  • #42SmartBlock Defer<%NOCURSOR%>
    
    • <%NOBLOCKOUTPUT%><%SET:varTodayDNP,<%DATE:today%>%>
      
    • <%JAVASCRIPT:
      ```javascript
      
      var curSBname = roam42.smartBlocks.activeWorkflow.name;
      //Remove the {Deferrals} text from end
      var blockText = roam42.smartBlocks.activeWorkflow.startingBlockContents;
      var foundButton = false;
      
      if(blockText.indexOf('{Deferrals:') > -1){document.activeElement.value = blockText.substring(0,blockText.indexOf('{Deferrals:')).trim(); foundButton = true;}
      //document.activeElement.value = document.activeElement.value.trim();
      
      var rmDateFormatStr = roam42.smartBlocks.activeWorkflow.vars["varDefDate"];
      var usingDNP = false;
      if(!foundButton) //No button found which means no date variable set so need to parse out a DNP date
      {
        //Find the last date in the block
        var arrDnp = blockText.split(/(\[\[[^\]]*?\s[0-9]+(?:st|nd|rd|th),\s[0-9]{4}\]\])/);
        if(arrDnp.length > 1){var lastDate = arrDnp[arrDnp.length-2]}else{var lastDate = roam42.smartBlocks.activeWorkflow.vars["varTodayDNP"]; usingDNP = true;}
        rmDateFormatStr = lastDate.replace('[[','').replace(']]','').replace("st,","").replace("rd,","").replace("th,","").replace("nd,","");
      }
      
      //Set some variables
      var btnCounter = 0;
      var finalString = '';
      
      var rmDateParse = new Date(Date.parse(rmDateFormatStr));
      var howManyDays = prompt('\nHow many days to defer?\n\nOR pick random date:\n\ntw = this week\nnw = next week\nMonth and Year: tm, nm, ty, ny\n','1');
      var howManyDaysInt = parseInt(howManyDays);
      
      if(isNaN(howManyDaysInt)) //Random dates for this week, next week, etc.
      {
          switch(howManyDays) {
              //Sunday = 0, Saturday = 6
              case "tw":
                  var curDayOfWeek = rmDateParse.getDay();
                  if(curDayOfWeek == 6){curDayOfWeek = 0;}
                  howManyDaysInt = 1 + (Math.floor(Math.random() * Math.floor(6 - curDayOfWeek)));
                  break;
              case "nw":
                  var curDayOfWeek = rmDateParse.getDay();
                  howManyDaysInt = 6 - curDayOfWeek + 1 + (Math.floor(Math.random() * Math.floor(7)));
                  break;
              case "tm":
                  var curDayOfMonth = rmDateParse.getDate();
                  var endOfMonth = new Date(dayjs(rmDateParse).endOf('month').format('MMMM DD, YYYY 00:00')).getDate();
                  var endOfNextMonth = new Date(dayjs(rmDateParse).add(1,'month').endOf('month').format('MMMM DD, YYYY 00:00')).getDate();
                  if(curDayOfMonth == endOfMonth){curDayOfMonth = 1; endOfMonth = endOfNextMonth;}
                  howManyDaysInt = 1 + (Math.floor(Math.random() * Math.floor(endOfMonth - curDayOfMonth)));
                  break;
              case "nm":
                  var curDayOfMonth = rmDateParse.getDate();
                  var endOfMonth = new Date(dayjs(rmDateParse).endOf('month').format('MMMM DD, YYYY 00:00')).getDate();
                  var endOfNextMonth = new Date(dayjs(rmDateParse).add(1,'month').endOf('month').format('MMMM DD, YYYY 00:00')).getDate();
                  howManyDaysInt = endOfMonth - curDayOfMonth + 1 + (Math.floor(Math.random() * Math.floor(endOfNextMonth)));
                  break;
              case "ty":
                  var daysLeftInYear = dayjs().endOf('year').diff(dayjs(rmDateParse),'day');
                  howManyDaysInt = 1 + (Math.floor(Math.random() * Math.floor(daysLeftInYear)));
                  break;
              case "ny":
                  var daysLeftInYear = dayjs().endOf('year').diff(dayjs(rmDateParse),'day');
                  howManyDaysInt = 1 + daysLeftInYear + (Math.floor(Math.random() * Math.floor(365)));
                  break;
              default:
                  howManyDaysInt = 1;
                  break;
          }
      }
      
      var nextDate = new Date(rmDateParse.getFullYear(),rmDateParse.getMonth(), rmDateParse.getDate() + howManyDaysInt);
      var rmDateFormat = roam42.dateProcessing.parseTextForDates( dayjs(nextDate).format('MMMM DD, YYYY') ).trim();
      rmDateFormatStr = rmDateFormat.replace('[[','').replace(']]','').replace("st,","").replace("rd,","").replace("th,","").replace("nd,","");
      
      if(blockText.split(/({Deferrals:.*})/).length > 1)
      {
        var foundNum = parseInt(blockText.split(/({Deferrals:.*})/)[1].replace('{Deferrals:','').replace('}','').trim());
        if(Number.isInteger(foundNum))
        {
          btnCounter = foundNum;
        }
        btnCounter++;
        finalString = ', ' + rmDateFormat + '  {{Defer:42SmartBlock:' + curSBname + ':varDeferred=y,varDefDate=' + rmDateFormatStr + '}} {Deferrals: ' + btnCounter + '}';
      }
      else
      {
        btnCounter++;
        if(usingDNP){finalString = roam42.smartBlocks.activeWorkflow.vars["varTodayDNP"] + ' ';}
        finalString = finalString + '**Deferred To:** ' + rmDateFormat + '  {{Defer:42SmartBlock:' + curSBname + ':varDeferred=y,varDefDate=' + rmDateFormatStr + '}} {Deferrals: ' + btnCounter + '}';
      }
      
      roam42.smartBlocks.activeWorkflow.vars["varDeferred"] = 'n';
      roam42.smartBlocks.activeWorkflow.vars["varExitNow"] = 'y';
      
      return finalString;
      ```
      %>
      

📋 Describe the SmartBlock

Defer a TODO or any other block for X days. If there is no date in the block already, it will start at today's date and then defer X days from today. It tracks each deferral so you can see what the original date was and how many times you deferred it.

✅ Describe any prerequisites or dependencies that are required for this SmartBlock

Just roam42

📷 Screenshot of your #42SmartBlock workflow/template from Roam

image

💡 Additional Info

https://user-images.githubusercontent.com/64155612/104762030-8eb52580-5718-11eb-9e22-7c6e5060d99c.mp4

GitMurf avatar Jan 15 '21 18:01 GitMurf

Updated today to account for some tweaks made by @roamhacker with the SB engine!

GitMurf avatar Jan 19 '21 06:01 GitMurf