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

Local file upload from local link messes up current note in case of multiple local references to the same file in original note #94

Open
gavvvr opened this issue Nov 8, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@gavvvr
Copy link
Owner

gavvvr commented Nov 8, 2024

This bug is related to feature allowing to convert local image to remote one.

Create a note with the following content:

![[example-local-image.png]]
some plain text
![[example-local-image.png]]

example-local-image.png should exist in vault

Put a cursor on first embedded local image and invoke an action to upload it to Imgur. When finished, the plugin will ask if user would like to replace all local references with remote one. If user agrees, the content of current note will be messed up.

@gavvvr gavvvr added the bug Something isn't working label Nov 8, 2024
@gavvvr
Copy link
Owner Author

gavvvr commented Nov 8, 2024

This issue is related to the fact that metadataCache doesn't instantly get updated after image upload. The mechanism proposing to perform references updates uses outdated metadataCache and messes up the note on attempt to replace 2nd local reference to image, because offsets from stale cache are no longer valid for just edited note:

const refsStartOffsetsSortedDescending = refs
.map((ref) => ({
start: ref.position.start.offset,
end: ref.position.end.offset,
}))

The potential solution is to replace the following immediate invocation of proposeToReplaceOtherLocalLinksIfAny:

const imageUrl = await this.uploadLocalImageFromEditor(editor, imageFile, start, end)
this.proposeToReplaceOtherLocalLinksIfAny(imageFile, imageUrl, {
path: noteFile.path,
startPosition: start,
})

with the event listener that will trigger proposeToReplaceOtherLocalLinksIfAny only when updated metadata cache is ready:

const ref = this.app.metadataCache.on('changed', (file: TFile) => {
  if (file.path !== noteFile.path) return

  this.proposeToReplaceOtherLocalLinksIfAny(imageFile, imageUrl, {
    path: noteFile.path,
    startPosition: start,
  })
  this.app.metadataCache.offref(ref)
})

Unfortunately, in practices it takes more than a second, based on my perception. I'd be happy to apply this solution if there was a way to trigger cache metadata update for given file faster.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant