Skip to content

Commit

Permalink
refactor: ♻️ Better handling of edge cases on splitAndTrim
Browse files Browse the repository at this point in the history
  • Loading branch information
SkepticMystic committed Nov 23, 2021
1 parent 2e6c2b7 commit e2b68d6
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 35 deletions.
40 changes: 14 additions & 26 deletions src/BreadcrumbsSettingTab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,17 @@ export class BCSettingTab extends PluginSettingTab {
.setName("Hierarchy Note(s)")
.setDesc("A list of notes used to create external Breadcrumb structures.")
.addText((text) => {
let finalValue: string[];
text
.setPlaceholder("Hierarchy Note(s)")
.setValue([settings.hierarchyNotes].flat().join(", "))
.onChange(async (value) => {
finalValue = splitAndTrim(value);
});
.setValue(settings.hierarchyNotes.join(", "));

text.inputEl.onblur = async () => {
if (finalValue[0] === "") {
settings.hierarchyNotes = finalValue;
const splits = splitAndTrim(text.getValue());
if (splits[0] === undefined) {
settings.hierarchyNotes = splits;
await plugin.saveSettings();
} else if (finalValue.every((note) => isInVault(this.app, note))) {
settings.hierarchyNotes = finalValue;
} else if (splits.every((note) => isInVault(this.app, note))) {
settings.hierarchyNotes = splits;
await plugin.saveSettings();
} else {
new Notice("Atleast one of the notes is not in your vault");
Expand Down Expand Up @@ -146,13 +143,9 @@ export class BCSettingTab extends PluginSettingTab {
"A comma-separated list of fields you use to specify note name aliases. These fields will be checked, in order, and be used to display an alternate note title in both the list/matrix view, and trail/grid view. This field will probably be `alias` or `aliases`, but it can be anything, like `title`, for example."
)
.addText((text) => {
let finalValue: string;

text.setValue(settings.altLinkFields.join(", ")).onChange((str) => {
finalValue = str;
});
text.setValue(settings.altLinkFields.join(", "));
text.inputEl.onblur = async () => {
settings.altLinkFields = splitAndTrim(finalValue);
settings.altLinkFields = splitAndTrim(text.getValue());
await plugin.saveSettings();
};
});
Expand Down Expand Up @@ -529,22 +522,17 @@ export class BCSettingTab extends PluginSettingTab {
"The note that all of your other notes lead back to. The parent of all your parent notes. Just enter the name. So if your index note is `000 Home.md`, enter `000 Home`. You can also have multiple index notes (comma-separated list). The breadcrumb trail will show the shortest path back to any one of the index notes listed. You can now leave this field empty, meaning the trail will show a path going as far up the parent-tree as possible."
)
.addText((text) => {
let finalValue: string[];
text
.setPlaceholder("Index Note")
.setValue([settings.indexNotes].flat().join(", "))
.onChange(async (value) => {
finalValue = splitAndTrim(value);
});
.setValue(settings.indexNotes.join(", "));

text.inputEl.onblur = async () => {
// TODO Refactor this to general purpose isInVault function

if (finalValue[0] === "") {
settings.indexNotes = finalValue;
const splits = splitAndTrim(text.getValue());
if (splits[0] === undefined) {
settings.indexNotes = splits;
await plugin.saveSettings();
} else if (finalValue.every((index) => isInVault(this.app, index))) {
settings.indexNotes = finalValue;
} else if (splits.every((index) => isInVault(this.app, index))) {
settings.indexNotes = splits;
await plugin.saveSettings();
} else {
new Notice(`Atleast one of the notes is not in your vault`);
Expand Down
8 changes: 2 additions & 6 deletions src/Components/UserHierarchies.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script lang="ts">
import { Notice } from "obsidian";
import type { UserHier } from "src";
import type { UserHier } from "src/interfaces";
import { ARROW_DIRECTIONS, blankUserHier, DIRECTIONS } from "src/constants";
import type BCPlugin from "src/main";
import { hierToStr, splitAndTrim, swapItems } from "src/sharedFunctions";
Expand Down Expand Up @@ -94,11 +94,7 @@
value={hier[dir]?.join(", ") ?? ""}
on:change={async (e) => {
const { value } = e.target;
if (value === "") {
currHiers[i][dir] = [];
} else {
currHiers[i][dir] = splitAndTrim(value);
}
currHiers[i][dir] = splitAndTrim(value);
await update(currHiers);
}}
/>
Expand Down
8 changes: 5 additions & 3 deletions src/sharedFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export function debugGroupEnd(
}
}

export function splitAndDrop(str: string): string[] | [] {
export function splitAndDrop(str: string): string[] {
return (
str
?.match(splitLinksRegex)
Expand All @@ -74,8 +74,10 @@ export function splitAndDrop(str: string): string[] | [] {
*/
export const getBasename = (path: string) => path.split("/").last();

export const splitAndTrim = (fields: string): string[] =>
fields.split(",").map((str: string) => str.trim());
export const splitAndTrim = (fields: string): string[] => {
if (fields === "") return [];
else return fields.split(",").map((str) => str.trim());
};

// This function takes the real & implied graphs for a given relation, and returns a new graphs with both.
// It makes implied relations real
Expand Down

0 comments on commit e2b68d6

Please sign in to comment.