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

Commit 23692d5

Browse files
committed
Fix some buggy behavior, update demo
1 parent 74fc383 commit 23692d5

File tree

3 files changed

+34
-21
lines changed

3 files changed

+34
-21
lines changed

demo/demo.html

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,14 @@
1010
<script type="text/javascript" src="../bower_components/tinymce-dist/tinymce.min.js"></script>
1111
<script type="text/javascript" src="../bower_components/angular/angular.js"></script>
1212
<script type="text/javascript" src="../src/tinymce.js"></script>
13+
<script type="text/javascript" src="demo.js"></script>
1314
</head>
14-
<body ng-app="ui.tinymce">
15+
<body ng-app="ui.tinymce" ng-controller="DemoCtrl as demo">
1516
<form method="post">
16-
<textarea id="tinymce" ui-tinymce ng-model="tinymce"></textarea>
17+
<textarea ui-tinymce="{trusted: true}"
18+
ng-model="demo.tinymce"
19+
ng-change="demo.updateHtml()"></textarea>
1720
</form>
18-
<div ng-bind-html="tinymce"></div>
21+
<div ng-bind-html="demo.tinymceHtml"></div>
1922
</body>
2023
</html>

demo/demo.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
angular.module('ui.tinymce')
2+
.controller('DemoCtrl', function($sce) {
3+
var ctrl = this;
4+
5+
this.updateHtml = function() {
6+
ctrl.tinymceHtml = $sce.trustAsHtml(ctrl.tinymce);
7+
};
8+
});

src/tinymce.js

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@ angular.module('ui.tinymce', [])
2424
var expression, options, tinyInstance,
2525
updateView = function(editor) {
2626
var content = editor.getContent({format: options.format}).trim();
27-
if (options.trusted) {
28-
content = $sce.trustAsHtml(content);
29-
}
27+
content = $sce.trustAsHtml(content);
3028

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

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

6160
// Update model on change
6261
ed.on('change', function(e) {
63-
if (!e.originalEvent) {
64-
ed.save();
65-
updateView(ed);
66-
}
62+
ed.save();
63+
updateView(ed);
6764
});
6865

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

10199
ngModel.$formatters.unshift(function(modelValue) {
102-
return modelValue || '';
100+
return modelValue ? $sce.trustAsHtml(modelValue) : '';
101+
});
102+
103+
ngModel.$parsers.unshift(function(viewValue) {
104+
return viewValue ? $sce.getTrustedHtml(viewValue) : '';
103105
});
104106

105107
ngModel.$render = function() {
106108
ensureInstance();
107109

108-
// tinymce replaces '\r\n' to '\n', so we have to do the same on model value
109-
// instance.getDoc() check is a guard against null value when destruction &
110-
// recreation of instances happen
110+
var viewValue = ngModel.$viewValue ?
111+
$sce.getTrustedHtml(ngModel.$viewValue) : '';
112+
113+
// instance.getDoc() check is a guard against null value
114+
// when destruction & recreation of instances happen
111115
if (tinyInstance &&
112-
tinyInstance.getDoc() &&
113-
(tinyInstance.getContent({format: options.format}).trim() !== ngModel.$viewValue.replace(/\r\n/g, '\n') ||
114-
!ngModel.$viewValue ||
115-
!ngModel.$viewValue.replace(/\r\n/g, '\n'))
116+
tinyInstance.getDoc()
116117
) {
117-
tinyInstance.setContent(ngModel.$viewValue);
118+
tinyInstance.setContent(viewValue);
119+
tinyInstance.fire('change');
118120
}
119121
};
120122

0 commit comments

Comments
 (0)