File size: 2,840 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
import { store } from "@/Store";
import * as api from "@/Api";
import { FileInfo } from "@/FileSystem/FileInfo";
import { TreeNodeList } from "@/TreeNodes/TreeNodeList/TreeNodeList";
import { molmodaStateKeysToRetain } from "../ParseMolModels/_ParseUsingMolModa";
import { setStoreIsDirty } from "@/Core/SaveOnClose/DirtyStore";
import { toRaw } from "vue";
import { customSelsAndStyles } from "@/Core/Styling/StyleManager";

/**
 * Runs the job when the user wants to save in the .molmoda format.
 *
 * @param {string} filename  The filename to save to.
 * @returns {Promise<undefined>}  A promise that resolves when the job is
 *     done.
 */
export function saveMolModa(filename: string): Promise<undefined> {
    // Add .molmoda to end if not already there
    if (!filename.endsWith(".molmoda")) {
        filename += ".molmoda";
    }

    return saveState(filename, store.state).then(() => {
        setStoreIsDirty(false);
        return undefined;
    });
}

/**
 * Converts the state to a JSON string.
 * 
 * @param {any} state  The state to convert.
 * @returns {string}  The JSON string.
 */
export function stateToJsonStr(state: any): string {
    const newMolData = (state.molecules as TreeNodeList).serialize();

    const newState: { [key: string]: any } = {};
    for (const key in state) {
        if (molmodaStateKeysToRetain.indexOf(key) === -1) {
            continue;
        }

        // If it's molecules specifically, use the new (serialized) version, not
        // the existing version.
        newState[key] = key === "molecules" ? newMolData : state[key];
    }

 const rawCustomStyles = toRaw(customSelsAndStyles);
 if (Object.keys(rawCustomStyles).length > 0) {
  newState["customSelsAndStyles"] = rawCustomStyles;
 }

    // Custom replacer function to handle circular references
    const seen = new WeakSet();
    return JSON.stringify(newState, (key, value) => {
        if (typeof value === "object" && value !== null) {
            if (seen.has(value)) {
                // Circular reference found, discard key or you can replace it with something else
                // console.warn("Circular reference found in state, discarding key", key);
                return;
            }
            seen.add(value);
        }
        return value;
    });
}

/**
 * Saves the state to a file.
 *
 * @param  {string} filename The filename to save to.
 * @param  {any}    state    The state to save.
 * @returns {Promise<any>} A promise that resolves when the save is complete.
 */
function saveState(filename: string, state: any): Promise<any> {
    const jsonStr = stateToJsonStr(state);

    // Create JSON file (compressed).
    return api.fs.saveTxt(
        new FileInfo({
            name: "molmoda_file.json",
            contents: jsonStr,
            compressedName: filename,
        })
    );
}