Skip to content
This repository has been archived by the owner on Sep 8, 2020. It is now read-only.

Commit

Permalink
Fix some buggy behavior, update demo
Browse files Browse the repository at this point in the history
  • Loading branch information
wesleycho committed Jun 8, 2015
1 parent 74fc383 commit 23692d5
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 21 deletions.
9 changes: 6 additions & 3 deletions demo/demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@
<script type="text/javascript" src="../bower_components/tinymce-dist/tinymce.min.js"></script>
<script type="text/javascript" src="../bower_components/angular/angular.js"></script>
<script type="text/javascript" src="../src/tinymce.js"></script>
<script type="text/javascript" src="demo.js"></script>
</head>
<body ng-app="ui.tinymce">
<body ng-app="ui.tinymce" ng-controller="DemoCtrl as demo">
<form method="post">
<textarea id="tinymce" ui-tinymce ng-model="tinymce"></textarea>
<textarea ui-tinymce="{trusted: true}"
ng-model="demo.tinymce"
ng-change="demo.updateHtml()"></textarea>
</form>
<div ng-bind-html="tinymce"></div>
<div ng-bind-html="demo.tinymceHtml"></div>
</body>
</html>
8 changes: 8 additions & 0 deletions demo/demo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
angular.module('ui.tinymce')
.controller('DemoCtrl', function($sce) {
var ctrl = this;

this.updateHtml = function() {
ctrl.tinymceHtml = $sce.trustAsHtml(ctrl.tinymce);
};
});
38 changes: 20 additions & 18 deletions src/tinymce.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ angular.module('ui.tinymce', [])
var expression, options, tinyInstance,
updateView = function(editor) {
var content = editor.getContent({format: options.format}).trim();
if (options.trusted) {
content = $sce.trustAsHtml(content);
}
content = $sce.trustAsHtml(content);

ngModel.$setViewValue(content);
if (!$rootScope.$$phase) {
Expand All @@ -42,7 +40,8 @@ angular.module('ui.tinymce', [])
angular.extend(expression, scope.$eval(attrs.uiTinymce));

options = {
// Update model when calling setContent (such as from the source editor popup)
// Update model when calling setContent
// (such as from the source editor popup)
setup: function(ed) {
ed.on('init', function() {
ngModel.$render();
Expand All @@ -60,10 +59,8 @@ angular.module('ui.tinymce', [])

// Update model on change
ed.on('change', function(e) {
if (!e.originalEvent) {
ed.save();
updateView(ed);
}
ed.save();
updateView(ed);
});

ed.on('blur', function() {
Expand All @@ -89,7 +86,8 @@ angular.module('ui.tinymce', [])
format: 'raw',
selector: '#' + attrs.id
};
// extend options with initial uiTinymceConfig and options from directive attribute value
// extend options with initial uiTinymceConfig and
// options from directive attribute value
angular.extend(options, uiTinymceConfig, expression);
// Wrapped in $timeout due to $tinymce:refresh implementation, requires
// element to be present in DOM before instantiating editor when
Expand All @@ -99,22 +97,26 @@ angular.module('ui.tinymce', [])
});

ngModel.$formatters.unshift(function(modelValue) {
return modelValue || '';
return modelValue ? $sce.trustAsHtml(modelValue) : '';
});

ngModel.$parsers.unshift(function(viewValue) {
return viewValue ? $sce.getTrustedHtml(viewValue) : '';
});

ngModel.$render = function() {
ensureInstance();

// tinymce replaces '\r\n' to '\n', so we have to do the same on model value
// instance.getDoc() check is a guard against null value when destruction &
// recreation of instances happen
var viewValue = ngModel.$viewValue ?
$sce.getTrustedHtml(ngModel.$viewValue) : '';

// instance.getDoc() check is a guard against null value
// when destruction & recreation of instances happen
if (tinyInstance &&
tinyInstance.getDoc() &&
(tinyInstance.getContent({format: options.format}).trim() !== ngModel.$viewValue.replace(/\r\n/g, '\n') ||
!ngModel.$viewValue ||
!ngModel.$viewValue.replace(/\r\n/g, '\n'))
tinyInstance.getDoc()
) {
tinyInstance.setContent(ngModel.$viewValue);
tinyInstance.setContent(viewValue);
tinyInstance.fire('change');
}
};

Expand Down

0 comments on commit 23692d5

Please sign in to comment.