Making a text adapt to draggable image in html document

I'm working on a rich text editor that includes text and images. I trying to figure out a way to make it so the text wraps around the images as they move to different areas of the document. I'm not entirely sure where to start. My first instinct structure the document so that it is a collection of divs (representing paragraphs) and then add/remove images from a specified div. But the thing is, I'm not entirely sure how to do this with a gui that triggers a javascript function. My initial thought was the calculate the height, width, and positioning for all the divs within the document and as the position of the image changes, call appendChild()/removeChild() methods for the relevant divs (I'd probably have to generate element ids dynamically). What I want to know is am I on the right track? Or should I approve this problem another way?

FYI, I'm working on a mobile device, the solution has to be for touch screens.

javascripthtml

Answers

answered 4 months ago nixkuroi #1

You can accomplish this by making the container object a contenteditable div, and making the image float to the left (or right).

If you want to know when the location of the image changes, you can just set a timer to check the innerHTML of the div against the last version you checked, or, if you only have one image, you can check for the location of "

Check out this snippet below:

var lastText = document.querySelector(".container").innerText;
setInterval(function(){  
   var currText = document.querySelector(".container").innerText;
   if (lastText != currText) { alert("dom changed");}
    lastText = currText;
   }, 1000);
.img {
   float:left;
   height:200px;
   width:200px;
}
<div style="height:500px;width:500px;display:block;" contenteditable="true" class="container">

What is Lorem Ipsum?
<p>
<img src="https://static.igrice123.rs/slike/12378-3195/kuca-ze-ove-peki-:d.jpg" class="img" style="position:relative">
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
<br><br>
Why do we use it?
<br><br>
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
<br><br>

Where does it come from?
<br><br>
Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.
<br><br>
The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.
</p>
</div>

comments powered by Disqus