Skip to content

Commit

Permalink
fix: rather focus leaf if item.to is already open
Browse files Browse the repository at this point in the history
  • Loading branch information
SkepticMystic committed Jul 3, 2021
1 parent 9800573 commit 4757999
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 53 deletions.
22 changes: 18 additions & 4 deletions src/List.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,22 @@
export let list: SquareProps;
const { realItems, impliedItems, fieldName, app } = list;
async function openLink(item: internalLinkObj) {
await app.workspace.openLinkText(item.to, item.currFile.path);
const currFile = app.workspace.getActiveFile();
async function linkClick(item: internalLinkObj) {
const openLeaves = [];
// For all open leaves, if the leave's basename is equal to the link destination, rather activate that leaf instead of opening it in two panes
app.workspace.iterateAllLeaves((leaf) => {
if (leaf.view?.file?.basename === item.to) {
openLeaves.push(leaf);
}
});
if (openLeaves.length) {
app.workspace.setActiveLeaf(openLeaves[0]);
} else {
await app.workspace.openLinkText(item.to, item.currFile.path);
}
}
function hoverPreview(e) {
Expand Down Expand Up @@ -43,7 +57,7 @@
<a
href="null"
class={realItem.cls}
on:click={async () => openLink(realItem)}
on:click={async () => linkClick(realItem)}
on:mouseover={hoverPreview}
>
{realItem.to.split("/").last()}
Expand All @@ -64,7 +78,7 @@
<a
href="null"
class={impliedItem.cls}
on:click={async () => openLink(impliedItem)}
on:click={async () => linkClick(impliedItem)}
on:mouseover={hoverPreview}
>{impliedItem.to.split("/").last()}
</a>
Expand Down
106 changes: 61 additions & 45 deletions src/MatrixView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,36 +104,56 @@ export default class MatrixView extends ItemView {
});
}

nextLevel(obj: { [x: string]: any }, gChildren: Graph): void {
for (const child in obj) {
if (Object.prototype.hasOwnProperty.call(obj, child)) {
const childrenOfChild = (gChildren.successors(obj[child]) ??
[]) as string[];
childrenOfChild.forEach((innerChild) => (obj[child] = { innerChild }));
obj[child] = { ...childrenOfChild };
}
getAdjList(
gChildren: Graph,
currFile: string,
depth: number
): Map<string, string[]> {
const adjList: Map<string, string[]> = new Map();

function addNode(node: string) {
adjList.set(node, []);
}

addNode(currFile)

// const immediateChildren = (gChildren.successors(currFile) ??
// []) as string[];
// immediateChildren.forEach(addNode);

const visited: Set<string> = new Set();

// Do this `depth` number of times
for (let i = 1; i < depth; i++) {
console.log(adjList);
adjList.forEach((childArr, parent) => {
let childrenOfKey: string[];
// If the node hasn't been visited before
if (!visited.has(parent)) {
// Get it's children
childrenOfKey = (gChildren.successors(parent) ?? []) as string[];
// Mark it as visited
visited.add(parent);
// Add it to the adjList
adjList.set(parent, childrenOfKey);
// Add the children as top-level map items
childrenOfKey.forEach((childOfKey) => {
addNode(childOfKey);
});
}
});
}

return adjList;
}

// createIndexObj(gChildren: Graph, currFile: string, depth: number) {
// const initialChildren: [string, number][][] = [[]];

// const immediateChildren = (gChildren.successors(currFile) ??
// []) as string[];
// immediateChildren.forEach((child) => initialChildren[0].push([child, 1]));

// console.log(initialChildren);

// for (let i = 0; i < depth; i++) {
// initialChildren.push([]);
// initialChildren[i].forEach((childArr) => {
// const childrenOfChild = (
// (gChildren.successors(childArr[0]) ?? []) as string[]
// ).map((child) => [child, i + 2]);
// initialChildren[i + 1].push(childrenOfChild);
// });
// }
// console.log(initialChildren);
// mapToMD(adjList: Map<string, string[]>): string {
// let md = '';
// adjList.forEach((childArr, parent) => {

// })

// return md
// }

async draw(): Promise<void> {
Expand All @@ -148,18 +168,14 @@ export default class MatrixView extends ItemView {
await this.draw();
});

// const createIndexButton = this.contentEl.createEl("button", {
// text: "Create Index",
// });
// createIndexButton.addEventListener("click", () =>
// console.log(
// this.createIndexObj(
// this.plugin.currGraphs.gChildren,
// currFile.basename,
// 2
// )
// )
// );
const createIndexButton = this.contentEl.createEl("button", {
text: "Create Index",
});
createIndexButton.addEventListener("click", () =>
console.log(
this.getAdjList(this.plugin.currGraphs.gChildren, currFile.basename, 2)
)
);

this.currGraphs = this.plugin.currGraphs;
const currFile = this.app.workspace.getActiveFile();
Expand All @@ -180,12 +196,12 @@ export default class MatrixView extends ItemView {
impliedParents,
impliedChildren,
] = [
this.squareItems(gParents, currFile),
this.squareItems(gSiblings, currFile),
this.squareItems(gChildren, currFile),
this.squareItems(gChildren, currFile, false),
this.squareItems(gParents, currFile, false),
];
this.squareItems(gParents, currFile),
this.squareItems(gSiblings, currFile),
this.squareItems(gChildren, currFile),
this.squareItems(gChildren, currFile, false),
this.squareItems(gParents, currFile, false),
];

/// Implied Siblings
const currParents = (gParents.successors(currFile.basename) ??
Expand Down
24 changes: 20 additions & 4 deletions src/Square.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,24 @@
export let settings: BreadcrumbsSettings;
export let matrixView: MatrixView;
async function openLink(item: internalLinkObj) {
await app.workspace.openLinkText(item.to, item.currFile.path);
// async function openLink(item: internalLinkObj) {
// await app.workspace.openLinkText(item.to, item.currFile.path);
// }
async function linkClick(item: internalLinkObj) {
const openLeaves = [];
// For all open leaves, if the leave's basename is equal to the link destination, rather activate that leaf instead of opening it in two panes
app.workspace.iterateAllLeaves((leaf) => {
if (leaf.view?.file?.basename === item.to) {
openLeaves.push(leaf);
}
});
if (openLeaves.length) {
app.workspace.setActiveLeaf(openLeaves[0]);
} else {
await app.workspace.openLinkText(item.to, item.currFile.path);
}
}
function hoverPreview(e) {
Expand Down Expand Up @@ -42,7 +58,7 @@
<a
href="null"
class={realItem.cls}
on:click={async () => openLink(realItem)}
on:click={async () => linkClick(realItem)}
on:mouseover={hoverPreview}
>{realItem.to.split("/").last()}
</a>
Expand All @@ -61,7 +77,7 @@
<a
href="null"
class={impliedItem.cls}
on:click={async () => openLink(impliedItem)}
on:click={async () => linkClick(impliedItem)}
on:mouseover={hoverPreview}
>{impliedItem.to.split("/").last()}
</a>
Expand Down

0 comments on commit 4757999

Please sign in to comment.