From 4fbfcefa9416334c73a4287103d31742697b15e1 Mon Sep 17 00:00:00 2001 From: iamvann Date: Sun, 18 May 2025 03:14:18 +0800 Subject: [PATCH 1/3] feat: Implement configurable date/time format for Alt+T shortcut --- apps/client/src/services/utils.ts | 27 ++++++- .../widgets/type_widgets/content_widget.ts | 2 + .../src/widgets/type_widgets/editable_text.ts | 31 ++++++- .../options/text_notes/date_time_format.ts | 80 +++++++++++++++++++ apps/server/src/routes/api/options.ts | 1 + packages/commons/src/lib/options_interface.ts | 1 + 6 files changed, 136 insertions(+), 6 deletions(-) create mode 100644 apps/client/src/widgets/type_widgets/options/text_notes/date_time_format.ts diff --git a/apps/client/src/services/utils.ts b/apps/client/src/services/utils.ts index a52b7443a..92ca2bc70 100644 --- a/apps/client/src/services/utils.ts +++ b/apps/client/src/services/utils.ts @@ -124,8 +124,31 @@ function formatDateISO(date: Date) { return `${date.getFullYear()}-${padNum(date.getMonth() + 1)}-${padNum(date.getDate())}`; } -function formatDateTime(date: Date) { - return `${formatDate(date)} ${formatTime(date)}`; + +// old version +// function formatDateTime(date: Date) { +// return `${formatDate(date)} ${formatTime(date)}`; +// } + +// new version +export function formatDateTime(date: Date, userSuppliedFormat?: string): string { + const DEFAULT_FORMAT = 'YYYY-MM-DD HH:mm'; + let formatToUse = DEFAULT_FORMAT; + + if (userSuppliedFormat && typeof userSuppliedFormat === 'string' && userSuppliedFormat.trim() !== "") { + formatToUse = userSuppliedFormat.trim(); + } + + if (!date) { + date = new Date(); + } + + try { + return dayjs(date).format(formatToUse); + } catch (e: any) { + console.warn(`TriliumNext: Day.js encountered an error with format string "${formatToUse}". Falling back to default. Error: ${e.message}`); + return dayjs(date).format(DEFAULT_FORMAT); + } } function localNowDateTime() { diff --git a/apps/client/src/widgets/type_widgets/content_widget.ts b/apps/client/src/widgets/type_widgets/content_widget.ts index 00c30f5f7..aa3eadcb0 100644 --- a/apps/client/src/widgets/type_widgets/content_widget.ts +++ b/apps/client/src/widgets/type_widgets/content_widget.ts @@ -45,6 +45,7 @@ import LanguageOptions from "./options/i18n/language.js"; import type BasicWidget from "../basic_widget.js"; import CodeTheme from "./options/code_notes/code_theme.js"; import RelatedSettings from "./options/related_settings.js"; +import DateTimeFormatOptions from "./options/text_notes/date_time_format.js"; const TPL = /*html*/`