perf: move expanded keys to tab store for external modification

This commit is contained in:
Lykin 2024-01-05 20:48:57 +08:00
parent 7f2fac7fe8
commit 9b3f2ba726
5 changed files with 94 additions and 44 deletions

View File

@ -104,7 +104,6 @@ const onReload = async () => {
tabStore.setSelectedKeys(props.server) tabStore.setSelectedKeys(props.server)
const db = props.db const db = props.db
browserStore.closeDatabase(props.server, db) browserStore.closeDatabase(props.server, db)
// browserTreeRef.value?.resetExpandKey(props.server, db)
let matchType = unref(filterForm.type) let matchType = unref(filterForm.type)
if (!types.hasOwnProperty(matchType)) { if (!types.hasOwnProperty(matchType)) {
@ -200,7 +199,6 @@ const handleSelectDB = async (db) => {
await nextTick() await nextTick()
await connectionStore.saveLastDB(props.server, db) await connectionStore.saveLastDB(props.server, db)
tabStore.upsertTab({ server: props.server, db, clearValue: true }) tabStore.upsertTab({ server: props.server, db, clearValue: true })
// browserTreeRef.value?.resetExpandKey(props.server, db)
fullyLoaded.value = await browserStore.loadMoreKeys(props.server, db) fullyLoaded.value = await browserStore.loadMoreKeys(props.server, db)
browserTreeRef.value?.refreshTree() browserTreeRef.value?.refreshTree()
tabStore.setSelectedKeys(props.server) tabStore.setSelectedKeys(props.server)

View File

@ -5,7 +5,7 @@ import { NIcon, NSpace, useThemeVars } from 'naive-ui'
import Key from '@/components/icons/Key.vue' import Key from '@/components/icons/Key.vue'
import Binary from '@/components/icons/Binary.vue' import Binary from '@/components/icons/Binary.vue'
import Database from '@/components/icons/Database.vue' import Database from '@/components/icons/Database.vue'
import { filter, find, get, includes, indexOf, isEmpty, map, remove, size, startsWith, toUpper } from 'lodash' import { filter, find, get, includes, isEmpty, map, size, toUpper } from 'lodash'
import { useI18n } from 'vue-i18n' import { useI18n } from 'vue-i18n'
import Refresh from '@/components/icons/Refresh.vue' import Refresh from '@/components/icons/Refresh.vue'
import CopyLink from '@/components/icons/CopyLink.vue' import CopyLink from '@/components/icons/CopyLink.vue'
@ -40,14 +40,24 @@ const props = defineProps({
const themeVars = useThemeVars() const themeVars = useThemeVars()
const render = useRender() const render = useRender()
const i18n = useI18n() const i18n = useI18n()
/** @type {Ref<UnwrapRef<string[]>>} */
const expandedKeys = ref([])
const connectionStore = useConnectionStore() const connectionStore = useConnectionStore()
const browserStore = useBrowserStore() const browserStore = useBrowserStore()
const prefStore = usePreferencesStore() const prefStore = usePreferencesStore()
const tabStore = useTabStore() const tabStore = useTabStore()
const dialogStore = useDialogStore() const dialogStore = useDialogStore()
/**
*
* @type {ComputedRef<string[]>}
*/
const expandedKeys = computed(() => {
const tab = find(tabStore.tabList, { name: props.server })
if (tab != null) {
return get(tab, 'expandedKeys', [])
}
return []
})
/** /**
* *
* @type {ComputedRef<string[]>} * @type {ComputedRef<string[]>}
@ -168,31 +178,6 @@ const renderContextLabel = (option) => {
return h('div', { class: 'context-menu-item' }, option.label) return h('div', { class: 'context-menu-item' }, option.label)
} }
/**
*
* @param {string} key
* @param {boolean} [toggle]
*/
const expandKey = (key, toggle) => {
const idx = indexOf(expandedKeys.value, key)
if (idx === -1) {
expandedKeys.value.push(key)
} else if (toggle !== false) {
expandedKeys.value.splice(idx, 1)
}
}
const resetExpandKey = (server, db, includeDB) => {
const prefix = `${server}/db${db}`
remove(expandedKeys.value, (k) => {
if (!!!includeDB) {
return k !== prefix && startsWith(k, prefix)
} else {
return startsWith(k, prefix)
}
})
}
const handleSelectContextMenu = (key) => { const handleSelectContextMenu = (key) => {
contextMenuParam.show = false contextMenuParam.show = false
const selectedKey = get(selectedKeys.value, 0) const selectedKey = get(selectedKeys.value, 0)
@ -298,7 +283,7 @@ const onUpdateSelectedKeys = (keys, options) => {
} }
const onUpdateExpanded = (value, option, meta) => { const onUpdateExpanded = (value, option, meta) => {
expandedKeys.value = value tabStore.setExpandedKeys(props.server, value)
if (!meta.node) { if (!meta.node) {
return return
} }
@ -310,13 +295,13 @@ const onUpdateExpanded = (value, option, meta) => {
switch (meta.action) { switch (meta.action) {
case 'expand': case 'expand':
node.expanded = true node.expanded = true
if (!includes(expandedKeys.value, key)) { if (!includes(value, key)) {
expandedKeys.value.push(key) tabStore.addExpandedKey(props.server, key)
} }
break break
case 'collapse': case 'collapse':
node.expanded = false node.expanded = false
remove(expandedKeys.value, (v) => v === key) tabStore.removeExpandedKey(props.server, key)
break break
} }
node = node.children[0] node = node.children[0]
@ -525,8 +510,8 @@ const nodeProps = ({ option }) => {
return return
} }
if (!props.checkMode) { if (!props.checkMode) {
// default handle is expand current node // default handle is toggle expand current node
nextTick().then(() => expandKey(option.key)) nextTick().then(() => tabStore.toggleExpandKey(props.server, option.key))
} }
}, },
onContextmenu(e) { onContextmenu(e) {
@ -558,7 +543,7 @@ watchEffect(
if (!props.checkMode) { if (!props.checkMode) {
tabStore.setCheckedKeys(props.server) tabStore.setCheckedKeys(props.server)
} else { } else {
expandKey(`${ConnectionType.RedisDB}`, false) tabStore.addExpandedKey(props.server, `${ConnectionType.RedisDB}`)
} }
}, },
{ flush: 'post' }, { flush: 'post' },
@ -569,10 +554,9 @@ watchEffect(
const treeKey = ref(0) const treeKey = ref(0)
defineExpose({ defineExpose({
handleSelectContextMenu, handleSelectContextMenu,
resetExpandKey,
refreshTree: () => { refreshTree: () => {
treeKey.value = Date.now() treeKey.value = Date.now()
expandedKeys.value = [] tabStore.setExpandedKeys(props.server)
tabStore.setCheckedKeys(props.server) tabStore.setCheckedKeys(props.server)
}, },
deleteCheckedItems: () => { deleteCheckedItems: () => {

View File

@ -16,6 +16,7 @@ export class TabItem {
* @param {string} subTab secondary tab value * @param {string} subTab secondary tab value
* @param {string} [title] tab title * @param {string} [title] tab title
* @param {string} [icon] tab icon * @param {string} [icon] tab icon
* @param {string[]} expandedKeys
* @param {string[]} selectedKeys * @param {string[]} selectedKeys
* @param {CheckedKey[]} checkedKeys * @param {CheckedKey[]} checkedKeys
* @param {string} [type] key type * @param {string} [type] key type
@ -39,8 +40,9 @@ export class TabItem {
blank, blank,
subTab, subTab,
icon, icon,
selectedKeys, expandedKeys = [],
checkedKeys, selectedKeys = [],
checkedKeys = [],
type, type,
value, value,
server, server,
@ -61,6 +63,7 @@ export class TabItem {
this.blank = blank this.blank = blank
this.subTab = subTab this.subTab = subTab
this.icon = icon this.icon = icon
this.expandedKeys = expandedKeys
this.selectedKeys = selectedKeys this.selectedKeys = selectedKeys
this.checkedKeys = checkedKeys this.checkedKeys = checkedKeys
this.type = type this.type = type

View File

@ -1806,6 +1806,7 @@ const useBrowserStore = defineStore('browser', {
tab.emptyTab(server) tab.emptyTab(server)
tab.setSelectedKeys(server) tab.setSelectedKeys(server)
tab.setCheckedKeys(server) tab.setCheckedKeys(server)
tab.setExpandedKeys(server)
return true return true
} }
} finally { } finally {

View File

@ -1,4 +1,4 @@
import { assign, find, findIndex, get, isEmpty, pullAt, remove, set, size } from 'lodash' import { assign, find, findIndex, get, indexOf, isEmpty, pullAt, remove, set, size } from 'lodash'
import { defineStore } from 'pinia' import { defineStore } from 'pinia'
import { TabItem } from '@/objects/tabItem.js' import { TabItem } from '@/objects/tabItem.js'
import { decodeRedisKey } from '@/utils/key_convert.js' import { decodeRedisKey } from '@/utils/key_convert.js'
@ -638,11 +638,73 @@ const useTabStore = defineStore('tab', {
}, },
/** /**
* set selected keys in current display browser tree * set expanded keys for server
* @param {string} server
* @param {string[]} keys
*/
setExpandedKeys(server, keys = []) {
/** @type TabItem**/
let tab = find(this.tabList, { name: server })
if (tab != null) {
if (isEmpty(keys)) {
tab.expandedKeys = []
} else {
tab.expandedKeys = keys
}
}
},
/**
*
* @param {string} server
* @param {string} key
*/
addExpandedKey(server, key) {
/** @type TabItem**/
let tab = find(this.tabList, { name: server })
if (tab != null) {
tab.expandedKeys.push(key)
}
},
/**
*
* @param {string} server
* @param {string} key
*/
toggleExpandKey(server, key) {
/** @type TabItem**/
let tab = find(this.tabList, { name: server })
if (tab != null) {
const idx = indexOf(tab.expandedKeys, key)
if (idx === -1) {
tab.expandedKeys.push(key)
} else {
tab.expandedKeys.splice(idx, 1)
}
}
},
/**
*
* @param {string} server
* @param {string} key
*/
removeExpandedKey(server, key) {
/** @type TabItem**/
let tab = find(this.tabList, { name: server })
if (tab != null) {
remove(tab.expandedKeys, (v) => v === key)
}
},
/**
* set selected keys for server
* @param {string} server * @param {string} server
* @param {string|string[]} [keys] * @param {string|string[]} [keys]
*/ */
setSelectedKeys(server, keys = null) { setSelectedKeys(server, keys = null) {
/** @type TabItem**/
let tab = find(this.tabList, { name: server }) let tab = find(this.tabList, { name: server })
if (tab != null) { if (tab != null) {
if (keys == null) { if (keys == null) {
@ -662,6 +724,7 @@ const useTabStore = defineStore('tab', {
* @returns {CheckedKey[]} * @returns {CheckedKey[]}
*/ */
getCheckedKeys(server) { getCheckedKeys(server) {
/** @type TabItem**/
let tab = find(this.tabList, { name: server }) let tab = find(this.tabList, { name: server })
if (tab != null) { if (tab != null) {
return tab.checkedKeys || [] return tab.checkedKeys || []
@ -670,11 +733,12 @@ const useTabStore = defineStore('tab', {
}, },
/** /**
* set checked keys in current display browser tree * set checked keys for server
* @param {string} server * @param {string} server
* @param {CheckedKey[]} [keys] * @param {CheckedKey[]} [keys]
*/ */
setCheckedKeys(server, keys = null) { setCheckedKeys(server, keys = null) {
/** @type TabItem**/
let tab = find(this.tabList, { name: server }) let tab = find(this.tabList, { name: server })
if (tab != null) { if (tab != null) {
if (isEmpty(keys)) { if (isEmpty(keys)) {