Line data Source code
1 : import { readonly, ref } from 'vue'
2 :
3 : import {
4 : DEFAULT_ARRANGE_GAP,
5 : useArrangeNodes
6 : } from '@/composables/graph/useArrangeNodes'
7 : import type { ArrangeLayout } from '@/composables/graph/useArrangeNodes'
8 :
9 29 : export function useArrangeSession() {
10 29 : const { arrangeNodes } = useArrangeNodes()
11 :
12 29 : const activeLayout = ref<ArrangeLayout | null>(null)
13 29 : const gap = ref(DEFAULT_ARRANGE_GAP)
14 29 : let pendingFrame: number | null = null
15 :
16 29 : const cancelPendingFrame = () => {
17 0 : if (pendingFrame === null) return
18 0 : cancelAnimationFrame(pendingFrame)
19 0 : pendingFrame = null
20 : }
21 :
22 29 : const reset = () => {
23 0 : cancelPendingFrame()
24 0 : activeLayout.value = null
25 0 : gap.value = DEFAULT_ARRANGE_GAP
26 : }
27 :
28 29 : const start = (layout: ArrangeLayout) => {
29 0 : gap.value = DEFAULT_ARRANGE_GAP
30 0 : activeLayout.value = layout
31 0 : arrangeNodes(layout, { gap: gap.value, captureUndo: true })
32 : }
33 :
34 29 : const previewGap = (nextGap: number) => {
35 0 : if (activeLayout.value === null) return
36 0 : gap.value = nextGap
37 0 : cancelPendingFrame()
38 0 : pendingFrame = requestAnimationFrame(() => {
39 0 : pendingFrame = null
40 0 : if (activeLayout.value === null) return
41 0 : arrangeNodes(activeLayout.value, { gap: nextGap, captureUndo: false })
42 : })
43 : }
44 :
45 29 : const commitGap = (nextGap: number) => {
46 0 : if (activeLayout.value === null) return
47 0 : cancelPendingFrame()
48 0 : gap.value = nextGap
49 0 : arrangeNodes(activeLayout.value, { gap: nextGap, captureUndo: true })
50 : }
51 :
52 29 : return {
53 : activeLayout: readonly(activeLayout),
54 : gap: readonly(gap),
55 : start,
56 : previewGap,
57 : commitGap,
58 : reset
59 : }
60 : }
|