Line data Source code
1 : import type { LinkId } from '@comfyorg/graph-model/linkId'
2 :
3 : export interface ConcatClipSlot {
4 : name: string
5 : linkId: LinkId
6 : }
7 :
8 0 : export function arrangeClips<T extends ConcatClipSlot>(
9 : order: readonly string[],
10 : clips: readonly T[]
11 : ): T[] {
12 0 : const byName = new Map(clips.map((clip) => [clip.name, clip]))
13 0 : const listed = [...new Set(order)].flatMap((name) => {
14 0 : const clip = byName.get(name)
15 0 : return clip ? [clip] : []
16 : })
17 0 : return [...listed, ...clips.filter((clip) => !listed.includes(clip))]
18 : }
19 :
20 0 : export function translateOrder(
21 : order: readonly string[],
22 : previous: readonly ConcatClipSlot[],
23 : current: readonly ConcatClipSlot[]
24 : ): string[] {
25 0 : const previousLinkByName = new Map(
26 : previous.map((clip) => [clip.name, clip.linkId])
27 : )
28 0 : const currentNameByLink = new Map(
29 : current.map((clip) => [clip.linkId, clip.name])
30 : )
31 0 : return order.flatMap((name) => {
32 0 : const linkId = previousLinkByName.get(name)
33 0 : if (linkId === undefined) return [name]
34 0 : const renamed = currentNameByLink.get(linkId)
35 0 : return renamed === undefined ? [] : [renamed]
36 : })
37 : }
38 :
39 0 : export function dropIndex(
40 : pointerX: number,
41 : tileCenters: readonly number[],
42 : draggedIndex: number
43 : ): number {
44 0 : return tileCenters.filter(
45 : (center, index) => index !== draggedIndex && center < pointerX
46 : ).length
47 : }
48 :
49 0 : export function moveItem<T>(items: readonly T[], from: number, to: number) {
50 0 : const next = [...items]
51 0 : const [moved] = next.splice(from, 1)
52 0 : next.splice(to, 0, moved)
53 0 : return next
54 : }
|