Skip to content

Commit

Permalink
✨ Improved simulation configurations / UX enhancements
Browse files Browse the repository at this point in the history
  • Loading branch information
chanjunren committed Feb 7, 2025
1 parent 6bd818a commit e6d9749
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 15 deletions.
2 changes: 2 additions & 0 deletions src/plugin/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export const GraphContext = createContext<IGraphContext>({
simulation: null,
expanded: false,
setExpanded: () => {},
containerWidth: 0,
containerHeight: 0,
},
graphStyle: {},
});
5 changes: 3 additions & 2 deletions src/plugin/css/index.module.css
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
:root {
--vaultusaurus-modal-width: 80%;
--vaultusaurus-modal-height: 60%;
--vaultusaurus-icon-size: 1.7rem;
--vaultusaurus-icon-size: 1.6rem;
}

.container {
Expand Down Expand Up @@ -50,11 +50,12 @@
transition-property: all;
transition-duration: 0.3s;
cursor: pointer;
color: var(--default-color);
color: var(--vaultusaurus-default-color);
}

.actionIcon:hover {
opacity: 1;
scale: 110%;
}

.graphComponent {
Expand Down
43 changes: 33 additions & 10 deletions src/plugin/hooks/useGraphManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@ import {
Simulation,
} from "d3-force";
import { useEffect, useMemo, useRef, useState } from "react";

const WIDTH = 240;
const HEIGHT = 240;
const LINK_DISTANCE = 50;

export default function useGraphManager(rawData: GraphInfo) {
const graphData = useMemo(() => structuredClone(rawData), [rawData]);

Expand All @@ -30,6 +25,10 @@ export default function useGraphManager(rawData: GraphInfo) {

const [expanded, setExpanded] = useState<boolean>(false);

const containerWidth = expanded ? 800 : 320;
const containerHeight = expanded ? 450 : 320;
const LINK_DISTANCE = expanded ? 55 : 40;

useEffect(() => {
if (!graphData?.nodes) {
return;
Expand All @@ -40,15 +39,17 @@ export default function useGraphManager(rawData: GraphInfo) {
ObsidianNoteLink
>(Object.values(nodes))
.alphaDecay(0.01)
.velocityDecay(0.1)
.force(
"link",
forceLink<ObsidianNoteNode, ObsidianNoteLink>(links)
.id((d) => d.id)
.distance(LINK_DISTANCE)
.strength(0.01) // Softer link strength to reduce rigidity
)
.force("center", forceCenter(WIDTH / 2, HEIGHT / 2))
.force("charge", forceManyBody())
.force("collision", forceCollide())
.force("center", forceCenter(containerWidth / 2, containerHeight / 2)) // Center the graph
.force("charge", forceManyBody().strength(-5)) // Softer repulsion
.force("collision", forceCollide().radius(10).strength(0.01)) // Smoother collision
.on("tick", () => {
const newNodes = toNodeMap(currentSimulation.nodes());
setNodes(newNodes);
Expand All @@ -61,9 +62,31 @@ export default function useGraphManager(rawData: GraphInfo) {
console.log("STOPPING");
currentSimulation.stop();
};
}, [graphData]);
}, [graphData, expanded]);

useEffect(() => {
const handleKeyDown = (event: KeyboardEvent) => {
if (event.key === "Escape") {
setExpanded(false);
}
};

window.addEventListener("keydown", handleKeyDown);

return () => {
window.removeEventListener("keydown", handleKeyDown);
};
}, []);

return { nodes, links, simulation, expanded, setExpanded };
return {
nodes,
links,
simulation,
expanded,
setExpanded,
containerWidth,
containerHeight,
};
}

function toNodeMap(nodes: ObsidianNoteNode[]) {
Expand Down
10 changes: 8 additions & 2 deletions src/plugin/theme/VaultusaurusGraph/GraphContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ export default function GraphContent({
modal = false,
}): ReactElement<IGraphContent> {
const { graphStyle, graphManager } = useContext(GraphContext);
const { setExpanded, nodes, links } = graphManager;
const { setExpanded, nodes, links, containerWidth, containerHeight } =
graphManager;

const inlineStyles = {
"--vaultusaurus-graph-bg": graphStyle.graphBg,
Expand All @@ -44,6 +45,7 @@ export default function GraphContent({
<div
className={modal ? styles.modalContainer : styles.container}
style={inlineStyles}
onClick={(e) => e.stopPropagation()}
>
{!modal && expandable && (
<ExpandIcon
Expand All @@ -57,7 +59,11 @@ export default function GraphContent({
onClick={() => setExpanded(false)}
/>
)}
<svg viewBox={`0 0 300 300`} ref={container}>
<svg
viewBox={`0 0 ${containerWidth} ${containerHeight}`}
preserveAspectRatio="xMidYMid meet"
ref={container}
>
<g transform={transform}>
{links.map((link, idx) => {
return <GraphLink key={`obsidian-link-${idx}`} link={link} />;
Expand Down
5 changes: 4 additions & 1 deletion src/plugin/theme/VaultusaurusGraph/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ export default function VaultusaurusGraph({
>
{expanded &&
createPortal(
<div className={expanded ? styles.modalOverlay : styles.hidden}>
<div
className={expanded ? styles.modalOverlay : styles.hidden}
onClick={() => graphManager.setExpanded(false)}
>
<GraphContent modal expandable={false} />
</div>,
document.body
Expand Down

0 comments on commit e6d9749

Please sign in to comment.