mdx-editor / editor

A rich text editor React component for markdown
https://mdxeditor.dev
MIT License
1.63k stars 131 forks source link

[BUG] Saving/inserting images from custom ImageDialog #437

Closed DaddyWarbucks closed 2 months ago

DaddyWarbucks commented 2 months ago

If you want to ask for support or request features, sponsor the project and contact me over email.

Describe the bug I have implemented a custom ImageDialog component that uses a Dropbox Chooser. The chooser returns some JSON with url, name, etc. When using saveImage$, insertImage$, etc nothing happens.

Reproduction Unfortunately, I can't really make a reproducible example because of Dropbox keys, origins, etc. And I don't think that screenshots will be particularly useful. I can add a video and some code that outlines what is happening. Please see code snippet at bottom.

Video: https://www.loom.com/share/1a92dc6b547546668fa188c373459c28

Expected behavior I expect the image to be loaded into the editor.

Desktop (please complete the following information):

const urlToObject = async (url, name) => { const response = await fetch(url); const blob = await response.blob(); const file = new File([blob], name, { type: blob.type }); return file; };

let getDataTransfer = () => new DataTransfer(); const { concat } = Array.prototype;

try { getDataTransfer(); } catch { getDataTransfer = () => new ClipboardEvent('').clipboardData; }

function createFileList() { // eslint-disable-next-line prefer-rest-params const files = concat.apply([], arguments); let index = 0; const { length } = files;

const dataTransfer = getDataTransfer();

for (; index < length; index++) { dataTransfer.items.add(files[index]); }

return dataTransfer.files; }

export default createFileList;

petyosi commented 2 months ago

Confirming that this is actually a bug of insertImage$. Your code seems correct as far as I can tell, will fix this.

DaddyWarbucks commented 2 months ago

It would also be helpful if the functions took a File OR FileList for the file property. You cannot construct a file list like new FileList(). My code is using: https://www.npmjs.com/package/create-file-list

Thanks!

petyosi commented 2 months ago

A new version should be built and published soon. Check the examples I did in the commit above.

DaddyWarbucks commented 2 months ago

There is still an error on this line: https://github.com/mdx-editor/editor/blob/75a501fd1f77c29f1e67222c1f7cc798918f660b/src/plugins/image/index.ts#L186 I believe that should be updated to use if 'file' in values, similar to how insertImage was updated.

petyosi commented 2 months ago

How can I reproduce the error?

DaddyWarbucks commented 2 months ago

Call saveImage({ src: '...' }) without a file property.

petyosi commented 2 months ago

Given that you're using a custom dialog component, you don't have to use the saveImage signal. It's bound to the data shape of the default dialog form. Instead, use the insertImage, it should work as expected.

DaddyWarbucks commented 2 months ago

OK. I was thinking saveImage might "update" the current image data. How would I go about editing the image data? For example, my custom ImageDialog now has inputs for file name and alt text. When I click the gear icon on an image, it opens the modal with the field values. How do I save those if the user edits them?