I'm allowing a user to highlight text in a document and when it is highlighted, it will be denoted by showing a vertical bar in the left gutter. I need help drawing the vertical bar highlights on the left side of the below screenshot.
Mockup screenshot where black bars would be text
CodePen with current attempts I Will be referencing
I am already able to capture the selection using getSelection()
. I am adding an inline element around the selection. I need help with the vertical bars that designate which part of the document was selected.
I've tried a couple things that get partially there and I'm a little lost.
This text is displayed in a <pre>
. The desire is to keep the <pre>
as prestine as possible. I'm trying to find a way to get this gutter through plain old HTML/CSS/JS without something complex like an angular GutterComponent that would have to track a ton of offsets, heights, and positions.
1) Append a relative-absolute <div>
pair
I thought I could get away with appending a relatively positioned div
that had the gutter position I needed with an absolute div
for height and top of inline element through a call to getBoundingClientRect()
.
This sucks because the call to getBoundingClientRect()
doesn't work well with inline elements, as illustrated in the CodePen. Also, the divs
create a new line which potentially breaks up blocks of text.
2) Make each highlighted section a block element and use border
This sucks because I can't position the border in the gutter properly without messing up the spacing with the rest of the document. Also it has the same issue as above with breaking up the text into block elements.
I'm mainly looking for guidance on how to create those highlights on the left so they span the entire height of the inline element.
Also I have read this question but I wasn't able to parse out how to actually apply these concepts here and I'd need some guidance Inline elements and line-height
Thank you