global: support day format options & $day$ title format & /day command

Signed-off-by: 01zulfi <85733202+01zulfi@users.noreply.github.com>
This commit is contained in:
01zulfi
2025-08-20 10:24:52 +05:00
committed by Ammar Ahmed
parent 67d47c283e
commit 5c21c0a498
17 changed files with 169 additions and 15 deletions

View File

@@ -32,11 +32,17 @@ declare module "@tiptap/core" {
* Insert time at current position
*/
insertTime: () => ReturnType;
/**
* Insert date at current position
*/
insertDate: () => ReturnType;
/**
* Insert day at current position
*/
insertDay: () => ReturnType;
/**
* Insert date & time at current position
*/
@@ -53,6 +59,7 @@ declare module "@tiptap/core" {
export type DateTimeOptions = {
dateFormat: string;
timeFormat: "12-hour" | "24-hour";
dayFormat: "short" | "long";
};
export const DateTime = Extension.create<DateTimeOptions>({
@@ -61,7 +68,8 @@ export const DateTime = Extension.create<DateTimeOptions>({
addOptions() {
return {
dateFormat: "DD-MM-YYYY",
timeFormat: "12-hour"
timeFormat: "12-hour",
dayFormat: "short"
};
},
@@ -98,6 +106,15 @@ export const DateTime = Extension.create<DateTimeOptions>({
type: "time"
})
),
insertDay:
() =>
({ commands }) =>
commands.insertContent(
formatDate(Date.now(), {
dayFormat: this.options.dayFormat,
type: "day"
})
),
insertDateTime:
() =>
({ commands }) =>
@@ -141,6 +158,15 @@ export const DateTime = Extension.create<DateTimeOptions>({
});
}
}),
shortcutInputRule({
shortcut: "/day",
replace: () => {
return formatDate(Date.now(), {
dayFormat: this.options.dayFormat,
type: "day"
});
}
}),
shortcutInputRule({
shortcut: "/now",
replace: () => {
@@ -206,7 +232,8 @@ function textInputRule(config: {
export function replaceDateTime(
value: string,
dateFormat = "DD-MM-YYYY",
timeFormat: "12-hour" | "24-hour" = "12-hour"
timeFormat: "12-hour" | "24-hour" = "12-hour",
dayFormat: DateTimeOptions["dayFormat"] = "short"
) {
value = value.replaceAll(
"/time ",
@@ -242,5 +269,13 @@ export function replaceDateTime(
}) + " "
);
value = value.replaceAll(
"/day ",
formatDate(Date.now(), {
dayFormat: dayFormat,
type: "day"
}) + " "
);
return value;
}

View File

@@ -130,7 +130,8 @@ export function TaskListComponent(
e.target.value = replaceDateTime(
e.target.value,
editor.storage.dateFormat,
editor.storage.timeFormat
editor.storage.timeFormat,
editor.storage.dayFormat
);
updateAttributes(
{ title: e.target.value },

View File

@@ -92,6 +92,7 @@ import "simplebar-react/dist/simplebar.min.css";
interface TiptapStorage {
dateFormat?: DateTimeOptions["dateFormat"];
timeFormat?: DateTimeOptions["timeFormat"];
dayFormat?: DateTimeOptions["dayFormat"];
openLink?: (url: string, openInNewTab?: boolean) => void;
downloadAttachment?: (attachment: Attachment) => void;
openAttachmentPicker?: (type: AttachmentType) => void;
@@ -148,6 +149,7 @@ const useTiptap = (
onBeforeCreate,
dateFormat,
timeFormat,
dayFormat,
copyToClipboard,
createInternalLink,
@@ -322,7 +324,7 @@ const useTiptap = (
KeepInView.configure({
scrollIntoViewOnWindowResize: !isMobile
}),
DateTime.configure({ dateFormat, timeFormat }),
DateTime.configure({ dateFormat, timeFormat, dayFormat }),
KeyMap,
WebClipNode,
CheckList,
@@ -375,6 +377,7 @@ const useTiptap = (
onBeforeCreate: ({ editor }) => {
editor.storage.dateFormat = dateFormat;
editor.storage.timeFormat = timeFormat;
editor.storage.dayFormat = dayFormat;
editor.storage.openLink = openLink;
editor.storage.downloadAttachment = downloadAttachment;