Skip to content

Commit

Permalink
✨ Implement rendering of global graph
Browse files Browse the repository at this point in the history
  • Loading branch information
chanjunren committed Feb 8, 2025
1 parent 324fc03 commit f5cb1b2
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 20 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: () => {},
globalModal: false,
setGlobalModal: () => {},
containerWidth: 0,
containerHeight: 0,
},
Expand Down
16 changes: 13 additions & 3 deletions src/plugin/hooks/useGraphManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ import {
Simulation,
} from "d3-force";
import { useEffect, useMemo, useRef, useState } from "react";
export default function useGraphManager(rawData: GraphInfo) {
export default function useGraphManager(
rawData: GraphInfo,
global: boolean,
minimizeCallback?: () => void
) {
const graphData = useMemo(() => structuredClone(rawData), [rawData]);

const [nodes, setNodes] = useState<{ [key: string]: ObsidianNoteNode }>(
Expand All @@ -24,9 +28,10 @@ export default function useGraphManager(rawData: GraphInfo) {
useRef<Simulation<ObsidianNoteNode, ObsidianNoteLink>>(null);

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

const containerWidth = expanded ? 1200 : 280;
const containerHeight = expanded ? (containerWidth * 9) / 16 : 280;
const containerWidth = global ? 1800 : expanded ? 1200 : 280;
const containerHeight = expanded || global ? (containerWidth * 9) / 16 : 280;
const LINK_DISTANCE = expanded ? 20 : 10;

useEffect(() => {
Expand Down Expand Up @@ -67,6 +72,9 @@ export default function useGraphManager(rawData: GraphInfo) {
const handleKeyDown = (event: KeyboardEvent) => {
if (event.key === "Escape") {
setExpanded(false);
if (minimizeCallback) {
minimizeCallback();
}
}
};

Expand All @@ -85,6 +93,8 @@ export default function useGraphManager(rawData: GraphInfo) {
setExpanded,
containerWidth,
containerHeight,
globalModal,
setGlobalModal,
};
}

Expand Down
22 changes: 16 additions & 6 deletions src/plugin/theme/VaultusaurusGraph/GraphContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,23 @@ interface IGraphContent {
expandable?: boolean;
modal?: boolean;
expanded?: boolean;
callback?: () => void;
}

export default function GraphContent({
expandable = true,
modal = false,
enableGlobal = true,
callback = () => {},
}): ReactElement<IGraphContent> {
const { graphStyle, graphManager } = useContext(GraphContext);
const { setExpanded, nodes, links, containerWidth, containerHeight } =
graphManager;
const {
setExpanded,
nodes,
links,
containerWidth,
containerHeight,
setGlobalModal,
} = graphManager;

const inlineStyles = {
"--vaultusaurus-graph-bg": graphStyle.graphBg,
Expand All @@ -48,18 +55,21 @@ export default function GraphContent({
<ExpandIcon />
</button>
)}
{!modal && enableGlobal && (
{!modal && (
<button
className={classNames(styles.iconOverlay, styles.nextButton)}
onClick={() => setExpanded(true)}
onClick={() => setGlobalModal(true)}
>
<GlobalGraphIcon />
</button>
)}
{modal && (
<button
className={classNames(styles.iconOverlay, styles.topButton)}
onClick={() => setExpanded(false)}
onClick={() => {
setExpanded(false);
callback();
}}
>
<CollapseIcon />
</button>
Expand Down
50 changes: 39 additions & 11 deletions src/plugin/theme/VaultusaurusGraph/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,38 @@ import { ReactElement } from "react";
import { createPortal } from "react-dom";
import GraphContent from "./GraphContent";

interface ILocalGraph {
interface IVaultusaurusGraph {
customGraph?: GraphInfo;
expandable?: boolean;
enableGlobal?: boolean;
global?: boolean;
minimizeGraphCallback?: () => void;
}

export default function VaultusaurusGraph({
customGraph,
expandable,
enableGlobal,
}): ReactElement<ILocalGraph> {
customGraph = null,
expandable = true,
global,
minimizeGraphCallback,
}): ReactElement<IVaultusaurusGraph> {
const globalData = usePluginData(
"docusaurus-plugin-vaultusaurus"
) as VaultusaurusGlobalData;
const graphInfo: GraphInfo =
globalData.graphInfo[window.location.pathname] || customGraph;
const graphInfo: GraphInfo = global
? globalData.globalGraphInfo
: globalData.graphInfo[window.location.pathname] || customGraph;

if (!graphInfo) {
return null;
}
const inputGraphStyle = globalData.graphStyle;

const graphManager = useGraphManager(graphInfo);
const graphManager = useGraphManager(
graphInfo,
global,
minimizeGraphCallback
);
const rawLinks = graphInfo?.links || [];
const expanded = graphManager.expanded;
const { expanded, globalModal, setGlobalModal } = graphManager;

return (
<GraphContext.Provider
Expand All @@ -50,6 +57,18 @@ export default function VaultusaurusGraph({
},
}}
>
{global && (
<div
className={styles.modalOverlay}
onClick={() => minimizeGraphCallback()}
>
<GraphContent
modal
expandable={false}
callback={() => minimizeGraphCallback()}
/>
</div>
)}
{expanded &&
createPortal(
<div
Expand All @@ -60,7 +79,16 @@ export default function VaultusaurusGraph({
</div>,
document.body
)}
<GraphContent enableGlobal={enableGlobal} expandable={expandable} />
{globalModal &&
createPortal(
<VaultusaurusGraph
global
minimizeGraphCallback={() => setGlobalModal(false)}
/>,
document.body
)}

{!global && <GraphContent expandable={expandable} />}
</GraphContext.Provider>
);
}

0 comments on commit f5cb1b2

Please sign in to comment.