-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
using atom/action pattern for import/export/layout #102
- Loading branch information
Showing
22 changed files
with
300 additions
and
298 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { write } from "graphology-gexf"; | ||
import { toUndirected } from "graphology-operators"; | ||
|
||
import { filteredGraphAtom, graphDatasetAtom, visualGettersAtom } from ".."; | ||
import { applyVisualProperties } from "../../appearance/utils"; | ||
import { atom } from "../../utils/atoms"; | ||
import { asyncAction } from "../../utils/producers"; | ||
import { dataGraphToFullGraph } from "../utils"; | ||
import { ExportState } from "./types"; | ||
|
||
function getEmptyExportState(): ExportState { | ||
return { type: "idle" }; | ||
} | ||
|
||
/** | ||
* Public API: | ||
* *********** | ||
*/ | ||
export const exportStateAtom = atom<ExportState>(getEmptyExportState()); | ||
|
||
/** | ||
* Actions: | ||
* ******** | ||
*/ | ||
export const exportAsGexf = asyncAction(async (callback: (content: string) => void | Promise<void>) => { | ||
// set loading | ||
exportStateAtom.set({ type: "loading" }); | ||
|
||
try { | ||
// get the full graph | ||
const graphDataset = graphDatasetAtom.get(); | ||
const filteredGraph = filteredGraphAtom.get(); | ||
let graphToExport = dataGraphToFullGraph(graphDataset, filteredGraph); | ||
|
||
// apply current apperanc eon the graph | ||
const visualGetters = visualGettersAtom.get(); | ||
applyVisualProperties(graphToExport, graphDataset, visualGetters); | ||
|
||
// change the type of the graph based on the meta type (default is directed) | ||
if (graphDataset.metadata.type === "undirected") graphToExport = toUndirected(graphToExport); | ||
|
||
// generate the gexf | ||
const content = write(graphToExport, {}); | ||
|
||
await callback(content); | ||
|
||
// idle state | ||
exportStateAtom.set({ type: "idle" }); | ||
} catch (e) { | ||
exportStateAtom.set({ type: "error" }); | ||
} | ||
}); | ||
|
||
export const exportActions = { | ||
exportAsGexf, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export type ExportState = { type: "idle" } | { type: "loading" } | { type: "error"; message?: string }; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import Graph from "graphology"; | ||
import { parse } from "graphology-gexf"; | ||
|
||
import { resetStates } from "../../context/dataContexts"; | ||
import { preferencesActions } from "../../preferences"; | ||
import { resetCamera } from "../../sigma"; | ||
import { atom } from "../../utils/atoms"; | ||
import { asyncAction } from "../../utils/producers"; | ||
import { graphDatasetActions } from "../index"; | ||
import { initializeGraphDataset } from "../utils"; | ||
import { ImportState, LocalFile, RemoteFile } from "./types"; | ||
|
||
function getEmptyImportState(): ImportState { | ||
return { type: "idle" }; | ||
} | ||
|
||
/** | ||
* Public API: | ||
* *********** | ||
*/ | ||
export const importStateAtom = atom<ImportState>(getEmptyImportState()); | ||
|
||
/** | ||
* Actions: | ||
* ******** | ||
*/ | ||
export const importRemoteGexf = asyncAction(async (remote: RemoteFile) => { | ||
const { setGraphDataset } = graphDatasetActions; | ||
const { addRemoteFile } = preferencesActions; | ||
|
||
if (importStateAtom.get().type === "loading") throw new Error("A file is already being loaded"); | ||
|
||
importStateAtom.set({ type: "loading" }); | ||
try { | ||
const response = await fetch(remote.url); | ||
const gexf = await response.text(); | ||
const graph = parse(Graph, gexf, { allowUndeclaredAttributes: true, addMissingNodes: true }); | ||
graph.setAttribute("title", remote.filename); | ||
resetStates(); | ||
setGraphDataset({ ...initializeGraphDataset(graph), origin: remote }); | ||
addRemoteFile(remote); | ||
resetCamera({ forceRefresh: true }); | ||
} catch (e) { | ||
importStateAtom.set({ type: "error", message: (e as Error).message }); | ||
throw e; | ||
} finally { | ||
importStateAtom.set({ type: "idle" }); | ||
} | ||
}); | ||
|
||
export const importLocalGexf = asyncAction(async (file: LocalFile) => { | ||
const { setGraphDataset } = graphDatasetActions; | ||
|
||
if (importStateAtom.get().type === "loading") throw new Error("A file is already being loaded"); | ||
|
||
importStateAtom.set({ type: "loading" }); | ||
try { | ||
const content = await file.source.text(); | ||
const graph = parse(Graph, content, { allowUndeclaredAttributes: true }); | ||
graph.setAttribute("title", file.filename); | ||
resetStates(); | ||
setGraphDataset({ ...initializeGraphDataset(graph), origin: file }); | ||
resetCamera({ forceRefresh: true }); | ||
} catch (e) { | ||
importStateAtom.set({ type: "error", message: (e as Error).message }); | ||
throw e; | ||
} finally { | ||
importStateAtom.set({ type: "idle" }); | ||
} | ||
}); | ||
|
||
export const importActions = { | ||
importRemoteGexf, | ||
importLocalGexf, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { CloudFile } from "../../cloud/types"; | ||
|
||
export interface GraphFile { | ||
type: string; | ||
filename: string; | ||
} | ||
export interface RemoteFile extends GraphFile { | ||
type: "remote"; | ||
url: string; | ||
} | ||
export interface LocalFile extends GraphFile { | ||
type: "local"; | ||
updatedAt: Date; | ||
size: number; | ||
source: File; | ||
} | ||
export type GraphOrigin = CloudFile | RemoteFile | LocalFile | null; | ||
|
||
export type ImportState = { type: "idle" } | { type: "loading" } | { type: "error"; message?: string }; |
Oops, something went wrong.