Skip to content

Commit

Permalink
feat: ✨ Field Suggestor! Type "BC-" at the start of a line to trigger
Browse files Browse the repository at this point in the history
hideTrailField settings has been removed in favour of a standard 'BC-hide-trail'
  • Loading branch information
SkepticMystic committed Nov 24, 2021
1 parent 865cf58 commit 652da4f
Show file tree
Hide file tree
Showing 6 changed files with 234 additions and 33 deletions.
111 changes: 98 additions & 13 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -24798,6 +24798,33 @@ const blankRealNImplied = () => {
prev: { reals: [], implieds: [] },
};
};
const BC_FIELDS = [
{
field: "BC-folder-note",
desc: "Set this note as a Breadcrumbs folder-note. All other notes in this folder will point up to this note",
after: ": true",
},
{
field: "BC-folder-note-up",
desc: "Manually choose the up field for this folder-note to use",
after: ": ",
},
{
field: "BC-tag-note",
desc: "Set this note as a Breadcrumbs tag-note. All other notes with this tag will point up to this note",
after: ": true",
},
{
field: "BC-tag-note-up",
desc: "Manually choose the up field for this tag-note to use",
after: ": ",
},
{
field: "BC-hide-trail",
desc: "Don't show the trail in this note",
after: ": true",
},
];
const DEFAULT_SETTINGS = {
aliasesInIndex: false,
alphaSortAsc: true,
Expand All @@ -24807,6 +24834,7 @@ const DEFAULT_SETTINGS = {
defaultView: true,
dvWaitTime: 5000,
dotsColour: "#000000",
fieldSuggestor: true,
filterImpliedSiblingsOfDifferentTypes: false,
limitWriteBCCheckboxStates: {},
indexNotes: [""],
Expand All @@ -24827,7 +24855,6 @@ const DEFAULT_SETTINGS = {
showGrid: true,
showPrevNext: true,
limitTrailCheckboxStates: {},
hideTrailField: "hide-trail",
gridDots: false,
gridHeatmap: false,
heatmapColour: getComputedStyle(document.body).getPropertyValue("--text-accent"),
Expand Down Expand Up @@ -25731,6 +25758,13 @@ class BCSettingTab extends obsidian.PluginSettingTab {
await plugin.saveSettings();
};
});
new obsidian.Setting(generalDetails)
.setName("Enable Field Suggestor")
.setDesc('Alot of Breadcrumbs features require a metadata (or inline Dataview) field to work. For example, `BC-folder-note`. The Field Suggestor will show an autocomplete menu with all available Breadcrumbs field options when the content you type matches the regex /^BC-.*$/. Basically, just type "BC-" at the start of a line to trigger it.')
.addToggle((toggle) => toggle.setValue(settings.fieldSuggestor).onChange(async (value) => {
settings.fieldSuggestor = value;
await plugin.saveSettings();
}));
new obsidian.Setting(generalDetails)
.setName("Refresh Index on Note Change")
.setDesc("Refresh the Breadcrumbs index data everytime you change notes. This is how Breadcrumbs used to work, making it responsive to changes immediately after changing notes. However, this can be very slow on large vaults, so it is off by default.")
Expand Down Expand Up @@ -25941,16 +25975,18 @@ class BCSettingTab extends obsidian.PluginSettingTab {
});
}
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}}`.")
.addText((text) => {
text.setValue(settings.hideTrailField);
text.inputEl.onblur = async () => {
settings.hideTrailField = text.getValue();
await plugin.saveSettings();
};
});
// new 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}}`."
// )
// .addText((text) => {
// text.setValue(settings.hideTrailField);
// text.inputEl.onblur = async () => {
// settings.hideTrailField = text.getValue();
// await plugin.saveSettings();
// };
// });
new obsidian.Setting(trailDetails)
.setName("Views to show")
.setDesc("Choose which of the views to show at the top of the note.\nTrail, Grid, and/or the Next-Previous view.")
Expand Down Expand Up @@ -26251,6 +26287,54 @@ class BCSettingTab extends obsidian.PluginSettingTab {
}
}

