Line data Source code
1 : import { defineStore } from 'pinia'
2 : import { computed, ref } from 'vue'
3 :
4 : import { t, te } from '@/i18n'
5 : import { useCommandStore } from '@/platform/keybindings/commandStore'
6 : import type { SidebarTabExtension } from '@/types/extensionTypes'
7 :
8 2356 : export const useSidebarTabStore = defineStore('sidebarTab', () => {
9 2355 : const sidebarTabs = ref<SidebarTabExtension[]>([])
10 2355 : const sourceExtensionNames = ref<Record<string, string>>({})
11 2355 : const activeSidebarTabId = ref<string | null>(null)
12 :
13 2355 : const activeSidebarTab = computed<SidebarTabExtension | null>(() => {
14 2676 : return (
15 : sidebarTabs.value.find((tab) => tab.id === activeSidebarTabId.value) ??
16 : null
17 : )
18 : })
19 :
20 2355 : const toggleSidebarTab = (tabId: string) => {
21 308 : activeSidebarTabId.value = activeSidebarTabId.value === tabId ? null : tabId
22 : }
23 :
24 2355 : let registeringExtensionName: string | undefined
25 :
26 : /**
27 : * Attributes tabs registered synchronously inside `run` to `extensionName`,
28 : * whichever `app` reference the extension registers them through.
29 : */
30 2355 : const attributeRegistrationsTo = <T>(
31 : extensionName: string,
32 : run: () => T
33 : ): T => {
34 7066 : const previous = registeringExtensionName
35 7066 : registeringExtensionName = extensionName
36 7066 : try {
37 7066 : return run()
38 : } finally {
39 7066 : registeringExtensionName = previous
40 : }
41 : }
42 :
43 2355 : const registerSidebarTab = (
44 : tab: SidebarTabExtension,
45 : options?: { prepend?: boolean },
46 : sourceExtensionName?: string
47 : ) => {
48 11790 : const source = sourceExtensionName ?? registeringExtensionName
49 11790 : if (source) {
50 2 : sourceExtensionNames.value[tab.id] = source
51 : }
52 11790 : sidebarTabs.value = options?.prepend
53 : ? [tab, ...sidebarTabs.value]
54 : : [...sidebarTabs.value, tab]
55 :
56 : // Generate label in format "Toggle X Sidebar"
57 11790 : const labelFunction = () => {
58 : const tabTitle = te(tab.title) ? t(tab.title) : tab.title
59 285 : return `Toggle ${tabTitle} Sidebar`
60 : }
61 11790 : const tooltipFunction = tab.tooltip
62 : ? te(tab.tooltip)
63 : ? () => t(String(tab.tooltip))
64 : : tab.tooltip
65 : : undefined
66 :
67 11790 : const menubarLabelFunction = () => {
68 : const menubarLabelKeys: Record<string, string> = {
69 : 'node-library': 'sideToolbar.nodeLibrary',
70 : 'model-library': 'sideToolbar.modelLibrary',
71 : workflows: 'sideToolbar.workflows',
72 : assets: 'sideToolbar.assets',
73 : 'job-history': 'queue.jobHistory'
74 : }
75 :
76 0 : const key = menubarLabelKeys[tab.id]
77 0 : if (key && te(key)) {
78 0 : return t(key)
79 : }
80 :
81 0 : return tab.title
82 : }
83 :
84 11790 : useCommandStore().registerCommand({
85 : id: `Workspace.ToggleSidebarTab.${tab.id}`,
86 : icon: typeof tab.icon === 'string' ? tab.icon : undefined,
87 : label: labelFunction,
88 : menubarLabel: menubarLabelFunction,
89 : tooltip: tooltipFunction,
90 : versionAdded: '1.3.9',
91 : category: 'view-controls' as const,
92 : function: async () => {
93 308 : if (await tab.onToggle?.()) return
94 307 : toggleSidebarTab(tab.id)
95 : },
96 : active: () => activeSidebarTab.value?.id === tab.id,
97 : source: 'System'
98 : })
99 : }
100 :
101 2355 : const unregisterSidebarTab = (id: string) => {
102 0 : const index = sidebarTabs.value.findIndex((tab) => tab.id === id)
103 0 : if (index !== -1) {
104 0 : const tab = sidebarTabs.value[index]
105 0 : if (tab.type === 'custom' && tab.destroy) {
106 0 : tab.destroy()
107 : }
108 0 : const newSidebarTabs = [...sidebarTabs.value]
109 0 : newSidebarTabs.splice(index, 1)
110 0 : sidebarTabs.value = newSidebarTabs
111 0 : if (activeSidebarTabId.value === id) {
112 0 : activeSidebarTabId.value = null
113 : }
114 0 : delete sourceExtensionNames.value[id]
115 : }
116 : }
117 :
118 2355 : const getSourceExtensionName = (id: string) => sourceExtensionNames.value[id]
119 :
120 2355 : return {
121 : sidebarTabs,
122 : activeSidebarTabId,
123 : activeSidebarTab,
124 : toggleSidebarTab,
125 : attributeRegistrationsTo,
126 : getSourceExtensionName,
127 : registerSidebarTab,
128 : unregisterSidebarTab
129 : }
130 : })
|