Skip to content

Commit 211e3f3

Browse files
committed
Added colors and function for writing colors. Currently the last color writeen overwrites all edges.
1 parent 47936cf commit 211e3f3

File tree

1 file changed

+44
-6
lines changed

1 file changed

+44
-6
lines changed

main.ts

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Plugin } from 'obsidian';
1+
import { Plugin, WorkspaceLeaf, Notice } from 'obsidian';
22
import { getAPI } from 'obsidian-dataview';
33

44
export default class UniqueMetadataKeysPlugin extends Plugin {
@@ -11,6 +11,11 @@ export default class UniqueMetadataKeysPlugin extends Plugin {
1111
name: 'Print Unique Metadata Keys',
1212
callback: () => this.printUniqueMetadataKeys()
1313
});
14+
this.addCommand({
15+
id: 'print-graph-leaf',
16+
name: 'Print Graph Leaf',
17+
callback: () => this.findGraphLeaf()
18+
});
1419
}
1520

1621
printUniqueMetadataKeys() {
@@ -28,10 +33,10 @@ export default class UniqueMetadataKeysPlugin extends Plugin {
2833

2934
// Define a list of categorical colors as single integers
3035
const colors = [
31-
9991159, 13948116, 16772608, 4280421, 16113345,
32-
9474192, 4617584, 15790320, 12320668, 16185078,
33-
32896, 15132410, 10040346, 16777128, 8388608,
34-
11184895, 8421376, 16761035, 255, 8421504
36+
16343842, 7260435, 11226103, 8810003, 1997538,
37+
11796368, 9731429, 16177103, 15601550, 7601461,
38+
1066150, 6197085, 5122908, 1339852, 2975129,
39+
1364806, 3203221, 14122353, 7027020, 8280444
3540
];
3641

3742
// Map each unique key to a color
@@ -44,7 +49,10 @@ export default class UniqueMetadataKeysPlugin extends Plugin {
4449
this.keyColorMap.forEach((color, key) => console.log(`${key}: ${color}`));
4550
}
4651

47-
getColorForKey(key: string): number | undefined {
52+
getColorForKey(key: string | null): number | undefined {
53+
if (key === null) {
54+
return 0;
55+
}
4856
return this.keyColorMap.get(key);
4957
}
5058

@@ -66,4 +74,34 @@ export default class UniqueMetadataKeysPlugin extends Plugin {
6674
isLink(value: any): boolean {
6775
return typeof value === 'object' && value.hasOwnProperty('path');
6876
}
77+
78+
findGraphLeaf(): WorkspaceLeaf | null{
79+
let graphLeaves = this.app.workspace.getLeavesOfType('graph');
80+
if (graphLeaves.length != 1) {
81+
if (graphLeaves.length < 1) {
82+
new Notice('No graph view open');
83+
} else {
84+
new Notice('More than one graph view open, please choose an active one');
85+
}
86+
return null;
87+
}
88+
const links = graphLeaves[0].view.renderer.links;
89+
90+
for (let i = 0; i < 2; i++) {
91+
const sourceId = links[i].source.id;
92+
const targetId = links[i].target.id;
93+
console.log(sourceId);
94+
console.log(targetId);
95+
const linkKey = this.getMetadataKeyForLink(sourceId, targetId);
96+
const linkColor = this.getColorForKey(linkKey);
97+
console.log(linkColor);
98+
console.log(links[i].renderer);
99+
links[i].line._tintRGB = linkColor;
100+
links[i].renderer.colors.line.rgb = links[i].line._tintRGB;
101+
102+
}
103+
104+
105+
return graphLeaves[0]
106+
}
69107
}

0 commit comments

Comments
 (0)