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

Commit 1999e41

Browse files
committed
Add support for refreshing instance
- Adds support for `$tinymce:refresh` $scope event to programmatically trigger re-render of UI. This allows the user to get around buggy behavior with TinyMCE editor iframe becoming blank. This fix can be viewed in action [here](http://plnkr.co/edit/btQmkhK3Db9mzdtQoe8E?p=preview). Fixes #12 Fixes #38
1 parent 5320bb7 commit 1999e41

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

src/tinymce.js

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
*/
44
angular.module('ui.tinymce', [])
55
.value('uiTinymceConfig', {})
6-
.directive('uiTinymce', ['$rootScope', '$window', 'uiTinymceConfig', function($rootScope, $window, uiTinymceConfig) {
6+
.directive('uiTinymce', ['$rootScope', '$compile', '$timeout', '$window', 'uiTinymceConfig', function($rootScope, $compile, $timeout, $window, uiTinymceConfig) {
77
uiTinymceConfig = uiTinymceConfig || {};
88
var generatedIds = 0;
9+
var ID_ATTR = 'ui-tinymce';
910
return {
1011
require: ['ngModel', '^?form'],
1112
link: function(scope, element, attrs, ctrls) {
@@ -31,10 +32,8 @@ angular.module('ui.tinymce', [])
3132
}
3233
};
3334

34-
// generate an ID if not present
35-
if (!attrs.id) {
36-
attrs.$set('id', 'uiTinymce' + generatedIds++);
37-
}
35+
// generate an ID
36+
attrs.$set('id', ID_ATTR + '-' + generatedIds++);
3837

3938
expression = {};
4039

@@ -84,12 +83,12 @@ angular.module('ui.tinymce', [])
8483
updateView: updateView
8584
});
8685
}
87-
},
88-
selector: '#' + attrs.id
86+
}
8987
};
9088
// extend options with initial uiTinymceConfig and options from directive attribute value
9189
angular.extend(options, uiTinymceConfig, expression);
9290
tinymce.init(options);
91+
tinymce.execCommand('mceAddEditor', false, attrs.id);
9392

9493
ngModel.$formatters.unshift(function(modelValue) {
9594
return modelValue || '';
@@ -124,6 +123,19 @@ angular.module('ui.tinymce', [])
124123
}
125124
});
126125

126+
scope.$on('$tinymce:refresh', function(e, id) {
127+
var eid = attrs.id;
128+
if (angular.isUndefined(id) || id === eid) {
129+
var parentElement = element.parent();
130+
var clonedElement = element.clone();
131+
clonedElement.removeAttr('id');
132+
tinymce.execCommand('mceRemoveEditor', false, eid);
133+
$timeout(function() {
134+
parentElement.append($compile(clonedElement)(scope));
135+
}, 3000);
136+
}
137+
});
138+
127139
scope.$on('$destroy', function() {
128140
ensureInstance();
129141

0 commit comments

Comments
 (0)