Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: insert only time (#4) #6

Merged
merged 1 commit into from
Jul 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 64 additions & 2 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ export default class DatepickerPlugin extends Plugin {
const datepicker = new Datepicker()
datepicker.open(
{ top: pos.top, left: pos.left, right: pos.right, bottom: pos.bottom }, cursorPosition,
"", (result) => {
"DATE", (result) => {
if (moment(result).isValid() === true) {
setTimeout(() => { // delay to wait for editor update to finish
editorView.dispatch({
Expand All @@ -375,6 +375,43 @@ export default class DatepickerPlugin extends Plugin {
datepicker.focus();
}
});
this.addCommand({
id: 'insert-time',
name: 'Insert new time',
editorCallback: (editor: Editor) => {
// @ts-expect-error, not typed
const editorView = editor.cm as EditorView;
const cursorPosition = editorView.state.selection.main.to;
if (cursorPosition === undefined) return;
const pos = editorView.coordsAtPos(cursorPosition);
if (!pos) return;
const datepicker = new Datepicker()
datepicker.open(
{ top: pos.top, left: pos.left, right: pos.right, bottom: pos.bottom }, cursorPosition,
"TIME", (result) => {
// TODO: format time according to picker local format

if (moment(result,"HH:mm").isValid() === true) {
let timeFormat: string;
if (DatepickerPlugin.settings.insertIn24HourFormat) timeFormat = "HH:mm";
else timeFormat = "hh:mm A";
setTimeout(() => { // delay to wait for editor update to finish
editorView.dispatch({
changes: {
from: cursorPosition,
to: cursorPosition,
insert: moment(result,"HH:mm").format(timeFormat)
}
})
}, 250);
Datepicker.performedInsertCommand = true;
datepicker.closeAll();
} else new Notice("Please enter a valid time");
}
)
datepicker.focus();
}
});
this.addCommand({
id: 'insert-datetime',
name: 'Insert new date and time',
Expand Down Expand Up @@ -411,6 +448,28 @@ export default class DatepickerPlugin extends Plugin {
datepicker.focus();
}
});
this.addCommand({
id: 'insert-current-time',
name: 'Insert current time',
editorCallback: (editor: Editor) => {
// @ts-expect-error, not typed
const editorView = editor.cm as EditorView;
const cursorPosition = editorView.state.selection.main.to;
if (cursorPosition === undefined) return;
const pos = editorView.coordsAtPos(cursorPosition);
if (!pos) return;
let timeFormat: string;
if (DatepickerPlugin.settings.insertIn24HourFormat) timeFormat = "HH:mm";
else timeFormat = "hh:mm A";
editorView.dispatch({
changes: {
from: cursorPosition,
to: cursorPosition,
insert: moment().format(timeFormat)
}
})
}
});

this.addSettingTab(new DatepickerSettingTab(this.app, this));
this.registerEvent(
Expand Down Expand Up @@ -516,7 +575,9 @@ class Datepicker {
this.pickerContainer.className = 'datepicker-container';
this.pickerContainer.id = 'datepicker-container';
this.pickerInput = this.pickerContainer.createEl('input');
if (datetime.length <= 10) this.pickerInput.type = 'date';
if (datetime === "TIME") {
this.pickerInput.type = 'time';
} else if (datetime === "DATE") this.pickerInput.type = 'date';
else this.pickerInput.type = 'datetime-local';
this.pickerInput.id = 'datepicker-input';
this.pickerInput.className = 'datepicker-input';
Expand All @@ -527,6 +588,7 @@ class Datepicker {
const buttonEventAbortController = new AbortController();
const acceptButtonEventHandler = (event: Event) => {
event.preventDefault();

if (this.pickerInput.value === '') {
new Notice('Please enter a valid date');
} else {
Expand Down