gradio-pr-bot commited on
Commit
8d9770b
·
verified ·
1 Parent(s): b8a378a

Upload folder using huggingface_hub

Browse files
5.49.1/datetime/DateTimePicker.svelte ADDED
@@ -0,0 +1,433 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ import { createEventDispatcher } from "svelte";
3
+ import {
4
+ format_date,
5
+ generate_calendar_days,
6
+ calculate_display_hour,
7
+ convert_display_hour_to_24h,
8
+ month_names
9
+ } from "./utils";
10
+
11
+ export let selected_date: Date;
12
+ export let current_year: number;
13
+ export let current_month: number;
14
+ export let selected_hour: number;
15
+ export let selected_minute: number;
16
+ export let selected_second: number;
17
+ export let is_pm: boolean;
18
+ export let include_time: boolean;
19
+ export let position: { top: number; left: number };
20
+
21
+ const dispatch = createEventDispatcher<{
22
+ close: void;
23
+ clear: void;
24
+ update: { date: Date; formatted: string };
25
+ }>();
26
+
27
+ $: display_hour = calculate_display_hour(selected_hour, is_pm);
28
+ $: calendar_days = generate_calendar_days(current_year, current_month);
29
+
30
+ const select_date = (day: number): void => {
31
+ selected_date = new Date(
32
+ current_year,
33
+ current_month,
34
+ day,
35
+ selected_hour,
36
+ selected_minute,
37
+ selected_second
38
+ );
39
+ update_value();
40
+ };
41
+
42
+ const update_value = (): void => {
43
+ const formatted = format_date(selected_date, include_time);
44
+ dispatch("update", { date: selected_date, formatted });
45
+ };
46
+
47
+ const update_time = (): void => {
48
+ selected_date = new Date(
49
+ current_year,
50
+ current_month,
51
+ selected_date.getDate(),
52
+ selected_hour,
53
+ selected_minute,
54
+ selected_second
55
+ );
56
+ update_value();
57
+ };
58
+
59
+ const previous_month = (): void => {
60
+ if (current_month === 0) {
61
+ current_month = 11;
62
+ current_year--;
63
+ } else {
64
+ current_month--;
65
+ }
66
+ };
67
+
68
+ const next_month = (): void => {
69
+ if (current_month === 11) {
70
+ current_month = 0;
71
+ current_year++;
72
+ } else {
73
+ current_month++;
74
+ }
75
+ };
76
+
77
+ const toggle_am_pm = (): void => {
78
+ is_pm = !is_pm;
79
+ if (is_pm && selected_hour < 12) {
80
+ selected_hour += 12;
81
+ } else if (!is_pm && selected_hour >= 12) {
82
+ selected_hour -= 12;
83
+ }
84
+ update_time();
85
+ };
86
+
87
+ const update_display_hour = (new_hour: number): void => {
88
+ selected_hour = convert_display_hour_to_24h(new_hour, is_pm);
89
+ update_time();
90
+ };
91
+
92
+ const handle_now = (): void => {
93
+ const now = new Date();
94
+ selected_date = now;
95
+ current_year = now.getFullYear();
96
+ current_month = now.getMonth();
97
+ selected_hour = now.getHours();
98
+ selected_minute = now.getMinutes();
99
+ selected_second = now.getSeconds();
100
+ is_pm = selected_hour >= 12;
101
+ update_value();
102
+ };
103
+ </script>
104
+
105
+ <div
106
+ class="picker-container"
107
+ style="top: {position.top}px; left: {position.left}px;"
108
+ >
109
+ <div class="picker">
110
+ <div class="picker-header">
111
+ <button type="button" class="nav-button" on:click={previous_month}
112
+ >‹</button
113
+ >
114
+ <div class="month-year">
115
+ {month_names[current_month]}
116
+ {current_year}
117
+ </div>
118
+ <button type="button" class="nav-button" on:click={next_month}>›</button>
119
+ </div>
120
+
121
+ <div class="calendar-grid">
122
+ <div class="weekdays">
123
+ <div class="weekday">Su</div>
124
+ <div class="weekday">Mo</div>
125
+ <div class="weekday">Tu</div>
126
+ <div class="weekday">We</div>
127
+ <div class="weekday">Th</div>
128
+ <div class="weekday">Fr</div>
129
+ <div class="weekday">Sa</div>
130
+ </div>
131
+
132
+ <div class="days">
133
+ {#each calendar_days as { day, is_current_month, is_next_month }}
134
+ <button
135
+ type="button"
136
+ class="day"
137
+ class:other-month={!is_current_month}
138
+ class:selected={is_current_month &&
139
+ day === selected_date.getDate() &&
140
+ current_month === selected_date.getMonth() &&
141
+ current_year === selected_date.getFullYear()}
142
+ on:click={() => {
143
+ if (is_current_month) {
144
+ select_date(day);
145
+ } else if (is_next_month) {
146
+ next_month();
147
+ select_date(day);
148
+ } else {
149
+ previous_month();
150
+ select_date(day);
151
+ }
152
+ }}
153
+ >
154
+ {day}
155
+ </button>
156
+ {/each}
157
+ </div>
158
+ </div>
159
+
160
+ {#if include_time}
161
+ <div class="time-picker">
162
+ <div class="time-inputs">
163
+ <div class="time-input-group">
164
+ <label for="hour">Hour</label>
165
+ <input
166
+ id="hour"
167
+ type="number"
168
+ min="1"
169
+ max="12"
170
+ bind:value={display_hour}
171
+ on:input={() => update_display_hour(display_hour)}
172
+ />
173
+ </div>
174
+ <div class="time-input-group">
175
+ <label for="minute">Min</label>
176
+ <input
177
+ id="minute"
178
+ type="number"
179
+ min="0"
180
+ max="59"
181
+ bind:value={selected_minute}
182
+ on:input={update_time}
183
+ />
184
+ </div>
185
+ <div class="time-input-group">
186
+ <label for="second">Sec</label>
187
+ <input
188
+ id="second"
189
+ type="number"
190
+ min="0"
191
+ max="59"
192
+ bind:value={selected_second}
193
+ on:input={update_time}
194
+ />
195
+ </div>
196
+ <div class="time-input-group">
197
+ <span class="am-pm-label">Period</span>
198
+ <button
199
+ type="button"
200
+ class="am-pm-toggle"
201
+ on:click={toggle_am_pm}
202
+ aria-label="Toggle AM/PM"
203
+ >
204
+ {is_pm ? "PM" : "AM"}
205
+ </button>
206
+ </div>
207
+ </div>
208
+ </div>
209
+ {/if}
210
+
211
+ <div class="picker-actions">
212
+ <button
213
+ type="button"
214
+ class="action-button"
215
+ on:click={() => dispatch("clear")}
216
+ >
217
+ Clear
218
+ </button>
219
+ <div class="picker-actions-right">
220
+ <button type="button" class="action-button" on:click={handle_now}>
221
+ Now
222
+ </button>
223
+ <button
224
+ type="button"
225
+ class="action-button"
226
+ on:click={() => dispatch("close")}
227
+ >
228
+ Done
229
+ </button>
230
+ </div>
231
+ </div>
232
+ </div>
233
+ </div>
234
+
235
+ <style>
236
+ .picker-container {
237
+ position: fixed;
238
+ z-index: 9999;
239
+ box-shadow: var(--shadow-drop-lg);
240
+ border-radius: var(--radius-lg);
241
+ background: var(--background-fill-primary);
242
+ border: 1px solid var(--border-color-primary);
243
+ }
244
+
245
+ .picker {
246
+ padding: var(--size-3);
247
+ min-width: 280px;
248
+ }
249
+
250
+ .picker-header {
251
+ display: flex;
252
+ justify-content: space-between;
253
+ align-items: center;
254
+ margin-bottom: var(--size-3);
255
+ }
256
+
257
+ .nav-button {
258
+ background: none;
259
+ border: none;
260
+ font-size: var(--text-lg);
261
+ padding: var(--size-1);
262
+ border-radius: var(--radius-sm);
263
+ transition: var(--button-transition);
264
+ cursor: pointer;
265
+ color: var(--body-text-color-subdued);
266
+ }
267
+
268
+ .nav-button:hover {
269
+ background: var(--button-secondary-background-fill-hover);
270
+ color: var(--body-text-color);
271
+ }
272
+
273
+ .month-year {
274
+ font-weight: var(--weight-semibold);
275
+ font-size: var(--text-base);
276
+ color: var(--body-text-color);
277
+ }
278
+
279
+ .calendar-grid {
280
+ margin-bottom: var(--size-3);
281
+ }
282
+
283
+ .weekdays {
284
+ display: grid;
285
+ grid-template-columns: repeat(7, 1fr);
286
+ gap: 1px;
287
+ margin-bottom: var(--size-2);
288
+ }
289
+
290
+ .weekday {
291
+ text-align: center;
292
+ font-size: var(--text-sm);
293
+ font-weight: var(--weight-semibold);
294
+ color: var(--body-text-color-subdued);
295
+ padding: var(--size-1);
296
+ }
297
+
298
+ .days {
299
+ display: grid;
300
+ grid-template-columns: repeat(7, 1fr);
301
+ gap: 1px;
302
+ }
303
+
304
+ .day {
305
+ aspect-ratio: 1;
306
+ border: none;
307
+ background: none;
308
+ border-radius: var(--radius-sm);
309
+ font-size: var(--text-sm);
310
+ transition: var(--button-transition);
311
+ color: var(--body-text-color);
312
+ cursor: pointer;
313
+ }
314
+
315
+ .day:hover {
316
+ background: var(--button-secondary-background-fill-hover);
317
+ }
318
+
319
+ .day.other-month {
320
+ color: var(--body-text-color-subdued);
321
+ }
322
+
323
+ .day.selected {
324
+ background: var(--button-primary-background-fill);
325
+ color: var(--button-primary-text-color);
326
+ }
327
+
328
+ .day.selected:hover {
329
+ background: var(--button-primary-background-fill-hover);
330
+ }
331
+
332
+ .time-picker {
333
+ border-top: 1px solid var(--border-color-primary);
334
+ padding-top: var(--size-3);
335
+ margin-bottom: var(--size-3);
336
+ }
337
+
338
+ .time-inputs {
339
+ display: flex;
340
+ gap: var(--size-2);
341
+ justify-content: center;
342
+ }
343
+
344
+ .time-input-group {
345
+ display: flex;
346
+ flex-direction: column;
347
+ align-items: center;
348
+ gap: var(--size-1);
349
+ }
350
+
351
+ .time-input-group label {
352
+ font-size: var(--text-xs);
353
+ color: var(--body-text-color-subdued);
354
+ font-weight: var(--weight-semibold);
355
+ }
356
+
357
+ .am-pm-label {
358
+ font-size: var(--text-xs);
359
+ color: var(--body-text-color-subdued);
360
+ font-weight: var(--weight-semibold);
361
+ }
362
+
363
+ .time-input-group input {
364
+ width: 50px;
365
+ padding: var(--size-1);
366
+ border: 1px solid var(--input-border-color);
367
+ border-radius: var(--radius-sm);
368
+ text-align: center;
369
+ font-size: var(--text-sm);
370
+ background: var(--input-background-fill);
371
+ color: var(--body-text-color);
372
+ }
373
+
374
+ .time-input-group input:focus {
375
+ outline: none;
376
+ border-color: var(--input-border-color-focus);
377
+ box-shadow: var(--input-shadow-focus);
378
+ }
379
+
380
+ .am-pm-toggle {
381
+ width: 50px;
382
+ padding: var(--size-1);
383
+ border: 1px solid var(--button-primary-border-color);
384
+ border-radius: var(--radius-sm);
385
+ text-align: center;
386
+ font-size: var(--text-sm);
387
+ background: var(--button-primary-background-fill);
388
+ color: var(--button-primary-text-color);
389
+ cursor: pointer;
390
+ transition: var(--button-transition);
391
+ }
392
+
393
+ .am-pm-toggle:hover {
394
+ background: var(--button-primary-background-fill-hover);
395
+ border-color: var(--button-primary-border-color-hover);
396
+ }
397
+
398
+ .am-pm-toggle:focus {
399
+ outline: none;
400
+ border-color: var(--button-primary-border-color-focus);
401
+ box-shadow: var(--button-primary-shadow-focus);
402
+ }
403
+
404
+ .picker-actions {
405
+ display: flex;
406
+ gap: var(--size-2);
407
+ justify-content: space-between;
408
+ align-items: center;
409
+ border-top: 1px solid var(--border-color-primary);
410
+ padding-top: var(--size-3);
411
+ }
412
+
413
+ .picker-actions-right {
414
+ display: flex;
415
+ gap: var(--size-2);
416
+ }
417
+
418
+ .action-button {
419
+ padding: var(--size-1) var(--size-3);
420
+ border: 1px solid var(--button-secondary-border-color);
421
+ border-radius: var(--radius-sm);
422
+ background: var(--button-secondary-background-fill);
423
+ color: var(--button-secondary-text-color);
424
+ font-size: var(--text-sm);
425
+ transition: var(--button-transition);
426
+ cursor: pointer;
427
+ }
428
+
429
+ .action-button:hover {
430
+ background: var(--button-secondary-background-fill-hover);
431
+ border-color: var(--button-secondary-border-color-hover);
432
+ }
433
+ </style>
5.49.1/datetime/Example.svelte ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ export let value: string | null;
3
+ </script>
4
+
5
+ {value || ""}
5.49.1/datetime/Index.svelte ADDED
@@ -0,0 +1,298 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script context="module" lang="ts">
2
+ export { default as BaseExample } from "./Example.svelte";
3
+ </script>
4
+
5
+ <script lang="ts">
6
+ import type { Gradio } from "@gradio/utils";
7
+ import { Block, BlockTitle } from "@gradio/atoms";
8
+ import { Calendar } from "@gradio/icons";
9
+ import { onDestroy } from "svelte";
10
+ import DateTimePicker from "./DateTimePicker.svelte";
11
+ import { format_date, date_is_valid_format, parse_date_value } from "./utils";
12
+
13
+ export let gradio: Gradio<{
14
+ change: undefined;
15
+ submit: undefined;
16
+ }>;
17
+ export let label = "Time";
18
+ export let show_label = true;
19
+ export let info: string | undefined = undefined;
20
+ export let interactive: boolean;
21
+ $: disabled = !interactive;
22
+ export let elem_id = "";
23
+ export let elem_classes: string[] = [];
24
+ export let visible: boolean | "hidden" = true;
25
+ export let value = "";
26
+ let old_value = value;
27
+ export let scale: number | null = null;
28
+ export let min_width: number | undefined = undefined;
29
+ export let include_time = true;
30
+
31
+ let show_picker = false;
32
+ let picker_ref: HTMLDivElement;
33
+ let input_ref: HTMLInputElement;
34
+ let calendar_button_ref: HTMLButtonElement;
35
+ let picker_position = { top: 0, left: 0 };
36
+
37
+ $: if (value !== old_value) {
38
+ old_value = value;
39
+ entered_value = value;
40
+ update_picker_from_value();
41
+ gradio.dispatch("change");
42
+ }
43
+
44
+ let entered_value = value;
45
+
46
+ $: valid = date_is_valid_format(entered_value, include_time);
47
+
48
+ const submit_values = (): void => {
49
+ if (entered_value === value) return;
50
+ if (!date_is_valid_format(entered_value, include_time)) return;
51
+ old_value = value = entered_value;
52
+ gradio.dispatch("change");
53
+ };
54
+
55
+ let current_year = new Date().getFullYear();
56
+ let current_month = new Date().getMonth();
57
+ let selected_date = new Date();
58
+ let selected_hour = new Date().getHours();
59
+ let selected_minute = new Date().getMinutes();
60
+ let selected_second = new Date().getSeconds();
61
+ let is_pm = selected_hour >= 12;
62
+
63
+ const update_picker_from_value = (): void => {
64
+ const parsed = parse_date_value(entered_value, include_time);
65
+ selected_date = parsed.selected_date;
66
+ current_year = parsed.current_year;
67
+ current_month = parsed.current_month;
68
+ selected_hour = parsed.selected_hour;
69
+ selected_minute = parsed.selected_minute;
70
+ selected_second = parsed.selected_second;
71
+ is_pm = parsed.is_pm;
72
+ };
73
+
74
+ const calculate_picker_position = (): void => {
75
+ if (calendar_button_ref) {
76
+ const rect = calendar_button_ref.getBoundingClientRect();
77
+ picker_position = {
78
+ top: rect.bottom + 4,
79
+ left: rect.right - 280
80
+ };
81
+ }
82
+ };
83
+
84
+ const toggle_picker = (event: MouseEvent): void => {
85
+ if (!disabled) {
86
+ event.stopPropagation();
87
+ show_picker = !show_picker;
88
+ if (show_picker) {
89
+ update_picker_from_value();
90
+ calculate_picker_position();
91
+ setTimeout(() => {
92
+ if (typeof window !== "undefined") {
93
+ window.addEventListener("click", handle_click_outside);
94
+ window.addEventListener("scroll", handle_scroll, true);
95
+ }
96
+ }, 10);
97
+ } else if (typeof window !== "undefined") {
98
+ window.removeEventListener("click", handle_click_outside);
99
+ window.removeEventListener("scroll", handle_scroll, true);
100
+ }
101
+ }
102
+ };
103
+
104
+ const close_picker = (): void => {
105
+ show_picker = false;
106
+ if (typeof window !== "undefined") {
107
+ window.removeEventListener("click", handle_click_outside);
108
+ window.removeEventListener("scroll", handle_scroll, true);
109
+ }
110
+ };
111
+
112
+ const handle_click_outside = (event: MouseEvent): void => {
113
+ if (
114
+ show_picker &&
115
+ picker_ref &&
116
+ !picker_ref.contains(event.target as Node) &&
117
+ calendar_button_ref &&
118
+ !calendar_button_ref.contains(event.target as Node)
119
+ ) {
120
+ close_picker();
121
+ }
122
+ };
123
+
124
+ const handle_scroll = (): void => {
125
+ if (show_picker) {
126
+ calculate_picker_position();
127
+ }
128
+ };
129
+
130
+ const handle_picker_update = (
131
+ event: CustomEvent<{ date: Date; formatted: string }>
132
+ ): void => {
133
+ entered_value = event.detail.formatted;
134
+ submit_values();
135
+ };
136
+
137
+ const handle_picker_clear = (): void => {
138
+ entered_value = "";
139
+ value = "";
140
+ close_picker();
141
+ gradio.dispatch("change");
142
+ };
143
+
144
+ onDestroy(() => {
145
+ if (typeof window !== "undefined") {
146
+ window.removeEventListener("click", handle_click_outside);
147
+ window.removeEventListener("scroll", handle_scroll, true);
148
+ }
149
+ });
150
+
151
+ update_picker_from_value();
152
+ </script>
153
+
154
+ <Block
155
+ {visible}
156
+ {elem_id}
157
+ {elem_classes}
158
+ {scale}
159
+ {min_width}
160
+ allow_overflow={false}
161
+ padding={true}
162
+ >
163
+ <div class="label-content">
164
+ <BlockTitle {show_label} {info}>{label}</BlockTitle>
165
+ </div>
166
+ <div class="timebox">
167
+ <input
168
+ bind:this={input_ref}
169
+ class="time"
170
+ bind:value={entered_value}
171
+ class:invalid={!valid}
172
+ on:keydown={(evt) => {
173
+ if (evt.key === "Enter") {
174
+ submit_values();
175
+ gradio.dispatch("submit");
176
+ }
177
+ }}
178
+ on:blur={submit_values}
179
+ {disabled}
180
+ placeholder={include_time ? "YYYY-MM-DD HH:MM:SS" : "YYYY-MM-DD"}
181
+ />
182
+
183
+ {#if interactive}
184
+ <button
185
+ bind:this={calendar_button_ref}
186
+ class="calendar"
187
+ {disabled}
188
+ on:click={toggle_picker}
189
+ >
190
+ <Calendar />
191
+ </button>
192
+ {/if}
193
+ </div>
194
+
195
+ {#if show_picker}
196
+ <div bind:this={picker_ref}>
197
+ <DateTimePicker
198
+ bind:selected_date
199
+ bind:current_year
200
+ bind:current_month
201
+ bind:selected_hour
202
+ bind:selected_minute
203
+ bind:selected_second
204
+ bind:is_pm
205
+ {include_time}
206
+ position={picker_position}
207
+ on:update={handle_picker_update}
208
+ on:clear={handle_picker_clear}
209
+ on:close={close_picker}
210
+ />
211
+ </div>
212
+ {/if}
213
+ </Block>
214
+
215
+ <style>
216
+ .label-content {
217
+ display: flex;
218
+ justify-content: space-between;
219
+ align-items: flex-start;
220
+ }
221
+
222
+ button {
223
+ cursor: pointer;
224
+ color: var(--body-text-color-subdued);
225
+ }
226
+
227
+ button:hover {
228
+ color: var(--body-text-color);
229
+ }
230
+
231
+ ::placeholder {
232
+ color: var(--input-placeholder-color);
233
+ }
234
+
235
+ .timebox {
236
+ flex-grow: 1;
237
+ flex-shrink: 1;
238
+ display: flex;
239
+ position: relative;
240
+ background: var(--input-background-fill);
241
+ }
242
+
243
+ .timebox :global(svg) {
244
+ height: 18px;
245
+ }
246
+
247
+ .time {
248
+ padding: var(--input-padding);
249
+ color: var(--body-text-color);
250
+ font-weight: var(--input-text-weight);
251
+ font-size: var(--input-text-size);
252
+ line-height: var(--line-sm);
253
+ outline: none;
254
+ flex-grow: 1;
255
+ background: none;
256
+ border: var(--input-border-width) solid var(--input-border-color);
257
+ border-right: none;
258
+ border-top-left-radius: var(--input-radius);
259
+ border-bottom-left-radius: var(--input-radius);
260
+ box-shadow: var(--input-shadow);
261
+ }
262
+
263
+ .time:disabled {
264
+ border-right: var(--input-border-width) solid var(--input-border-color);
265
+ border-top-right-radius: var(--input-radius);
266
+ border-bottom-right-radius: var(--input-radius);
267
+ }
268
+
269
+ .time.invalid {
270
+ color: var(--body-text-color-subdued);
271
+ }
272
+
273
+ .calendar {
274
+ display: inline-flex;
275
+ justify-content: center;
276
+ align-items: center;
277
+ transition: var(--button-transition);
278
+ box-shadow: var(--button-primary-shadow);
279
+ text-align: center;
280
+ background: var(--button-secondary-background-fill);
281
+ color: var(--button-secondary-text-color);
282
+ font-weight: var(--button-large-text-weight);
283
+ font-size: var(--button-large-text-size);
284
+ border-top-right-radius: var(--input-radius);
285
+ border-bottom-right-radius: var(--input-radius);
286
+ padding: var(--size-2);
287
+ border: var(--input-border-width) solid var(--input-border-color);
288
+ }
289
+
290
+ .calendar:hover {
291
+ background: var(--button-secondary-background-fill-hover);
292
+ box-shadow: var(--button-primary-shadow-hover);
293
+ }
294
+
295
+ .calendar:active {
296
+ box-shadow: var(--button-primary-shadow-active);
297
+ }
298
+ </style>
5.49.1/datetime/package.json ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "@gradio/datetime",
3
+ "version": "0.3.22",
4
+ "description": "Gradio UI packages",
5
+ "type": "module",
6
+ "author": "",
7
+ "license": "ISC",
8
+ "private": false,
9
+ "main_changeset": true,
10
+ "main": "Index.svelte",
11
+ "exports": {
12
+ ".": {
13
+ "gradio": "./Index.svelte",
14
+ "svelte": "./dist/Index.svelte",
15
+ "types": "./dist/Index.svelte.d.ts"
16
+ },
17
+ "./example": {
18
+ "gradio": "./Example.svelte",
19
+ "svelte": "./dist/Example.svelte",
20
+ "types": "./dist/Example.svelte.d.ts"
21
+ },
22
+ "./package.json": "./package.json"
23
+ },
24
+ "dependencies": {
25
+ "@gradio/atoms": "workspace:^",
26
+ "@gradio/icons": "workspace:^",
27
+ "@gradio/statustracker": "workspace:^",
28
+ "@gradio/utils": "workspace:^"
29
+ },
30
+ "peerDependencies": {
31
+ "svelte": "^4.0.0"
32
+ },
33
+ "devDependencies": {
34
+ "@gradio/preview": "workspace:^"
35
+ },
36
+ "repository": {
37
+ "type": "git",
38
+ "url": "git+https://github.com/gradio-app/gradio.git",
39
+ "directory": "js/datetime"
40
+ }
41
+ }
5.49.1/datetime/utils.ts ADDED
@@ -0,0 +1,183 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ export const format_date = (date: Date, include_time: boolean): string => {
2
+ if (date.toJSON() === null) return "";
3
+ const pad = (num: number): string => num.toString().padStart(2, "0");
4
+
5
+ const year = date.getFullYear();
6
+ const month = pad(date.getMonth() + 1);
7
+ const day = pad(date.getDate());
8
+ const hours = pad(date.getHours());
9
+ const minutes = pad(date.getMinutes());
10
+ const seconds = pad(date.getSeconds());
11
+
12
+ const date_str = `${year}-${month}-${day}`;
13
+ const time_str = `${hours}:${minutes}:${seconds}`;
14
+ if (include_time) {
15
+ return `${date_str} ${time_str}`;
16
+ }
17
+ return date_str;
18
+ };
19
+
20
+ export const date_is_valid_format = (
21
+ date: string | null,
22
+ include_time: boolean
23
+ ): boolean => {
24
+ if (date === null || date === "") return true;
25
+ const valid_regex = include_time
26
+ ? /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/
27
+ : /^\d{4}-\d{2}-\d{2}$/;
28
+ const is_valid_date = date.match(valid_regex) !== null;
29
+ const is_valid_now =
30
+ date.match(/^(?:\s*now\s*(?:-\s*\d+\s*[dmhs])?)?\s*$/) !== null;
31
+ return is_valid_date || is_valid_now;
32
+ };
33
+
34
+ export const get_days_in_month = (year: number, month: number): number => {
35
+ return new Date(year, month + 1, 0).getDate();
36
+ };
37
+
38
+ export const get_first_day_of_month = (year: number, month: number): number => {
39
+ return new Date(year, month, 1).getDay();
40
+ };
41
+
42
+ export const parse_date_value = (
43
+ entered_value: string,
44
+ include_time: boolean
45
+ ): {
46
+ selected_date: Date;
47
+ current_year: number;
48
+ current_month: number;
49
+ selected_hour: number;
50
+ selected_minute: number;
51
+ selected_second: number;
52
+ is_pm: boolean;
53
+ } => {
54
+ if (!entered_value || entered_value === "") {
55
+ const now = new Date();
56
+ return {
57
+ selected_date: now,
58
+ current_year: now.getFullYear(),
59
+ current_month: now.getMonth(),
60
+ selected_hour: now.getHours(),
61
+ selected_minute: now.getMinutes(),
62
+ selected_second: now.getSeconds(),
63
+ is_pm: now.getHours() >= 12
64
+ };
65
+ }
66
+
67
+ try {
68
+ let date_to_parse = entered_value;
69
+ if (!include_time && entered_value.match(/^\d{4}-\d{2}-\d{2}$/)) {
70
+ date_to_parse += " 00:00:00";
71
+ }
72
+
73
+ const parsed = new Date(date_to_parse.replace(" ", "T"));
74
+ if (!isNaN(parsed.getTime())) {
75
+ return {
76
+ selected_date: parsed,
77
+ current_year: parsed.getFullYear(),
78
+ current_month: parsed.getMonth(),
79
+ selected_hour: parsed.getHours(),
80
+ selected_minute: parsed.getMinutes(),
81
+ selected_second: parsed.getSeconds(),
82
+ is_pm: parsed.getHours() >= 12
83
+ };
84
+ }
85
+ } catch (e) {
86
+ // fallback to current date
87
+ }
88
+
89
+ const now = new Date();
90
+ return {
91
+ selected_date: now,
92
+ current_year: now.getFullYear(),
93
+ current_month: now.getMonth(),
94
+ selected_hour: now.getHours(),
95
+ selected_minute: now.getMinutes(),
96
+ selected_second: now.getSeconds(),
97
+ is_pm: now.getHours() >= 12
98
+ };
99
+ };
100
+
101
+ export const generate_calendar_days = (
102
+ current_year: number,
103
+ current_month: number
104
+ ): {
105
+ day: number;
106
+ is_current_month: boolean;
107
+ is_next_month: boolean;
108
+ }[] => {
109
+ const days_in_month = get_days_in_month(current_year, current_month);
110
+ const first_day = get_first_day_of_month(current_year, current_month);
111
+ const days = [];
112
+
113
+ const prev_month = current_month === 0 ? 11 : current_month - 1;
114
+ const prev_year = current_month === 0 ? current_year - 1 : current_year;
115
+ const days_in_prev_month = get_days_in_month(prev_year, prev_month);
116
+
117
+ for (let i = first_day - 1; i >= 0; i--) {
118
+ days.push({
119
+ day: days_in_prev_month - i,
120
+ is_current_month: false,
121
+ is_next_month: false
122
+ });
123
+ }
124
+
125
+ for (let day = 1; day <= days_in_month; day++) {
126
+ days.push({
127
+ day,
128
+ is_current_month: true,
129
+ is_next_month: false
130
+ });
131
+ }
132
+
133
+ const remaining_slots = 42 - days.length;
134
+ for (let day = 1; day <= remaining_slots; day++) {
135
+ days.push({
136
+ day,
137
+ is_current_month: false,
138
+ is_next_month: true
139
+ });
140
+ }
141
+
142
+ return days;
143
+ };
144
+
145
+ export const calculate_display_hour = (
146
+ selected_hour: number,
147
+ is_pm: boolean
148
+ ): number => {
149
+ return is_pm
150
+ ? selected_hour === 0
151
+ ? 12
152
+ : selected_hour > 12
153
+ ? selected_hour - 12
154
+ : selected_hour
155
+ : selected_hour === 0
156
+ ? 12
157
+ : selected_hour;
158
+ };
159
+
160
+ export const convert_display_hour_to_24h = (
161
+ display_hour: number,
162
+ is_pm: boolean
163
+ ): number => {
164
+ if (is_pm) {
165
+ return display_hour === 12 ? 12 : display_hour + 12;
166
+ }
167
+ return display_hour === 12 ? 0 : display_hour;
168
+ };
169
+
170
+ export const month_names = [
171
+ "January",
172
+ "February",
173
+ "March",
174
+ "April",
175
+ "May",
176
+ "June",
177
+ "July",
178
+ "August",
179
+ "September",
180
+ "October",
181
+ "November",
182
+ "December"
183
+ ];