mirror of
https://github.com/zadam/trilium.git
synced 2025-06-06 18:08:33 +02:00
implement custom date/time formatting for Alt + T
This commit is contained in:
parent
5da0053f6a
commit
d26e8758ca
@ -73,8 +73,32 @@ function formatDateISO(date) {
|
||||
return `${date.getFullYear()}-${padNum(date.getMonth() + 1)}-${padNum(date.getDate())}`;
|
||||
}
|
||||
|
||||
function formatDateTime(date) {
|
||||
return `${formatDate(date)} ${formatTime(date)}`;
|
||||
// old version
|
||||
// function formatDateTime(date) {
|
||||
// return `${formatDate(date)} ${formatTime(date)}`;
|
||||
// }
|
||||
|
||||
// In utils.js
|
||||
// import dayjs from 'dayjs'; // Assuming dayjs is available in this scope
|
||||
|
||||
function formatDateTime(date, userSuppliedFormat) {
|
||||
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) {
|
||||
console.warn(`Trilium: Day.js encountered an error with format string "${formatToUse}". Falling back to default. Error: ${e.message}`);
|
||||
return dayjs(date).format(DEFAULT_FORMAT);
|
||||
}
|
||||
}
|
||||
|
||||
function localNowDateTime() {
|
||||
|
@ -225,10 +225,29 @@ export default class EditableTextTypeWidget extends AbstractTextTypeWidget {
|
||||
}
|
||||
}
|
||||
|
||||
insertDateTimeToTextCommand() {
|
||||
const date = new Date();
|
||||
const dateString = utils.formatDateTime(date);
|
||||
// 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 = ""; //Default
|
||||
|
||||
try {
|
||||
const allOptions = await server.get('options');
|
||||
|
||||
if (allOptions && typeof allOptions.customDateTimeFormatString === 'string') {
|
||||
userPreferredFormat = allOptions.customDateTimeFormatString;
|
||||
}
|
||||
} catch (e) {
|
||||
console.error("Trilium: Failed to fetch options for custom date/time format. Using default.", e);
|
||||
}
|
||||
const dateString = utils.formatDateTime(date, userPreferredFormat);
|
||||
this.addTextToEditor(dateString);
|
||||
}
|
||||
|
||||
|
@ -47,7 +47,6 @@ export default class DateTimeFormatOptions extends OptionsWidget {
|
||||
}
|
||||
|
||||
async optionsLoaded(options) {
|
||||
//todo: update the key in updateOption
|
||||
const currentFormat = options.customDateTimeFormatString || "";
|
||||
|
||||
|
||||
|
@ -57,7 +57,8 @@ const ALLOWED_OPTIONS = new Set([
|
||||
'customSearchEngineName',
|
||||
'customSearchEngineUrl',
|
||||
'promotedAttributesOpenInRibbon',
|
||||
'editedNotesOpenInRibbon'
|
||||
'editedNotesOpenInRibbon',
|
||||
'customDateTimeFormatString'
|
||||
]);
|
||||
|
||||
function getOptions() {
|
||||
|
Loading…
x
Reference in New Issue
Block a user