class FieldSuggestor extends obsidian.EditorSuggest {
constructor(plugin) {
super(plugin.app);
this.getSuggestions = (context) => {
const { query } = context;
return BC_FIELDS.map((sug) => sug.field).filter((sug) => sug.includes(query));
};
this.plugin = plugin;
}
onTrigger(cursor, editor, _) {
var _a;
if (this.plugin.settings.fieldSuggestor) {
const sub = editor.getLine(cursor.line).substring(0, cursor.ch);
const match = (_a = sub.match(/^BC-(.*)$/)) === null || _a === void 0 ? void 0 : _a[1];
if (match !== undefined) {
return {
end: cursor,
start: {
ch: sub.lastIndexOf(match),
line: cursor.line,
},
query: match,
};
}
}
return null;
}
renderSuggestion(suggestion, el) {
var _a;
el.createDiv({
text: suggestion.replace("BC-", ""),
cls: "BC-suggester-container",
attr: {
"aria-label": (_a = BC_FIELDS.find((f) => f.field === suggestion)) === null || _a === void 0 ? void 0 : _a.desc,
"aria-label-position": "right",
},
});
}
selectSuggestion(suggestion) {
var _a;
const { context } = this;
if (context) {
const replacement = `${suggestion}${(_a = BC_FIELDS.find((f) => f.field === suggestion)) === null || _a === void 0 ? void 0 : _a.after}`;
context.editor.replaceRange(replacement, { ch: 0, line: context.start.line }, context.end);
}
}
}

