Skip to content

Commit

Permalink
Resolve #22, support weeknum
Browse files Browse the repository at this point in the history
  • Loading branch information
leenzhu committed Oct 6, 2023
1 parent 82e74b1 commit 8260f4f
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 5 deletions.
4 changes: 3 additions & 1 deletion src/calendar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ const time_input = document.querySelector("#j_time") as HTMLInputElement
date_input.value = today
time_input.value = now
const date_ele = document.getElementById('datepicker')
const monday_first = date_ele.getAttribute('iso8601') === 'true';
const monday_first = date_ele.getAttribute('iso8601') === 'true'
const timeFmt:any = parseInt(date_ele.getAttribute('timeFmt'),10)
const theme:any = date_ele.getAttribute('theme')
const enableWeekNum = date_ele.getAttribute('weekNum') === 'true'
const calendar = new VanillaCalendar('#datepicker', {
actions: {
clickDay(e, dates) {
Expand All @@ -46,6 +47,7 @@ const calendar = new VanillaCalendar('#datepicker', {
},
visibility: {
theme: theme,
weekNumbers: enableWeekNum,
},
selection: {
time: timeFmt,
Expand Down
59 changes: 55 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@ const defaultNoteName = 'Journal/{{year}}/{{monthName}}/{{year}}-{{month}}-{{day
const defaultMonthName = '01-Jan,02-Feb,03-Mar,04-Apr,05-May,06-Jun,07-Jul,08-Aug,09-Sep,10-Oct,11-Nov,12-Dec';
const defaultWeekdayName = 'Sun,Mon,Tue,Wed,Thu,Fri,Sat';

function getWeek(d) {
var date = new Date(d.getTime());
date.setHours(0, 0, 0, 0);
// Thursday in current week decides the year.
date.setDate(date.getDate() + 3 - (date.getDay() + 6) % 7);
// January 4 is always in week 1.
var week1 = new Date(date.getFullYear(), 0, 4);
// Adjust to Thursday in week 1 and count number of weeks from date to week1.
return 1 + Math.round(((date.getTime() - week1.getTime()) / 86400000
- 3 + (week1.getDay() + 6) % 7) / 7);
}

function padding(s) {
return ('0' + s).slice(-2);
}
Expand Down Expand Up @@ -32,6 +44,7 @@ async function makeNoteName(d) {
const min = d.getMinutes();
const sec = d.getSeconds();
const weekday = d.getDay();
const weekNum = getWeek(d)

const noteTmpl = await joplin.settings.value('NoteTemplate') || defaultNoteName;

Expand All @@ -42,6 +55,7 @@ async function makeNoteName(d) {
const monthName = await joplin.settings.value('MonthName') || defaultMonthName;
const weekdayName = await joplin.settings.value('WeekdayName') || defaultWeekdayName;

const weekNumStyle = await joplin.settings.value('WeekNumStyle') || 'pad_num';
let monthNames = monthName.split(',');
if (monthNames.length != 12) {
monthNames = defaultMonthName.split(',');
Expand All @@ -53,7 +67,7 @@ async function makeNoteName(d) {
}

console.log(`Jouranl tmpl: ${noteTmpl}, monthStyle:${monthStyle}, dayStyle:${dayStyle}, weekdayStyle:${weekdayStyle}`);
let data = { year: '', month: '', monthName: '', day: '', hour: '', min: '', sec: '', weekday: '', weekdayName: '' };
let data = { year: '', month: '', monthName: '', day: '', hour: '', min: '', sec: '', weekday: '', weekdayName: '', weekNum:'' };
data.year = '' + year; // convert number to string
switch (monthStyle) {
case 'pad_num':
Expand Down Expand Up @@ -91,6 +105,19 @@ async function makeNoteName(d) {
break;
}

switch (weekNumStyle) {
case 'pad_num':
data.weekNum = padding(weekNum);
break;
case 'num':
data.weekNum = '' + (weekNum);
break;
default:
data.weekday = 'invalid';
break;

}

data.monthName = monthNames[month - 1];
data.weekdayName = weekdayNames[weekday];
data.hour = padding(hour);
Expand Down Expand Up @@ -168,9 +195,10 @@ joplin.plugins.register({
async function getDateByDialog() {
const iso8601 = await joplin.settings.value('iso8601');
const timeFmt = await joplin.settings.value('TimeFmt') || 0;
const theme = await joplin.settings.value('Theme') || "light";
const theme = await joplin.settings.value('Theme') || "light"
const enableWeekNum = await joplin.settings.value('WeekNum') || false

await dialogs.setHtml(dialog, `<form name="picker" ><div id="datepicker" iso8601=${iso8601} timeFmt=${timeFmt} theme=${theme}></div><input id="j_date" name="date" type="hidden"><input id="j_time" name="time" type="hidden"></form>`);
await dialogs.setHtml(dialog, `<form name="picker" ><div id="datepicker" iso8601=${iso8601} timeFmt=${timeFmt} theme=${theme} weekNum=${enableWeekNum}></div><input id="j_date" name="date" type="hidden"><input id="j_time" name="time" type="hidden"></form>`);
const ret = await dialogs.open(dialog);

if (ret.id == "ok") {
Expand Down Expand Up @@ -199,7 +227,7 @@ joplin.plugins.register({
section: 'Journal',
public: true,
label: 'Note Name Template',
description: `There are several variables: {{year}}, {{month}}, {{monthName}}, {{day}}, {{hour}}, {{min}}, {{weekday}}, {{weekdayName}}, which will expand into the actual value when opening or creating notes. The '/' character will create a hierarchical folder. The default vaule is: '${defaultNoteName}'.`
description: `There are several variables: {{year}}, {{month}}, {{monthName}}, {{day}}, {{hour}}, {{min}}, {{weekday}}, {{weekdayName}}, {{weekNum}}, which will expand into the actual value when opening or creating notes. The '/' character will create a hierarchical folder. The default vaule is: '${defaultNoteName}'.`
},

'MonthStyle': {
Expand Down Expand Up @@ -307,6 +335,29 @@ joplin.plugins.register({
},
description: "Change the theme of calendar",
},
'WeekNum': {
value: false,
type: SettingItemType.Bool,
section: 'Journal',
public: true,
advanced: true,
label: 'Enable week numbers',
description: "Show week numbers in calendar",
},
'WeekNumStyle': {
value: 'pad_num',
type: SettingItemType.String,
section: 'Journal',
isEnum: true,
public: true,
advanced: true,
label: 'WeekNum Style',
options: {
'pad_num': 'Padding number',
'num': 'Number',
},
description: "Padding number: 01, 02, ..., 06, 07, Number: 1, 2, ..., 6, 7."
},

});

Expand Down

0 comments on commit 8260f4f

Please sign in to comment.