diff options
author | Thomas F. K. Jorna <[email protected]> | 2021-07-18 19:37:03 +0200 |
---|---|---|
committer | Thomas F. K. Jorna <[email protected]> | 2021-07-18 19:37:03 +0200 |
commit | 86b2cf2696ecf3171c9ef05734c940ffbc750537 (patch) | |
tree | 8d70d4cd6e1b1bebaaa6b728df27ce85daae147f /app | |
parent | a38f0d5f69f5583732b5a844f472718160d07334 (diff) |
exiting and entering local mode works well
Diffstat (limited to 'app')
-rw-r--r-- | app/components/graph/graph.tsx | 69 | ||||
-rw-r--r-- | app/components/tweaks/tweaks.tsx | 4 | ||||
-rw-r--r-- | app/screens/graph/graph-screen.tsx | 14 |
3 files changed, 43 insertions, 44 deletions
diff --git a/app/components/graph/graph.tsx b/app/components/graph/graph.tsx index 453e15d..8a3b6be 100644 --- a/app/components/graph/graph.tsx +++ b/app/components/graph/graph.tsx @@ -84,39 +84,37 @@ export const Graph = observer(function Graph(props: GraphProps): JSX.Element { // For the expandable version of the graph - const nodesById = useMemo(() => { - const nodesById = Object.fromEntries(gData.nodes.map((node) => [node.index, node])) - console.log(nodesById) - // link parent/children - gData.nodes.forEach((node) => { - typeof physics.rootId === "number" - ? (node.collapsed = node.index !== physics.rootId) - : (node.collapsed = node.id !== physics.rootId) - node.childLinks = [] - }) - gData.links.forEach((link) => nodesById[link.sourceIndex].childLinks.push(link)) - return nodesById - }, [gData]) - - const getPrunedTree = useCallback(() => { - const visibleNodes = [] - const visibleLinks = [] - ;(function traverseTree(node = nodesById[physics.rootId]) { - visibleNodes.push(node) - if (node.collapsed) return - visibleLinks.push(...node.childLinks) - node.childLinks - .map((link) => - typeof link.targetIndex === "object" ? link.targetIndex : nodesById[link.targetIndex], - ) // get child node - .forEach(traverseTree) - })() - - return { nodes: visibleNodes, links: visibleLinks } - }, [nodesById]) - - const [prunedTree, setPrunedTree] = useState(getPrunedTree()) - + /* const nodesById = useMemo(() => { + * const nodesById = Object.fromEntries(gData.nodes.map((node) => [node.index, node])) + * console.log(nodesById) + * // link parent/children + * gData.nodes.forEach((node) => { + * typeof physics.rootId === "number" + * ? (node.collapsed = node.index !== physics.rootId) + * : (node.collapsed = node.id !== physics.rootId) + * node.childLinks = [] + * }) + * gData.links.forEach((link) => nodesById[link.sourceIndex].childLinks.push(link)) + * return nodesById + * }, [gData]) + * const getPrunedTree = useCallback(() => { + * const visibleNodes = [] + * const visibleLinks = [] + * ;(function traverseTree(node = nodesById[physics.rootId]) { + * visibleNodes.push(node) + * if (node.collapsed) return + * visibleLinks.push(...node.childLinks) + * node.childLinks + * .map((link) => + * typeof link.targetIndex === "object" ? link.targetIndex : nodesById[link.targetIndex], + * ) // get child node + * .forEach(traverseTree) + * })() + + * return { nodes: visibleNodes, links: visibleLinks } + * }, [nodesById]) + * const [prunedTree, setPrunedTree] = useState(getPrunedTree()) + */ const handleNodeClick = useCallback((node) => { node.collapsed = !node.collapsed // toggle collapse state setPrunedTree(getPrunedTree()) @@ -204,7 +202,7 @@ onLinkHover={handleLinkHover} const [localGraphData, setLocalGraphData] = useState({ nodes: [], links: [] }) useEffect(() => { - !physics.local && setPhysics({ ...physics, local: true }) + localGraphData.nodes.length && !physics.local && setPhysics({ ...physics, local: true }) }, [localGraphData]) const getLocalGraphData = (node) => { @@ -213,6 +211,7 @@ onLinkHover={handleLinkHover} let g = localGraphData console.log(g.nodes) if (!node.local) { + g = {nodes: [], links: []} console.log("length is 0") node.local = true //keep track of these boys g.nodes.push(node) //only add the clicked node if its the first @@ -278,7 +277,7 @@ onLinkHover={handleLinkHover} ref={fgRef} autoPauseRedraw={false} //graphData={gData} - graphData={physics.local ? localGraphData : physics.collapse ? prunedTree : gData} + graphData={physics.local ? localGraphData : gData} nodeAutoColorBy={physics.colorful ? "id" : undefined} nodeColor={ !physics.colorful diff --git a/app/components/tweaks/tweaks.tsx b/app/components/tweaks/tweaks.tsx index e52e587..9e23afb 100644 --- a/app/components/tweaks/tweaks.tsx +++ b/app/components/tweaks/tweaks.tsx @@ -62,11 +62,11 @@ export const Tweaks = observer(function Tweaks(props: TweaksProps): JSX.Element step={1} /> <Text preset="fieldLabel" text={"Link Force: " + physics.linkStrength} /> <Slider style={{ height: 40, width: "90%" }} - minimumValue={-2} + minimumValue={0} maximumValue={2} onValueChange={(value) => { setPhysics({ ...physics, linkStrength: value }) }} value={physics.linkStrength} - step={0.1} + step={0.01} /> <Text preset="fieldLabel" text={"'Link Iterations': " + physics.linkIts} /> <Slider style={{ height: 40, width: "90%" }} diff --git a/app/screens/graph/graph-screen.tsx b/app/screens/graph/graph-screen.tsx index f06afc9..f537128 100644 --- a/app/screens/graph/graph-screen.tsx +++ b/app/screens/graph/graph-screen.tsx @@ -48,7 +48,7 @@ export const GraphScreen = observer(function GraphScreen() { labels: true, labelScale: 1.5, alphaDecay: 0.16, - alphaTarget: 0.01, + alphaTarget: 0, velocityDecay: 0.25, gravity: 0.5, gravityOn: true, @@ -64,8 +64,8 @@ export const GraphScreen = observer(function GraphScreen() { const value: string = await AsyncStorage.getItem("@physics") if (value !== null) { const valueJson = JSON.parse(value) - console.log(Object.keys(valueJson).length + " is not " + Object.keys(physicsInit).length) if (Object.keys(valueJson).length === Object.keys(physicsInit).length) { + valueJson.local=false; return valueJson } else { return physicsInit @@ -102,23 +102,23 @@ export const GraphScreen = observer(function GraphScreen() { //"ComponentOnMount" // Get previous settings and the data from the org-roam-server - const sanitizeGraph = (data, nodeIds: string[]) => { + const sanitizeGraph = (data) => { const cleanLinks = [] data.links.forEach((link, j) => { let target let source - for (let i = 0; i < nodeIds.length; i++) { + for (let i = 0; i < data.nodes.length; i++) { let a = data.nodes[i] !a.neighbors && (a.neighbors = []) !a.links && (a.links = []) // the first time around, // index the node as not a part of the local graph !j && (a.local=false); - if (link.target === nodeIds[i]) { + if (link.target === data.nodes[i].id) { a.links.push(link) target = [a, i] link.target === link.source ? null : cleanLinks.push(link) - } else if (link.source === nodeIds[i]) { + } else if (link.source === data.nodes[i].id) { a.links.push(link) source = [a, i] } @@ -155,7 +155,7 @@ export const GraphScreen = observer(function GraphScreen() { let nods = getNodesById(dataa.data) setNodeIds(nods) console.log(nodeIds) - let cleanData = sanitizeGraph(dataa.data, nods) + let cleanData = sanitizeGraph(dataa.data) console.log(cleanData) setGraphData(cleanData) }) |