generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 9
/
linkManager.ts
283 lines (241 loc) · 11.2 KB
/
linkManager.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
import { ObsidianRenderer, ObsidianLink, LinkPair, GltLink, DataviewLinkType } from 'types';
import { getAPI } from 'obsidian-dataview';
import { Text, TextStyle } from 'pixi.js';
// @ts-ignore
import extractLinks from 'markdown-link-extractor';
export class LinkManager {
linksMap: Map<string, GltLink>;
api = getAPI();
currentTheme : string;
textColor : string;
constructor() {
this.linksMap = new Map<string, GltLink>();
// Detect changes to the theme.
this.detectThemeChange();
}
generateKey(sourceId: string, targetId: string): string {
return `${sourceId}-${targetId}`;
}
private detectThemeChange(): void {
let lastTheme = '';
let lastStyleSheetHref = '';
let debounceTimer: number;
const themeObserver = new MutationObserver(() => {
clearTimeout(debounceTimer);
debounceTimer = window.setTimeout(() => {
this.currentTheme = document.body.classList.contains('theme-dark') ? 'theme-dark' : 'theme-light';
const currentStyleSheetHref = document.querySelector('link[rel="stylesheet"][href*="theme"]')?.getAttribute('href');
if ((this.currentTheme && this.currentTheme !== lastTheme) || (currentStyleSheetHref !== lastStyleSheetHref)) {
console.log(`Theme has changed to: ${this.currentTheme}`);
this.textColor = this.getComputedColorFromClass(this.currentTheme, '--text-normal');
console.log(this.textColor);
lastTheme = this.currentTheme;
if (currentStyleSheetHref) {
lastStyleSheetHref = currentStyleSheetHref;
}
}
}, 100); // Debounce delay
});
themeObserver.observe(document.body, { attributes: true, attributeFilter: ['class'] });
themeObserver.observe(document.head, { childList: true, subtree: true, attributes: true, attributeFilter: ['href'] });
}
private getComputedColorFromClass(className : string, cssVariable : string) : string {
// Create a temporary element
const tempElement = document.createElement('div');
// Apply the class to the temporary element
tempElement.classList.add(className);
document.body.appendChild(tempElement);
// Get the computed style of the temporary element
const style = getComputedStyle(tempElement);
const colorValue = style.getPropertyValue(cssVariable).trim();
// Remove the temporary element
document.body.removeChild(tempElement);
// Check if the color is in HSL format
if (colorValue.startsWith('hsl')) {
// Return a default color based on some condition, e.g., current theme
// This is a placeholder condition
return document.body.classList.contains('theme-dark') ? '#b3b3b3' : '#5c5c5c';
} else {
// If it's not HSL, return the color as-is
return colorValue;
}
}
addLink(renderer: ObsidianRenderer, obLink: ObsidianLink): void {
const key = this.generateKey(obLink.source.id, obLink.target.id);
const reverseKey = this.generateKey(obLink.target.id, obLink.source.id);
const pairStatus = (obLink.source.id !== obLink.target.id) && this.linksMap.has(reverseKey) ? LinkPair.Second : LinkPair.None;
const newLink: GltLink = {
obsidianLink: obLink,
pairStatus: pairStatus,
pixiText: this.createTextForLink(renderer, obLink, pairStatus)
};
this.linksMap.set(key, newLink);
if ((obLink.source.id !== obLink.target.id) && this.linksMap.has(reverseKey)) {
const reverseLink = this.linksMap.get(reverseKey);
if (reverseLink) {
reverseLink.pairStatus = LinkPair.First;
}
}
}
removeLink(renderer: ObsidianRenderer, link: ObsidianLink): void {
const key = this.generateKey(link.source.id, link.target.id);
const reverseKey = this.generateKey(link.target.id, link.source.id);
const gltLink = this.linksMap.get(key);
if (gltLink && gltLink.pixiText && renderer.px && renderer.px.stage && renderer.px.stage.children && renderer.px.stage.children.includes(gltLink.pixiText)) {
renderer.px.stage.removeChild(gltLink.pixiText);
gltLink.pixiText.destroy();
}
this.linksMap.delete(key);
const reverseLink = this.linksMap.get(reverseKey);
if (reverseLink && reverseLink.pairStatus !== LinkPair.None) {
reverseLink.pairStatus = LinkPair.None;
}
}
removeLinks(renderer: ObsidianRenderer, currentLinks: ObsidianLink[]): void {
const currentKeys = new Set(currentLinks.map(link => this.generateKey(link.source.id, link.target.id)));
// remove any links in our map that aren't in this list
this.linksMap.forEach((_, key) => {
if (!currentKeys.has(key)) {
const link = this.linksMap.get(key);
if (link) {
this.removeLink(renderer, link.obsidianLink);
}
}
});
}
getLinkPairStatus(key: string): LinkPair {
const link = this.linksMap.get(key);
return link ? link.pairStatus : LinkPair.None;
}
// Update the position of the text on the graph
updateTextPosition(renderer: ObsidianRenderer, link: ObsidianLink): void {
if (!renderer || !link || !link.source || !link.target) {
// If any of these are null, exit the function
return;
}
const linkKey = this.generateKey(link.source.id, link.target.id);
const obsLink = this.linksMap.get(linkKey);
let text;
if (obsLink) {
text = obsLink.pixiText;
} else {
return
};
// Calculate the mid-point of the link
const midX: number = (link.source.x + link.target.x) / 2;
const midY: number = (link.source.y + link.target.y) / 2;
// Transform the mid-point coordinates based on the renderer's pan and scale
const { x, y } = this.getLinkToTextCoordinates(midX, midY, renderer.panX, renderer.panY, renderer.scale);
if (text && renderer.px && renderer.px.stage && renderer.px.stage.children && renderer.px.stage.children.includes(text)) {
// Set the position and scale of the text
text.x = x;
text.y = y;
text.scale.set(1 / (3 * renderer.nodeScale));
text.style.fill = this.textColor;
}
}
// Create or update text for a given link
private createTextForLink(renderer: ObsidianRenderer, link: ObsidianLink, pairStatus : LinkPair): Text | null{
// Get the text to display for the link
let linkString: string | null = this.getMetadataKeyForLink(link.source.id, link.target.id);
if (linkString === null) {
return null;
} //doesn't add if link is null
if (link.source.id === link.target.id) {
linkString = "";
}
if (pairStatus === LinkPair.None) {
} else if (pairStatus === LinkPair.First) {
linkString = linkString + "\n\n";
} else if (pairStatus === LinkPair.Second) {
linkString = "\n\n" + linkString;
} else {
}
// Define the style for the text
const textStyle: TextStyle = new TextStyle({
fontFamily: 'Arial',
fontSize: 36,
fill: this.textColor
});
// Create new text node
const text: Text = new Text(linkString, textStyle);
text.alpha = 0.7;
text.anchor.set(0.5, 0.5);
this.updateTextPosition(renderer, link);
renderer.px.stage.addChild(text);
return text
}
// Utility function to extract the file path from a Markdown link
private extractPathFromMarkdownLink(markdownLink: string | unknown): string {
const links = extractLinks(markdownLink).links;
// The package returns an array of links. Assuming you want the first link.
return links.length > 0 ? links[0] : '';
}
// Method to determine the type of a value, now a class method
private determineDataviewLinkType(value: any): DataviewLinkType {
if (typeof value === 'object' && value !== null && 'path' in value) {
return DataviewLinkType.WikiLink;
} else if (typeof value === 'string' && value.includes('](')) {
return DataviewLinkType.MarkdownLink;
} else if (typeof value === 'string') {
return DataviewLinkType.String;
} else if (Array.isArray(value)) {
return DataviewLinkType.Array;
} else {
return DataviewLinkType.Other;
}
}
// Remove all text nodes from the graph
destroyMap(renderer: ObsidianRenderer): void {
if (this.linksMap.size > 0) {
this.linksMap.forEach((gltLink, linkKey) => {
if (gltLink.pixiText && renderer.px && renderer.px.stage && renderer.px.stage.children && renderer.px.stage.children.includes(gltLink.pixiText)) {
renderer.px.stage.removeChild(gltLink.pixiText);
gltLink.pixiText.destroy();
}
this.linksMap.delete(linkKey);
});
}
}
// Get the metadata key for a link between two pages
private getMetadataKeyForLink(sourceId: string, targetId: string): string | null {
const sourcePage: any = this.api.page(sourceId);
if (!sourcePage) return null;
for (const [key, value] of Object.entries(sourcePage)) {
const valueType = this.determineDataviewLinkType(value);
switch (valueType) {
case DataviewLinkType.WikiLink:
// @ts-ignore
if (value.path === targetId) {
return key;
}
break;
case DataviewLinkType.MarkdownLink:
if (this.extractPathFromMarkdownLink(value) === targetId) {
return key;
}
break;
case DataviewLinkType.Array:
// @ts-ignore
for (const item of value) {
if (this.determineDataviewLinkType(item) === DataviewLinkType.WikiLink && item.path === targetId) {
return key;
}
if (this.determineDataviewLinkType(item) === DataviewLinkType.MarkdownLink && this.extractPathFromMarkdownLink(item) === targetId) {
return key;
}
}
break;
default:
//metadata is not a link, return null
return null;
}
}
return null;
}
// Function to calculate the coordinates for placing the link text.
private getLinkToTextCoordinates(linkX: number, linkY: number, panX: number, panY: number, scale: number): { x: number, y: number } {
// Apply scaling and panning to calculate the actual position.
return { x: linkX * scale + panX, y: linkY * scale + panY };
}
}