Я пытаюсь создать редактор, который делает «подсветку синтаксиса», это довольно просто:angularjs с цветами текстового поля (с html5 contenteditable)
yellow -> <span style="color:yellow">yellow</span>
Я также использую <code contenteditable>
html5 тег для замены <textarea>
, и имеют цветовую мощность.
Я начал с документации angularjs и создал следующую простую директиву. Он работает, за исключением того, что он не обновляет область contenteditable
сгенерированным html. Если я использую element.html(htmlTrusted)
вместо ngModel.$setViewValue(htmlTrusted)
, все работает, за исключением того, что курсор переходит к началу при каждом нажатии клавиши.
директива:
app.directive("contenteditable", function($sce) {
return {
restrict: "A", // only activate on element attribute
require: "?ngModel", // get ng-model, if not provided in html, then null
link: function(scope, element, attrs, ngModel) {
if (!ngModel) {return;} // do nothing if no ng-model
element.on('blur keyup change', function() {
console.log('app.directive->contenteditable->link->element.on()');
//runs at each event inside <div contenteditable>
scope.$evalAsync(read);
});
function read() {
console.log('app.directive->contenteditable->link->read()');
var html = element.html();
// When we clear the content editable the browser leaves a <br> behind
// If strip-br attribute is provided then we strip this out
if (attrs.stripBr && html == '<br>') {
html = '';
}
html = html.replace(/</, '<');
html = html.replace(/>/, '>');
html = html.replace(/<span\ style=\"color:\w+\">(.*?)<\/span>/g, "$1");
html = html.replace('yellow', '<span style="color:yellow">yellow</span>');
html = html.replace('green', '<span style="color:green">green</span>');
html = html.replace('purple', '<span style="color:purple">purple</span>');
html = html.replace('blue', '<span style="color:yellow">blue</span>');
console.log('read()-> html:', html);
var htmlTrusted = $sce.trustAsHtml(html);
ngModel.$setViewValue(htmlTrusted);
}
read(); // INITIALIZATION, run read() when initializing
}
};
});
HTML:
<body ng-app="MyApp">
<code contenteditable
name="myWidget" ng-model="userContent"
strip-br="true"
required>This <span style="color:purple">text is purple.</span> Change me!</code>
<hr>
<pre>{{userContent}}</pre>
</body>
plunkr: demo (тип yellow
, green
или blue
в изменении меня область ввода)
Я попытался scope.$apply()
, ngModel.$render()
но имеет нет эффекта. Я должен пропустить что-то действительно очевидное ...
Ссылки я уже прочитал:
- others' plunker demo 1
- others' plunker demo 2
- angularjs documentation's example
- $sce.trustAsHtml stackoverflow question
- setViewValue stackoverflow question
- setViewValue not updating stackoverflow question
Любая помощь очень ценится. См. Демонстрацию plunker выше.