2021-05-22 12:35:41 +02:00
|
|
|
import NoteContextAwareWidget from "./note_context_aware_widget.js";
|
2020-01-19 21:40:23 +01:00
|
|
|
import SpacedUpdate from "../services/spaced_update.js";
|
2025-01-28 14:07:56 +02:00
|
|
|
import appContext, { type EventData } from "../components/app_context.js";
|
2021-09-23 23:34:51 +02:00
|
|
|
import branchService from "../services/branches.js";
|
2022-12-01 00:17:15 +01:00
|
|
|
import shortcutService from "../services/shortcuts.js";
|
2020-01-12 19:05:09 +01:00
|
|
|
|
2021-05-22 12:35:41 +02:00
|
|
|
export default class NoteTitleWidget extends NoteContextAwareWidget {
|
2025-01-28 14:07:56 +02:00
|
|
|
|
|
|
|
|
private $noteTitle!: JQuery<HTMLElement>;
|
|
|
|
|
private deleteNoteOnEscape: boolean;
|
|
|
|
|
private spacedUpdate: SpacedUpdate;
|
|
|
|
|
|
2020-02-27 10:03:14 +01:00
|
|
|
constructor() {
|
|
|
|
|
super();
|
2020-01-19 20:18:02 +01:00
|
|
|
|
2021-09-23 23:34:51 +02:00
|
|
|
this.deleteNoteOnEscape = false;
|
2020-01-19 20:18:02 +01:00
|
|
|
}
|
|
|
|
|
|
2020-01-12 20:15:05 +01:00
|
|
|
doRender() {
|
2020-01-14 20:27:40 +01:00
|
|
|
this.$widget = $(TPL);
|
|
|
|
|
this.$noteTitle = this.$widget.find(".note-title");
|
2025-01-09 18:07:02 +02:00
|
|
|
this.$noteTitle.on("blur", () => {
|
2022-01-17 20:54:57 +01:00
|
|
|
this.spacedUpdate.updateNowIfNecessary();
|
|
|
|
|
|
|
|
|
|
this.deleteNoteOnEscape = false;
|
|
|
|
|
});
|
2021-09-23 23:34:51 +02:00
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
shortcutService.bindElShortcut(this.$noteTitle, "esc", () => {
|
2025-01-28 14:07:56 +02:00
|
|
|
if (this.deleteNoteOnEscape && this.noteContext?.isActive() && this.noteContext?.note) {
|
2021-09-23 23:34:51 +02:00
|
|
|
branchService.deleteNotes(Object.values(this.noteContext.note.parentToBranch));
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2020-01-12 19:05:09 +01:00
|
|
|
}
|
|
|
|
|
|
2025-01-28 14:07:56 +02:00
|
|
|
async beforeNoteSwitchEvent({ noteContext }: EventData<"beforeNoteSwitch">) {
|
2021-05-22 12:42:34 +02:00
|
|
|
if (this.isNoteContext(noteContext.ntxId)) {
|
2020-01-19 21:12:53 +01:00
|
|
|
await this.spacedUpdate.updateNowIfNecessary();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-28 14:07:56 +02:00
|
|
|
async beforeNoteContextRemoveEvent({ ntxIds }: EventData<"beforeNoteContextRemove">) {
|
2021-05-22 12:42:34 +02:00
|
|
|
if (this.isNoteContext(ntxIds)) {
|
2020-01-19 20:18:02 +01:00
|
|
|
await this.spacedUpdate.updateNowIfNecessary();
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-01-24 17:54:47 +01:00
|
|
|
|
2020-02-16 19:23:49 +01:00
|
|
|
focusOnTitleEvent() {
|
2021-05-22 12:26:45 +02:00
|
|
|
if (this.noteContext && this.noteContext.isActive()) {
|
2025-01-09 18:07:02 +02:00
|
|
|
this.$noteTitle.trigger("focus");
|
2020-01-24 17:54:47 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
focusAndSelectTitleEvent({ isNewNote } = { isNewNote: false }) {
|
2021-05-22 12:26:45 +02:00
|
|
|
if (this.noteContext && this.noteContext.isActive()) {
|
2025-01-09 18:07:02 +02:00
|
|
|
this.$noteTitle.trigger("focus").trigger("select");
|
2021-09-23 23:34:51 +02:00
|
|
|
|
|
|
|
|
this.deleteNoteOnEscape = isNewNote;
|
2020-01-24 17:54:47 +01:00
|
|
|
}
|
|
|
|
|
}
|
2020-06-14 14:30:57 +02:00
|
|
|
}
|