I try to integrate The beautifull WYSIWYG Redactor (http://imperavi.com/redactor/) in an custom AngularJS directive.
Visualy it works, but my custom directive is not compatible with ng-model (and I don't understand why)
This is how you can use my directive :
<wysiwyg ng-model="edited.comment" id="contactEditCom" content="{{content}}" required></wysiwyg>
And this is the directive code :
var myApp = angular.module('myApp', []);
myApp.directive("wysiwyg", function(){
var linkFn = function(scope, el, attr, ngModel) {
scope.redactor = null;
scope.$watch('content', function(val) {
if (val !== "")
{
scope.redactor = $("#" + attr.id).redactor({
focus : false,
callback: function(o) {
o.setCode(val);
$("#" + attr.id).keydown(function(){
scope.$apply(read);
});
}
});
}
});
function read() {
var content = scope.redactor.getCode();
console.log(content);
if (ngModel.viewValue != content)
{
ngModel.$setViewValue(content);
console.log(ngModel);
}
}
};
return {
require: 'ngModel',
link: linkFn,
restrict: 'E',
scope: {
content: '@'
},
transclude: true
};
});
And finally this is the fiddle -> http://fiddle.jshell.net/MyBoon/STLW5/
I made one based on Angular-UI's TinyMCE directive. This one also listen to format button clicks. It also handles the case when the model is changed outside the directive.
directive.coffee (sorry for the coffeescript)
angular.module("ui.directives").directive "uiRedactor", ["ui.config", (uiConfig) ->
require: "ngModel"
link: (scope, elm, attrs, ngModelCtrl) ->
redactor = null
getVal = -> redactor?.getCode()
apply = ->
ngModelCtrl.$pristine = false
scope.$apply()
options =
execCommandCallback: apply
keydownCallback: apply
keyupCallback: apply
scope.$watch getVal, (newVal) ->
ngModelCtrl.$setViewValue newVal unless ngModelCtrl.$pristine
#watch external model change
ngModelCtrl.$render = ->
redactor?.setCode(ngModelCtrl.$viewValue or '')
expression = if attrs.uiRedactor then scope.$eval(attrs.uiRedactor) else {}
angular.extend options, expression
setTimeout ->
redactor = elm.redactor options
]
html
<textarea ui-redactor='{minHeight: 500}' ng-model='content'></textarea>
UPDATED --- Rails 4.2 --- Angular-Rails 1.3.14
Alright guys, after lots of research and some help from other members on stack overflow here is a solution that feeds straight into the controller $scope and into the ng-model you apply to the textarea:
** Render Raw HTML**
# Filter for raw HTML
app.filter "unsafe", ['$sce', ($sce) ->
(htmlCode) ->
$sce.trustAsHtml htmlCode
]
Credit for Filter
Directive:
# For Redactor WYSIWYG
app.directive "redactor", ->
require: "?ngModel"
link: ($scope, elem, attrs, controller) ->
controller.$render = ->
elem.redactor
changeCallback: (value) ->
$scope.$apply controller.$setViewValue value
buttons: ['html', '|', 'formatting', '|',
'fontcolor', 'backcolor', '|', 'image', 'video', '|',
'alignleft', 'aligncenter', 'alignright', 'justify', '|',
'bold', 'italic', 'deleted', 'underline', '|',
'unorderedlist', 'orderedlist', 'outdent', 'indent', '|',
'table', 'link', 'horizontalrule', '|']
imageUpload: '/modules/imageUpload'
elem.redactor 'insert.set', controller.$viewValue
Last line update reason
in the HTML View:
<div ng-controller="PostCtrl">
<form ng-submit="addPost()">
<textarea ng-model="newPost.content" redactor required></textarea>
<br />
<input type="submit" value="add post">
</form>
{{newPost.content}} <!-- This outputs the raw html with tags -->
<br />
<div ng-bind-html="newPost.content | unsafe"></div> <!-- This outputs the html -->
</div>
And the Controller:
$scope.addPost = ->
post = Post.save($scope.newPost)
console.log post
$scope.posts.unshift post
$scope.newPost.content = "<p>Add a new post...</p>"
To PREVENT a TypeError with redactor populate a value into the textarea before the action is called, this worked best for me to preserve formatting:
# Set the values of Reactor to prevent error
$scope.newPost = {content: '<p>Add a new post...</p>'}
If you are on running into CSRF errors this will solve that problem:
# Fixes CSRF Error OR: https://github.com/xrd/ng-rails-csrf
app.config ["$httpProvider", (provider) ->
provider.defaults.headers.common['X-CSRF-Token'] = angular.element('meta[name=csrf-token]').attr('content')
]
Much Thanks to: AngularJS & Redactor Plugin
Lastly....
If you are using an ng-repeat to create these redactor text areas and are having trouble accessing the scope, check out this answer: Accessing the model inside a ng-repeat
See if this fiddle is what you want.
ng-model can be set to content:
<wysiwyg ng-model="content" required></wysiwyg>
Inside the linking function, el is already set to the element where the directive is defined, so an id
is not needed. And el is already a wrapped jQuery element.
Link function:
var linkFn = function (scope, el, attr, ngModel) {
scope.redactor = el.redactor({
focus: false,
callback: function (o) {
o.setCode(scope.content);
el.keydown(function () {
console.log(o.getCode());
scope.$apply(ngModel.$setViewValue(o.getCode()));
My solution is
1) clone https://github.com/dybskiy/redactor-js.git
2) include jquery, redactor.js, redactor.css
3) add tag: <textarea wysiwyg ng-model="post.content" cols="18" required></textarea>
to your html body
4) add directive:
yourapp.directive('wysiwyg', function () {
return {
require: 'ngModel',
link: function (scope, el, attrs, ngModel) {
el.redactor({
keyupCallback: function(obj, e) {
scope.$apply(ngModel.$setViewValue(obj.getCode()));
}
});
el.setCode(scope.content);
}
};
});
Best regards, Jeliuc Alexandr
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With