Skip to content

Commit 7e8776c

Browse files
committed
Improved naming scheme
1 parent 645104c commit 7e8776c

File tree

3 files changed

+40
-37
lines changed

3 files changed

+40
-37
lines changed

linkManager.ts

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,13 @@
11

22
import { CustomLink, LinkStatus } from 'types';
33

4-
5-
// Create a mapping from numeric values to string values
6-
const LinkStatusStringMap: Record<LinkStatus, 'first' | 'second' | 'none'> = {
7-
[LinkStatus.First]: 'first',
8-
[LinkStatus.Second]: 'second',
9-
[LinkStatus.None]: 'none',
10-
};
11-
124
export class LinkManager {
135
linksMap: Map<string, CustomLink>;
14-
linkStatus: Map<string, LinkStatus>;
6+
linkStatus: Map<string, LinkStatus.First | LinkStatus.Second>;
157

168
constructor() {
179
this.linksMap = new Map<string, CustomLink>();
18-
this.linkStatus = new Map<string, LinkStatus>();
10+
this.linkStatus = new Map<string, LinkStatus.First | LinkStatus.Second>();
1911
}
2012

2113
generateKey(sourceId: string, targetId: string): string {
@@ -65,7 +57,12 @@ export class LinkManager {
6557
}
6658
}
6759

68-
getLinkStatus(key: string): 'first' | 'second' | 'none' {
69-
return LinkStatusStringMap[this.linkStatus.get(key) || LinkStatus.None];
60+
getLinkStatus(key: string): LinkStatus {
61+
const status = this.linkStatus.get(key)
62+
if (status !== undefined) {
63+
return status
64+
} else{
65+
return LinkStatus.None
66+
}
7067
}
71-
}
68+
}

