11import { AbstractInputField } from './AbstractInputField' ;
22import { DropdownComponent , TextComponent } from 'obsidian' ;
33import { InputFieldMarkdownRenderChild } from '../InputFieldMarkdownRenderChild' ;
4- import { Date , DateFormat , DateParser } from '../parsers/DateParser' ;
4+ import { DateParser } from '../parsers/DateParser' ;
55import { MetaBindInternalError } from '../utils/Utils' ;
6+ import { moment } from 'obsidian' ;
67
78export class DateInputField extends AbstractInputField {
89 container : HTMLDivElement | undefined ;
9- date : Date ;
10+ date : moment . Moment ;
1011
1112 months : Record < string , string > = {
12- '1 ' : 'January' ,
13- '2 ' : 'February' ,
14- '3 ' : 'March' ,
15- '4 ' : 'April' ,
16- '5 ' : 'May' ,
17- '6 ' : 'June' ,
18- '7 ' : 'July' ,
19- '8 ' : 'August' ,
20- '9 ' : 'September' ,
21- '10 ' : 'October' ,
22- '11 ' : 'November' ,
23- '12 ' : 'December' ,
13+ '0 ' : 'January' ,
14+ '1 ' : 'February' ,
15+ '2 ' : 'March' ,
16+ '3 ' : 'April' ,
17+ '4 ' : 'May' ,
18+ '5 ' : 'June' ,
19+ '6 ' : 'July' ,
20+ '7 ' : 'August' ,
21+ '8 ' : 'September' ,
22+ '9 ' : 'October' ,
23+ '10 ' : 'November' ,
24+ '11 ' : 'December' ,
2425 } ;
2526 days : Record < string , string > ;
2627
@@ -65,9 +66,9 @@ export class DateInputField extends AbstractInputField {
6566
6667 this . date = DateParser . parse ( value ) ?? DateParser . getDefaultDate ( ) ;
6768 // console.log(this.date);
68- this . monthComponent . setValue ( this . date . getMonth ( ) . toString ( ) ) ;
69- this . dayComponent . setValue ( this . date . getDay ( ) . toString ( ) ) ;
70- this . yearComponent . setValue ( this . date . getYear ( ) . toString ( ) ) ;
69+ this . monthComponent . setValue ( this . date . month ( ) . toString ( ) ) ;
70+ this . dayComponent . setValue ( this . date . date ( ) . toString ( ) ) ;
71+ this . yearComponent . setValue ( this . date . year ( ) . toString ( ) ) ;
7172 }
7273
7374 public isEqualValue ( value : any ) : boolean {
@@ -80,40 +81,41 @@ export class DateInputField extends AbstractInputField {
8081
8182 public render ( container : HTMLDivElement ) : void {
8283 this . date = DateParser . parse ( this . inputFieldMarkdownRenderChild . getInitialValue ( ) ) ?? DateParser . getDefaultDate ( ) ;
84+ let useUsInputOrder = this . inputFieldMarkdownRenderChild . plugin . settings . useUsDateInputOrder ;
8385
8486 container . removeClass ( 'meta-bind-plugin-input-wrapper' ) ;
8587 container . addClass ( 'meta-bind-plugin-flex-input-wrapper' , 'meta-bind-plugin-input-element-group' ) ;
8688
87- if ( DateParser . dateFormat === DateFormat . EU ) {
89+ if ( ! useUsInputOrder ) {
8890 this . dayComponent = new DropdownComponent ( container ) ;
8991 this . dayComponent . addOptions ( this . days ) ;
90- this . dayComponent . setValue ( this . date . getDay ( ) . toString ( ) ) ;
92+ this . dayComponent . setValue ( this . date . date ( ) . toString ( ) ) ;
9193 this . dayComponent . onChange ( this . onDayChange . bind ( this ) ) ;
9294
9395 this . monthComponent = new DropdownComponent ( container ) ;
9496 this . monthComponent . addOptions ( this . months ) ;
95- this . monthComponent . setValue ( this . date . getMonth ( ) . toString ( ) ) ;
97+ this . monthComponent . setValue ( this . date . month ( ) . toString ( ) ) ;
9698 this . monthComponent . onChange ( this . onMonthChange . bind ( this ) ) ;
9799
98100 this . dayComponent . selectEl . addClass ( 'meta-bind-plugin-input-element-group-element' ) ;
99101 this . monthComponent . selectEl . addClass ( 'meta-bind-plugin-input-element-group-element' ) ;
100102 } else {
101103 this . monthComponent = new DropdownComponent ( container ) ;
102104 this . monthComponent . addOptions ( this . months ) ;
103- this . monthComponent . setValue ( this . date . getMonth ( ) . toString ( ) ) ;
105+ this . monthComponent . setValue ( this . date . month ( ) . toString ( ) ) ;
104106 this . monthComponent . onChange ( this . onMonthChange . bind ( this ) ) ;
105107
106108 this . dayComponent = new DropdownComponent ( container ) ;
107109 this . dayComponent . addOptions ( this . days ) ;
108- this . dayComponent . setValue ( this . date . getDay ( ) . toString ( ) ) ;
110+ this . dayComponent . setValue ( this . date . date ( ) . toString ( ) ) ;
109111 this . dayComponent . onChange ( this . onDayChange . bind ( this ) ) ;
110112
111113 this . dayComponent . selectEl . addClass ( 'meta-bind-plugin-input-element-group-element' ) ;
112114 this . monthComponent . selectEl . addClass ( 'meta-bind-plugin-input-element-group-element' ) ;
113115 }
114116
115117 this . yearComponent = new TextComponent ( container ) ;
116- this . yearComponent . setValue ( this . date . getYear ( ) . toString ( ) ) ;
118+ this . yearComponent . setValue ( this . date . year ( ) . toString ( ) ) ;
117119 this . yearComponent . onChange ( this . onYearChange . bind ( this ) ) ;
118120
119121 this . yearComponent . inputEl . addClass ( 'meta-bind-plugin-date-input-year-input' ) ;
@@ -125,19 +127,41 @@ export class DateInputField extends AbstractInputField {
125127 }
126128
127129 private onMonthChange ( value : string ) : void {
128- // console.log(value);
129- this . date . setMonthFromString ( value ) ;
130+ this . date . month ( value ) ;
131+
132+ const clampedDay = this . clampDay ( this . date . date ( ) ) ;
133+ this . dayComponent ?. setValue ( clampedDay . toString ( ) ) ;
134+ this . date . date ( clampedDay ) ;
135+
130136 this . onValueChange ( this . getValue ( ) ) ;
131137 }
132138
133139 private onDayChange ( value : string ) : void {
134- this . date . setDayFromString ( value ) ;
140+ const day = Number . parseInt ( value ) ;
141+ const clampedDay = this . clampDay ( day ) ;
142+ if ( clampedDay !== day ) {
143+ this . dayComponent ?. setValue ( clampedDay . toString ( ) ) ;
144+ }
145+
146+ this . date . date ( clampedDay ) ;
135147 this . onValueChange ( this . getValue ( ) ) ;
136148 }
137149
138150 private onYearChange ( value : string ) : void {
139- this . date . setYearFromString ( value ) ;
151+ const year = Number . parseInt ( value ) ;
152+ this . date . year ( Number . isNaN ( year ) ? DateParser . getDefaultYear ( ) : year ) ;
140153 this . onValueChange ( this . getValue ( ) ) ;
141154 }
142155
156+ private clampDay ( day : number ) : number {
157+ if ( Number . isNaN ( day ) ) {
158+ return DateParser . getDefaultDay ( ) ;
159+ } else if ( day < 1 ) {
160+ return 1 ;
161+ } else if ( day > this . date . daysInMonth ( ) ) {
162+ return this . date . daysInMonth ( ) ;
163+ }
164+ return day ;
165+ }
166+
143167}
0 commit comments