Skip to content

Commit 47936cf

Browse files
committed
Added default categorical color to keys as a pair.
1 parent 5821b7b commit 47936cf

File tree

3 files changed

+46
-3
lines changed

3 files changed

+46
-3
lines changed

main.ts

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { getAPI } from 'obsidian-dataview';
33

44
export default class UniqueMetadataKeysPlugin extends Plugin {
55
api = getAPI();
6+
keyColorMap = new Map<string, number>();
67

78
onload() {
89
this.addCommand({
@@ -25,13 +26,44 @@ export default class UniqueMetadataKeysPlugin extends Plugin {
2526
}
2627
uniqueKeys.delete("file");
2728

29+
// Define a list of categorical colors as single integers
30+
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
35+
];
36+
37+
// Map each unique key to a color
38+
Array.from(uniqueKeys).forEach((key, index) => {
39+
const color = colors[index % colors.length]; // Cycle through colors
40+
this.keyColorMap.set(key, color);
41+
});
42+
2843
console.log('Unique Metadata Keys with Link Values:');
29-
uniqueKeys.forEach(key => console.log(key));
44+
this.keyColorMap.forEach((color, key) => console.log(`${key}: ${color}`));
45+
}
46+
47+
getColorForKey(key: string): number | undefined {
48+
return this.keyColorMap.get(key);
49+
}
50+
51+
getMetadataKeyForLink(sourceId: string, targetId: string): string | null {
52+
const sourcePage = this.api.page(sourceId);
53+
if (!sourcePage) {
54+
return null;
55+
}
56+
57+
for (const [key, value] of Object.entries(sourcePage)) {
58+
if (this.isLink(value) && value.path === targetId) {
59+
return key;
60+
}
61+
}
62+
63+
return null;
3064
}
3165

3266
isLink(value: any): boolean {
33-
// Check if the value is a link object
3467
return typeof value === 'object' && value.hasOwnProperty('path');
3568
}
3669
}
37-

package-lock.json

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,8 @@
2121
"obsidian-dataview": "^0.5.64",
2222
"tslib": "2.4.0",
2323
"typescript": "4.7.4"
24+
},
25+
"dependencies": {
26+
"chroma-js": "^2.4.2"
2427
}
2528
}

0 commit comments

Comments
 (0)