Add parentId field to Button Context #630
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Pull Request Description
This PR adds a
parentId
field to the ButtonContext object, which is passed in the context for JS functions when a button is clicked.The field is a string containing the ID of the parent element in the DOM.
This is so you can write JS that the button executes, and it can know where in the DOM it is to be able to perform position-dependant logic.
Potential Issues
Test results
All tests pass:
Motivation
The obvious question here is "what is this even for?"
My use case is that I have an ever-growing to-do list that I add to and update daily.
Upon opening my to-do list, a dataview script reads through all my daily notes and compiles the to-do list items from each one into a table. It then adds meta-bind buttons (and a date spinner) to each row to allow me to edit data for each row which gets inserted as frontmatter.
The issue is that using dataviewjs to create buttons in this way is a bit hard. As there's custom logic for each different button (to point to different frontmatter keys), I ended up instantiating a new button with a unique ID for every row with data prefilled in. This has led to a ballooning in processing time.
To combat this, I thought it would be better if it was just one set of buttons duplicated per row, but it knew what row it was on to not need the instantiation. Hence this PR. (I had tried passing in the DOM element directly into the context instead, but JS wasn't having that due to
structuredClone
shenanigans. Thus, the parent's ID was the best I could come up with.)I currently have a little over 700 entries in my list, and this change cuts down the loading time from 40 seconds to 10 (It also automatically reloads every time I make a change so it really adds up). It's still a bit on the high side. I realize I should probably be using a different (or custom) plugin for this at this point.