zadam / trilium

Build your personal knowledge base with Trilium Notes
GNU Affero General Public License v3.0
27.2k stars 1.9k forks source link

runOnNoteChange event not firing #340

Closed bojjenclon closed 5 years ago

bojjenclon commented 5 years ago

Unless I'm misunderstanding the intent of the event, the runOnNoteChange relation never seems to fire for my notes when I change text within them. I know the code node attached to it works, as I have the same node bound to runOnNoteView and that works fine.

zadam commented 5 years ago

Hello, this might be confusing, but runOnNoteChange is triggered on backend so it might not do what you expect. Actually most of these events are backend only, runOnNoteView is kind of an exception because it was meant to modify/inject content of the note view.

Can I ask what was your intention with runOnNoteChange on the frontend?

bojjenclon commented 5 years ago

Ah, interesting. I wrote a code node (well technically two) that creates a statusbar, with a word count widget on it. I wanted to detect changes to the note so I could recalculate the current word count, and figured that would be the correct event.

bojjenclon commented 5 years ago

image

Example of what I'm trying to accomplish.

zadam commented 5 years ago

That looks pretty nice! I will think about this, this looks like a valid use case which I hadn't thought of.

bojjenclon commented 5 years ago

Thanks! In the mean time I suppose I'll see if I can hook into some other type of event like DOMSubtreeModified I suppose. Great work on this app by the way!

zadam commented 5 years ago

Hello, I added runOnNoteChange firing on the frontend as well. Just make sure to have your script in "JavaScript frontend" note as opposed to "JavaScript backend" - that's how Trilium knows whether to execute such note for runOnNoteChange event on frontend or backend. I'll probably follow this pattern with other events as well.

This event is fired after the note is auto-saved so when continuously typing it should be triggered every ~3 seconds.

This will be in the soon to be released 0.28.1-beta.

bojjenclon commented 5 years ago

Great work! The timing is slightly slow for a word count feature (my original use case) so I think I'll stick with the MutationObserver approach I came up with in the mean time, but this will certainly be useful for other scripts.

rahatzamancse commented 4 years ago

@bojjenclon . I was trying to do this same thing. Care to share a link of the script note you made?

zadam commented 4 years ago

Word count added as an example of custom widget. However, note that this will work only with not yet released version 0.42.8.