var d3Array = createCommonjsModule(function (module, exports) {
// https://d3js.org/d3-array/ v2.12.1 Copyright 2021 Mike Bostock
(function (global, factory) {
Expand Down Expand Up @@ -49435,6 +49519,7 @@ class BCPlugin extends obsidian.Plugin {
for (const view of VIEWS) {
this.registerView(view.type, (leaf) => new view.constructor(leaf, this));
}
this.registerEditorSuggest(new FieldSuggestor(this));
this.app.workspace.onLayoutReady(async () => {
var _a;
if (this.app.plugins.enabledPlugins.has("dataview")) {
Expand Down Expand Up @@ -50171,7 +50256,7 @@ class BCPlugin extends obsidian.Plugin {
async drawTrail() {
var _a, _b, _c;
const { settings } = this;
const { showBCs, hideTrailField, noPathMessage, respectReadableLineLength, showTrail, showGrid, showPrevNext, } = settings;
const { showBCs, noPathMessage, respectReadableLineLength, showTrail, showGrid, showPrevNext, } = settings;
debugGroupStart(settings, "debugMode", "Draw Trail");
const activeMDView = this.app.workspace.getActiveViewOfType(obsidian.MarkdownView);
if (!showBCs || !activeMDView) {
Expand All @@ -50186,7 +50271,7 @@ class BCPlugin extends obsidian.Plugin {
}
const { file } = activeMDView;
const { frontmatter } = (_a = this.app.metadataCache.getFileCache(file)) !== null && _a !== void 0 ? _a : {};
if ((frontmatter === null || frontmatter === void 0 ? void 0 : frontmatter[hideTrailField]) || (frontmatter === null || frontmatter === void 0 ? void 0 : frontmatter["kanban-plugin"])) {
if ((frontmatter === null || frontmatter === void 0 ? void 0 : frontmatter["BC-hide-trail"]) || (frontmatter === null || frontmatter === void 0 ? void 0 : frontmatter["kanban-plugin"])) {
debugGroupEnd(settings, "debugMode");
return;
}
Expand Down
36 changes: 24 additions & 12 deletions src/BreadcrumbsSettingTab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,18 @@ export class BCSettingTab extends PluginSettingTab {
};
});

new Setting(generalDetails)
.setName("Enable Field Suggestor")
.setDesc(
'Alot of Breadcrumbs features require a metadata (or inline Dataview) field to work. For example, `BC-folder-note`. The Field Suggestor will show an autocomplete menu with all available Breadcrumbs field options when the content you type matches the regex /^BC-.*$/. Basically, just type "BC-" at the start of a line to trigger it.'
)
.addToggle((toggle) =>
toggle.setValue(settings.fieldSuggestor).onChange(async (value) => {
settings.fieldSuggestor = value;
await plugin.saveSettings();
})
);

new Setting(generalDetails)
.setName("Refresh Index on Note Change")
.setDesc(
Expand Down Expand Up @@ -413,18 +425,18 @@ export class BCSettingTab extends PluginSettingTab {

drawLimitTrailCheckboxes(checkboxDiv);

new 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}}`."
)
.addText((text) => {
text.setValue(settings.hideTrailField);
text.inputEl.onblur = async () => {
settings.hideTrailField = text.getValue();
await plugin.saveSettings();
};
});
// new 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}}`."
// )
// .addText((text) => {
// text.setValue(settings.hideTrailField);
// text.inputEl.onblur = async () => {
// settings.hideTrailField = text.getValue();
// await plugin.saveSettings();
// };
// });

new Setting(trailDetails)
.setName("Views to show")
Expand Down
73 changes: 73 additions & 0 deletions src/FieldSuggestor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import {
Editor,
EditorPosition,
EditorSuggest,
EditorSuggestContext,
EditorSuggestTriggerInfo,
TFile,
} from "obsidian";
import { BC_FIELDS } from "src/constants";
import type BCPlugin from "src/main";

export class FieldSuggestor extends EditorSuggest<string> {
plugin: BCPlugin;

constructor(plugin: BCPlugin) {
super(plugin.app);
this.plugin = plugin;
}

onTrigger(
cursor: EditorPosition,
editor: Editor,
_: TFile
): EditorSuggestTriggerInfo | null {
if (this.plugin.settings.fieldSuggestor) {
const sub = editor.getLine(cursor.line).substring(0, cursor.ch);
const match = sub.match(/^BC-(.*)$/)?.[1];
if (match !== undefined) {
return {
end: cursor,
start: {
ch: sub.lastIndexOf(match),
line: cursor.line,
},
query: match,
};
}
}
return null;
}

getSuggestions = (context: EditorSuggestContext) => {
const { query } = context;
return BC_FIELDS.map((sug) => sug.field).filter((sug) =>
sug.includes(query)
);
};

renderSuggestion(suggestion: string, el: HTMLElement): void {
el.createDiv({
text: suggestion.replace("BC-", ""),
cls: "BC-suggester-container",
attr: {
"aria-label": BC_FIELDS.find((f) => f.field === suggestion)?.desc,
"aria-label-position": "right",
},
});
}

selectSuggestion(suggestion: string): void {
const { context } = this;
if (context) {
const replacement = `${suggestion}${
BC_FIELDS.find((f) => f.field === suggestion)?.after
}`;
context.editor.replaceRange(
replacement,
{ ch: 0, line: context.start.line },
context.end
);
}
}
}
32 changes: 30 additions & 2 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export const VISTYPES: visTypes[] = [
"Radial Tree",
];

export const DIRECTIONS: Directions[] = ["up", "same", "down", "next", "prev"];
export const DIRECTIONS = ["up", "same", "down", "next", "prev"] as const;
export const ARROW_DIRECTIONS: { [dir in Directions]: string } = {
up: "↑",
same: "↔",
Expand Down Expand Up @@ -84,6 +84,34 @@ export const blankRealNImplied = () => {
};
};

export const BC_FIELDS = [
{
field: "BC-folder-note",
desc: "Set this note as a Breadcrumbs folder-note. All other notes in this folder will point up to this note",
after: ": true",
},
{
field: "BC-folder-note-up",
desc: "Manually choose the up field for this folder-note to use",
after: ": ",
},
{
field: "BC-tag-note",
desc: "Set this note as a Breadcrumbs tag-note. All other notes with this tag will point up to this note",
after: ": true",
},
{
field: "BC-tag-note-up",
desc: "Manually choose the up field for this tag-note to use",
after: ": ",
},
{
field: "BC-hide-trail",
desc: "Don't show the trail in this note",
after: ": true",
},
];

export const DEFAULT_SETTINGS: BCSettings = {
aliasesInIndex: false,
alphaSortAsc: true,
Expand All @@ -93,6 +121,7 @@ export const DEFAULT_SETTINGS: BCSettings = {
defaultView: true,
dvWaitTime: 5000,
dotsColour: "#000000",
fieldSuggestor: true,
filterImpliedSiblingsOfDifferentTypes: false,
limitWriteBCCheckboxStates: {},
indexNotes: [""],
Expand All @@ -113,7 +142,6 @@ export const DEFAULT_SETTINGS: BCSettings = {
showGrid: true,
showPrevNext: true,
limitTrailCheckboxStates: {},
hideTrailField: "hide-trail",
gridDots: false,
gridHeatmap: false,
heatmapColour: getComputedStyle(document.body).getPropertyValue(
Expand Down
9 changes: 5 additions & 4 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import type Graph from "graphology";
import type { Constructor, FrontMatterCache, Pos, TFile } from "obsidian";
import type { DIRECTIONS } from "src/constants";
import type DucksView from "src/DucksView";
import type StatsView from "src/StatsView";
import type MatrixView from "src/MatrixView";
import type StatsView from "src/StatsView";

export interface BCSettings {
aliasesInIndex: boolean;
Expand All @@ -13,11 +14,11 @@ export interface BCSettings {
debugMode: boolean;
defaultView: boolean;
dotsColour: string;
fieldSuggestor: boolean;
filterImpliedSiblingsOfDifferentTypes: boolean;
gridDots: boolean;
gridHeatmap: boolean;
heatmapColour: string;
hideTrailField: string;
hierarchyNotes: string[];
HNUpField: string;
indexNotes: string[];
Expand Down Expand Up @@ -73,12 +74,12 @@ export interface dvFrontmatterCache {
| TFile;
}

export type Directions = "up" | "same" | "down" | "next" | "prev";
export type Directions = typeof DIRECTIONS[number];
export type UserHier = {
[dir in Directions]: string[];
};

export type MyView = MatrixView | StatsView | DucksView;
export type MyView = MatrixView | DucksView | StatsView;
export type ViewInfo = {
plain: string;
type: string;
Expand Down
Loading

0 comments on commit 652da4f

Please sign in to comment.