-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add openDailyNote command and remove openTodayNote
- Loading branch information
Showing
10 changed files
with
279 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { commands, workspace } from 'vscode'; | ||
import moment from 'moment'; | ||
|
||
import openDailyNote from './openDailyNote'; | ||
import { closeEditorsAndCleanWorkspace } from '../test/testUtils'; | ||
|
||
describe('openDailyNote command', () => { | ||
beforeEach(closeEditorsAndCleanWorkspace); | ||
|
||
afterEach(closeEditorsAndCleanWorkspace); | ||
|
||
it('should not fail on direct call', async () => { | ||
expect(() => { | ||
openDailyNote(); | ||
}).not.toThrow(); | ||
|
||
await commands.executeCommand('workbench.action.acceptSelectedQuickOpenItem'); | ||
}); | ||
|
||
it("should open today's note", async () => { | ||
const today = moment().format('YYYY-MM-DD'); | ||
|
||
openDailyNote(); | ||
|
||
await commands.executeCommand('workbench.action.acceptSelectedQuickOpenItem'); | ||
|
||
const uris = await workspace.findFiles('**/*.md')!; | ||
|
||
expect(uris).toHaveLength(1); | ||
|
||
expect(uris[0].fsPath.endsWith(`${today}.md`)).toBe(true); | ||
}); | ||
|
||
it("should open tomorrow's note", async () => { | ||
const tomorrow = moment().add(1, 'day').format('YYYY-MM-DD'); | ||
|
||
openDailyNote(); | ||
|
||
await commands.executeCommand('workbench.action.quickOpenSelectNext'); | ||
await commands.executeCommand('workbench.action.acceptSelectedQuickOpenItem'); | ||
|
||
const uris = await workspace.findFiles('**/*.md')!; | ||
|
||
expect(uris).toHaveLength(1); | ||
|
||
expect(uris[0].fsPath.endsWith(`${tomorrow}.md`)).toBe(true); | ||
}); | ||
|
||
it("should open yesterday's note", async () => { | ||
const yesterday = moment().add(-1, 'day').format('YYYY-MM-DD'); | ||
|
||
openDailyNote(); | ||
|
||
await commands.executeCommand('workbench.action.quickOpenSelectNext'); | ||
await commands.executeCommand('workbench.action.quickOpenSelectNext'); | ||
await commands.executeCommand('workbench.action.acceptSelectedQuickOpenItem'); | ||
|
||
const uris = await workspace.findFiles('**/*.md')!; | ||
|
||
expect(uris).toHaveLength(1); | ||
|
||
expect(uris[0].fsPath.endsWith(`${yesterday}.md`)).toBe(true); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { commands } from 'vscode'; | ||
|
||
import { createDailyQuickPick } from '../utils'; | ||
|
||
const openDailyNote = () => { | ||
const dailyQuickPick = createDailyQuickPick(); | ||
|
||
dailyQuickPick.onDidChangeSelection((selection) => | ||
commands.executeCommand('_memo.openDocumentByReference', { | ||
reference: selection[0].detail, | ||
}), | ||
); | ||
|
||
dailyQuickPick.onDidHide(() => dailyQuickPick.dispose()); | ||
|
||
dailyQuickPick.show(); | ||
}; | ||
|
||
export default openDailyNote; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import moment from 'moment'; | ||
|
||
import createDailyQuickPick from './createDailyQuickPick'; | ||
import { closeEditorsAndCleanWorkspace, createFile } from '../test/testUtils'; | ||
|
||
describe('createDailyQuickPick()', () => { | ||
beforeEach(closeEditorsAndCleanWorkspace); | ||
|
||
afterEach(closeEditorsAndCleanWorkspace); | ||
|
||
it('should not fail on call', async () => { | ||
expect(createDailyQuickPick).not.toThrow(); | ||
}); | ||
|
||
it.each(['Today', 'Yesterday', 'Tomorrow'])( | ||
'should contain %s in the item label', | ||
(labelSubstr) => { | ||
const dailyQuickPick = createDailyQuickPick(); | ||
|
||
expect(dailyQuickPick.items.some((item) => item.label.includes(labelSubstr))).toBe(true); | ||
}, | ||
); | ||
|
||
it('should return 60 items (days) + 1 day (today)', () => { | ||
const dailyQuickPick = createDailyQuickPick(); | ||
|
||
expect(dailyQuickPick.items).toHaveLength(63); | ||
}); | ||
|
||
it('should contain an item with an indicator on note existence', async () => { | ||
const dateInYYYYMMDDFormat = moment().format('YYYY-MM-DD'); | ||
|
||
await createFile(`${dateInYYYYMMDDFormat}.md`); | ||
|
||
const dailyQuickPick = createDailyQuickPick(); | ||
|
||
const quickPickItem = dailyQuickPick.items.find((item) => item.description === 'Exists')!; | ||
|
||
expect(quickPickItem).not.toBeFalsy(); | ||
|
||
expect([...quickPickItem.label][0]).toMatchInlineSnapshot(`"✓"`); | ||
}); | ||
|
||
it('should return all items with an indicator about missing note', async () => { | ||
const dailyQuickPick = createDailyQuickPick(); | ||
|
||
expect(dailyQuickPick.items.every((item) => item.description === 'Missing')).toBe(true); | ||
|
||
expect([...dailyQuickPick.items[0].label][0]).toMatchInlineSnapshot(`"✕"`); | ||
}); | ||
|
||
it('should be able to provide items older than one month', async () => { | ||
await createFile('2000-01-01.md'); | ||
await createFile('2000-01-02.md'); | ||
await createFile('2000-01-03.md'); | ||
|
||
const dailyQuickPick = createDailyQuickPick(); | ||
|
||
const item1 = dailyQuickPick.items.find((item) => item.detail === '2000-01-01')!; | ||
const item2 = dailyQuickPick.items.find((item) => item.detail === '2000-01-02')!; | ||
const item3 = dailyQuickPick.items.find((item) => item.detail === '2000-01-03')!; | ||
|
||
expect(dailyQuickPick.items).toHaveLength(66); | ||
|
||
expect(item1).not.toBeFalsy(); | ||
expect(item2).not.toBeFalsy(); | ||
expect(item3).not.toBeFalsy(); | ||
}); | ||
|
||
it('should be able to provide items newer than one month', async () => { | ||
const now = moment(); | ||
|
||
const note1 = `${now.clone().add(60, 'days').format('YYYY-MM-DD')}`; | ||
const note2 = `${now.clone().add(61, 'days').format('YYYY-MM-DD')}`; | ||
const note3 = `${now.clone().add(62, 'days').format('YYYY-MM-DD')}`; | ||
|
||
await createFile(`${note1}.md`); | ||
await createFile(`${note2}.md`); | ||
await createFile(`${note3}.md`); | ||
|
||
const dailyQuickPick = createDailyQuickPick(); | ||
|
||
const item1 = dailyQuickPick.items.find((item) => item.detail === note1)!; | ||
const item2 = dailyQuickPick.items.find((item) => item.detail === note2)!; | ||
const item3 = dailyQuickPick.items.find((item) => item.detail === note3)!; | ||
|
||
expect(dailyQuickPick.items).toHaveLength(66); | ||
|
||
expect(item1).not.toBeFalsy(); | ||
expect(item2).not.toBeFalsy(); | ||
expect(item3).not.toBeFalsy(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import moment from 'moment'; | ||
import range from 'lodash.range'; | ||
import path from 'path'; | ||
import { window } from 'vscode'; | ||
|
||
import { findUriByRef, getWorkspaceCache } from './utils'; | ||
|
||
const toOffsetLabel = (dayOffset: number) => { | ||
if (dayOffset === -1) { | ||
return '-1 day Yesterday'; | ||
} else if (dayOffset === 0) { | ||
return 'Today'; | ||
} else if (dayOffset === 1) { | ||
return '+1 day Tomorrow'; | ||
} | ||
|
||
return `${dayOffset > 0 ? '+' : ''}${dayOffset} days`; | ||
}; | ||
|
||
const yyyymmddRegExp = /([12]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01]))/; | ||
|
||
const createQuickPick = () => { | ||
const now = moment().startOf('day'); | ||
const allDailyDates = getWorkspaceCache() | ||
.markdownUris.map((uri) => path.parse(uri.fsPath).name) | ||
.filter((name) => yyyymmddRegExp.exec(name) && moment(name).isValid()); | ||
const existingDayOffsets = allDailyDates.map((dateStr) => | ||
moment(dateStr).startOf('day').diff(now, 'days'), | ||
); | ||
const pastDayOffsets = existingDayOffsets | ||
.filter((dayOffset) => dayOffset <= -32) | ||
.sort((a, b) => b - a); | ||
const futureDayOffsets = existingDayOffsets | ||
.filter((dayOffset) => dayOffset >= 32) | ||
.sort((a, b) => a - b); | ||
|
||
const dayOffsets = [ | ||
0, // Today | ||
1, // Tomorrow | ||
-1, // Yesterday | ||
...range(2, 32), // Next month | ||
...futureDayOffsets, | ||
...range(-2, -32), // Prev month | ||
...pastDayOffsets, | ||
]; | ||
const quickPick = window.createQuickPick(); | ||
|
||
quickPick.matchOnDescription = true; | ||
quickPick.matchOnDetail = true; | ||
|
||
quickPick.items = dayOffsets.map((dayOffset) => { | ||
const date = now.clone().add(dayOffset, 'day'); | ||
const dateYYYYMMDD = date.format('YYYY-MM-DD'); | ||
const ref = findUriByRef(getWorkspaceCache().markdownUris, dateYYYYMMDD); | ||
|
||
return { | ||
label: `${ref ? '✓' : '✕'} ${toOffsetLabel(dayOffset)} | ${date.format( | ||
'dddd, MMMM D, YYYY', | ||
)}`, | ||
description: ref ? 'Exists' : 'Missing', | ||
detail: dateYYYYMMDD, | ||
}; | ||
}); | ||
|
||
return quickPick; | ||
}; | ||
|
||
export default createQuickPick; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.