Skip to content

Commit

Permalink
Add a Setting to enter default search string.
Browse files Browse the repository at this point in the history
  • Loading branch information
JohannesGeisler committed Jan 13, 2023
1 parent c7fd88f commit a84c846
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 12 deletions.
71 changes: 62 additions & 9 deletions main.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
import { App, Plugin, View} from 'obsidian';
import { App, Plugin, PluginSettingTab, View, Setting} from 'obsidian';

interface EasyGraphSearchSettings {
defaultSearchString: string;
}

const DEFAULT_SETTINGS: EasyGraphSearchSettings = {
defaultSearchString: ''
}

export default class EasyGraphSearch extends Plugin {
settings: EasyGraphSearchSettings;

async onload() {
await this.loadSettings();

// This adds an editor command that can perform some operation on the current editor instance
this.addCommand({
Expand All @@ -11,34 +21,77 @@ export default class EasyGraphSearch extends Plugin {
hotkeys: [{ modifiers: ["Alt"], key: "f" }],
checkCallback: (checking: boolean) => {
// Conditions to check
const graphView = this.app.workspace.getActiveViewOfType(View);
console.log(graphView);
const graphView = this.app.workspace.getActiveViewOfType(View);
// Check if null
if (graphView) {
// Check if the active view is a graph
if (graphView.getViewType() == "graph") {
// make sure that the graph controls is visable
graphView.dataEngine.controlsEl.getElementsByClassName("clickable-icon graph-controls-button mod-open")[0].click()
graphView.dataEngine.controlsEl.getElementsByClassName("clickable-icon graph-controls-button mod-open")[0].click();
// make sure that the filter options are not collapsed
graphView.dataEngine.filterOptions.setCollapsed(false)
graphView.dataEngine.filterOptions.setCollapsed(false);
// define the searchbox element
const searchBox = graphView.dataEngine.filterOptions.search.inputEl;
// select the input field TODO: copy search to clipboard? or add to settings
searchBox.select(true)
const searchBox = graphView.dataEngine.filterOptions.search;
const searchBoxEl = searchBox.inputEl;
// select the input field
searchBoxEl.select(true);
// input a default search term from the settings into the search (only if one exists)
if(this.settings.defaultSearchString)
{
console.log(this.settings.defaultSearchString);
searchBox.setValue(this.settings.defaultSearchString);
}
// Put the cursor in the end of the input field
searchBox.setSelectionRange(searchBox.selectionEnd, searchBox.selectionEnd)
searchBoxEl.setSelectionRange(searchBoxEl.selectionEnd, searchBoxEl.selectionEnd);
}
}

}
});

// This adds a settings tab so the user can configure various aspects of the plugin
this.addSettingTab(new EasyGraphSearchSettingsTab(this.app, this));

}

onunload() {

}

async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}

async saveSettings() {
await this.saveData(this.settings);
}
}


class EasyGraphSearchSettingsTab extends PluginSettingTab {
plugin: EasyGraphSearch;

constructor(app: App, plugin: EasyGraphSearch) {
super(app, plugin);
this.plugin = plugin;
}

display(): void {
const {containerEl} = this;

containerEl.empty();

containerEl.createEl('h2', {text: 'Settings for the Graph Search Hotkey Plugin:'});

new Setting(containerEl)
.setName('Default search string')
.setDesc('Search String to put into Graph search, this is especially useful if you have certain folders you dont want to show in your graph, e.g. \"-path:journal\". Leave this blank to always put the curso in the end.')
.addText(text => text
.setPlaceholder('Enter your search string')
.setValue(this.plugin.settings.defaultSearchString)
.onChange(async (value) => {
this.plugin.settings.defaultSearchString = value;
await this.plugin.saveSettings();
}));
}
}
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "obsidian-graph-search-hotkey",
"name": "Graph Search Hotkey",
"version": "1.0.0",
"version": "1.0.1",
"minAppVersion": "0.15.0",
"description": "This is a simple plugin so you can set a hotkey to switch to the search bar in the Graph view. It is Alt+f as a default.",
"author": "Johannes Geisler",
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "obsidian-graph-search-hotkey",
"version": "1.0.0",
"version": "1.0.1",
"description": "This is a simple plugin so you can set a hotkey to switch to the search bar in the Graph view. It is Alt+f as a default.",
"main": "main.js",
"scripts": {
Expand All @@ -9,7 +9,7 @@
"version": "node version-bump.mjs && git add manifest.json versions.json"
},
"keywords": [],
"author": "",
"author": "Johannes Geisler",
"license": "MIT",
"devDependencies": {
"@types/node": "^16.11.6",
Expand Down

0 comments on commit a84c846

Please sign in to comment.