Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accessing DataviewInlineApi (the "dv" that is used in every reference) in custom JS code #2506

Open
dima-stefantsov opened this issue Jan 13, 2025 · 1 comment
Labels
feature-request New feature or request.

Comments

@dima-stefantsov
Copy link

dima-stefantsov commented Jan 13, 2025

I want to have common functions for common tasks. I have found it's possible with customJS.
I want to be able to do $=customJS.my.get_duration(), that would

dv = app.plugins.plugins.dataview.api
get_duration() {
    var duration = get_my_duration()
    return this.dv.el("code", duration)
}

, currently, app.plugins.plugins.dataview.api is an instance of DataviewApi, while .el() method is only available in DataviewInlineApi.
Therefore I found I can achieve dataview-native Duration rendering by calling customJS.my.get_duration(dv), to pass an instance of DataviewInlineApi into my function. Otherwise I'd have to spam $=dv.el("code", customJS.my.get_duration()) which is bad.

upon further lurking, I've found a workaround

dv = app.plugins.plugins.dataview.api
get_duration() {
    var duration = get_my_duration()
    duration = duration.shiftToAll().normalize()
    duration = this.dv.luxon.Duration.fromObject(Object.fromEntries(Object.entries(duration.toObject()).filter(([, quantity]) => quantity != 0)));
    var result = duration.toHuman()
    return `<code>${result}</code>`
}

Every step in developing using dataview is painful, stuff just acts randomly, for no obvious to me architectural reasons.

when there's inline property on the line, inline render doesn't work; inline render can't seem to reference text of current line/paragraph, at least I haven't found it; block data is refreshed, yet inline data does not; DQL is ....; arrays are magical and I always need to be thinking in which state current array is, "easiness" of arr.file.name implicit mapping magic (magic is always bad) does not outweight complications like result.concat(this.dv.array([item])); etc etc

Is this the right place to request help accessing DataviewInlineApi, preferably somewhere like app.plugins.plugins.dataview.api.inline or app.plugins.plugins.dataview.apiinline, or even at app.plugins.plugins.dataview.api because that's the "main" interface all the reference manuals are written about.

@dima-stefantsov dima-stefantsov added the feature-request New feature or request. label Jan 13, 2025
@holroy
Copy link
Contributor

holroy commented Jan 23, 2025

It's the right place I reckon, but there is not a whole lot of action currently in this repository.

It's common to pass that instance around. I know Templater uses the same kind of logic, even though I don't why it has arisen as a practive. But for now, I reckon that's the safer option to use, pass the dv as variable.

With CustomJS you could potentially have a common method to set it once for each time you run a script, but you would need to have some fail safe in place, just in case it hasn't been set when you call another method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request New feature or request.
Projects
None yet
Development

No branches or pull requests

2 participants