| import { visualizationApi } from "@/Api/Visualization"; |
| import { waitForCondition } from "@/Core/Utils/MiscUtils"; |
| import type { FileInfo } from "@/FileSystem/FileInfo"; |
| import { store } from "@/Store"; |
| import { pushToStoreList, setStoreVar } from "@/Store/StoreExternalAccess"; |
| import { treeNodeListDeserialize } from "@/TreeNodes/Deserializers"; |
| import type { ITreeNode } from "@/TreeNodes/TreeNode/TreeNode"; |
| import type { TreeNodeList } from "@/TreeNodes/TreeNodeList/TreeNodeList"; |
| import type { ILog } from "@/UI/Panels/Log/LogUtils"; |
| import { ViewerParent } from "@/UI/Panels/Viewer/Viewers/ViewerParent"; |
| import { replaceAllCustomStyles } from "@/Core/Styling/StyleManager"; |
| import { goldenLayout } from "@/UI/Layout/GoldenLayout/GoldenLayoutCommon"; |
| import { layoutApi } from "@/Api/Layout"; |
| import { sanitizeHtml } from "@/Core/Security/Sanitize"; |
| import { isMobile } from "@/Core/GlobalVars"; |
| export const molmodaStateKeysToRetain = [ |
| "molecules", |
| "log", |
| "goldenLayout", |
| "viewerVantagePoint", |
| "projectTitle", |
| ]; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| export async function parseUsingMolModa( |
| fileInfo: FileInfo |
| ): Promise<void | TreeNodeList> { |
| const stateFromJson = await jsonStrToState(fileInfo.contents); |
| |
| for (const key of molmodaStateKeysToRetain) { |
| let viewer: ViewerParent; |
|
|
| let sanitizedLog: any = null; |
| if (key === "log") { |
| sanitizedLog = (stateFromJson[key] as ILog[]).map( |
| async (logEntry) => ({ |
| ...logEntry, |
| message: await sanitizeHtml(logEntry.message), |
| parameters: await sanitizeHtml(logEntry.parameters), |
| }) |
| ); |
| sanitizedLog = await Promise.all(sanitizedLog); |
| } |
|
|
| switch (key) { |
| case "log": |
| pushToStoreList(key, sanitizedLog); |
| break; |
| case "molecules": |
| (stateFromJson[key] as TreeNodeList).addToMainTree( |
| null, |
| false, |
| true, |
| false |
| ); |
| break; |
| case "goldenLayout": |
| if (isMobile) { |
| break; |
| } |
| if (stateFromJson[key]) { |
| setStoreVar("goldenLayout", stateFromJson[key]); |
| if (goldenLayout) { |
| layoutApi.setSessionLayoutActive(true); |
| |
| |
| goldenLayout.loadLayout(stateFromJson[key]); |
| } else { |
| console.warn( |
| "GoldenLayout object not available to load layout state." |
| ); |
| } |
| } |
| break; |
| case "viewerVantagePoint": |
| setStoreVar("viewerVantagePoint", stateFromJson[key]); |
| viewer = await visualizationApi.viewer; |
|
|
| |
| |
| |
| await waitForCondition(() => { |
| |
| |
| return viewer._mol3dObj !== undefined; |
| }); |
| viewer.setView(stateFromJson[key]); |
| break; |
| case "projectTitle": |
| setStoreVar("projectTitle", stateFromJson[key] || ""); |
| break; |
| } |
| } |
| |
| if (stateFromJson["customSelsAndStyles"]) { |
| replaceAllCustomStyles(stateFromJson["customSelsAndStyles"]); |
| } |
| fixLog(); |
|
|
| return undefined; |
| } |
|
|
| |
| |
| |
| function fixLog() { |
| |
| let log = store.state["log"]; |
|
|
| |
| log = log.sort((a: ILog, b: ILog) => { |
| const an = a.timestampSecs as number; |
| const bn = b.timestampSecs as number; |
| if (an < bn) { |
| return -1; |
| } |
| if (an > bn) { |
| return 1; |
| } |
| return 0; |
| }); |
|
|
| |
| |
| const newLog: ILog[] = []; |
| const keysSeen: Set<string> = new Set([]); |
| for (const logItem of log) { |
| const key = [ |
| logItem.timestampSecs.toString(), |
| logItem.message, |
| logItem.parameters, |
| logItem.jobId, |
| ].join("-"); |
| if (!keysSeen.has(key)) { |
| newLog.push(logItem); |
| keysSeen.add(key); |
| } |
| } |
|
|
| setStoreVar("log", newLog); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| async function jsonStrToState(jsonStr: string): Promise<any> { |
| |
| |
| jsonStr = jsonStr.replace(/"viewerDirty": *false/g, '"viewerDirty":true'); |
| |
| const obj = JSON.parse(jsonStr); |
|
|
| |
| |
| |
| |
| |
| |
| const sanitizeTitles = async function (currentObj: any) { |
| if (!currentObj || typeof currentObj !== "object") { |
| return; |
| } |
| if (Array.isArray(currentObj)) { |
| currentObj.forEach(sanitizeTitles); |
| } else { |
| if (typeof currentObj.title === "string") { |
| currentObj.title = await sanitizeHtml(currentObj.title); |
| } |
| if (currentObj.nodes) { |
| await sanitizeTitles(currentObj.nodes); |
| } |
| } |
| }; |
| if (obj.molecules) { |
| await sanitizeTitles(obj.molecules); |
| } |
| return treeNodeListDeserialize(obj.molecules as ITreeNode[]) |
| .then((mols: TreeNodeList) => { |
| obj.molecules = mols; |
| return obj; |
| }) |
| .catch((err: any) => { |
| throw err; |
| }); |
| } |
|
|