Trying to implement a textarea
component with emoticons support while writing.
I want to be able to backup the original text (ascii chars only) while presenting the filtered/generated html outcome (with an angular emoticons filter) on a div
.
My initial solution is to
<textarea ng-model="text" ng-change="..." ng-focus="..."></textarea>
<div ng-bind-html="text | myEmoticonsFilter"></div>
but I'm having trouble getting to the part of using a hidden textarea. Also, with this I wouldn't be able to mouse-select text and delete or copy/paste safely.
I also thought of using a <div contenteditable="true">
but ng-focus
and ng-change
wouldn't be handled.
Does anyone have any sugestion on how to continue this?
Edit 1: here is a jsfiddle with an attempt on what I'm doing. Up until now, able to replace the first occurrence, but the behavior remains erratic since that. I'm using a contenteditable
directive for 2-way data binding and to filter the emoticon pattern.
Edit 2: regarding my statement saying that ng-focus
and ng-change
wouldn't be handled, that is not true - ng-focus
works natively on <div contenteditable="true">
and ng-change
will work as long as a directive is declared using the ngModel
and setting the appropriate $modelValue
and $viewValue
(an example is provided in the jsfiddle in Edit 1).