view-dialog-refresh.ts 957 B

1234567891011121314151617181920212223242526272829303132333435
  1. import type { Ref } from 'vue'
  2. export function syncViewRowFromFlatList<T extends { id: string | number }>(
  3. viewRow: Ref<T | null>,
  4. list: T[]
  5. ) {
  6. const current = viewRow.value
  7. if (!current?.id) return
  8. const updated = list.find(row => row.id === current.id)
  9. if (updated) viewRow.value = updated
  10. }
  11. export function findNodeInTreeById<T extends { id: string | number; children?: T[] }>(
  12. nodes: T[],
  13. id: string | number
  14. ): T | undefined {
  15. for (const node of nodes) {
  16. if (node.id === id) return node
  17. if (node.children?.length) {
  18. const found = findNodeInTreeById(node.children, id)
  19. if (found) return found
  20. }
  21. }
  22. return undefined
  23. }
  24. export function syncViewRowFromTree<T extends { id: string | number; children?: T[] }>(
  25. viewRow: Ref<T | null>,
  26. roots: T[]
  27. ) {
  28. const current = viewRow.value
  29. if (!current?.id) return
  30. const updated = findNodeInTreeById(roots, current.id)
  31. if (updated) viewRow.value = updated
  32. }