motla / vue-document-editor

:page_facing_up: Paper-sized WYSIWYG document editor for Vue apps
https://motla.github.io/vue-document-editor/
MIT License
313 stars 70 forks source link

Image draggable and resizable #40

Closed Manakelll closed 9 months ago

Manakelll commented 9 months ago

Hi, First of all, vue-document-editor is just great. I'm not a basic JS coder but I'm getting started and looking to integrate into vue-document-editor the possibility of making images draggable and resizable. Have you implemented such a thing? Do you have any ideas, I have already tried vue-draggable-resizable but I must be missing something if you could help me. Thank you and congratulations again for the work accomplished.

motla commented 9 months ago

Hi @Manakelll,

Thanks for your interest on the library and for your kind words! Unfortunately resizable/draggable image is not trivial to implement on top of this library, I think it would take at least a week of development and testing to have a minimal behavior. I don't know if a library could to that out of the box without you having to code it. However it is a great project to learn web programming.

I would say to achieve the resizable part, I would first replace images of the content by <div>s, set with CSS position: relative or absolute depending if you want the image to be over the text or aligned with the text. You can set the image either as a CSS background-image property (also set background-size: 100% 100%), or directly inside of the <div> (in this case width/height set to 100%). The <div> would need width/height to be set to the desired image size, and this size would be changed with JavaScript if the user resizes the image. Inside this div, I would set square <div>s with CSS position: absolute, for example width: 10px; height: 10px and background set with a fixed color, representing the handles (located at CSS left/top 0%, 50% and 100% all around of the image). You can set CSS cursor accordingly. Then for the dynamic part, I would call a JavaScript function on the mousedown event of every square, which does only three things:

Then for the draggable part, if the image is in position: absolute I would do exactly the same process, but acting on top/left properties on mousedown of the image <div>. However I have currently no idea how to implement dragging if the image has to stay aligned with the text. It doesn't seem feasible with the current contenteditable implementation.

Don't hesitate to read MDN website for info if you don't already know the functions or CSS properties I listed (like addEventListener).

Manakelll commented 7 months ago

Hello, Sorry for my late response but my project is taking up all my time. I finally succeeded using draggable and resizable. I used a listener on paste which detects if it is an image and if so converts the image to base64 and creates a canvas with the resizable class. In short, it works. Thanks again I'm discovering JS being a python dev and I'm really having fun