fix: browser node tree not sync when new or delete node

This commit is contained in:
tiny-craft 2023-07-20 16:02:02 +08:00
parent c447f2cdf0
commit 9d897894b6
3 changed files with 55 additions and 31 deletions

View File

@ -1 +1 @@
e8efe46ff15777b1af82225bac5f4626 5f51299692e0fa4f59808fc597b869bc

View File

@ -74,6 +74,10 @@ watch(
) )
const keyType = redisTypes.STRING const keyType = redisTypes.STRING
/**
* view value
* @type {ComputedRef<string>}
*/
const viewValue = computed(() => { const viewValue = computed(() => {
switch (viewAs.value) { switch (viewAs.value) {
case types.PLAIN_TEXT: case types.PLAIN_TEXT:
@ -199,7 +203,7 @@ const onSaveValue = async () => {
</div> </div>
<div class="value-wrapper flex-item-expand flex-box-v"> <div class="value-wrapper flex-item-expand flex-box-v">
<n-scrollbar v-if="!inEdit" class="flex-item-expand"> <n-scrollbar v-if="!inEdit" class="flex-item-expand">
<n-code :code="viewValue" :language="viewLanguage" show-line-numbers word-wrap /> <n-code :code="viewValue" :language="viewLanguage" show-line-numbers word-wrap style="cursor: text" />
</n-scrollbar> </n-scrollbar>
<n-input <n-input
v-else v-else

View File

@ -1,5 +1,5 @@
import { defineStore } from 'pinia' import { defineStore } from 'pinia'
import { endsWith, get, isEmpty, join, remove, size, slice, split, sumBy, toUpper, uniq } from 'lodash' import { endsWith, get, isEmpty, join, remove, size, slice, sortedIndexBy, split, sumBy, toUpper, uniq } from 'lodash'
import { import {
AddHashField, AddHashField,
AddListItem, AddListItem,
@ -618,11 +618,14 @@ const useConnectionStore = defineStore('connections', {
* @param {string} connName * @param {string} connName
* @param {number} db * @param {number} db
* @param {string[]} keys * @param {string[]} keys
* @param {boolean} [sortInsert]
* @return {{success: boolean, newKey: number, newLayer: number, replaceKey: number}}
* @private * @private
*/ */
_addKeyNodes(connName, db, keys) { _addKeyNodes(connName, db, keys, sortInsert) {
const result = { success: false, newLayer: 0, newKey: 0, replaceKey: 0 }
if (isEmpty(keys)) { if (isEmpty(keys)) {
return return result
} }
const separator = this._getSeparator(connName) const separator = this._getSeparator(connName)
const dbs = this.databases[connName] const dbs = this.databases[connName]
@ -655,8 +658,16 @@ const useConnectionStore = defineStore('connections', {
children: [], children: [],
} }
nodeMap.set(nodeKey, selectedNode) nodeMap.set(nodeKey, selectedNode)
if (sortInsert) {
const index = sortedIndexBy(children, selectedNode, (elem) => {
return elem.key
})
children.splice(index, 0, selectedNode)
} else {
children.push(selectedNode) children.push(selectedNode)
} }
result.newLayer += 1
}
children = selectedNode.children children = selectedNode.children
handlePath += separator handlePath += separator
} else { } else {
@ -674,11 +685,22 @@ const useConnectionStore = defineStore('connections', {
} }
nodeMap.set(nodeKey, selectedNode) nodeMap.set(nodeKey, selectedNode)
if (!replaceKey) { if (!replaceKey) {
if (sortInsert) {
const index = sortedIndexBy(children, selectedNode, (elem) => {
return elem.key > selectedNode.key
})
children.splice(index, 0, selectedNode)
} else {
children.push(selectedNode) children.push(selectedNode)
} }
result.newKey += 1
} else {
result.replaceKey += 1
} }
} }
} }
}
return result
}, },
/** /**
@ -708,25 +730,24 @@ const useConnectionStore = defineStore('connections', {
const separator = this._getSeparator(connName) const separator = this._getSeparator(connName)
const keyParts = split(key, separator) const keyParts = split(key, separator)
const totalParts = size(keyParts) const totalParts = size(keyParts)
const parentKey = slice(keyParts, 0, totalParts - 1)
const dbNode = get(this.databases, [connName, db], {}) const dbNode = get(this.databases, [connName, db], {})
const isDBRoot = isEmpty(parentKey)
let node let node
if (isDBRoot) { // find last exists ancestor key
// use db root node let i = totalParts - 1
node = dbNode for (; i > 0; i--) {
} else { const parentKey = join(slice(keyParts, 0, i), separator)
node = nodeMap.get(`${ConnectionType.RedisKey}/${join(parentKey, separator)}`) node = nodeMap.get(`${ConnectionType.RedisKey}/${parentKey}`)
if (node != null) {
break
}
} }
if (node == null) { if (node == null) {
return false node = dbNode
} }
const keyCountUpdated = this._tidyNodeChildren(node, skipResort) const keyCountUpdated = this._tidyNodeChildren(node, skipResort)
if (keyCountUpdated) { if (keyCountUpdated) {
// update key count of parent and above // update key count of parent and above
if (!isDBRoot) {
let i = totalParts - 1
for (; i > 0; i--) { for (; i > 0; i--) {
const parentKey = join(slice(keyParts, 0, i), separator) const parentKey = join(slice(keyParts, 0, i), separator)
const parentNode = nodeMap.get(`${ConnectionType.RedisKey}/${parentKey}`) const parentNode = nodeMap.get(`${ConnectionType.RedisKey}/${parentKey}`)
@ -735,7 +756,6 @@ const useConnectionStore = defineStore('connections', {
} }
parentNode.keys = sumBy(parentNode.children, 'keys') parentNode.keys = sumBy(parentNode.children, 'keys')
} }
}
// update key count of db // update key count of db
dbNode.keys = sumBy(dbNode.children, 'keys') dbNode.keys = sumBy(dbNode.children, 'keys')
} }
@ -785,8 +805,10 @@ const useConnectionStore = defineStore('connections', {
const { data, success, msg } = await SetKeyValue(connName, db, key, keyType, value, ttl) const { data, success, msg } = await SetKeyValue(connName, db, key, keyType, value, ttl)
if (success) { if (success) {
// update tree view data // update tree view data
this._addKeyNodes(connName, db, [key]) const { newKey = 0 } = this._addKeyNodes(connName, db, [key], true)
if (newKey > 0) {
this._tidyNode(connName, db, key) this._tidyNode(connName, db, key)
}
return { success } return { success }
} else { } else {
return { success, msg } return { success, msg }
@ -1170,14 +1192,12 @@ const useConnectionStore = defineStore('connections', {
const anceKey = join(slice(keyParts, 0, i), separator) const anceKey = join(slice(keyParts, 0, i), separator)
if (i > 0) { if (i > 0) {
const anceNode = nodeMap.get(`${ConnectionType.RedisKey}/${anceKey}`) const anceNode = nodeMap.get(`${ConnectionType.RedisKey}/${anceKey}`)
const redisKey = join(slice(keyParts, 0, i + 1), separator)
remove(anceNode.children, { type: ConnectionType.RedisKey, redisKey })
if (isEmpty(anceNode.children)) { if (isEmpty(anceNode.children)) {
nodeMap.delete(`${ConnectionType.RedisKey}/${anceKey}`) nodeMap.delete(`${ConnectionType.RedisKey}/${anceKey}`)
} else { } else {
// remove last empty layer node from parent
if (i !== totalParts - 1) {
const redisKey = slice(keyParts, 0, i + 1)
remove(anceNode.children, { type: ConnectionType.RedisKey, redisKey })
}
break break
} }
} else { } else {