Skip to content

Commit e87b997

Browse files
authored
chore: refactor (#3)
* clean up package.jsons * start refactoring draw module * finish refactor of draw function * docs * add simulation config api
1 parent 4ee689c commit e87b997

14 files changed

+415
-321
lines changed

LICENSE.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Copyright (c) 2023 Jeremiah Tabb
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4+
5+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
7+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# mindgraph

package.json

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
{
22
"name": "mindgraph",
3-
"version": "0.0.0",
43
"description": "monorepo for @mindgraph/*",
54
"scripts": {
65
"ci": "pnpm run format:check && turbo run build lint typecheck",
@@ -11,17 +10,14 @@
1110
"format": "prettier . --write",
1211
"format:check": "prettier . --check"
1312
},
14-
"private": true,
1513
"type": "module",
16-
"author": "jollyjerr",
14+
"private": true,
15+
"author": "jollyjerr <[email protected]>",
1716
"license": "MIT",
1817
"repository": {
1918
"type": "git",
2019
"url": "git+https://github.com/jollyjerr/mindgraph.git"
2120
},
22-
"bugs": {
23-
"url": "https://github.com/jollyjerr/mindgraph/issues"
24-
},
2521
"homepage": "https://github.com/jollyjerr/mindgraph#readme",
2622
"devDependencies": {
2723
"@typescript-eslint/eslint-plugin": "^5.48.2",

packages/draw/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# draw

packages/draw/package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
{
22
"name": "@mindgraph/draw",
33
"version": "0.0.1",
4-
"description": "markdown note graph view builder",
4+
"description": "web based graph view for markdown notes",
55
"scripts": {
66
"build": "vite build && tsc --emitDeclarationOnly",
77
"lint": "eslint src/**",
88
"typecheck": "tsc --noEmit",
99
"dev": "node example/server.js"
1010
},
11-
"private": true,
1211
"type": "module",
12+
"private": true,
13+
"author": "jollyjerr <[email protected]>",
1314
"license": "MIT",
1415
"files": [
1516
"dist"

packages/draw/src/canvas.ts

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import { create, select } from 'd3-selection';
2+
import { ZoomTransform } from 'd3-zoom';
3+
import { getStyles } from './style';
4+
import { loadSvgElements } from './svg';
5+
import { SimulationNode } from './types';
6+
7+
export function loadCanvas(
8+
{ width, height, deviceScale }: ReturnType<typeof getStyles>,
9+
scaledToDevice: boolean,
10+
canvasElement?: HTMLCanvasElement,
11+
) {
12+
const element = canvasElement ? select(canvasElement) : create('canvas');
13+
14+
const appliedWidth = scaledToDevice ? width * deviceScale : width;
15+
const appliedHeight = scaledToDevice ? height * deviceScale : height;
16+
17+
element.attr('width', appliedWidth);
18+
element.attr('height', appliedHeight);
19+
20+
const context = element.node()?.getContext('2d');
21+
22+
if (scaledToDevice) {
23+
context?.scale(deviceScale, deviceScale);
24+
}
25+
26+
return { element, context };
27+
}
28+
29+
export interface DrawFrameArgs {
30+
canvas: ReturnType<typeof loadCanvas>;
31+
svgElements: ReturnType<typeof loadSvgElements>;
32+
style: ReturnType<typeof getStyles>;
33+
zoomTransform: ZoomTransform;
34+
uniqueNodeColors?: string[];
35+
}
36+
37+
export function drawFrame({
38+
canvas: { element, context },
39+
svgElements: { nodeObjects, linkObjects },
40+
uniqueNodeColors,
41+
zoomTransform,
42+
style,
43+
}: DrawFrameArgs) {
44+
if (!context) return {};
45+
46+
context.save();
47+
48+
context.clearRect(
49+
0,
50+
0,
51+
Number(element.attr('width')),
52+
Number(element.attr('height')),
53+
);
54+
context.translate(zoomTransform.x, zoomTransform.y);
55+
context.scale(zoomTransform.k, zoomTransform.k);
56+
57+
linkObjects.each(function (link) {
58+
if (link.source.x && link.source.y && link.target.x && link.target.y) {
59+
context.beginPath();
60+
context.strokeStyle = style.linkColor;
61+
62+
context.moveTo(link.source.x, link.source.y);
63+
context.lineTo(link.target.x, link.target.y);
64+
65+
context.stroke();
66+
context.closePath();
67+
}
68+
});
69+
70+
const mapColorToNode = !!uniqueNodeColors;
71+
const nodeColorMap: Record<string, SimulationNode> = {};
72+
nodeObjects.each(function (n, i) {
73+
if (n.x && n.y) {
74+
const radius = Number(select(this).attr('r'));
75+
const nodeFill = mapColorToNode ? uniqueNodeColors[i] : style.nodeColor;
76+
77+
context.beginPath();
78+
context.fillStyle = nodeFill;
79+
context.arc(n.x, n.y, radius, 0, Math.PI * 2);
80+
context.fill();
81+
context.closePath();
82+
83+
const name = n.name.split('.md')[0];
84+
85+
context.beginPath();
86+
context.fillStyle = style.titleColor;
87+
context.fillText(
88+
name,
89+
n.x - context.measureText(name).width / 2,
90+
n.y + radius + style.nodeTitlePadding,
91+
);
92+
context.fill();
93+
context.closePath();
94+
95+
if (mapColorToNode) {
96+
nodeColorMap[nodeFill] = n;
97+
}
98+
}
99+
});
100+
101+
context.restore();
102+
103+
return nodeColorMap;
104+
}

packages/draw/src/draw.ts

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import { zoomIdentity, zoom, ZoomTransform } from 'd3-zoom';
2+
import { drawFrame, loadCanvas } from './canvas';
3+
import {
4+
buildSimulation,
5+
getSimulationConfig,
6+
loadSimulationNodeDatums,
7+
} from './simulation';
8+
import {
9+
convertRgbArrayToStyle,
10+
generateUniqueColors,
11+
getStyles,
12+
} from './style';
13+
import { loadSvgElements, nextFrame } from './svg';
14+
import { MindGraphConfig, NodeClickEvent } from './types';
15+
16+
export function draw({
17+
data,
18+
canvasElement,
19+
onNodeClick,
20+
style,
21+
simulationConfig,
22+
}: MindGraphConfig) {
23+
let zoomTransform = zoomIdentity;
24+
25+
const styleConfig = getStyles(style);
26+
const simulationSettings = getSimulationConfig(simulationConfig);
27+
28+
const simulationDatums = loadSimulationNodeDatums(data);
29+
const simulation = buildSimulation({
30+
...simulationDatums,
31+
...simulationSettings,
32+
width: styleConfig.width,
33+
height: styleConfig.height,
34+
});
35+
36+
const svgElements = loadSvgElements(simulationDatums, styleConfig);
37+
const visualCanvas = loadCanvas(styleConfig, true, canvasElement);
38+
39+
const tick = () => {
40+
nextFrame(svgElements);
41+
drawFrame({
42+
canvas: visualCanvas,
43+
style: styleConfig,
44+
svgElements,
45+
zoomTransform,
46+
});
47+
};
48+
49+
visualCanvas.element.call(
50+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
51+
zoom<HTMLCanvasElement, any>()
52+
.extent([
53+
[0, 0],
54+
[styleConfig.width, styleConfig.height],
55+
])
56+
.scaleExtent([simulationSettings.minZoom, simulationSettings.maxZoom])
57+
.on('zoom', (e: { transform: ZoomTransform }) => {
58+
zoomTransform = e.transform;
59+
60+
tick();
61+
}),
62+
);
63+
64+
simulation.on('tick', tick);
65+
66+
const clickMapCanvas = loadCanvas(styleConfig, false);
67+
const clickMapColors = generateUniqueColors(data.nodes.length);
68+
69+
visualCanvas.element.on('click', ({ layerX, layerY }: NodeClickEvent) => {
70+
if (!clickMapCanvas.context || typeof onNodeClick !== 'function') return;
71+
72+
const uniqueColorToNode = drawFrame({
73+
canvas: clickMapCanvas,
74+
style: styleConfig,
75+
uniqueNodeColors: clickMapColors,
76+
svgElements,
77+
zoomTransform,
78+
});
79+
80+
const clickedNode =
81+
uniqueColorToNode[
82+
convertRgbArrayToStyle(
83+
Array.from(
84+
clickMapCanvas.context.getImageData(layerX, layerY, 1, 1).data,
85+
),
86+
)
87+
];
88+
89+
if (clickedNode) {
90+
onNodeClick(clickedNode);
91+
}
92+
});
93+
}

0 commit comments

Comments
 (0)