Skip to content

Commit

Permalink
feat(LimitTrailFields): ✨ Ability to limit which fields are shown in …
Browse files Browse the repository at this point in the history
…the trail/grid view
  • Loading branch information
SkepticMystic committed Sep 8, 2021
1 parent c8b57ad commit ba08396
Show file tree
Hide file tree
Showing 5 changed files with 197 additions and 10 deletions.
94 changes: 92 additions & 2 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -6614,6 +6614,21 @@ function getAllGsInDir(userHierarchies, currGraphs, dir) {
// console.log({ allXGs, allGsInDir });
return allGsInDir;
}
function getAllFieldGs(fields, currGraphs) {
const fieldGs = [];
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);
}
});
});
});
return fieldGs;
}
function hierToStr(hier) {
return `↑: ${hier.up.join(", ")}
→: ${hier.same.join(", ")}
Expand Down Expand Up @@ -6670,7 +6685,15 @@ const writeBCToFile = (app, plugin, currGraphs, file) => {
});
});
});
};
};
function oppFields(field, dir, userHierarchies) {
var _a, _b;
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 : [];
}

/**
* @license
Expand Down Expand Up @@ -24258,6 +24281,18 @@ class BreadcrumbsSettingTab extends obsidian.PluginSettingTab {
saveButton.toggleClass("hierarchy-unsaved", true);
saveButton.textContent = "Save";
}));
async function resetLimitTrailCheckboxes() {
settings.limitTrailCheckboxStates = {};
settings.userHierarchies.forEach(userHier => {
userHier.up.forEach(async (field) => {
// First sort out limitTrailCheckboxStates
settings.limitTrailCheckboxStates[field] = true;
await plugin.saveSettings();
});
});
await plugin.saveSettings();
drawLimitTrailCheckboxes(checkboxDiv);
}
row.createEl("button", { text: "X" }, (el) => {
el.addEventListener("click", async () => {
row.remove();
Expand All @@ -24266,6 +24301,8 @@ class BreadcrumbsSettingTab extends obsidian.PluginSettingTab {
settings.userHierarchies.splice(removeIndex, 1);
await plugin.saveSettings();
}
// Refresh limitTrailFields
resetLimitTrailCheckboxes();
new obsidian.Notice("Hierarchy Removed.");
});
});
Expand All @@ -24283,6 +24320,7 @@ class BreadcrumbsSettingTab extends obsidian.PluginSettingTab {
if (removeIndex > -1) {
settings.userHierarchies.splice(removeIndex, 1);
await plugin.saveSettings();
resetLimitTrailCheckboxes();
}
}
cleanInputs = [upInput.value, sameInput.value, downInput.value].map(splitAndTrim);
Expand All @@ -24299,6 +24337,7 @@ class BreadcrumbsSettingTab extends obsidian.PluginSettingTab {
});
await plugin.saveSettings();
new obsidian.Notice("Hierarchy saved.");
resetLimitTrailCheckboxes();
}
});
});
Expand Down Expand Up @@ -24563,6 +24602,33 @@ class BreadcrumbsSettingTab extends obsidian.PluginSettingTab {
await plugin.saveSettings();
await plugin.drawTrail();
}));
const limitTrailFieldsDiv = trailDetails.createDiv({ cls: 'limit-ML-fields' });
limitTrailFieldsDiv.createEl('strong', { 'text': 'Limit M/L View to only show certain fields' });
const checkboxDiv = limitTrailFieldsDiv.createDiv({ cls: 'checkboxes' });
function drawLimitTrailCheckboxes(div) {
checkboxDiv.empty();
const checkboxStates = settings.limitTrailCheckboxStates;
settings.userHierarchies.forEach(userHier => {
userHier.up.forEach(async (field) => {
// First sort out limitTrailCheckboxStates
if (checkboxStates[field] === undefined) {
checkboxStates[field] = true;
await plugin.saveSettings();
}
const cbDiv = div.createDiv();
const checkedQ = checkboxStates[field];
const cb = cbDiv.createEl('input', { type: 'checkbox', attr: { id: field } });
cb.checked = checkedQ;
cbDiv.createEl('label', { text: field, attr: { for: field } });
cb.addEventListener('change', async (event) => {
checkboxStates[field] = cb.checked;
await plugin.saveSettings();
console.log(settings.limitTrailCheckboxStates);
});
});
});
}
drawLimitTrailCheckboxes(checkboxDiv);
new obsidian.Setting(trailDetails)
.setName("Field name to hide trail")
.setDesc("A note-specific toggle to hide the Trail View. By default, it is `hide-trail`. So, to hide the trail on a specific note, add the field to that note's yaml, like so: `hide-trail: {{anything}}`.")
Expand Down Expand Up @@ -24784,6 +24850,9 @@ class BreadcrumbsSettingTab extends obsidian.PluginSettingTab {
settings.superDebugMode = value;
await plugin.saveSettings();
}));
debugDetails.createEl('button', { text: 'Console log `settings`' }, (el) => {
el.addEventListener('click', () => console.log(settings));
});
new KoFi({ target: this.containerEl });
}
}
Expand Down Expand Up @@ -37022,6 +37091,7 @@ const DEFAULT_SETTINGS = {
filterImpliedSiblingsOfDifferentTypes: false,
rlLeaf: true,
showTrail: true,
limitTrailCheckboxStates: {},
hideTrailFieldName: 'hide-trail',
trailOrTable: 3,
gridDots: false,
Expand Down Expand Up @@ -37376,6 +37446,7 @@ class BreadcrumbsPlugin extends obsidian.Plugin {
hierGs: [],
mergedGs: { up: undefined, same: undefined, down: undefined },
closedGs: { up: undefined, same: undefined, down: undefined },
limitTrailG: undefined
};
userHierarchies.forEach((hier, i) => {
const newGraphs = { up: {}, same: {}, down: {} };
Expand Down Expand Up @@ -37436,6 +37507,25 @@ class BreadcrumbsPlugin extends obsidian.Plugin {
graphs.closedGs[dir] = closeImpliedLinks(graphs.mergedGs[dir], graphs.mergedGs[dir]);
}
});
// LimitTrailG
if (Object.values(settings.limitTrailCheckboxStates).every(val => val)) {
graphs.limitTrailG = graphs.closedGs.up;
}
else {
const allUps = getAllGsInDir(userHierarchies, graphs.hierGs, 'up');
const allLimitedTrailsGsKeys = Object.keys(allUps).filter(field => settings.limitTrailCheckboxStates[field]);
const allLimitedTrailsGs = [];
allLimitedTrailsGsKeys.forEach(key => allLimitedTrailsGs.push(allUps[key]));
const mergedLimitedUpGs = mergeGs(...allLimitedTrailsGs);
const allLimitedDownGs = [];
Object.keys(settings.limitTrailCheckboxStates).forEach(limitedField => {
const oppFieldsArr = oppFields(limitedField, 'up', userHierarchies);
const oppGs = getAllFieldGs(oppFieldsArr, graphs.hierGs);
allLimitedDownGs.push(...oppGs);
});
const mergedLimitedDownGs = mergeGs(...allLimitedDownGs);
graphs.limitTrailG = closeImpliedLinks(mergedLimitedUpGs, mergedLimitedDownGs);
}
debug(settings, "graphs inited");
debug(settings, { graphs });
debugGroupEnd(settings, "debugMode");
Expand Down Expand Up @@ -37545,7 +37635,7 @@ class BreadcrumbsPlugin extends obsidian.Plugin {
debugGroupEnd(settings, "debugMode");
return;
}
const closedUp = this.currGraphs.closedGs.up;
const closedUp = this.currGraphs.limitTrailG;
const sortedTrails = this.getBreadcrumbs(closedUp, currFile);
debug(settings, { sortedTrails });
// Get the container div of the active note
Expand Down
60 changes: 60 additions & 0 deletions src/BreadcrumbsSettingTab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from "obsidian";
import {
ALLUNLINKED,
DIRECTIONS,
REAlCLOSED,
RELATIONS,
VIEW_TYPE_BREADCRUMBS_MATRIX,
Expand Down Expand Up @@ -80,6 +81,20 @@ export class BreadcrumbsSettingTab extends PluginSettingTab {
})
);

async function resetLimitTrailCheckboxes() {
settings.limitTrailCheckboxStates = {}
settings.userHierarchies.forEach(userHier => {
userHier.up.forEach(async field => {
// First sort out limitTrailCheckboxStates
settings.limitTrailCheckboxStates[field] = true;
await plugin.saveSettings()

})
})
await plugin.saveSettings()
drawLimitTrailCheckboxes(checkboxDiv)
}

const deleteButton = row.createEl("button", { text: "X" }, (el) => {
el.addEventListener("click", async () => {
row.remove();
Expand All @@ -94,6 +109,10 @@ export class BreadcrumbsSettingTab extends PluginSettingTab {
settings.userHierarchies.splice(removeIndex, 1);
await plugin.saveSettings();
}

// Refresh limitTrailFields
resetLimitTrailCheckboxes()

new Notice("Hierarchy Removed.");
});
});
Expand Down Expand Up @@ -128,6 +147,7 @@ export class BreadcrumbsSettingTab extends PluginSettingTab {
if (removeIndex > -1) {
settings.userHierarchies.splice(removeIndex, 1);
await plugin.saveSettings();
resetLimitTrailCheckboxes()
}
}
cleanInputs = [upInput.value, sameInput.value, downInput.value].map(
Expand All @@ -149,6 +169,8 @@ export class BreadcrumbsSettingTab extends PluginSettingTab {
});
await plugin.saveSettings();
new Notice("Hierarchy saved.");

resetLimitTrailCheckboxes()
}
});
}
Expand Down Expand Up @@ -424,6 +446,7 @@ export class BreadcrumbsSettingTab extends PluginSettingTab {
})
);


// TODO I don't think this setting works anymore. I removed it's functionality when adding multiple hierarchies
new Setting(MLViewDetails)
.setName("Show all field names or just relation types")
Expand Down Expand Up @@ -496,6 +519,39 @@ export class BreadcrumbsSettingTab extends PluginSettingTab {
})
);

const limitTrailFieldsDiv = trailDetails.createDiv({ cls: 'limit-ML-fields' })
limitTrailFieldsDiv.createEl('strong', { 'text': 'Limit M/L View to only show certain fields' })

const checkboxDiv = limitTrailFieldsDiv.createDiv({ cls: 'checkboxes' })

function drawLimitTrailCheckboxes(div: HTMLDivElement) {
checkboxDiv.empty()
const checkboxStates = settings.limitTrailCheckboxStates

settings.userHierarchies.forEach(userHier => {
userHier.up.forEach(async field => {
// First sort out limitTrailCheckboxStates
if (checkboxStates[field] === undefined) {
checkboxStates[field] = true;
await plugin.saveSettings()
}
const cbDiv = div.createDiv()
const checkedQ = checkboxStates[field]
const cb = cbDiv.createEl('input', { type: 'checkbox', attr: { id: field } })
cb.checked = checkedQ
const label = cbDiv.createEl('label', { text: field, attr: { for: field } })

cb.addEventListener('change', async (event) => {
checkboxStates[field] = cb.checked;
await plugin.saveSettings()
console.log(settings.limitTrailCheckboxStates)
})
})
})
}

drawLimitTrailCheckboxes(checkboxDiv)


new Setting(trailDetails)
.setName("Field name to hide trail")
Expand Down Expand Up @@ -786,6 +842,10 @@ export class BreadcrumbsSettingTab extends PluginSettingTab {
})
);

debugDetails.createEl('button', { text: 'Console log `settings`' }, (el) => {
el.addEventListener('click', () => console.log(settings))
})

new KoFi({ target: this.containerEl });
}
}
2 changes: 2 additions & 0 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export interface BreadcrumbsSettings {
filterImpliedSiblingsOfDifferentTypes: boolean;
rlLeaf: boolean;
showTrail: boolean;
limitTrailCheckboxStates: { [field: string]: boolean };
hideTrailFieldName: string;
trailOrTable: 1 | 2 | 3;
gridDots: boolean;
Expand Down Expand Up @@ -180,6 +181,7 @@ export interface BCIndex {
hierGs: HierarchyGraphs[];
mergedGs: MergedGraphs;
closedGs: ClosedGraphs;
limitTrailG: Graph;
}

export type HierarchyGraphs = {
Expand Down
Loading

0 comments on commit ba08396

Please sign in to comment.