From 1a7945b5fe63a305828cdf462f837dead2022f73 Mon Sep 17 00:00:00 2001 From: Vincent Batts Date: Mon, 20 Feb 2017 13:57:53 -0500 Subject: [PATCH] *: adding next and previous weeks --- extension.js | 54 ++++++++++++++++++++++++++++++++++++++++++++++------ package.json | 12 +++++++++++- 2 files changed, 59 insertions(+), 7 deletions(-) diff --git a/extension.js b/extension.js index 7242506..7b7530a 100644 --- a/extension.js +++ b/extension.js @@ -25,6 +25,15 @@ function activate(context) { }); })); + context.subscriptions.push(vscode.commands.registerTextEditorCommand('extension.nextNoteOpenPreviousWeek', function () { + try { + var basedir = getBasedir(); + } catch(e) { + vscode.window.showErrorMessage("next-note: failed to make directory: "+basedir); + return; + } + openNote(basedir +"/"+ formatWeekFilename(previousWeek())); + })); context.subscriptions.push(vscode.commands.registerTextEditorCommand('extension.nextNoteOpenCurrentWeek', function () { try { @@ -33,10 +42,17 @@ function activate(context) { vscode.window.showErrorMessage("next-note: failed to make directory: "+basedir); return; } + openNote(basedir +"/"+ formatWeekFilename(currentWeek())); + })); - var notepath = basedir +"/"+ currentWeekFilename(); - - openNote(notepath); + context.subscriptions.push(vscode.commands.registerTextEditorCommand('extension.nextNoteOpenNextWeek', function () { + try { + var basedir = getBasedir(); + } catch(e) { + vscode.window.showErrorMessage("next-note: failed to make directory: "+basedir); + return; + } + openNote(basedir +"/"+ formatWeekFilename(nextWeek())); })); } exports.activate = activate; @@ -110,10 +126,11 @@ function dateLine() { /** * get the base file name of the current week * + * @param {String} output of a function like `currentWeek()` * @returns {String} (i.e. "Tasks-20170219-20170225.md") */ -function currentWeekFilename() { - return "Tasks-"+currentWeek()+".md"; +function formatWeekFilename(week) { + return "Tasks-"+week+".md"; } /** @@ -128,5 +145,30 @@ function currentWeek() { d.setDate(beginDate.getDate()+6); // this is a week from that sunday return dateFormat(beginDate, "yyyymmdd")+"-"+dateFormat(d,"yyyymmdd"); - //return String(beginDate.getFullYear())+String(beginDate.getMonth())+String(beginDate.getDate()) +"-"+ String(d.getFullYear())+String(d.getMonth())+String(d.getDate()); +} + +/** + * the string of the previous week's date, sunday-saturday + * + * @return {String} (i.e. "20170219-20170225") + */ +function previousWeek() { + var d = new Date(); + var beginDate = new Date(); + beginDate.setDate(d.getDate()-d.getDay()-7); // This will be sunday of current previous week + d.setDate(beginDate.getDate()+6); // this is a week from that sunday + return dateFormat(beginDate, "yyyymmdd")+"-"+dateFormat(d,"yyyymmdd"); +} + +/** + * the string of the next week's date, sunday-saturday + * + * @return {String} (i.e. "20170219-20170225") + */ +function nextWeek() { + var d = new Date(); + var beginDate = new Date(); + beginDate.setDate(d.getDate()-d.getDay()+7); // This will be sunday of current next week + d.setDate(beginDate.getDate()+6); // this is a week from that sunday + return dateFormat(beginDate, "yyyymmdd")+"-"+dateFormat(d,"yyyymmdd"); } \ No newline at end of file diff --git a/package.json b/package.json index 45890cc..f10f0b0 100644 --- a/package.json +++ b/package.json @@ -16,15 +16,25 @@ ], "activationEvents": [ "onCommand:extension.nextNoteInsertDate", - "onCommand:extension.nextNoteOpenCurrentWeek" + "onCommand:extension.nextNoteOpenCurrentWeek", + "onCommand:extension.nextNoteOpenPreviousWeek", + "onCommand:extension.nextNoteOpenNextWeek" ], "main": "./extension", "contributes": { "commands": [ + { + "command": "extension.nextNoteOpenPreviousWeek", + "title": "NextNote: Open Current Week" + }, { "command": "extension.nextNoteOpenCurrentWeek", "title": "NextNote: Open Current Week" }, + { + "command": "extension.nextNoteOpenNextWeek", + "title": "NextNote: Open Current Week" + }, { "command": "extension.nextNoteInsertDate", "title": "NextNote: Date"