From 35568ec4d64ac453d0a1d342ad1903fa6f314029 Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Fri, 22 Sep 2023 10:42:16 -0700 Subject: [PATCH] flatpickr: Convert module to TypeScript. Signed-off-by: Anders Kaseorg --- tools/test-js-with-node | 2 +- web/src/bundles/app.ts | 1 - web/src/{flatpickr.js => flatpickr.ts} | 29 ++++++++++++++++++-------- 3 files changed, 21 insertions(+), 11 deletions(-) rename web/src/{flatpickr.js => flatpickr.ts} (82%) diff --git a/tools/test-js-with-node b/tools/test-js-with-node index 9a51e88478..06ed8a4971 100755 --- a/tools/test-js-with-node +++ b/tools/test-js-with-node @@ -89,7 +89,7 @@ EXEMPT_FILES = make_set( "web/src/emojisets.ts", "web/src/favicon.ts", "web/src/feedback_widget.ts", - "web/src/flatpickr.js", + "web/src/flatpickr.ts", "web/src/gear_menu.ts", "web/src/giphy.js", "web/src/global.d.ts", diff --git a/web/src/bundles/app.ts b/web/src/bundles/app.ts index d3cf6d5a09..fd851c7966 100644 --- a/web/src/bundles/app.ts +++ b/web/src/bundles/app.ts @@ -7,7 +7,6 @@ import "jquery-caret-plugin/dist/jquery.caret"; import "../../third/jquery-idle/jquery.idle"; import "spectrum-colorpicker"; import "jquery-validation"; -import "flatpickr"; // Import app JS import "../setup"; diff --git a/web/src/flatpickr.js b/web/src/flatpickr.ts similarity index 82% rename from web/src/flatpickr.js rename to web/src/flatpickr.ts index 445ae1104f..082a6a22a0 100644 --- a/web/src/flatpickr.js +++ b/web/src/flatpickr.ts @@ -1,24 +1,31 @@ import {formatISO} from "date-fns"; -import ConfirmDatePlugin from "flatpickr/dist/plugins/confirmDate/confirmDate"; +import flatpickr from "flatpickr"; +import confirmDatePlugin from "flatpickr/dist/plugins/confirmDate/confirmDate"; import $ from "jquery"; +import assert from "minimalistic-assert"; import {$t} from "./i18n"; import {user_settings} from "./user_settings"; -function is_numeric_key(key) { +function is_numeric_key(key: string): boolean { return ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"].includes(key); } -export function show_flatpickr(element, callback, default_timestamp, options = {}) { - const $flatpickr_input = $("").attr("id", "#timestamp_flatpickr"); +export function show_flatpickr( + element: HTMLElement, + callback: (time: string) => void, + default_timestamp: flatpickr.Options.DateOption, + options: flatpickr.Options.Options = {}, +): flatpickr.Instance { + const $flatpickr_input = $("").attr("id", "#timestamp_flatpickr"); - const instance = $flatpickr_input.flatpickr({ + const instance = flatpickr($flatpickr_input[0], { mode: "single", enableTime: true, clickOpens: false, defaultDate: default_timestamp, plugins: [ - new ConfirmDatePlugin({ + confirmDatePlugin({ showAlways: true, confirmText: $t({defaultMessage: "Confirm"}), confirmIcon: "", @@ -57,6 +64,7 @@ export function show_flatpickr(element, callback, default_timestamp, options = { const target = elems[Math.floor(remain >= 0 ? remain : remain + n)]; event.preventDefault(); event.stopPropagation(); + assert(target !== undefined); target.focus(); } else { // Prevent keypresses from propagating to our general hotkey.js @@ -69,7 +77,7 @@ export function show_flatpickr(element, callback, default_timestamp, options = { ...options, }); - const $container = $(instance.innerContainer).parent(); + const $container = $(instance.calendarContainer); $container.on("keydown", (e) => { // Main keyboard UI implementation. @@ -116,16 +124,19 @@ export function show_flatpickr(element, callback, default_timestamp, options = { }); $container.on("click", ".flatpickr-confirm", () => { - callback($flatpickr_input.val()); + const time = $flatpickr_input.val(); + assert(typeof time === "string"); + callback(time); instance.close(); instance.destroy(); }); instance.open(); + assert(instance.selectedDateElem !== undefined); instance.selectedDateElem.focus(); return instance; } -export function close_all() { +export function close_all(): void { $(".flatpickr-calendar").removeClass("open"); }