Skip to content

Conversation

tiensonqin
Copy link
Contributor

@tiensonqin tiensonqin commented Aug 26, 2022

What?

This PR provides logseq.Assets storage related APIs, mainly for plugin developers to be able to store files in a specific subdirectory of ${current_graph_root}/assets/.

How?

const s = logseq.Assets.makeSandboxStorage()

// file stored in `${current_graph_root}/assets/storages/${Plugin_ID}/data.json`
await s.setItem('data.json', '{}')

// remove item
await s.removeItem('data.json')

// get item raw data
await s.getItem('data.json')

// list all items
await s.allKeys()
  • Write an image item with arraybuffer data type.
const s = logseq.Assets.makeSandboxStorage()

// return an image as an ArrayBuffer.
const xhr = new XMLHttpRequest()

xhr.open('GET', 'https://images.pexels.com/photos/3361739/pexels-photo-3361739.jpeg?auto=compress&cs=tinysrgb&dpr=2&w=500', true)
xhr.responseType = 'arraybuffer'
xhr.onload = function (e) {
  // Obtain a blob: URL for the image data.
  s.setItem(
    `${Date.now()}.jpg`,
    xhr.response
  ).then((one) => {
    logseq.UI.showMsg(`Write DONE 🎉 - ${one}`, 'success')
  })
}
xhr.send()

Showcase ⬇️

CleanShot.2022-09-06.at.16.12.49.mp4

@github-actions github-actions bot added the :type/enhancement Enhancement to product. Does not affect the overall basic use. label Aug 26, 2022
@tiensonqin
Copy link
Contributor Author

@xyhp915 Is this ready now?

@xyhp915
Copy link
Collaborator

xyhp915 commented Sep 6, 2022

@xyhp915 Is this ready now?

Still need to test on Windows. But soon for that.

@xyhp915 xyhp915 requested review from andelf and cnrpman September 6, 2022 08:43
@xyhp915
Copy link
Collaborator

xyhp915 commented Sep 6, 2022

@xyhp915 Is this ready now?

Still need to test on Windows. But soon for that.

@tiensonqin Tested on Windows. Ready to review. And this PR should be merged into the nightly version for plugin developers experiencing.

@xyhp915 xyhp915 self-requested a review October 1, 2022 01:56
@tiensonqin tiensonqin marked this pull request as ready for review October 4, 2022 04:20
@tiensonqin tiensonqin merged commit f4262cf into master Oct 4, 2022
@tiensonqin tiensonqin deleted the enhance/api-storages-for-graph-assets branch October 4, 2022 04:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
:type/enhancement Enhancement to product. Does not affect the overall basic use.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants