2020-11-07 17:59:37 +02:00
|
|
|
import { CommandRuntime, CommandDeclaration, CommandContext } from '@joplin/lib/services/CommandService';
|
2023-12-13 21:24:58 +02:00
|
|
|
import eventManager, { EventName } from '@joplin/lib/eventManager';
|
2020-11-07 17:59:37 +02:00
|
|
|
import { _ } from '@joplin/lib/locale';
|
|
|
|
import { stateUtils } from '@joplin/lib/reducer';
|
2021-01-22 19:41:11 +02:00
|
|
|
import Note from '@joplin/lib/models/Note';
|
|
|
|
import time from '@joplin/lib/time';
|
2024-03-02 16:25:27 +02:00
|
|
|
import { NoteEntity } from '@joplin/lib/services/database/types';
|
2020-07-03 23:32:39 +02:00
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
export const declaration: CommandDeclaration = {
|
2020-07-03 23:32:39 +02:00
|
|
|
name: 'editAlarm',
|
|
|
|
label: () => _('Set alarm'),
|
2020-09-15 15:01:07 +02:00
|
|
|
iconName: 'icon-alarm',
|
2020-07-03 23:32:39 +02:00
|
|
|
};
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
export const runtime = (comp: any): CommandRuntime => {
|
2020-07-03 23:32:39 +02:00
|
|
|
return {
|
2020-11-12 21:13:28 +02:00
|
|
|
execute: async (context: CommandContext, noteId: string = null) => {
|
2020-10-18 22:52:10 +02:00
|
|
|
noteId = noteId || stateUtils.selectedNoteId(context.state);
|
|
|
|
|
2020-07-03 23:32:39 +02:00
|
|
|
const note = await Note.load(noteId);
|
|
|
|
|
|
|
|
const defaultDate = new Date(Date.now() + 2 * 3600 * 1000);
|
|
|
|
defaultDate.setMinutes(0);
|
|
|
|
defaultDate.setSeconds(0);
|
|
|
|
|
|
|
|
comp.setState({
|
|
|
|
promptOptions: {
|
|
|
|
label: _('Set alarm:'),
|
|
|
|
inputType: 'datetime',
|
|
|
|
buttons: ['ok', 'cancel', 'clear'],
|
|
|
|
value: note.todo_due ? new Date(note.todo_due) : defaultDate,
|
2020-11-12 21:13:28 +02:00
|
|
|
onClose: async (answer: any, buttonType: string) => {
|
2024-03-02 16:25:27 +02:00
|
|
|
let newNote: NoteEntity = null;
|
2020-07-03 23:32:39 +02:00
|
|
|
|
|
|
|
if (buttonType === 'clear') {
|
|
|
|
newNote = {
|
|
|
|
id: note.id,
|
|
|
|
todo_due: 0,
|
|
|
|
};
|
|
|
|
} else if (answer !== null) {
|
|
|
|
newNote = {
|
|
|
|
id: note.id,
|
|
|
|
todo_due: answer.getTime(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (newNote) {
|
|
|
|
await Note.save(newNote);
|
2023-12-13 21:24:58 +02:00
|
|
|
eventManager.emit(EventName.AlarmChange, { noteId: note.id, note: newNote });
|
2020-07-03 23:32:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
comp.setState({ promptOptions: null });
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
},
|
2020-10-09 19:35:46 +02:00
|
|
|
|
2020-10-18 22:52:10 +02:00
|
|
|
enabledCondition: 'oneNoteSelected && noteIsTodo && !noteTodoCompleted',
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
mapStateToTitle: (state: any) => {
|
2020-10-18 22:52:10 +02:00
|
|
|
const note = stateUtils.selectedNote(state);
|
|
|
|
return note && note.todo_due ? time.formatMsToLocal(note.todo_due) : null;
|
2020-07-03 23:32:39 +02:00
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|