perf: expand or collapse automatically when node owns one child #40

This commit is contained in:
tiny-craft 2023-09-28 20:47:03 +08:00
parent 3de2a47d12
commit da4b8b9a4f
1 changed files with 19 additions and 9 deletions

View File

@ -4,7 +4,7 @@ import { ConnectionType } from '@/consts/connection_type.js'
import { NIcon, NSpace, NTag } from 'naive-ui' import { NIcon, NSpace, NTag } from 'naive-ui'
import Key from '@/components/icons/Key.vue' import Key from '@/components/icons/Key.vue'
import ToggleDb from '@/components/icons/ToggleDb.vue' import ToggleDb from '@/components/icons/ToggleDb.vue'
import { find, get, includes, indexOf, isEmpty, remove } from 'lodash' import { find, get, includes, indexOf, isEmpty, pull, remove, size } 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'
@ -300,15 +300,25 @@ const onUpdateExpanded = (value, option, meta) => {
if (!meta.node) { if (!meta.node) {
return return
} }
// console.log(JSON.stringify(meta))
// keep expand or collapse children while they own more than 1 child
let node = meta.node
while (node != null && size(node.children) === 1) {
const key = node.children[0].key
switch (meta.action) { switch (meta.action) {
case 'expand': case 'expand':
meta.node.expanded = true node.expanded = true
if (!includes(expandedKeys.value, key)) {
expandedKeys.value.push(key)
}
break break
case 'collapse': case 'collapse':
meta.node.expanded = false node.expanded = false
remove(expandedKeys.value, (v) => v === key)
break break
} }
node = node.children[0]
}
} }
const onUpdateSelectedKeys = (keys, options) => { const onUpdateSelectedKeys = (keys, options) => {