fix: address the review feedback

This commit is contained in:
iamvann 2025-05-20 19:46:50 +08:00
parent 2e3a8dfacb
commit d1f8e651f4
4 changed files with 27 additions and 32 deletions

View File

@ -125,19 +125,11 @@ function formatDateISO(date: 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();
}
const formatToUse = (typeof userSuppliedFormat === 'string' && userSuppliedFormat.trim() !== "")
? userSuppliedFormat.trim()
: DEFAULT_FORMAT;
if (!date) {
date = new Date();

View File

@ -1956,5 +1956,16 @@
"title": "Appearance",
"word_wrapping": "Word wrapping",
"color-scheme": "Color scheme"
},
"custom_date_time_format": {
"title": "Custom Date/Time Format (Alt+T)",
"desc1": "Define a custom format for the date and time inserted using the Alt+T shortcut.",
"desc2": "Uses <a href=\"https://day.js.org/docs/en/display/format\" target=\"_blank\" rel=\"noopener noreferrer\">Day.js format tokens</a>. Refer to the Day.js documentation for valid tokens.",
"important_label": "Important:",
"desc3": "If you provide a format string that Day.js does not recognize (e.g., mostly plain text without valid Day.js tokens), the text you typed might be inserted literally. If the format string is left empty, or if Day.js encounters a critical internal error with your format, a default format (e.g., YYYY-MM-DD HH:mm) will be used.",
"format_string_label": "Format String:",
"placeholder": "e.g., DD/MM/YYYY HH:mm:ss or dddd, MMMM D",
"examples_label": "Examples of valid Day.js formats:",
"example_default": "Default-like"
}
}

View File

@ -373,15 +373,7 @@ export default class EditableTextTypeWidget extends AbstractTextTypeWidget {
});
}
}
// old version
// insertDateTimeToTextCommand() {
// const date = new Date();
// const dateString = utils.formatDateTime(date);
// this.addTextToEditor(dateString);
// }
// new version
async insertDateTimeToTextCommand() {
const date = new Date();
let userPreferredFormat = "";

View File

@ -5,28 +5,28 @@ import type { OptionMap } from "@triliumnext/commons";
const TPL = /*html*/`
<div class="options-section">
<h4>${t("options.customDateTimeFormatTitle", "Custom Date/Time Format (Alt+T)")}</h4>
<h4>${t("custom_date_time_format.title")}</h4>
<p>
${t("options.customDateTimeFormatDesc1", "Define a custom format for the date and time inserted using the Alt+T shortcut.")}
${t("options.customDateTimeFormatDesc2", "Uses <a href=\"https://day.js.org/docs/en/display/format\" target=\"_blank\" rel=\"noopener noreferrer\">Day.js format tokens</a>. Refer to the Day.js documentation for valid tokens.")}
${t("custom_date_time_format.desc1")}
${t("custom_date_time_format.desc2")}
</p>
<p>
<strong>${t("options.customDateTimeFormatImportant", "Important:")}</strong>
${t("options.customDateTimeFormatDesc3", "If you provide a format string that Day.js does not recognize (e.g., mostly plain text without valid Day.js tokens), the text you typed might be inserted literally. If the format string is left empty, or if Day.js encounters a critical internal error with your format, a default format (e.g., YYYY-MM-DD HH:mm) will be used.")}
<strong>${t("custom_date_time_format.important_label")}</strong>
${t("custom_date_time_format.desc3")}
</p>
<div class="form-group">
<label for="customDateTimeFormatInput" style="margin-right: 10px;">
${t("options.customDateTimeFormatLabel", "Format String:")}
${t("custom_date_time_format.format_string_label")}
</label>
<input type="text" id="customDateTimeFormatInput" class="form-control custom-datetime-format-input"
placeholder="${t("options.customDateTimeFormatPlaceholder", "e.g., DD/MM/YYYY HH:mm:ss or dddd, MMMM D")}"
placeholder="${t("custom_date_time_format.placeholder")}"
style="width: 300px; display: inline-block;">
</div>
<p style="margin-top: 5px;">
<em>${t("options.customDateTimeFormatExamplesLabel", "Examples of valid Day.js formats:")}</em>
<code>YYYY-MM-DD HH:mm</code> (${t("options.customDateTimeFormatExampleDefault", "Default-like")}),
<em>${t("custom_date_time_format.examples_label")}</em>
<code>YYYY-MM-DD HH:mm</code> (${t("custom_date_time_format.example_default")}),
<code>DD.MM.YYYY</code>,
<code>MMMM D, YYYY h:mm A</code>,
<code>[Today is] dddd</code>