From 1977c16a09852455df9b5a4f76463d064999e683 Mon Sep 17 00:00:00 2001 From: windingwind <33902321+windingwind@users.noreply.github.com> Date: Sat, 6 Apr 2024 11:43:45 +0800 Subject: [PATCH] fix: tab title update --- src/hooks.ts | 5 ++- src/modules/workspace/tab.ts | 63 ++++++++---------------------------- 2 files changed, 16 insertions(+), 52 deletions(-) diff --git a/src/hooks.ts b/src/hooks.ts index 493bccc..8bb3262 100644 --- a/src/hooks.ts +++ b/src/hooks.ts @@ -9,10 +9,10 @@ import { } from "./modules/template/controller"; import { registerMenus } from "./modules/menu"; import { - registerWorkspaceTab, openWorkspaceTab, onTabSelect, restoreNoteTabs, + onUpdateNoteTabsTitle, } from "./modules/workspace/tab"; import { initWorkspace } from "./modules/workspace/content"; import { registerNotify } from "./modules/notify"; @@ -78,8 +78,6 @@ async function onMainWindowLoad(win: Window): Promise { registerMenus(); - registerWorkspaceTab(win); - initTemplates(); patchViewItems(win); @@ -123,6 +121,7 @@ function onNotify( reason: "item-modify", }); addon.hooks.onUpdateRelated(modifiedNotes, { skipActive: true }); + onUpdateNoteTabsTitle(modifiedNotes); } } else { return; diff --git a/src/modules/workspace/tab.ts b/src/modules/workspace/tab.ts index 0c53408..5f7f66f 100644 --- a/src/modules/workspace/tab.ts +++ b/src/modules/workspace/tab.ts @@ -3,55 +3,6 @@ import { initWorkspace } from "./content"; export const TAB_TYPE = "note"; -export function registerWorkspaceTab(win: Window) { - const doc = win.document; - const spacer = doc.querySelector("#zotero-collections-toolbar > spacer"); - if (!spacer) { - return; - } - const tabButton = ztoolkit.UI.insertElementBefore( - { - tag: "toolbarbutton", - classList: ["zotero-tb-button"], - styles: { - listStyleImage: `url("chrome://${config.addonRef}/content/icons/icon-linear-20.svg")`, - }, - attributes: { - tooltiptext: "Open workspace", - }, - listeners: [ - { - type: "command", - listener: (ev) => { - // if ((ev as MouseEvent).shiftKey) { - // addon.hooks.onOpenWorkspace("window"); - // } else { - // addon.hooks.onOpenWorkspace("tab"); - // } - }, - }, - ], - }, - spacer, - ) as XUL.ToolBarButton; - const collectionSearch = doc.querySelector("#zotero-collections-search")!; - const ob = new (ztoolkit.getGlobal("MutationObserver"))((muts) => { - tabButton.hidden = !!collectionSearch?.classList.contains("visible"); - }); - ob.observe(collectionSearch, { - attributes: true, - attributeFilter: ["class"], - }); - - win.addEventListener( - "unload", - () => { - ob.disconnect(); - }, - { once: true }, - ); -} - export async function openWorkspaceTab( item: Zotero.Item, options: { select?: boolean; index?: number } = { @@ -110,3 +61,17 @@ export function restoreNoteTabs() { }); } } + +export function onUpdateNoteTabsTitle(noteItems: Zotero.Item[]) { + const ids = noteItems.map((item) => item.id); + for (const tab of Zotero_Tabs._tabs) { + if (tab.type !== TAB_TYPE) continue; + if (ids.includes(tab.data.itemID)) { + const newTitle = Zotero.Items.get(tab.data.itemID).getNoteTitle(); + if (tab.title === newTitle) { + continue; + } + Zotero_Tabs.rename(tab.id, newTitle); + } + } +}