This was based on this vue project. And it's not complete with all the v-textarea API options, but you can help to add them :D.
I think I broke the nowrap behavior from the original project (I do not need it, so I did not care much).
For source code, see ONLY src/VLinedTextarea.vue
. The rest are just webpack vue template. Run npm install
and npm run build
to test.
For example usage, checkout src/App.vue
.
There are hard-coded style values and other known issues with it. It's just a 300-lines-less toy. So if there any meaning for this, it would be some thoughts I can share, if you want to add line numbers to textarea.
<div>
<div>
<p>1</p>
<p>2</p>
<p>3</p>
<p>4</p>
</di>
<textarea></textarea>
</div>
User monospace font to have, mono-spaced font.
Problem: When I resize the textarea, I want the left div
to have the same height as the textarea
.
How I did it: css flexbox
Problem: When scrolling happens on textarea
, I want the left div
to be scrolled as well.
How I did it: Callback on scroll event and ajust transform: translateY()
value.
Problem: When lines are added/removed, the width of the left div
should be updated. For example, when you have 9 lines at first, then you add one line, now you have 10 lines. Width should be changes from 1 to 2 (characters long). (p
s are absolute positioned)
How I did it: Adjust padding-left
of the DIV.
Problem: There isn't wrap event I can listen to. So I have to manually calculate that. :(
How I did it:
- Calculate how many characters one line can contain
- Calculate how many lines one lines would spread into
I have functions written for these: calculateCharactersPerLine
and getWrapTimes
. Unfortunately they are not correct 100% of the time. This is the main problem of this component.
Problem: Number of characters per line can be changed.
How I did it:
Events that would require re-calculate:
- Left DIV grows/shrinks
- Scrollbar appears/disappears
- Textarea is resized
Update when these events occur.