main.ts

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Plugin, WorkspaceLeaf, Notice} from 'obsidian';
22
import { getAPI, Page } from 'obsidian-dataview';
3-
import { CustomRenderer, CustomLink, DataviewLinkType} from 'types';
3+
import { ObsidianRenderer, ObsidianLink, DataviewLinkType, LinkStatus} from 'types';
44
import { LinkManager } from 'linkManager';
55
import * as PIXI from 'pixi.js';
66
import extractLinks from 'markdown-link-extractor';
@@ -9,8 +9,8 @@ export default class GraphLinkTypesPlugin extends Plugin {
99
// Retrieve the Dataview API
1010
api = getAPI();
1111
// A map to keep track of the text nodes created for each link
12-
linkTextMap: Map<CustomLink, PIXI.Text> = new Map();
13-
currentRenderer: CustomRenderer | null = null;
12+
linkTextMap: Map<ObsidianLink, PIXI.Text> = new Map();
13+
currentRenderer: ObsidianRenderer | null = null;
1414
animationFrameId: number | null = null;
1515

1616
// Lifecycle method called when the plugin is loaded
@@ -38,7 +38,7 @@ export default class GraphLinkTypesPlugin extends Plugin {
3838

3939
toyLinks() {
4040
// Example frames: Each frame is an array of links
41-
const frames: CustomLink[][] = [
41+
const frames: ObsidianLink[][] = [
4242
// Frame 1: Simple links, some will form pairs later
4343
[
4444
{ source: { id: "A", x: 0, y: 0 }, target: { id: "B", x: 0, y: 0 } },
@@ -65,6 +65,12 @@ export default class GraphLinkTypesPlugin extends Plugin {
6565
{ source: { id: "B", x: 0, y: 0 }, target: { id: "A", x: 0, y: 0 } },
6666
{ source: { id: "G", x: 0, y: 0 }, target: { id: "H", x: 0, y: 0 } }, // New link
6767
],
68+
// Frame 5: Keeping a pair, removing a single link, adding a new link
69+
[
70+
{ source: { id: "A", x: 0, y: 0 }, target: { id: "B", x: 0, y: 0 } }, // Existing link
71+
{ source: { id: "B", x: 0, y: 0 }, target: { id: "A", x: 0, y: 0 } }, // Existing pair
72+
{ source: { id: "G", x: 0, y: 0 }, target: { id: "H", x: 0, y: 0 } }, // New link
73+
],
6874
];
6975

7076
const linkManager = new LinkManager();
@@ -84,9 +90,9 @@ export default class GraphLinkTypesPlugin extends Plugin {
8490
const status = linkManager.getLinkStatus(key);
8591

8692
// Print link status
87-
if (status === 'first') {
93+
if (status === LinkStatus.First) {
8894
console.log('first: ' + key);
89-
} else if (status === 'second') {
95+
} else if (status === LinkStatus.Second) {
9096
console.log('second: ' + key);
9197
} else {
9298
console.log(key); // Not part of a pair
@@ -99,19 +105,19 @@ export default class GraphLinkTypesPlugin extends Plugin {
99105

100106

101107
// Find the first valid graph renderer in the workspace
102-
findRenderer(): CustomRenderer | null {
108+
findRenderer(): ObsidianRenderer | null {
103109
let graphLeaves = this.app.workspace.getLeavesOfType('graph');
104110
for (const leaf of graphLeaves) {
105111
const renderer = leaf.view.renderer;
106-
if (this.isCustomRenderer(renderer)) {
112+
if (this.isObsidianRenderer(renderer)) {
107113
return renderer;
108114
}
109115
}
110116

111117
graphLeaves = this.app.workspace.getLeavesOfType('localgraph');
112118
for (const leaf of graphLeaves) {
113119
const renderer = leaf.view.renderer;
114-
if (this.isCustomRenderer(renderer)) {
120+
if (this.isObsidianRenderer(renderer)) {
115121
return renderer;
116122
}
117123
}
@@ -154,7 +160,7 @@ export default class GraphLinkTypesPlugin extends Plugin {
154160
}
155161

156162
// Create or update text for a given link
157-
createTextForLink(renderer: CustomRenderer, link: CustomLink, reverseString : string | null = null): void {
163+
createTextForLink(renderer: ObsidianRenderer, link: ObsidianLink, reverseString : string | null = null): void {
158164

159165
// Get the text to display for the link
160166
let linkString: string | null = this.getMetadataKeyForLink(link.source.id, link.target.id);
@@ -164,7 +170,7 @@ export default class GraphLinkTypesPlugin extends Plugin {
164170
if (link.source.id === link.target.id) {
165171
linkString = "";
166172
} else if (reverseString === null) {
167-
const reverseLink : CustomLink | undefined = renderer.links.find(linkFromLoop => linkFromLoop.source.id === link.target.id && linkFromLoop.target.id === link.source.id);
173+
const reverseLink : ObsidianLink | undefined = renderer.links.find(linkFromLoop => linkFromLoop.source.id === link.target.id && linkFromLoop.target.id === link.source.id);
168174

169175
if (reverseLink) {
170176
this.createTextForLink(renderer, reverseLink, linkString);
@@ -199,7 +205,7 @@ export default class GraphLinkTypesPlugin extends Plugin {
199205
}
200206

201207
// Update the position of the text on the graph
202-
updateTextPosition(renderer: CustomRenderer, link: CustomLink): void {
208+
updateTextPosition(renderer: ObsidianRenderer, link: ObsidianLink): void {
203209
if (!renderer || !link || !link.source || !link.target) {
204210
// If any of these are null, exit the function
205211
return;
@@ -220,7 +226,7 @@ export default class GraphLinkTypesPlugin extends Plugin {
220226
}
221227

222228
// Remove all text nodes from the graph
223-
destroyMap(renderer: CustomRenderer): void {
229+
destroyMap(renderer: ObsidianRenderer): void {
224230
if (this.linkTextMap.size > 0) {
225231
this.linkTextMap.forEach((text, link) => {
226232
if (text && renderer.px && renderer.px.stage && renderer.px.stage.children && renderer.px.stage.children.includes(text)) {
@@ -240,11 +246,11 @@ export default class GraphLinkTypesPlugin extends Plugin {
240246
}
241247
return;
242248
}
243-
const renderer : CustomRenderer = this.currentRenderer;
249+
const renderer : ObsidianRenderer = this.currentRenderer;
244250
// Remove existing text from the graph.
245251
this.destroyMap(renderer);
246252
// Create text for each link in the graph.
247-
renderer.links.forEach((link: CustomLink) => this.createTextForLink(renderer, link));
253+
renderer.links.forEach((link: ObsidianLink) => this.createTextForLink(renderer, link));
248254
// Call the function to update positions in the next animation frame.
249255
requestAnimationFrame(this.updatePositions.bind(this));
250256
}
@@ -260,17 +266,17 @@ export default class GraphLinkTypesPlugin extends Plugin {
260266
}
261267

262268
let updateMap = false;
263-
let rendererLinks: Set<CustomLink>;
269+
let rendererLinks: Set<ObsidianLink>;
264270

265271
if (this.animationFrameId && this.animationFrameId % 20 == 0) {
266272
updateMap = true;
267273
rendererLinks = new Set();
268274
}
269275

270-
const renderer: CustomRenderer = this.currentRenderer;
276+
const renderer: ObsidianRenderer = this.currentRenderer;
271277

272278
// For each link in the graph, update the position of its text.
273-
renderer.links.forEach((link: CustomLink) => {
279+
renderer.links.forEach((link: ObsidianLink) => {
274280
if (updateMap) {
275281
// Add text for new links.
276282
if (!this.linkTextMap.has(link)) {
@@ -284,7 +290,7 @@ export default class GraphLinkTypesPlugin extends Plugin {
284290

285291
// Remove text that should no longer be on stage.
286292
if (updateMap) {
287-
this.linkTextMap.forEach((text, link : CustomLink) => {
293+
this.linkTextMap.forEach((text, link : ObsidianLink) => {
288294
if (!rendererLinks.has(link)) {
289295
if (text && renderer.px && renderer.px.stage && renderer.px.stage.children && renderer.px.stage.children.includes(text)) {
290296
renderer.px.stage.removeChild(text);
@@ -377,7 +383,7 @@ export default class GraphLinkTypesPlugin extends Plugin {
377383
return typeof value === 'object' && value.hasOwnProperty('path');
378384
}
379385

380-
isCustomRenderer(renderer: any): renderer is CustomRenderer {
386+
isObsidianRenderer(renderer: any): renderer is ObsidianRenderer {
381387
return renderer
382388
&& renderer.px
383389
&& renderer.px.stage
@@ -388,7 +394,7 @@ export default class GraphLinkTypesPlugin extends Plugin {
388394
&& Array.isArray(renderer.links);
389395
}
390396

391-
isCustomLink(link: any): link is CustomLink {
397+
isObsidianLink(link: any): link is ObsidianLink {
392398
return link
393399
&& link.source
394400
&& typeof link.source.id === 'string'

types.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export interface CustomRenderer {
1+
export interface ObsidianRenderer {
22
px: {
33
stage: {
44
addChild: (child: any) => void;
@@ -13,7 +13,7 @@ export interface CustomRenderer {
1313
scale: number;
1414
}
1515

16-
export interface CustomLink {
16+
export interface ObsidianLink {
1717
source: {
1818
id: string;
1919
x: number;
@@ -37,7 +37,7 @@ export enum DataviewLinkType {
3737

3838
// Define a numeric enum for link statuses
3939
export enum LinkStatus {
40+
None,
4041
First,
4142
Second,
42-
None,
4343
}

0 commit comments

Comments
 (0)