gradio-pr-bot commited on
Commit
0214cbf
·
verified ·
1 Parent(s): aa63cc5

Upload folder using huggingface_hub

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