mirror of
https://github.com/streetwriters/notesnook.git
synced 2025-12-16 19:57:52 +01:00
editor: make date-time format configurable
This commit is contained in:
committed by
Abdullah Atta
parent
8ebb58b70c
commit
0deaed4ccd
@@ -7,6 +7,7 @@
|
|||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@emotion/react": "^11.10.0",
|
"@emotion/react": "^11.10.0",
|
||||||
"@notesnook/theme": "*",
|
"@notesnook/theme": "*",
|
||||||
|
"@notesnook/core": "*",
|
||||||
"@social-embed/lib": "^0.0.2-next.1",
|
"@social-embed/lib": "^0.0.2-next.1",
|
||||||
"@theme-ui/components": "^0.14.7",
|
"@theme-ui/components": "^0.14.7",
|
||||||
"@theme-ui/core": "^0.14.7",
|
"@theme-ui/core": "^0.14.7",
|
||||||
|
|||||||
@@ -23,6 +23,9 @@ import {
|
|||||||
InputRuleFinder,
|
InputRuleFinder,
|
||||||
ExtendedRegExpMatchArray
|
ExtendedRegExpMatchArray
|
||||||
} from "@tiptap/core";
|
} from "@tiptap/core";
|
||||||
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||||
|
// @ts-ignore
|
||||||
|
import { formatDate } from "@notesnook/core/utils/date";
|
||||||
|
|
||||||
declare module "@tiptap/core" {
|
declare module "@tiptap/core" {
|
||||||
interface Commands<ReturnType> {
|
interface Commands<ReturnType> {
|
||||||
@@ -44,8 +47,21 @@ declare module "@tiptap/core" {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const DateTime = Extension.create({
|
export type DateTimeOptions = {
|
||||||
|
dateFormat: string;
|
||||||
|
timeFormat: "12-hour" | "24-hour";
|
||||||
|
};
|
||||||
|
|
||||||
|
export const DateTime = Extension.create<DateTimeOptions>({
|
||||||
name: "datetime",
|
name: "datetime",
|
||||||
|
|
||||||
|
addOptions() {
|
||||||
|
return {
|
||||||
|
dateFormat: "DD-MM-YYYY",
|
||||||
|
timeFormat: "12-hour"
|
||||||
|
};
|
||||||
|
},
|
||||||
|
|
||||||
addKeyboardShortcuts() {
|
addKeyboardShortcuts() {
|
||||||
return {
|
return {
|
||||||
"Alt-t": ({ editor }) => editor.commands.insertTime(),
|
"Alt-t": ({ editor }) => editor.commands.insertTime(),
|
||||||
@@ -59,15 +75,31 @@ export const DateTime = Extension.create({
|
|||||||
insertDate:
|
insertDate:
|
||||||
() =>
|
() =>
|
||||||
({ commands }) =>
|
({ commands }) =>
|
||||||
commands.insertContent(currentDate()),
|
commands.insertContent(
|
||||||
|
formatDate(Date.now(), {
|
||||||
|
dateFormat: this.options.dateFormat,
|
||||||
|
type: "date"
|
||||||
|
})
|
||||||
|
),
|
||||||
insertTime:
|
insertTime:
|
||||||
() =>
|
() =>
|
||||||
({ commands }) =>
|
({ commands }) =>
|
||||||
commands.insertContent(currentTime()),
|
commands.insertContent(
|
||||||
|
formatDate(Date.now(), {
|
||||||
|
timeFormat: this.options.timeFormat,
|
||||||
|
type: "time"
|
||||||
|
})
|
||||||
|
),
|
||||||
insertDateTime:
|
insertDateTime:
|
||||||
() =>
|
() =>
|
||||||
({ commands }) =>
|
({ commands }) =>
|
||||||
commands.insertContent(currentDateTime())
|
commands.insertContent(
|
||||||
|
formatDate(Date.now(), {
|
||||||
|
dateFormat: this.options.dateFormat,
|
||||||
|
timeFormat: this.options.timeFormat,
|
||||||
|
type: "date-time"
|
||||||
|
})
|
||||||
|
)
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -76,44 +108,35 @@ export const DateTime = Extension.create({
|
|||||||
shortcutInputRule({
|
shortcutInputRule({
|
||||||
shortcut: "/time",
|
shortcut: "/time",
|
||||||
replace: () => {
|
replace: () => {
|
||||||
return currentTime();
|
return formatDate(Date.now(), {
|
||||||
|
timeFormat: this.options.timeFormat,
|
||||||
|
type: "time"
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
shortcutInputRule({
|
shortcutInputRule({
|
||||||
shortcut: "/date",
|
shortcut: "/date",
|
||||||
replace: () => {
|
replace: () => {
|
||||||
return currentDate();
|
return formatDate(Date.now(), {
|
||||||
|
dateFormat: this.options.dateFormat,
|
||||||
|
type: "date"
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
shortcutInputRule({
|
shortcutInputRule({
|
||||||
shortcut: "/now",
|
shortcut: "/now",
|
||||||
replace: () => {
|
replace: () => {
|
||||||
return currentDateTime();
|
return formatDate(Date.now(), {
|
||||||
|
dateFormat: this.options.dateFormat,
|
||||||
|
timeFormat: this.options.timeFormat,
|
||||||
|
type: "date-time"
|
||||||
|
});
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
function currentTime() {
|
|
||||||
return new Date().toLocaleTimeString(window.navigator.languages.slice(), {
|
|
||||||
hour: "2-digit",
|
|
||||||
minute: "2-digit"
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function currentDateTime() {
|
|
||||||
return `${currentDate()}, ${currentTime()}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
function currentDate() {
|
|
||||||
return new Date().toLocaleDateString(window.navigator.languages.slice(), {
|
|
||||||
month: "long",
|
|
||||||
day: "2-digit",
|
|
||||||
year: "numeric"
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function shortcutInputRule(config: {
|
function shortcutInputRule(config: {
|
||||||
shortcut: string;
|
shortcut: string;
|
||||||
replace: () => string;
|
replace: () => string;
|
||||||
|
|||||||
@@ -44,7 +44,7 @@ import BulletList from "./extensions/bullet-list";
|
|||||||
import { ClipboardTextSerializer } from "./extensions/clipboard-text-serializer";
|
import { ClipboardTextSerializer } from "./extensions/clipboard-text-serializer";
|
||||||
import { CodeBlock } from "./extensions/code-block";
|
import { CodeBlock } from "./extensions/code-block";
|
||||||
import { Codemark } from "./extensions/code-mark";
|
import { Codemark } from "./extensions/code-mark";
|
||||||
import { DateTime } from "./extensions/date-time";
|
import { DateTime, DateTimeOptions } from "./extensions/date-time";
|
||||||
import { EmbedNode } from "./extensions/embed";
|
import { EmbedNode } from "./extensions/embed";
|
||||||
import FontFamily from "./extensions/font-family";
|
import FontFamily from "./extensions/font-family";
|
||||||
import FontSize from "./extensions/font-size";
|
import FontSize from "./extensions/font-size";
|
||||||
@@ -88,10 +88,11 @@ const CoreExtensions = Object.entries(TiptapCoreExtensions)
|
|||||||
.filter(([name]) => name !== "ClipboardTextSerializer")
|
.filter(([name]) => name !== "ClipboardTextSerializer")
|
||||||
.map(([, extension]) => extension);
|
.map(([, extension]) => extension);
|
||||||
|
|
||||||
type TiptapOptions = EditorOptions &
|
export type TiptapOptions = EditorOptions &
|
||||||
Omit<AttachmentOptions, "HTMLAttributes"> &
|
Omit<AttachmentOptions, "HTMLAttributes"> &
|
||||||
Omit<WebClipOptions, "HTMLAttributes"> &
|
Omit<WebClipOptions, "HTMLAttributes"> &
|
||||||
Omit<ImageOptions, "HTMLAttributes"> &
|
Omit<ImageOptions, "HTMLAttributes"> &
|
||||||
|
DateTimeOptions &
|
||||||
OpenLinkOptions & {
|
OpenLinkOptions & {
|
||||||
downloadOptions?: DownloadOptions;
|
downloadOptions?: DownloadOptions;
|
||||||
theme: Theme;
|
theme: Theme;
|
||||||
@@ -113,6 +114,8 @@ const useTiptap = (
|
|||||||
onOpenLink,
|
onOpenLink,
|
||||||
onBeforeCreate,
|
onBeforeCreate,
|
||||||
downloadOptions,
|
downloadOptions,
|
||||||
|
dateFormat,
|
||||||
|
timeFormat,
|
||||||
...restOptions
|
...restOptions
|
||||||
} = options;
|
} = options;
|
||||||
const PortalProviderAPI = usePortalProvider();
|
const PortalProviderAPI = usePortalProvider();
|
||||||
@@ -235,7 +238,7 @@ const useTiptap = (
|
|||||||
MathBlock,
|
MathBlock,
|
||||||
KeepInView,
|
KeepInView,
|
||||||
SelectionPersist,
|
SelectionPersist,
|
||||||
DateTime,
|
DateTime.configure({ dateFormat, timeFormat }),
|
||||||
KeyMap,
|
KeyMap,
|
||||||
WebClipNode
|
WebClipNode
|
||||||
],
|
],
|
||||||
@@ -252,7 +255,9 @@ const useTiptap = (
|
|||||||
onOpenAttachmentPicker,
|
onOpenAttachmentPicker,
|
||||||
PortalProviderAPI,
|
PortalProviderAPI,
|
||||||
onBeforeCreate,
|
onBeforeCreate,
|
||||||
onOpenLink
|
onOpenLink,
|
||||||
|
dateFormat,
|
||||||
|
timeFormat
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user