generated from obsidianmd/obsidian-sample-plugin
-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
01bc1b1
commit 22b8949
Showing
7 changed files
with
111 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Credits go to Liam's Periodic Notes Plugin: https://github.com/liamcain/obsidian-periodic-notes | ||
import { App, TAbstractFile, TFolder } from "obsidian"; | ||
import { TextInputSuggest } from "./suggest"; | ||
import { ModalFormError } from "src/utils/Error"; | ||
|
||
/** | ||
* Offers suggestions based on a dataview query. | ||
* It requires the dataview plugin to be installed and enabled. | ||
* For now, we are not very strict with the checks and just throw errors | ||
*/ | ||
export class DataviewSuggest extends TextInputSuggest<string> { | ||
sandboxedQuery: (dv: any, pages: any) => string[] | ||
|
||
constructor( | ||
public inputEl: HTMLInputElement, | ||
private dvQuery: string, | ||
protected app: App, | ||
) { | ||
super(app, inputEl); | ||
this.sandboxedQuery = eval(`(function sandboxedQuery(dv, pages) { return ${dvQuery} })`) | ||
|
||
} | ||
|
||
getSuggestions(inputStr: string): string[] { | ||
const dv = this.app.plugins.plugins.dataview?.api | ||
if (!dv) { | ||
throw new ModalFormError("Dataview plugin is not enabled") | ||
} | ||
const result = this.sandboxedQuery(dv, dv.pages) | ||
if (!Array.isArray(result)) { | ||
throw new ModalFormError("The dataview query did not return an array") | ||
} | ||
return result | ||
} | ||
|
||
renderSuggestion(option: string, el: HTMLElement): void { | ||
el.setText(option); | ||
} | ||
|
||
selectSuggestion(option: string): void { | ||
this.inputEl.value = option; | ||
this.inputEl.trigger("input"); | ||
this.close(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// https://github.com/blacksmithgu/obsidian-dataview/blob/bb594a27ba1eed130d7c2ab7eff0990578e93f62/src/typings/obsidian-ex.d.ts | ||
import type { DataviewApi } from "api/plugin-api"; | ||
import "obsidian"; | ||
|
||
declare module "obsidian" { | ||
interface MetadataCache { | ||
trigger(...args: Parameters<MetadataCache["on"]>): void; | ||
trigger(name: string, ...data: any[]): void; | ||
} | ||
|
||
interface App { | ||
appId?: string; | ||
plugins: { | ||
enabledPlugins: Set<string>; | ||
plugins: { | ||
dataview?: { | ||
api: DataviewApi; | ||
}; | ||
}; | ||
}; | ||
} | ||
|
||
interface Workspace { | ||
/** Sent to rendered dataview components to tell them to possibly refresh */ | ||
on(name: "dataview:refresh-views", callback: () => void, ctx?: any): EventRef; | ||
} | ||
} | ||
|
||
declare global { | ||
interface Window { | ||
DataviewAPI?: DataviewApi; | ||
} | ||
} |