Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tsk-1040: Support draft for DraggableList #2898

Merged
merged 2 commits into from
Apr 6, 2023
Merged
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
23 changes: 18 additions & 5 deletions packages/presentation/src/components/DraggableList.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,21 @@
<script lang="ts">
import { flip } from 'svelte/animate'

import { Doc } from '@hcengineering/core'
import { Doc, DocData, Ref } from '@hcengineering/core'

import { IconMoreV } from '@hcengineering/ui'
import { getClient } from '../utils'

type DocWithRank = Doc & { rank: string }
type DraftDoc = DocData<Doc> & { _id: Ref<Doc> }
type DraftDocWithRank = DocData<DocWithRank> & { _id: Ref<Doc> }
type ListType = Doc[] | DocWithRank[] | DraftDoc[] | DraftDocWithRank[]

export let objects: Doc[] | DocWithRank[]
export let objects: ListType
export let handleMove: ((fromIndex: number, toIndex: number) => void) | undefined = undefined
export let calcRank: (doc: DocWithRank, next: DocWithRank) => string
export let showContextMenu: ((evt: MouseEvent, doc: Doc) => void) | undefined = undefined
export let isDraft = false

const client = getClient()

Expand All @@ -47,10 +51,14 @@
}
}

function checkHasRank (objects: Doc[] | (Doc & { rank: string })[]): objects is (Doc & { rank: string })[] {
function checkHasRank (objects: ListType): objects is DocWithRank[] {
return 'rank' in objects[0]
}

function checkIsNotDraft (object: Doc | DocWithRank | DraftDoc | DraftDocWithRank): object is Doc | DocWithRank {
return !('_class' in object) && !isDraft
}

async function handleDrop (ev: DragEvent, toIndex: number) {
if (ev.dataTransfer && draggingIndex !== null && toIndex !== draggingIndex) {
ev.dataTransfer.dropEffect = 'move'
Expand All @@ -68,7 +76,12 @@
]
const object = objects[draggingIndex]

await client.update(object, { rank: calcRank(prev, next) })
const newRank = calcRank(prev, next)
if (isDraft) {
object.rank = newRank
} else {
await client.update(object, { rank: newRank })
}
}
resetDrag()
}
Expand All @@ -83,7 +96,7 @@
class:is-dragged-over-down={draggingIndex !== null && index > draggingIndex && index === hoveringIndex}
class:drag-over-highlight={index === dragOverIndex}
draggable={true}
on:contextmenu|preventDefault={(ev) => showContextMenu?.(ev, object)}
on:contextmenu|preventDefault={(ev) => checkIsNotDraft(object) && showContextMenu?.(ev, object)}
on:dragstart={(ev) => handleDragStart(ev, index)}
on:dragover|preventDefault={() => {
dragOverIndex = index
Expand Down