i hope you are well. I am working on a medium like text editor and i am using tiptap for that and im implementing it with react. One thing that im stuck in that is uploading images. I want to implement upload image using Image extension or anything else like this:
- When a user inserts an image, i want to insert it with a base64 src.
- Then, it starts uploading that image and user can do anything while that image is being uploaded.
- After that the image was uploaded, it changes the base64 image to the uploaded image url.
I want to achive something like this system but i couldn't get close to the solution. I searched about it and i found this gist on github but it didnt help me to create such an extension with upload feature. THE LINK OF THE GITHUB GIST
So i need your help to achive this. thanks for helping.
I finaly found the solution for this. As i am using
tiptapwithreact, and i wanted to let users write caption for images, i decided to usenode viewfeature oftiptapextensions. I addedaddNodeViewto tiptapImage extensionto render my component(you need to extend theImageextension. you can read about extending a tiptap extension here):You can read about creating and rendering node views in tiptap by using vue, react, vanilla js or etc in tiptap docs here
In the next step, i added
uploadImageHandlerto existing attributes of Image extension. Like this:So when you want to add image with
editor.commands.setImageadd your upload image handler function to it. And make sure to not add uploadImageHandler to the HTML attributes of the rendering element. So to do this, you can do something like this(the code might be a bit different for you because i render HTML related to caption):There will be a
nodeprop in the props of the rendering component and you can access to the attributes like this:Then i added a
useEffectto start uploading after the component is rendered:My
uploadImageHandlerlooks like this:I pass this function to
editor.commands.setImagelike this:An other solution for this is to execute your
uploadImageHandlerin theonloadevent ofimgelement. Base on what you want, you can choose between these two solutions. Theonloadsolution is not very different with the solution i explained. I couldnt share all parts of my codes but i think you can implement the upload image feature with parts of codes that i shared and things that i explained. I hope it is useful for you.