diff --git a/chrome/content/zotero/elements/contextPane.js b/chrome/content/zotero/elements/contextPane.js index 1b90b4c654..881d6731b1 100644 --- a/chrome/content/zotero/elements/contextPane.js +++ b/chrome/content/zotero/elements/contextPane.js @@ -170,9 +170,6 @@ this._sidenav.hidden = true; } else if (Zotero_Tabs.selectedType == 'reader') { - let currentNoteContext = this._getCurrentNotesContext(); - currentNoteContext?._cacheViewType(); - let reader = Zotero.Reader.getByTabID(Zotero_Tabs.selectedID); this._handleReaderReady(reader); @@ -218,15 +215,7 @@ } let currentNoteContext = this._getCurrentNotesContext(); - let tabNotesDeck = currentNoteContext.querySelector('.zotero-context-pane-tab-notes-deck'); - let selectedIndex = Array.from(tabNotesDeck.children).findIndex(x => x.getAttribute('data-tab-id') == reader.tabID); - if (selectedIndex != -1) { - tabNotesDeck.setAttribute('selectedIndex', selectedIndex); - currentNoteContext.mode = "childNote"; - } - else { - currentNoteContext._restoreViewType(); - } + currentNoteContext.switchToTab(reader.tabID); } _getCurrentNotesContext() { diff --git a/chrome/content/zotero/elements/notesContext.js b/chrome/content/zotero/elements/notesContext.js index 80eac00736..92d724e48a 100644 --- a/chrome/content/zotero/elements/notesContext.js +++ b/chrome/content/zotero/elements/notesContext.js @@ -44,12 +44,21 @@ - + + + + + + - - + + + + + + @@ -128,18 +137,17 @@ init() { this.node = this.querySelector(".context-node"); - this.editor = this.querySelector(".zotero-context-pane-pinned-note"); this.notesList = this.querySelector("context-notes-list"); + this.standaloneNoteContainer = this.querySelector('.context-note-standalone'); + this.tabNotesDeck = this.querySelector('.zotero-context-pane-tab-notes-deck'); this.input = this.querySelector("search-textbox"); this.input.addEventListener('command', () => { this.notesList.expanded = false; this._updateNotesList(); }); - this._preventViewTypeCache = false; - this._cachedViewType = ""; - this._initNotesList(); + this._initNoteEditor(); } focus() { @@ -193,6 +201,11 @@ }); } + _initNoteEditor() { + this.querySelectorAll(".zotero-tb-note-return").forEach( + btn => btn.addEventListener("command", this._handleNoteEditorReturn)); + } + async _createNoteFromAnnotations(child) { let attachment = this._getCurrentAttachment(); if (!attachment) { @@ -247,7 +260,18 @@ _getCurrentEditor() { let splitter = ZoteroContextPane.splitter; if (splitter.getAttribute('state') == 'collapsed' || ZoteroContextPane.context.mode != "notes") return null; - return this.node.selectedPanel.querySelector('note-editor'); + switch (this.mode) { + case "childNote": { + return this.tabNotesDeck.selectedPanel.querySelector("note-editor"); + } + case "standaloneNote": { + return this.standaloneEditor; + } + case "notesList": + default: { + return null; + } + } } _getCurrentAttachment() { @@ -259,8 +283,6 @@ } _setPinnedNote(item) { - let { editor, node } = this; - let isChild = false; let reader = Zotero.Reader.getByTabID(Zotero_Tabs.selectedID); if (reader) { @@ -270,66 +292,82 @@ } } - let tabNotesDeck = this.querySelector('.zotero-context-pane-tab-notes-deck'); - let parentTitleContainer; - let vbox; + let editor; + if (isChild) { - vbox = document.createXULElement('vbox'); + let vbox = document.createXULElement('vbox'); vbox.setAttribute('data-tab-id', Zotero_Tabs.selectedID); vbox.style.display = 'flex'; editor = new (customElements.get('note-editor')); editor.style.flex = "1"; + vbox.append(editor); - tabNotesDeck.append(vbox); - - editor.mode = this.editable ? 'edit' : 'view'; - editor.item = item; - editor.parentItem = null; + this.tabNotesDeck.append(vbox); this.mode = "childNote"; - tabNotesDeck.setAttribute('selectedIndex', tabNotesDeck.children.length - 1); - - parentTitleContainer = this.querySelector('.context-note-child > .zotero-context-pane-editor-parent-line'); + this.tabNotesDeck.selectedIndex = this.tabNotesDeck.children.length - 1; } else { - this.mode = "standaloneNote"; - editor.mode = this.editable ? 'edit' : 'view'; - editor.item = item; - editor.parentItem = null; + // Try to reuse existing editor + if (this.standaloneEditor) { + editor = this.standaloneEditor; + } + else { + editor = new (customElements.get('note-editor')); + editor.classList.add("zotero-context-pane-pinned-note"); + editor.style.flex = "1"; - parentTitleContainer = node.querySelector('.context-note-standalone > .zotero-context-pane-editor-parent-line'); + this.standaloneNoteContainer.append(editor); + this.standaloneEditor = editor; + } + this.mode = "standaloneNote"; } + editor.mode = this.editable ? 'edit' : 'view'; + editor.item = item; + editor.parentItem = null; editor.focus(); - let parentItem = item.parentItem; - let container = document.createElement('div'); - container.classList.add("parent-title-container"); - let returnBtn = document.createXULElement("toolbarbutton"); - returnBtn.classList.add("zotero-tb-note-return"); - returnBtn.addEventListener("command", () => { - // Immediately save note content before vbox with note-editor iframe is destroyed below - editor.saveSync(); - ZoteroContextPane.context.mode = "notes"; - this.mode = "notesList"; - vbox?.remove(); - ZoteroContextPane.updateAddToNote(); - this._preventViewTypeCache = true; - }); - let title = document.createElement('div'); - title.className = 'parent-title'; - title.textContent = parentItem?.getDisplayTitle() || ''; - container.append(returnBtn, title); - parentTitleContainer.replaceChildren(container); + this.updatePinnedNoteTitle(); ZoteroContextPane.updateAddToNote(); } + updatePinnedNoteTitle() { + let item = this._getCurrentEditor()?.item; + let title = this.selectedPanel?.querySelector('.parent-title'); + let parentItem = item.parentItem; + title.textContent = parentItem?.getDisplayTitle() || ''; + } + updateNotesListFromCache() { this._updateNotesList(true); } + + switchToTab(tabID) { + if (ZoteroContextPane.context.mode !== "notes") { + return; + } + // Use childNote if find one + let childNoteContainer = this.tabNotesDeck.querySelector(`:scope > [data-tab-id=${tabID}]`); + if (childNoteContainer) { + this.tabNotesDeck.selectedPanel = childNoteContainer; + this.mode = "childNote"; + this.updatePinnedNoteTitle(); + } + // Use standalone note if find one + else if (this.standaloneEditor) { + this.mode = "standaloneNote"; + } + // Otherwise, show notes list + else { + this.mode = "notesList"; + } + ZoteroContextPane.updateAddToNote(); + } + async _updateNotesList(useCached) { let query = this.input.value; let notes; @@ -419,19 +457,6 @@ })); } - _cacheViewType() { - if (ZoteroContextPane.context.mode == "notes" - && this.mode != "childNote" && !this._preventViewTypeCache) { - this._cachedViewType = this.mode; - } - this._preventViewTypeCache = false; - } - - _restoreViewType() { - this.mode = this._cachedViewType || "notesList"; - this._cachedViewType = ""; - } - _handleListPopupClick(id, event) { switch (event.originalTarget.id) { case 'context-pane-list-show-in-library': @@ -488,6 +513,31 @@ default: } }; + + _handleNoteEditorReturn = () => { + let editor = this._getCurrentEditor(); + // Immediately save note content before vbox with note-editor iframe is destroyed below + editor.saveSync(); + ZoteroContextPane.context.mode = "notes"; + + switch (this.mode) { + case "childNote": { + this.tabNotesDeck.selectedPanel?.remove(); + break; + } + case "standaloneNote": { + this.standaloneEditor?.remove(); + this.standaloneEditor = undefined; + break; + } + default: { + break; + } + } + + this.mode = "notesList"; + ZoteroContextPane.updateAddToNote(); + }; } customElements.define("notes-context", NotesContext); }