File size: 3,655 Bytes
71174bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import { getMoleculesFromStore } from "@/Store/StoreExternalAccess";
import type { TreeNode } from "@/TreeNodes/TreeNode/TreeNode";
import { selectProgramatically } from "@/UI/Navigation/TitleBar/MolSelecting";
import { SelectedType } from "@/UI/Navigation/TreeView/TreeInterfaces";

// Get start molecule

/**
 * Get the molecules to act on for up and down tree navigation.
 *
 * @returns {any} The molecules to act on (siblings).
 */
export function getUpDownTreeNavMoleculesToActOn(): {
    molBefore: TreeNode | null;
    molToConsider: TreeNode
     | null;
    molAfter: TreeNode | null;
} {
    // Get all molecules.
    const molecules = getMoleculesFromStore().flattened;

    // Get the first one that is selected.
    let molsToConsider = molecules.filters.keepSelected();

    if (molsToConsider.length == 0) {
        // Since no selected molecules, consider the visible ones.
        molsToConsider = molecules.filters.keepVisible();
    }

    if (molsToConsider.length == 0) {
        // Since no visible molecules, consider all.
        molsToConsider = molecules;
    }

    if (molsToConsider.length == 0) {
        // Since no molecules, do nothing.
        return { molBefore: null, molToConsider: null, molAfter: null };
    }

    // Remove all selections (because will be set in a bit).
    molecules.map((m) => m.selected = SelectedType.False);

    // Remove all focused
    molecules.map((m) => m.focused = false);

    // Get the first one.
    const molToConsider = molsToConsider.get(0);

    // Now keep only the molecules with the same parentId.
    const {parentId} = molToConsider;
    const siblings = molecules.filter((m) => {
        return m.parentId === parentId
    });

    // Now get the index in the list of all molecules.
    const index = siblings._nodes.indexOf(molToConsider);

    const indexBefore = index - 1 < 0 ? siblings._nodes.length - 1 : index - 1;
    const indexAfter = index + 1 >= siblings._nodes.length ? 0 : index + 1;

    const molBefore = siblings._nodes[indexBefore];
    const molAfter = siblings._nodes[indexAfter];

    return { molBefore, molToConsider, molAfter };
}

/**
 * Toggle the visibility of two tree nodes.
 *
 * @param {TreeNode} newTreeNode The new tree node.
 * @param {TreeNode} oldTreeNode The old tree node.
 */
export function toggleUpDownTreeNav(newTreeNode: TreeNode, oldTreeNode: TreeNode) {
    const treeNode1Visibility = newTreeNode.visible;
    const treeNode2Visibility = oldTreeNode.visible;
    newTreeNode.visible = treeNode2Visibility;
    oldTreeNode.visible = treeNode1Visibility;

    newTreeNode.selected = SelectedType.True;
    newTreeNode.focused = true;

    newTreeNode.viewerDirty = true;
    oldTreeNode.viewerDirty = true;

    selectProgramatically(newTreeNode.id as string);
}

// let listenerAdded = false;

// export function setupUpDownTreeNav() {
//     if (listenerAdded) {
//         return;
//     }
//     document.addEventListener("keydown", handleKeyDown);
//     listenerAdded = true;
// }

// function handleKeyDown(event: KeyboardEvent) {
//     const { key } = event;

//     // Get all molecules.
//     const molecules = getMoleculesFromStore().flattened;
//     // Get the first one that is selected.
//     const selected = molecules.filters.keepSelected();

//     if (key === "ArrowUp") {
//         // Handle the up arrow key press
//         console.log("Up arrow key pressed");
//         // Add your desired behavior here
//     } else if (key === "ArrowDown") {
//         // Handle the down arrow key press
//         console.log("Down arrow key pressed");
//         // Add your desired behavior here
//     }
// }