import TabAwareWidget from "./tab_aware_widget.js"; import utils from "../services/utils.js"; import protectedSessionHolder from "../services/protected_session_holder.js"; import treeCache from "../services/tree_cache.js"; const TPL = `
`; export default class NoteTitleWidget extends TabAwareWidget { doRender() { this.$widget = $(TPL); this.$noteTitle = this.$widget.find(".note-title"); this.$noteTitle.on('input', () => this.titleChanged()); if (utils.isDesktop()) { // keyboard plugin is not loaded in mobile utils.bindElShortcut(this.$noteTitle, 'return', () => { this.getComponent().focus(); return false; // to not propagate the enter into the editor (causes issues with codemirror) }); } return this.$widget; } async titleChanged() { const {note} = this.tabContext; if (!note) { return; } note.title = this.$noteTitle.val(); const noteFromCache = await treeCache.getNote(note.noteId); noteFromCache.title = note.title; this.trigger(`noteTitleChanged`, { tabId: this.tabContext.tabId, // used to identify that the event comes from this tab so we should not update this tab's input title: note.title, noteId: note.noteId }); } noteTitleChangedListener({tabId, title, noteId}) { if (tabId === this.tabContext.tabId || !this.tabContext.note || this.tabContext.note.noteId !== noteId) { return; } this.$noteTitle.val(title); } async refreshWithNote(note) { this.$noteTitle.val(note.title); if (note.isProtected && !protectedSessionHolder.isProtectedSessionAvailable()) { this.$noteTitle.prop("readonly", true); } } }