ozntel / oz-image-in-editor-obsidian

This Obsidian plugin to view Images, Transclusions, iFrames and PDF Files within the Editor without a necessity to switch to Preview.
339 stars 13 forks source link

Block Ref embed transclusion rendering error when block ref ^block-id was created recently/quickly #54

Closed GitMurf closed 3 years ago

GitMurf commented 3 years ago

I initially posted / linked from this issue: https://github.com/ozntel/oz-image-in-editor-obsidian/issues/45#issuecomment-907889135

I am having a "small" problem which isn't that big of a deal but I thought I would let you know. It seems when I create a block ref "too quickly" it is not finding it when it tries to render and it gives an error like screenshot below. Primarily this is happening when I am using my new block ref drag and drop plugin which creates the block ref when dragging it from the source file/line so then it seems your plugin tries to load/render it right away but the block ref hasn't likely been "cached" yet in the metadatacache (assuming without looking at your code).

Pasted image 20210829153552

Proposed solution / idea:

1) it throws up an error in the console so you should add some sort of NULL check so this error doesn't happen.

2) an idea/solution to help with this problem is maybe initially try to render as usual but if you get a NULL like the error above it likely is because block ref was created very recently and hasn't had time to "propagate" / get cached so then set a "timeout" or whatever you want to call it to try again in 3 seconds or something to see if can load it then. Just an idea.... maybe that won't work with your solution but it does make the block ref rendering a bit inconsistent.

See this video for example of what is happening:

https://user-images.githubusercontent.com/64155612/131267886-21083449-0533-4ac2-a743-114b4010b5b5.mp4

ozntel commented 3 years ago

I just released a new version, which should solve the error.

The plugin actually waits for the new block id in cache with the help of pollUntil. Once it finds the id, it starts to render again. At least, it works in my vault.

Let me know if the issue still persists.

GitMurf commented 3 years ago

@ozntel this fixes the dev console error but it still isn't rendering the block ref at initial creation with my drag n drop plugin. Although this may be something that is more of a limitation using our plugins together? If I go back to the end of the block ref line and hit enter key to create a line break underneath it, it will then render the block ref.

If you want to do some testing here is my early release of my plugin. Alt + Drag will create a block ref: https://github.com/GitMurf/obsidian-drag-and-drop-blocks/releases/tag/v0.3

One thing to note is that technically the block ref is created AFTER the link is dragged into the new location because I am waiting for the drag n drop onDrop event to fire to confirm the drag and drop was successful/completed before I then turn the original source block into a block ref by adding the ^block-id on the end of it. So that is probably part of the problem. Your plugin would need to have some sort of "wait" in it to give it a second or two for the block ref to be completed and then "propogated" to the metadatacache and then have your plugin check for rendering. Maybe if it doesn't find the block ref at first when creating a link you have it give a couple second wait and then try again? If it still doesn't find it then it can just ignore/stop. Not sure how realistic that is but that way it doesn't effect current state usage but gives an ability to handle the quick block ref creations with plugins like mine using drag and drop creation "on the fly".

ozntel commented 3 years ago

It should work now. Plugin checks 5 times with 1 second interval if the cache is created and inserts if exists.