Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 3 additions & 19 deletions plugins/drive-resources/src/components/MoveResource.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,18 @@
<script lang="ts">
import { Ref } from '@hcengineering/core'
import type { Drive, Folder, Resource } from '@hcengineering/drive'
import presentation, { Card, getClient, SpaceSelector } from '@hcengineering/presentation'
import presentation, { Card, SpaceSelector } from '@hcengineering/presentation'
import view from '@hcengineering/view'
import { ObjectBox } from '@hcengineering/view-resources'
import { createEventDispatcher } from 'svelte'

import drive from '../plugin'
import { moveResources } from '../utils'
import { findAllChildren, moveResources } from '../utils'

import ResourcePresenter from './ResourcePresenter.svelte'

export let value: Resource

const client = getClient()
const hierarchy = client.getHierarchy()
const dispatch = createEventDispatcher()

let space: Ref<Drive> = value.space as Ref<Drive>
Expand All @@ -42,21 +40,7 @@
$: void updateChildren(value)

async function updateChildren (resource: Resource): Promise<void> {
children = await findChildren(resource)
}

async function findChildren (resource: Resource): Promise<Array<Ref<Folder>>> {
if (hierarchy.isDerived(resource._class, drive.class.Folder)) {
const children = await client.findAll(
drive.class.Folder,
{ space: resource.space, path: resource._id as Ref<Folder> },
{ projection: { _id: 1 } }
)

return children.map((p) => p._id)
}

return []
children = await findAllChildren(resource)
}

$: canSave = space !== value.space || parent !== value.parent
Expand Down
32 changes: 32 additions & 0 deletions plugins/drive-resources/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,38 @@ export async function resolveParents (object: Resource): Promise<Doc[]> {
return parents.reverse()
}

export async function findAllChildren (resource: Resource, maxDepth: number = 10): Promise<Array<Ref<Folder>>> {
const client = getClient()
const hierarchy = client.getHierarchy()

if (!hierarchy.isDerived(resource._class, drive.class.Folder)) {
return []
}

const allChildren: Array<Ref<Folder>> = []
let currentLevel: Array<Ref<Folder>> = [resource._id as Ref<Folder>]
let depth = 0

while (currentLevel.length > 0 && depth < maxDepth) {
const children = await client.findAll(
drive.class.Folder,
{ space: resource.space, parent: { $in: currentLevel } },
{ projection: { _id: 1 } }
)

if (children.length === 0) {
break
}

const childIds = children.map((p) => p._id)
allChildren.push(...childIds)
currentLevel = childIds
depth++
}

return allChildren
}

export async function getUploadOptionsByFragment (space: Ref<Drive>, fragment: string): Promise<FileUploadOptions> {
const [, _id, _class] = decodeURIComponent(fragment).split('|')
if (_class === drive.class.Folder) {
Expand Down
Loading