Skip to content

Commit 675d3aa

Browse files
committed
date picker year input
1 parent 0f7a30a commit 675d3aa

File tree

6 files changed

+162
-88
lines changed

6 files changed

+162
-88
lines changed

exampleVault/.obsidian/plugins/obsidian-meta-bind-plugin/main.js

Lines changed: 91 additions & 63 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

exampleVault/other note.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ select: option b
44
multi-select:
55
- option b
66
- option c
7-
date: Thursday, September 22nd 2022
7+
date: Friday, September 10th 2004
88
time: 19:20
99
---

src/inputFields/DatePicker/Calender.svelte

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,13 @@
2727
}
2828
2929
.calendar-header {
30-
display: flex;
31-
flex-wrap: wrap;
32-
gap: 2px;
33-
background: var(--background-primary);
34-
border-radius: var(--meta-bind-plugin-border-radius);
35-
margin-bottom: 2px;
30+
display: flex;
31+
justify-content: space-around;
32+
flex-wrap: wrap;
33+
gap: 2px;
34+
background: var(--background-primary);
35+
border-radius: var(--meta-bind-plugin-border-radius);
36+
margin-bottom: 2px;
3637
}
3738
3839
.calendar-content {
@@ -43,7 +44,7 @@
4344
}
4445
4546
.cell {
46-
width: 40px;
47+
min-width: 40px;
4748
height: 30px;
4849
padding: 5px;
4950
display: flex;

src/inputFields/DatePicker/DatePicker.svelte

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@
1313
export let dateChangeCallback: (date: moment.Moment) => void;
1414
1515
// state
16-
let date;
17-
let month;
18-
let year;
19-
let showDatePicker;
16+
let date: number;
17+
let month: number;
18+
let year: number;
19+
let showDatePicker: boolean;
2020
2121
// so that these change with props
2222
$: {
23-
// console.log('update date picker', selectedDate);
23+
console.log('update date picker', selectedDate);
2424
date = selectedDate.date();
2525
month = selectedDate.month();
2626
year = selectedDate.year();
@@ -49,6 +49,14 @@
4949
month -= 1;
5050
}
5151
52+
function yearChange(value: any) {
53+
const v = value.target.value;
54+
const vNum = Number.parseInt(v);
55+
if (!Number.isNaN(vNum)) {
56+
year = vNum;
57+
}
58+
}
59+
5260
function onDateChange(d: { detail: moment.Moment }) {
5361
showDatePicker = false;
5462
selectedDate = d.detail;
@@ -79,7 +87,7 @@
7987
background: var(--background-secondary);
8088
border-radius: var(--meta-bind-plugin-border-radius);
8189
border: var(--meta-bind-plugin-border-width) solid var(--background-modifier-border);
82-
padding: 5px 10px;
90+
padding: 5px 5px 5px 7px;
8391
cursor: pointer;
8492
width: fit-content;
8593
display: inline-block;
@@ -96,13 +104,28 @@
96104
97105
.date-picker-header {
98106
display: flex;
99-
justify-content: center;
107+
gap: 5px;
100108
align-items: center;
109+
justify-content: space-around;
101110
}
102111
103112
.date-picker-header-text {
104-
flex: 1;
105-
text-align: center;
113+
flex: 1;
114+
text-align: center;
115+
display: flex;
116+
gap: 5px;
117+
align-items: center;
118+
justify-content: center;
119+
width: min-content;
120+
}
121+
122+
.date-picker-header-text-year {
123+
width: 60px;
124+
padding: 5px;
125+
}
126+
127+
.date-picker-header-text-month {
128+
height: min-content;
106129
}
107130
108131
.month-switch-button {
@@ -121,7 +144,9 @@
121144
<div class="date-picker-header">
122145
<button class="month-switch-button" on:click={prev}>Prev</button>
123146
<div class="date-picker-header-text">
124-
{getMonthName(month)} {year}
147+
<span class="date-picker-header-text-month">{getMonthName(month)}</span>
148+
<input class="date-picker-header-text-year" type="number" value="{year.toString()}"
149+
on:input="{yearChange}">
125150
</div>
126151
<button class="month-switch-button" on:click={next}>Next</button>
127152
</div>

src/inputFields/DatePicker/DatePickerInputSvelteHelpers.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,17 @@ export function getMonthName(index: number): string {
2121

2222
export function getDateRows(monthIndex: number, year: number): number[] {
2323
const days: number = moment(new Date(year, monthIndex)).daysInMonth(); // amount of days in month
24-
const rows: number[] = (new Array(42)).fill(undefined); // empty 42 long array
24+
let rows: number[] = (new Array(42)).fill(undefined); // empty 42 long array
2525
const startIndex: number = getWeekDay(new Date(year, monthIndex, 1)); // index offset based on weekday of first day in month
2626

2727
for (let i = 0; i < days; i++) {
2828
rows[i + startIndex] = i + 1;
2929
}
3030

31-
return rows[rows.length - 7] ? rows : rows.slice(0, -7);
31+
rows = rows[rows.length - 7] ? rows : rows.slice(0, -7); // slice last week if it is empty
32+
rows = rows[rows.length - 7] ? rows : rows.slice(0, -7); // slice last week if it is empty
33+
34+
return rows;
3235
}
3336

3437
// first day of the week is monday where I live

src/inputFields/DatePicker/Icon.svelte

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,35 @@
22
<!--adapted from @javalent's code: https://discord.com/channels/686053708261228577/840286264964022302/902949764209987654-->
33
<script lang="ts">
44
import {setIcon} from 'obsidian';
5+
import {onMount} from 'svelte';
56
6-
export let iconName: String = '';
7+
export let iconName: string = '';
8+
export let iconSize: number = 20;
79
8-
function icon(node: HTMLElement, icon: string) {
9-
setIcon(node, icon);
10-
}
10+
let iconEl: HTMLElement;
11+
12+
onMount(() => {
13+
setIcon(iconEl, iconName, iconSize);
14+
});
1115
</script>
1216

1317
<style>
18+
.icon-wrapper {
19+
display: inline-block;
20+
position: relative;
21+
width: 20px;
22+
}
1423
24+
.icon {
25+
position: absolute;
26+
height: 20px;
27+
width: 20px;
28+
bottom: -5px;
29+
}
1530
</style>
1631

1732
{#if iconName.length > 0}
18-
<span use:icon={iconName} style=""></span>
33+
<div class="icon-wrapper">
34+
<div bind:this={iconEl} class="icon"></div>
35+
</div>
1936
{/if}

0 commit comments

Comments
 (0)