@@ -6,13 +6,18 @@ import { toNumber } from 'lodash-es';
6
6
import { MatterData } from './types' ;
7
7
import { ConfirmCode , openConfirmModal } from './confirm-modal' ;
8
8
import { AbstractModal } from './abstract-modal' ;
9
+ import IMask , { DynamicMaskType , InputMask } from 'imask' ;
10
+ import { SafeAny } from './utils' ;
11
+ import { format , parse } from 'date-fns' ;
9
12
10
13
11
14
/**
12
15
* WordPress publish modal.
13
16
*/
14
17
export class WpPublishModal extends AbstractModal {
15
18
19
+ private dateInputMask : InputMask < DynamicMaskType > | null = null ;
20
+
16
21
constructor (
17
22
readonly plugin : WordpressPlugin ,
18
23
private readonly categories : {
@@ -46,6 +51,9 @@ export class WpPublishModal extends AbstractModal {
46
51
onClose ( ) {
47
52
const { contentEl } = this ;
48
53
contentEl . empty ( ) ;
54
+ if ( this . dateInputMask ) {
55
+ this . dateInputMask . destroy ( ) ;
56
+ }
49
57
}
50
58
51
59
private display ( params : WordPressPostParams ) : void {
@@ -61,19 +69,103 @@ export class WpPublishModal extends AbstractModal {
61
69
dropdown
62
70
. addOption ( PostStatus . Draft , this . t ( 'publishModal_postStatusDraft' ) )
63
71
. addOption ( PostStatus . Publish , this . t ( 'publishModal_postStatusPublish' ) )
64
- // .addOption(PostStatus.Future, 'future')
65
- . setValue ( this . plugin . settings . defaultPostStatus )
72
+ . addOption ( PostStatus . Private , this . t ( 'publishModal_postStatusPrivate' ) )
73
+ . addOption ( PostStatus . Future , this . t ( 'publishModal_postStatusFuture' ) )
74
+ . setValue ( params . status )
66
75
. onChange ( ( value ) => {
67
76
params . status = value as PostStatus ;
77
+ this . display ( params ) ;
68
78
} ) ;
69
79
} ) ;
80
+
81
+ if ( params . status === PostStatus . Future ) {
82
+ new Setting ( contentEl )
83
+ . setName ( this . t ( 'publishModal_postDateTime' ) )
84
+ . setDesc ( this . t ( 'publishModal_postDateTimeDesc' ) )
85
+ . addText ( text => {
86
+ const dateFormat = 'yyyy-MM-dd' ;
87
+ const dateTimeFormat = 'yyyy-MM-dd HH:mm:ss' ;
88
+ const dateBlocks = {
89
+ yyyy : {
90
+ mask : IMask . MaskedRange ,
91
+ from : 1970 ,
92
+ to : 9999 ,
93
+ } ,
94
+ MM : {
95
+ mask : IMask . MaskedRange ,
96
+ from : 1 ,
97
+ to : 12 ,
98
+ } ,
99
+ dd : {
100
+ mask : IMask . MaskedRange ,
101
+ from : 1 ,
102
+ to : 31 ,
103
+ } ,
104
+ } ;
105
+ const dateMask = {
106
+ mask : Date ,
107
+ lazy : false ,
108
+ overwrite : true ,
109
+ } ;
110
+ if ( this . dateInputMask ) {
111
+ this . dateInputMask . destroy ( ) ;
112
+ }
113
+ this . dateInputMask = IMask ( text . inputEl , [
114
+ {
115
+ ...dateMask ,
116
+ pattern : dateFormat ,
117
+ blocks : dateBlocks ,
118
+ format : ( date : SafeAny ) => format ( date , dateFormat ) ,
119
+ parse : ( str : string ) => parse ( str , dateFormat , new Date ( ) )
120
+ } ,
121
+ {
122
+ ...dateMask ,
123
+ pattern : dateTimeFormat ,
124
+ blocks : {
125
+ ...dateBlocks ,
126
+ HH : {
127
+ mask : IMask . MaskedRange ,
128
+ from : 0 ,
129
+ to : 23 ,
130
+ } ,
131
+ mm : {
132
+ mask : IMask . MaskedRange ,
133
+ from : 0 ,
134
+ to : 59 ,
135
+ } ,
136
+ ss : {
137
+ mask : IMask . MaskedRange ,
138
+ from : 0 ,
139
+ to : 59 ,
140
+ } ,
141
+ } ,
142
+ format : ( date : SafeAny ) => format ( date , dateTimeFormat ) ,
143
+ parse : ( str : string ) => parse ( str , dateTimeFormat , new Date ( ) )
144
+ }
145
+ ] ) ;
146
+
147
+ this . dateInputMask . on ( 'accept' , ( ) => {
148
+ if ( this . dateInputMask ) {
149
+ if ( this . dateInputMask . masked . isComplete ) {
150
+ text . inputEl . style . borderColor = '' ;
151
+ params . datetime = this . dateInputMask . typedValue ;
152
+ } else {
153
+ text . inputEl . style . borderColor = 'red' ;
154
+ }
155
+ }
156
+ } ) ;
157
+ } ) ;
158
+ } else {
159
+ delete params . datetime ;
160
+ }
161
+
70
162
new Setting ( contentEl )
71
163
. setName ( this . t ( 'publishModal_commentStatus' ) )
72
164
. addDropdown ( ( dropdown ) => {
73
165
dropdown
74
166
. addOption ( CommentStatus . Open , this . t ( 'publishModal_commentStatusOpen' ) )
75
167
. addOption ( CommentStatus . Closed , this . t ( 'publishModal_commentStatusClosed' ) )
76
- . setValue ( this . plugin . settings . defaultCommentStatus )
168
+ . setValue ( params . commentStatus )
77
169
. onChange ( ( value ) => {
78
170
params . commentStatus = value as CommentStatus ;
79
171
} ) ;
0 commit comments