Skip to content

Commit

Permalink
feat: Juggl visualization now supports all BC hierarchy options
Browse files Browse the repository at this point in the history
  • Loading branch information
HEmile committed Jan 13, 2022
1 parent cefac73 commit d62b915
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 8 deletions.
117 changes: 112 additions & 5 deletions src/Visualisations/CBJuggl.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
import type {ParsedCodeblock} from "../interfaces";
import type BCPlugin from "../main";
import {getPlugin, IJugglSettings} from 'juggl-api';
import {
DataStoreEvents,
getPlugin,
ICoreDataStore,
IDataStore,
IJuggl,
IJugglPlugin, IJugglStores,
nodeFromFile,
VizId
} from 'juggl-api';
import {JUGGL_CB_DEFAULTS} from "../constants";
import {Component, EventRef, Events, MetadataCache, TFile} from "obsidian";
import type {MultiGraph} from "graphology";
import type {EdgeDefinition, NodeSingular} from "cytoscape";

const STORE_ID = "core";

function indentToDepth(indent: string) {
return indent.length / 2 + 1;
Expand All @@ -16,6 +30,93 @@ function meetsConditions(indent: string, node: string, froms: string[], min: num
);
}

class BCStoreEvents extends Events implements DataStoreEvents {

}

class BCStore extends Component implements ICoreDataStore {

graph: MultiGraph;
cache: MetadataCache;
plugin: IJugglPlugin;
constructor(graph: MultiGraph, metadata: MetadataCache, plugin: IJugglPlugin) {
super();
this.graph = graph;
this.cache = metadata;
this.plugin = plugin;
}

asString(node: NodeSingular): string {
const id = VizId.fromNode(node);
if (id.storeId === STORE_ID) {
const file = this.cache.getFirstLinkpathDest(id.id, '');
if (file) {
return id.id.slice(0, -3);
}
}
return
}

getFile(nodeId: VizId): TFile {
return this.cache.getFirstLinkpathDest(nodeId.id, '');
}

async connectNodes(allNodes: cytoscape.NodeCollection, newNodes: cytoscape.NodeCollection, graph: IJuggl): Promise<cytoscape.EdgeDefinition[]> {
const edges: EdgeDefinition[] = [];
const nodesListS = new Set(allNodes
.map((node) => this.asString(node))
.filter((s) => s));
newNodes.forEach((node) => {
this.graph.forEachOutEdge(this.asString(node), (key, attr, source, target) => {
if (nodesListS.has(target)) {
edges.push({
data: {
id: `BC:${source}->${target}`,
source: VizId.toId(source, STORE_ID) + ".md",
target: VizId.toId(target, STORE_ID) + ".md",
type: attr.field,
dir: attr.dir,
},
classes: `type-${attr.field} dir-${attr.dir} breadcrumbs$`
});
}
});
});
return Promise.resolve(edges);
}

getEvents(): DataStoreEvents {
return new BCStoreEvents();
}

getNeighbourhood(nodeId: VizId[]): Promise<cytoscape.NodeDefinition[]> {
// TODO
return Promise.resolve([]);
}

refreshNode(view: IJuggl, id: VizId): void | Promise<void> {
return;
}

storeId(): string {
return STORE_ID;
}

get(nodeId: VizId): Promise<cytoscape.NodeDefinition> {
const file = this.getFile(nodeId);
if (file === null) {
return null;
}
const cache = this.cache.getFileCache(file);
if (cache === null) {
console.log('returning empty cache', nodeId);
return null;
}
return Promise.resolve(nodeFromFile(file, this.plugin, nodeId.toId()));
}

}

export function createdJugglCB(plugin: BCPlugin,
target: HTMLElement,
args: ParsedCodeblock,
Expand All @@ -34,16 +135,22 @@ export function createdJugglCB(plugin: BCPlugin,
args[key] = JUGGL_CB_DEFAULTS[key];
}
}
console.log({args});
const nodes = lines
.filter(([indent, node]) => meetsConditions(indent, node, froms, min, max))
.map(([_, node]) => node);
const juggl = jugglPlugin.createJuggl(target, args, null, nodes)
.map(([_, node]) => node + ".md");

const bcStore = new BCStore(plugin.mainG, plugin.app.metadataCache, jugglPlugin);
const stores: IJugglStores = {
coreStore: bcStore,
dataStores: [bcStore]
}

const juggl = jugglPlugin.createJuggl(target, args, stores, nodes)
plugin.addChild(juggl);
juggl.load();
console.log({juggl});
}
catch (error) {
plugin.codeblockError(args)
console.log({error});
}
}
4 changes: 2 additions & 2 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type {
UserHier,
visTypes,
} from "./interfaces";
import {IJugglSettings} from "juggl-api";
import type {IJugglSettings} from "juggl-api";

export const MATRIX_VIEW = "BC-matrix";
export const STATS_VIEW = "BC-stats";
Expand Down Expand Up @@ -67,7 +67,7 @@ export const JUGGL_CB_DEFAULTS: IJugglSettings = {
autoAddNodes: false,
autoExpand: false,
autoZoom: false,
coreStore: "Obsidian",
coreStore: "core",
expandInitial: false,
fdgdLayout: 'cola',
filter: '',
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2888,7 +2888,7 @@
"through" ">=2.2.7 <3"

"juggl-api@github:HEmile/juggl-api":
"resolved" "git+ssh://[email protected]/HEmile/juggl-api.git#f286b3855858686bc381ed4c47a429d196aafcfd"
"resolved" "git+ssh://[email protected]/HEmile/juggl-api.git#1716ba13ffc44df33e8efd248459a4e3b2746dec"
"version" "1.0.0"
dependencies:
"@types/cytoscape" "^3.14.11"
Expand Down

0 comments on commit d62b915

Please sign in to comment.