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.
feat(inputs): dataview can be used as source for multi-select
fixes #50
- Loading branch information
1 parent
264df13
commit cd39785
Showing
7 changed files
with
90 additions
and
25 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,9 @@ | ||
{ | ||
"plugins": [ | ||
"prettier-plugin-svelte" | ||
] | ||
], | ||
"arrowParens": "always", | ||
"editorconfig": true, | ||
"svelteAllowShorthand": true, | ||
"trailingComma": "all" | ||
} |
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,56 @@ | ||
import { E, Either, flow } from "@std"; | ||
import { pipe } from "fp-ts/lib/function"; | ||
import { App } from "obsidian"; | ||
import { ModalFormError } from "src/utils/Error"; | ||
import { log_error } from "src/utils/Log"; | ||
|
||
type DataviewQuery = (dv: unknown, pages: unknown) => unknown; | ||
export type SafeDataviewQuery = (dv: unknown, pages: unknown) => Either<ModalFormError, string[]>; | ||
/** | ||
* From a string representing a dataview query, it returns the safest possible | ||
* function that can be used to evaluate the query. | ||
* The function is sandboxed and it will return an Either<ModalFormError, string[]>. | ||
* If you want a convenient way to execute the query, use executeSandboxedDvQuery. | ||
* @param query string representing a dataview query that will be evaluated in a sandboxed environment | ||
* @returns SafeDataviewQuery | ||
*/ | ||
export function sandboxedDvQuery(query: string): SafeDataviewQuery { | ||
if (!query.startsWith('return')) { | ||
query = 'return ' + query; | ||
} | ||
const run = new Function('dv', 'pages', query) as DataviewQuery; | ||
return flow( | ||
E.tryCatchK(run, () => new ModalFormError('Error evaluating the dataview query')), | ||
E.flatMap((result) => { | ||
if (!Array.isArray(result)) { | ||
return E.left(new ModalFormError('The dataview query did not return an array')); | ||
} | ||
return E.right(result); | ||
}) | ||
); | ||
} | ||
|
||
/** | ||
* Executes and unwraps the result of a SafeDataviewQuery. | ||
* Use this function if you want a convenient way to execute the query. | ||
* It will log the errors to the UI and return an empty array if the query fails. | ||
* @param query SafeDataviewQuery to execute | ||
* @param app the global obsidian app | ||
* @returns string[] if the query was executed successfully, otherwise an empty array | ||
*/ | ||
export function executeSandboxedDvQuery(query: SafeDataviewQuery, app: App): string[] { | ||
const dv = app.plugins.plugins.dataview?.api; | ||
|
||
if (!dv) { | ||
log_error(new ModalFormError("Dataview plugin is not enabled")) | ||
return [] as string[]; | ||
} | ||
const pages = dv.pages; | ||
return pipe( | ||
query(dv, pages), | ||
E.getOrElse((e) => { | ||
log_error(e); | ||
return [] as string[]; | ||
}) | ||
) | ||
} |
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