From 7e8776ccbd336ae0e04a115e4ee8cbac9b38fd7e Mon Sep 17 00:00:00 2001 From: Kidan Nelson <44580969+natefrisch01@users.noreply.github.com> Date: Thu, 11 Jan 2024 09:46:09 -0500 Subject: [PATCH] Improved naming scheme --- linkManager.ts | 23 ++++++++++------------- main.ts | 48 +++++++++++++++++++++++++++--------------------- types.ts | 6 +++--- 3 files changed, 40 insertions(+), 37 deletions(-) diff --git a/linkManager.ts b/linkManager.ts index 3951e46..4da1333 100644 --- a/linkManager.ts +++ b/linkManager.ts @@ -1,21 +1,13 @@ import { CustomLink, LinkStatus } from 'types'; - -// Create a mapping from numeric values to string values -const LinkStatusStringMap: Record = { - [LinkStatus.First]: 'first', - [LinkStatus.Second]: 'second', - [LinkStatus.None]: 'none', -}; - export class LinkManager { linksMap: Map; - linkStatus: Map; + linkStatus: Map; constructor() { this.linksMap = new Map(); - this.linkStatus = new Map(); + this.linkStatus = new Map(); } generateKey(sourceId: string, targetId: string): string { @@ -65,7 +57,12 @@ export class LinkManager { } } - getLinkStatus(key: string): 'first' | 'second' | 'none' { - return LinkStatusStringMap[this.linkStatus.get(key) || LinkStatus.None]; + getLinkStatus(key: string): LinkStatus { + const status = this.linkStatus.get(key) + if (status !== undefined) { + return status + } else{ + return LinkStatus.None + } } -} +} \ No newline at end of file diff --git a/main.ts b/main.ts index 27d4241..b64c1e5 100644 --- a/main.ts +++ b/main.ts @@ -1,6 +1,6 @@ import { Plugin, WorkspaceLeaf, Notice} from 'obsidian'; import { getAPI, Page } from 'obsidian-dataview'; -import { CustomRenderer, CustomLink, DataviewLinkType} from 'types'; +import { ObsidianRenderer, ObsidianLink, DataviewLinkType, LinkStatus} from 'types'; import { LinkManager } from 'linkManager'; import * as PIXI from 'pixi.js'; import extractLinks from 'markdown-link-extractor'; @@ -9,8 +9,8 @@ export default class GraphLinkTypesPlugin extends Plugin { // Retrieve the Dataview API api = getAPI(); // A map to keep track of the text nodes created for each link - linkTextMap: Map = new Map(); - currentRenderer: CustomRenderer | null = null; + linkTextMap: Map = new Map(); + currentRenderer: ObsidianRenderer | null = null; animationFrameId: number | null = null; // Lifecycle method called when the plugin is loaded @@ -38,7 +38,7 @@ export default class GraphLinkTypesPlugin extends Plugin { toyLinks() { // Example frames: Each frame is an array of links - const frames: CustomLink[][] = [ + const frames: ObsidianLink[][] = [ // Frame 1: Simple links, some will form pairs later [ { source: { id: "A", x: 0, y: 0 }, target: { id: "B", x: 0, y: 0 } }, @@ -65,6 +65,12 @@ export default class GraphLinkTypesPlugin extends Plugin { { source: { id: "B", x: 0, y: 0 }, target: { id: "A", x: 0, y: 0 } }, { source: { id: "G", x: 0, y: 0 }, target: { id: "H", x: 0, y: 0 } }, // New link ], + // Frame 5: Keeping a pair, removing a single link, adding a new link + [ + { source: { id: "A", x: 0, y: 0 }, target: { id: "B", x: 0, y: 0 } }, // Existing link + { source: { id: "B", x: 0, y: 0 }, target: { id: "A", x: 0, y: 0 } }, // Existing pair + { source: { id: "G", x: 0, y: 0 }, target: { id: "H", x: 0, y: 0 } }, // New link + ], ]; const linkManager = new LinkManager(); @@ -84,9 +90,9 @@ export default class GraphLinkTypesPlugin extends Plugin { const status = linkManager.getLinkStatus(key); // Print link status - if (status === 'first') { + if (status === LinkStatus.First) { console.log('first: ' + key); - } else if (status === 'second') { + } else if (status === LinkStatus.Second) { console.log('second: ' + key); } else { console.log(key); // Not part of a pair @@ -99,11 +105,11 @@ export default class GraphLinkTypesPlugin extends Plugin { // Find the first valid graph renderer in the workspace - findRenderer(): CustomRenderer | null { + findRenderer(): ObsidianRenderer | null { let graphLeaves = this.app.workspace.getLeavesOfType('graph'); for (const leaf of graphLeaves) { const renderer = leaf.view.renderer; - if (this.isCustomRenderer(renderer)) { + if (this.isObsidianRenderer(renderer)) { return renderer; } } @@ -111,7 +117,7 @@ export default class GraphLinkTypesPlugin extends Plugin { graphLeaves = this.app.workspace.getLeavesOfType('localgraph'); for (const leaf of graphLeaves) { const renderer = leaf.view.renderer; - if (this.isCustomRenderer(renderer)) { + if (this.isObsidianRenderer(renderer)) { return renderer; } } @@ -154,7 +160,7 @@ export default class GraphLinkTypesPlugin extends Plugin { } // Create or update text for a given link - createTextForLink(renderer: CustomRenderer, link: CustomLink, reverseString : string | null = null): void { + createTextForLink(renderer: ObsidianRenderer, link: ObsidianLink, reverseString : string | null = null): void { // Get the text to display for the link let linkString: string | null = this.getMetadataKeyForLink(link.source.id, link.target.id); @@ -164,7 +170,7 @@ export default class GraphLinkTypesPlugin extends Plugin { if (link.source.id === link.target.id) { linkString = ""; } else if (reverseString === null) { - const reverseLink : CustomLink | undefined = renderer.links.find(linkFromLoop => linkFromLoop.source.id === link.target.id && linkFromLoop.target.id === link.source.id); + const reverseLink : ObsidianLink | undefined = renderer.links.find(linkFromLoop => linkFromLoop.source.id === link.target.id && linkFromLoop.target.id === link.source.id); if (reverseLink) { this.createTextForLink(renderer, reverseLink, linkString); @@ -199,7 +205,7 @@ export default class GraphLinkTypesPlugin extends Plugin { } // Update the position of the text on the graph - updateTextPosition(renderer: CustomRenderer, link: CustomLink): void { + updateTextPosition(renderer: ObsidianRenderer, link: ObsidianLink): void { if (!renderer || !link || !link.source || !link.target) { // If any of these are null, exit the function return; @@ -220,7 +226,7 @@ export default class GraphLinkTypesPlugin extends Plugin { } // Remove all text nodes from the graph - destroyMap(renderer: CustomRenderer): void { + destroyMap(renderer: ObsidianRenderer): void { if (this.linkTextMap.size > 0) { this.linkTextMap.forEach((text, link) => { 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 { } return; } - const renderer : CustomRenderer = this.currentRenderer; + const renderer : ObsidianRenderer = this.currentRenderer; // Remove existing text from the graph. this.destroyMap(renderer); // Create text for each link in the graph. - renderer.links.forEach((link: CustomLink) => this.createTextForLink(renderer, link)); + renderer.links.forEach((link: ObsidianLink) => this.createTextForLink(renderer, link)); // Call the function to update positions in the next animation frame. requestAnimationFrame(this.updatePositions.bind(this)); } @@ -260,17 +266,17 @@ export default class GraphLinkTypesPlugin extends Plugin { } let updateMap = false; - let rendererLinks: Set; + let rendererLinks: Set; if (this.animationFrameId && this.animationFrameId % 20 == 0) { updateMap = true; rendererLinks = new Set(); } - const renderer: CustomRenderer = this.currentRenderer; + const renderer: ObsidianRenderer = this.currentRenderer; // For each link in the graph, update the position of its text. - renderer.links.forEach((link: CustomLink) => { + renderer.links.forEach((link: ObsidianLink) => { if (updateMap) { // Add text for new links. if (!this.linkTextMap.has(link)) { @@ -284,7 +290,7 @@ export default class GraphLinkTypesPlugin extends Plugin { // Remove text that should no longer be on stage. if (updateMap) { - this.linkTextMap.forEach((text, link : CustomLink) => { + this.linkTextMap.forEach((text, link : ObsidianLink) => { if (!rendererLinks.has(link)) { if (text && renderer.px && renderer.px.stage && renderer.px.stage.children && renderer.px.stage.children.includes(text)) { renderer.px.stage.removeChild(text); @@ -377,7 +383,7 @@ export default class GraphLinkTypesPlugin extends Plugin { return typeof value === 'object' && value.hasOwnProperty('path'); } - isCustomRenderer(renderer: any): renderer is CustomRenderer { + isObsidianRenderer(renderer: any): renderer is ObsidianRenderer { return renderer && renderer.px && renderer.px.stage @@ -388,7 +394,7 @@ export default class GraphLinkTypesPlugin extends Plugin { && Array.isArray(renderer.links); } - isCustomLink(link: any): link is CustomLink { + isObsidianLink(link: any): link is ObsidianLink { return link && link.source && typeof link.source.id === 'string' diff --git a/types.ts b/types.ts index a5a209b..2d74400 100644 --- a/types.ts +++ b/types.ts @@ -1,4 +1,4 @@ -export interface CustomRenderer { +export interface ObsidianRenderer { px: { stage: { addChild: (child: any) => void; @@ -13,7 +13,7 @@ export interface CustomRenderer { scale: number; } -export interface CustomLink { +export interface ObsidianLink { source: { id: string; x: number; @@ -37,7 +37,7 @@ export enum DataviewLinkType { // Define a numeric enum for link statuses export enum LinkStatus { + None, First, Second, - None, }