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())}`;
|
return `${date.getFullYear()}-${padNum(date.getMonth() + 1)}-${padNum(date.getDate())}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
function formatDateTime(date) {
|
// old version
|
||||||
return `${formatDate(date)} ${formatTime(date)}`;
|
// 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() {
|
function localNowDateTime() {
|
||||||
|
@ -109,7 +109,7 @@ export default class EditableTextTypeWidget extends AbstractTextTypeWidget {
|
|||||||
(await mimeTypesService.getMimeTypes())
|
(await mimeTypesService.getMimeTypes())
|
||||||
.filter(mt => mt.enabled)
|
.filter(mt => mt.enabled)
|
||||||
.map(mt => ({
|
.map(mt => ({
|
||||||
language: mt.mime.toLowerCase().replace(/[\W_]+/g,"-"),
|
language: mt.mime.toLowerCase().replace(/[\W_]+/g, "-"),
|
||||||
label: mt.title
|
label: mt.title
|
||||||
}));
|
}));
|
||||||
|
|
||||||
@ -211,7 +211,7 @@ export default class EditableTextTypeWidget extends AbstractTextTypeWidget {
|
|||||||
this.watchdog?.editor.editing.view.focus();
|
this.watchdog?.editor.editing.view.focus();
|
||||||
}
|
}
|
||||||
|
|
||||||
show() {}
|
show() { }
|
||||||
|
|
||||||
getEditor() {
|
getEditor() {
|
||||||
return this.watchdog?.editor;
|
return this.watchdog?.editor;
|
||||||
@ -225,10 +225,29 @@ export default class EditableTextTypeWidget extends AbstractTextTypeWidget {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
insertDateTimeToTextCommand() {
|
// old version
|
||||||
const date = new Date();
|
// insertDateTimeToTextCommand() {
|
||||||
const dateString = utils.formatDateTime(date);
|
// 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);
|
this.addTextToEditor(dateString);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -237,7 +256,7 @@ export default class EditableTextTypeWidget extends AbstractTextTypeWidget {
|
|||||||
|
|
||||||
this.watchdog.editor.model.change(writer => {
|
this.watchdog.editor.model.change(writer => {
|
||||||
const insertPosition = this.watchdog.editor.model.document.selection.getFirstPosition();
|
const insertPosition = this.watchdog.editor.model.document.selection.getFirstPosition();
|
||||||
writer.insertText(linkTitle, {linkHref: linkHref}, insertPosition);
|
writer.insertText(linkTitle, { linkHref: linkHref }, insertPosition);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -250,7 +269,7 @@ export default class EditableTextTypeWidget extends AbstractTextTypeWidget {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
addTextToActiveEditorEvent({text}) {
|
addTextToActiveEditorEvent({ text }) {
|
||||||
if (!this.isActive()) {
|
if (!this.isActive()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -283,7 +302,7 @@ export default class EditableTextTypeWidget extends AbstractTextTypeWidget {
|
|||||||
return !selection.isCollapsed;
|
return !selection.isCollapsed;
|
||||||
}
|
}
|
||||||
|
|
||||||
async executeWithTextEditorEvent({callback, resolve, ntxId}) {
|
async executeWithTextEditorEvent({ callback, resolve, ntxId }) {
|
||||||
if (!this.isNoteContext(ntxId)) {
|
if (!this.isNoteContext(ntxId)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -300,7 +319,7 @@ export default class EditableTextTypeWidget extends AbstractTextTypeWidget {
|
|||||||
addLinkToTextCommand() {
|
addLinkToTextCommand() {
|
||||||
const selectedText = this.getSelectedText();
|
const selectedText = this.getSelectedText();
|
||||||
|
|
||||||
this.triggerCommand('showAddLinkDialog', {textTypeWidget: this, text: selectedText})
|
this.triggerCommand('showAddLinkDialog', { textTypeWidget: this, text: selectedText })
|
||||||
}
|
}
|
||||||
|
|
||||||
getSelectedText() {
|
getSelectedText() {
|
||||||
@ -347,29 +366,29 @@ export default class EditableTextTypeWidget extends AbstractTextTypeWidget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
addIncludeNoteToTextCommand() {
|
addIncludeNoteToTextCommand() {
|
||||||
this.triggerCommand("showIncludeNoteDialog", {textTypeWidget: this});
|
this.triggerCommand("showIncludeNoteDialog", { textTypeWidget: this });
|
||||||
}
|
}
|
||||||
|
|
||||||
addIncludeNote(noteId, boxSize) {
|
addIncludeNote(noteId, boxSize) {
|
||||||
this.watchdog.editor.model.change( writer => {
|
this.watchdog.editor.model.change(writer => {
|
||||||
// Insert <includeNote>*</includeNote> at the current selection position
|
// Insert <includeNote>*</includeNote> at the current selection position
|
||||||
// in a way that will result in creating a valid model structure
|
// in a way that will result in creating a valid model structure
|
||||||
this.watchdog.editor.model.insertContent(writer.createElement('includeNote', {
|
this.watchdog.editor.model.insertContent(writer.createElement('includeNote', {
|
||||||
noteId: noteId,
|
noteId: noteId,
|
||||||
boxSize: boxSize
|
boxSize: boxSize
|
||||||
}));
|
}));
|
||||||
} );
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async addImage(noteId) {
|
async addImage(noteId) {
|
||||||
const note = await froca.getNote(noteId);
|
const note = await froca.getNote(noteId);
|
||||||
|
|
||||||
this.watchdog.editor.model.change( writer => {
|
this.watchdog.editor.model.change(writer => {
|
||||||
const encodedTitle = encodeURIComponent(note.title);
|
const encodedTitle = encodeURIComponent(note.title);
|
||||||
const src = `api/images/${note.noteId}/${encodedTitle}`;
|
const src = `api/images/${note.noteId}/${encodedTitle}`;
|
||||||
|
|
||||||
this.watchdog.editor.execute( 'insertImage', { source: src } );
|
this.watchdog.editor.execute('insertImage', { source: src });
|
||||||
} );
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async createNoteForReferenceLink(title) {
|
async createNoteForReferenceLink(title) {
|
||||||
@ -385,7 +404,7 @@ export default class EditableTextTypeWidget extends AbstractTextTypeWidget {
|
|||||||
return resp.note.getBestNotePathString();
|
return resp.note.getBestNotePathString();
|
||||||
}
|
}
|
||||||
|
|
||||||
async refreshIncludedNoteEvent({noteId}) {
|
async refreshIncludedNoteEvent({ noteId }) {
|
||||||
this.refreshIncludedNote(this.$editor, noteId);
|
this.refreshIncludedNote(this.$editor, noteId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -47,7 +47,6 @@ export default class DateTimeFormatOptions extends OptionsWidget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async optionsLoaded(options) {
|
async optionsLoaded(options) {
|
||||||
//todo: update the key in updateOption
|
|
||||||
const currentFormat = options.customDateTimeFormatString || "";
|
const currentFormat = options.customDateTimeFormatString || "";
|
||||||
|
|
||||||
|
|
||||||
|
@ -57,7 +57,8 @@ const ALLOWED_OPTIONS = new Set([
|
|||||||
'customSearchEngineName',
|
'customSearchEngineName',
|
||||||
'customSearchEngineUrl',
|
'customSearchEngineUrl',
|
||||||
'promotedAttributesOpenInRibbon',
|
'promotedAttributesOpenInRibbon',
|
||||||
'editedNotesOpenInRibbon'
|
'editedNotesOpenInRibbon',
|
||||||
|
'customDateTimeFormatString'
|
||||||
]);
|
]);
|
||||||
|
|
||||||
function getOptions() {
|
function getOptions() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user