File size: 4,881 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/* eslint-disable @typescript-eslint/ban-ts-comment */

import { store } from "@/Store";
import { treeNodeListDeepClone } from "@/TreeNodes/Deserializers";
import { TreeNode } from "@/TreeNodes/TreeNode/TreeNode";
import { TreeNodeList } from "@/TreeNodes/TreeNodeList/TreeNodeList";

let timeoutId: number;
let pauseAddToUndoStack = false;

// Don't make this too big. The undo stack can take up a lot of memory. TODO:
// Perhaps in time you could store it in localstorage or something.
const maxItemsOnUndoStack = 2;

// setInterval(() => {
//     console.log(
//         store.state.undoStack.map((m: any) => m.undoStackId).length,
//         store.state.redoStack.map((m: any) => m.undoStackId).length,
//     );
// }
// , 1000);

/**
 * Temporarily pauses adding to the undo stack.
 */
function _tmpPauseAddToUndoStack() {
    // Cancel any pending additions to undo stack

    window.clearTimeout(timeoutId);

    // Prevent new ones.
    pauseAddToUndoStack = true;

    // In a bit, start allowing again
    setTimeout(() => {
        pauseAddToUndoStack = false;
    }, 500);
}

/**
 * Makes all molecules dirty.
 *
 * @param  {TreeNodeList} mols The molecules.
 */
function _makeAllMolsDirty(mols: TreeNodeList) {
    mols.filters.onlyTerminal.forEach((mol: TreeNode) => {
        mol.viewerDirty = true;
    });
}

/**
 * Pushes a new item to the undo stack.
 *
 * @param  {TreeNodeList} item The item to push.
 */
function _addItemToUndoStack(item: TreeNodeList) {
    const undoStack = store.state.undoStack as TreeNodeList[];
    if (undoStack.length > maxItemsOnUndoStack) {
        undoStack.shift();
    }
    undoStack.push(item);
    store.commit("setVar", {
        name: "undoStack",
        val: undoStack,
    });
}

/**
 * Adds an item to the undo stack after the user has't done anything in a bit.
 * By adding to the stack only after a delay, you prevent adding excessive
 * changes in rapid succession.
 *
 * @param  {TreeNodeList} molecules The item to push.
 */
export function addToUndoStackAfterUserInaction(molecules: TreeNodeList) {
    if (pauseAddToUndoStack) {
        return;
    }

    if (timeoutId) {
        window.clearTimeout(timeoutId);
    }
    timeoutId = window.setTimeout(async () => {
        // Remove any redos.
        store.commit("setVar", {
            name: "redoStack",
            val: [],
        });

        // Make new molecule.
        const moleculesObjToAddToStack = await treeNodeListDeepClone(molecules);

        _makeAllMolsDirty(moleculesObjToAddToStack);
        // moleculesObjToAddToStack.filters.onlyTerminal
        // .forEach((mol: TreeNode) => { mol.viewerDirty = true; });

        // Move new molecules to the stack
        _addItemToUndoStack(moleculesObjToAddToStack);
        // console.log("added");
    }, 1000);
}

/**
 * Undo the last user action.
 *
 * @param  {any} store The Vuex store.
 */
export function undo(store: any) {
    const undoStack = store.state.undoStack as TreeNodeList[];
    const redoStack = store.state.redoStack as TreeNodeList[];

    let lastItemOnUndoStack = undoStack.pop();

    // Update the stacks
    store.commit("setVar", {
        name: "undoStack",
        val: undoStack,
    });

    if (lastItemOnUndoStack) {
        // Move current last one to redo.
        redoStack.push(lastItemOnUndoStack);

        store.commit("setVar", {
            name: "redoStack",
            val: redoStack,
        });

        // Get the new last one, but keep on stack
        lastItemOnUndoStack = undoStack[undoStack.length - 1];

        if (lastItemOnUndoStack) {
            // _makeAllMolsDirty(lastItemOnUndoStack)

            // store.commit("replaceMolecules", lastItemOnUndoStack);
            store.commit("setVar", {
                name: "molecules",
                val: lastItemOnUndoStack,
            });

            // Make sure this undo isn't itself added to the undo stack.
            _tmpPauseAddToUndoStack();
        }

        // console.log("undo");
    }
}

/**
 * Redo the last user action.
 *
 * @param  {any} store The Vuex store.
 */
export async function redo(store: any) {
    const redoStack = store.state.redoStack as TreeNodeList[];

    const lastItemRedoStack = redoStack.pop();

    // Update the stacks
    store.commit("setVar", {
        name: "redoStack",
        val: redoStack,
    });

    if (lastItemRedoStack) {
        // Move current last one to redo.
        _addItemToUndoStack(lastItemRedoStack);

        // store.commit("replaceMolecules", lastItemOnUndoStack);
        store.commit("setVar", {
            name: "molecules",
            val: lastItemRedoStack,
        });

        // const viewer = await api.visualization.viewer;
        // viewer.renderAll();

        // Make sure this undo isn't itself added to the undo stack.
        _tmpPauseAddToUndoStack();

        // console.log("redo");
    }
}