Skip to content

Commit

Permalink
fix(getFieldValues): 🐛 Be more explicit with dvLinks
Browse files Browse the repository at this point in the history
  • Loading branch information
SkepticMystic committed Nov 9, 2021
1 parent a933f0b commit db9c259
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 81 deletions.
48 changes: 26 additions & 22 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -6411,8 +6411,12 @@ function getFieldValues(frontmatterCache, field, settings) {
values.push(rawItemAsString.split("/").last());
}
}
else if (rawItem.path) {
values.push(rawItem.path.split("/").last());
else if (rawItem.path !== undefined) {
superDebug(settings, { rawItem });
const lastSplit = rawItem.path.split("/").last();
if (lastSplit !== undefined) {
values.push(lastSplit);
}
}
});
}
Expand Down Expand Up @@ -6526,7 +6530,7 @@ async function openOrSwitch(app, dest, currFile, event) {
}
else {
const mode = app.vault.getConfig("defaultViewMode");
const leaf = (event.ctrlKey || event.getModifierState('Meta'))
const leaf = event.ctrlKey || event.getModifierState("Meta")
? workspace.splitActiveLeaf()
: workspace.getUnpinnedLeaf();
await leaf.openFile(destFile, { active: true, mode });
Expand Down Expand Up @@ -6616,9 +6620,9 @@ function getAllGsInDir(userHierarchies, currGraphs, dir) {
}
function getAllFieldGs(fields, currGraphs) {
const fieldGs = [];
currGraphs.forEach(hierGs => {
DIRECTIONS.forEach(dir => {
Object.keys(hierGs[dir]).forEach(fieldName => {
currGraphs.forEach((hierGs) => {
DIRECTIONS.forEach((dir) => {
Object.keys(hierGs[dir]).forEach((fieldName) => {
if (fields.includes(fieldName)) {
const fieldG = hierGs[dir][fieldName];
if (fieldG instanceof graphlib.Graph)
Expand Down Expand Up @@ -6651,13 +6655,13 @@ const createOrUpdateYaml = async (key, value, file, frontmatter, api) => {
console.log(`Creating: ${key}: ${valueStr}`);
await api.createYamlProperty(key, `['${valueStr}']`, file);
}
else if ([...[frontmatter[key]]].flat(3).some(val => val == valueStr)) {
console.log('Already Exists!');
else if ([...[frontmatter[key]]].flat(3).some((val) => val == valueStr)) {
console.log("Already Exists!");
return;
}
else {
const oldValueFlat = [...[frontmatter[key]]].flat(4);
const newValue = [...oldValueFlat, valueStr].map(val => `'${val}'`);
const newValue = [...oldValueFlat, valueStr].map((val) => `'${val}'`);
console.log(`Updating: ${key}: ${newValue}`);
await api.update(key, `[${newValue.join(", ")}]`, file);
}
Expand All @@ -6667,26 +6671,26 @@ const writeBCToFile = (app, plugin, currGraphs, file) => {
const frontmatter = (_a = app.metadataCache.getFileCache(file)) === null || _a === void 0 ? void 0 : _a.frontmatter;
const api = (_b = app.plugins.plugins.metaedit) === null || _b === void 0 ? void 0 : _b.api;
if (!api) {
new obsidian.Notice('Metaedit must be enabled for this function to work');
new obsidian.Notice("Metaedit must be enabled for this function to work");
return;
}
currGraphs.hierGs.forEach(hier => {
DIRECTIONS.forEach(dir => {
currGraphs.hierGs.forEach((hier) => {
DIRECTIONS.forEach((dir) => {
let oppDir;
if (dir === 'up')
oppDir = 'down';
if (dir === 'down')
oppDir = 'up';
if (dir === 'same')
oppDir = 'same';
Object.keys(hier[dir]).forEach(field => {
if (dir === "up")
oppDir = "down";
if (dir === "down")
oppDir = "up";
if (dir === "same")
oppDir = "same";
Object.keys(hier[dir]).forEach((field) => {
const fieldG = hier[dir][field];
const succs = fieldG.predecessors(file.basename);
succs.forEach(async (succ) => {
const { fieldName } = fieldG.node(succ);
if (!plugin.settings.limitWriteBCCheckboxStates[fieldName])
return;
const currHier = plugin.settings.userHierarchies.filter(hier => hier[dir].includes(fieldName))[0];
const currHier = plugin.settings.userHierarchies.filter((hier) => hier[dir].includes(fieldName))[0];
let oppField = currHier[oppDir][0];
if (!oppField)
oppField = `<Reverse>${fieldName}`;
Expand All @@ -6698,11 +6702,11 @@ const writeBCToFile = (app, plugin, currGraphs, file) => {
};
function oppFields(field, dir, userHierarchies) {
var _a, _b;
let oppDir = 'same';
let oppDir = "same";
if (dir !== "same") {
oppDir = dir === "up" ? "down" : "up";
}
return (_b = (_a = userHierarchies.find(hier => hier[oppDir].includes(field))) === null || _a === void 0 ? void 0 : _a[oppDir]) !== null && _b !== void 0 ? _b : [];
return ((_b = (_a = userHierarchies.find((hier) => hier[oppDir].includes(field))) === null || _a === void 0 ? void 0 : _a[oppDir]) !== null && _b !== void 0 ? _b : []);
}

/**
Expand Down
134 changes: 75 additions & 59 deletions src/sharedFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
Notice,
Pos,
TFile,
WorkspaceLeaf
WorkspaceLeaf,
} from "obsidian";
import { DIRECTIONS, dropHeaderOrAlias, splitLinksRegex } from "src/constants";
import type {
Expand All @@ -19,13 +19,11 @@ import type {
HierarchyFields,
HierarchyGraphs,
JugglLink,
userHierarchy
userHierarchy,
} from "src/interfaces";
import type BreadcrumbsPlugin from "src/main";
import type MatrixView from "src/MatrixView";



export function sum(arr: number[]): number {
return arr.reduce((a, b) => a + b);
}
Expand Down Expand Up @@ -265,8 +263,12 @@ export function getFieldValues(
} else {
values.push(rawItemAsString.split("/").last());
}
} else if (rawItem.path) {
values.push((rawItem as dvLink).path.split("/").last());
} else if (rawItem.path !== undefined) {
superDebug(settings, { rawItem });
const lastSplit = rawItem.path.split("/").last();
if (lastSplit !== undefined) {
values.push(lastSplit);
}
}
});
}
Expand Down Expand Up @@ -384,7 +386,11 @@ export const isInVault = (app: App, note: string): boolean =>
app.workspace.getActiveFile().path
);

export function hoverPreview(event: MouseEvent, matrixView: MatrixView, to: string): void {
export function hoverPreview(
event: MouseEvent,
matrixView: MatrixView,
to: string
): void {
const targetEl = event.target as HTMLElement;

matrixView.app.workspace.trigger("hover-link", {
Expand All @@ -408,7 +414,9 @@ export async function openOrSwitch(
// If dest doesn't exist, make it
if (!destFile) {
const newFileFolder = app.fileManager.getNewFileParent(currFile.path).path;
const newFilePath = `${newFileFolder}${newFileFolder === "/" ? "" : "/"}${dest}.md`;
const newFilePath = `${newFileFolder}${
newFileFolder === "/" ? "" : "/"
}${dest}.md`;
await app.vault.create(newFilePath, "");
destFile = app.metadataCache.getFirstLinkpathDest(
newFilePath,
Expand All @@ -430,9 +438,10 @@ export async function openOrSwitch(
workspace.setActiveLeaf(leavesWithDestAlreadyOpen[0]);
} else {
const mode = (app.vault as any).getConfig("defaultViewMode");
const leaf = (event.ctrlKey || event.getModifierState('Meta'))
? workspace.splitActiveLeaf()
: workspace.getUnpinnedLeaf();
const leaf =
event.ctrlKey || event.getModifierState("Meta")
? workspace.splitActiveLeaf()
: workspace.getUnpinnedLeaf();

await leaf.openFile(destFile, { active: true, mode });
}
Expand Down Expand Up @@ -598,18 +607,17 @@ export function getAllGsInDir(

export function getAllFieldGs(fields: string[], currGraphs: HierarchyGraphs[]) {
const fieldGs: Graph[] = [];
currGraphs.forEach(hierGs => {
DIRECTIONS.forEach(dir => {
Object.keys(hierGs[dir]).forEach(fieldName => {
currGraphs.forEach((hierGs) => {
DIRECTIONS.forEach((dir) => {
Object.keys(hierGs[dir]).forEach((fieldName) => {
if (fields.includes(fieldName)) {
const fieldG = hierGs[dir][fieldName];
if (fieldG instanceof graphlib.Graph) fieldGs.push(fieldG)
if (fieldG instanceof graphlib.Graph) fieldGs.push(fieldG);
}
})
})
})
return fieldGs

});
});
});
return fieldGs;
}

export function hierToStr(hier: userHierarchy) {
Expand All @@ -636,66 +644,74 @@ export const createOrUpdateYaml = async (
frontmatter: FrontMatterCache | undefined,
api: { [fun: string]: (...args: any) => any }
) => {
let valueStr = value.toString()
let valueStr = value.toString();

if (!frontmatter || frontmatter[key] === undefined) {
console.log(`Creating: ${key}: ${valueStr}`)
console.log(`Creating: ${key}: ${valueStr}`);
await api.createYamlProperty(key, `['${valueStr}']`, file);
} else if ([...[frontmatter[key]]].flat(3).some(val => val == valueStr)) {
console.log('Already Exists!')
return
}
else {
} else if ([...[frontmatter[key]]].flat(3).some((val) => val == valueStr)) {
console.log("Already Exists!");
return;
} else {
const oldValueFlat: string[] = [...[frontmatter[key]]].flat(4);
const newValue = [...oldValueFlat, valueStr].map(val => `'${val}'`);
console.log(`Updating: ${key}: ${newValue}`)
const newValue = [...oldValueFlat, valueStr].map((val) => `'${val}'`);
console.log(`Updating: ${key}: ${newValue}`);
await api.update(key, `[${newValue.join(", ")}]`, file);
}
};

}

export const writeBCToFile = (app: App, plugin: BreadcrumbsPlugin, currGraphs: BCIndex, file: TFile) => {

export const writeBCToFile = (
app: App,
plugin: BreadcrumbsPlugin,
currGraphs: BCIndex,
file: TFile
) => {
const frontmatter = app.metadataCache.getFileCache(file)?.frontmatter;
const api = app.plugins.plugins.metaedit?.api
const api = app.plugins.plugins.metaedit?.api;

if (!api) {
new Notice('Metaedit must be enabled for this function to work');
return
new Notice("Metaedit must be enabled for this function to work");
return;
}

currGraphs.hierGs.forEach(hier => {
DIRECTIONS.forEach(dir => {
currGraphs.hierGs.forEach((hier) => {
DIRECTIONS.forEach((dir) => {
let oppDir: Directions;
if (dir === 'up') oppDir = 'down';
if (dir === 'down') oppDir = 'up';
if (dir === 'same') oppDir = 'same';

Object.keys(hier[dir]).forEach(field => {

if (dir === "up") oppDir = "down";
if (dir === "down") oppDir = "up";
if (dir === "same") oppDir = "same";

Object.keys(hier[dir]).forEach((field) => {
const fieldG = hier[dir][field];
const succs = fieldG.predecessors(file.basename) as string[];

succs.forEach(async succ => {
succs.forEach(async (succ) => {
const { fieldName } = fieldG.node(succ);
if (!plugin.settings.limitWriteBCCheckboxStates[fieldName]) return
if (!plugin.settings.limitWriteBCCheckboxStates[fieldName]) return;

const currHier = plugin.settings.userHierarchies.filter(hier => hier[dir].includes(fieldName))[0]
const currHier = plugin.settings.userHierarchies.filter((hier) =>
hier[dir].includes(fieldName)
)[0];
let oppField: string = currHier[oppDir][0];
if (!oppField) oppField = `<Reverse>${fieldName}`
if (!oppField) oppField = `<Reverse>${fieldName}`;

await createOrUpdateYaml(oppField, succ, file, frontmatter, api)
})
})
})
})
}
await createOrUpdateYaml(oppField, succ, file, frontmatter, api);
});
});
});
});
};

export function oppFields(field: string, dir: Directions, userHierarchies: userHierarchy[]): string[] {
let oppDir: Directions = 'same';
export function oppFields(
field: string,
dir: Directions,
userHierarchies: userHierarchy[]
): string[] {
let oppDir: Directions = "same";
if (dir !== "same") {
oppDir = dir === "up" ? "down" : "up"
oppDir = dir === "up" ? "down" : "up";
}
return userHierarchies.find(hier => hier[oppDir].includes(field))?.[oppDir] ?? []
}
return (
userHierarchies.find((hier) => hier[oppDir].includes(field))?.[oppDir] ?? []
);
}

0 comments on commit db9c259

Please sign in to comment.