gtg922r / obsidian-numerals

An obsidian plugin which turns a math code block into a full featured calculator
Other
391 stars 8 forks source link

[Question/Feature Request] local/global variables inside a note #21

Open VictorJermiin opened 1 year ago

VictorJermiin commented 1 year ago

I don't know if this is the right place to ask but I can't find any information about this.

Is it possible to have variables that span all the code blocks inside the same note? (So I don't have to define the same variable more than once if I chose to make a new code block) Something like this: '''math K = 200 S = 200 ''' here is a new code block '''math K + S => outputs: 400 '''

gtg922r commented 1 year ago

Thanks, I can definitely understand the use-case and want this myself.

I think it might be tricky because all code blocks in a note would have to be re-calculated, but I will definitely look in to it when i get some of the near-term features out the door.

jwhitley commented 1 year ago

I'd thought about this when I was briefly contemplating having a go at a similar plugin. The main thing here is that e.g. numeralsMathBlockHandler should use one calculation context, and "accumulate" all new block content as it's repeatedly called. But on L227 scope is created anew on every handler invocation. I spent a bit of time researching this, but don't have a solid solution yet. I'll writeup something for the forum (a query on Discord didn't produce much) and will report back here if I get anything useful.

My best idea so far would be to attach the state to the Document via an incantation such as this.doc[this.manifest.id]. Obviously, that state would need to be setup or torn down when the plugin was loaded/unloaded. I believe that would correctly scope the mathjs state to an instance of a note in one window. But I'm not finding a lifecycle hook that lets the plugin know when rendering starts, and therefore when to reset that state ala this.doc[this.manifest.id] = {}. (also, any approach should be wrapped in this.{get,set}MathState() or the like)

gtg922r commented 1 year ago

Some other things that come to mind to solve for this: