@@ -19,11 +19,7 @@ angular.module('ui.tinymce', [])
19
19
20
20
var expression , options , tinyInstance ,
21
21
updateView = function ( editor ) {
22
- if ( options . raw === true ) {
23
- ngModel . $setViewValue ( editor . getContent ( { format : 'text' } ) . trim ( ) ) ;
24
- } else {
25
- ngModel . $setViewValue ( editor . getContent ( ) . trim ( ) ) ;
26
- }
22
+ ngModel . $setViewValue ( editor . getContent ( { format : options . format } ) . trim ( ) ) ;
27
23
if ( ! $rootScope . $$phase ) {
28
24
scope . $apply ( ) ;
29
25
}
@@ -83,12 +79,15 @@ angular.module('ui.tinymce', [])
83
79
updateView : updateView
84
80
} ) ;
85
81
}
86
- }
82
+ } ,
83
+ format : 'raw' ,
84
+ selector : '#' + attrs . id
87
85
} ;
88
86
// extend options with initial uiTinymceConfig and options from directive attribute value
89
87
angular . extend ( options , uiTinymceConfig , expression ) ;
90
- tinymce . init ( options ) ;
91
- tinymce . execCommand ( 'mceAddEditor' , false , attrs . id ) ;
88
+ $timeout ( function ( ) {
89
+ tinymce . init ( options ) ;
90
+ } ) ;
92
91
93
92
ngModel . $formatters . unshift ( function ( modelValue ) {
94
93
return modelValue || '' ;
@@ -97,11 +96,12 @@ angular.module('ui.tinymce', [])
97
96
ngModel . $render = function ( ) {
98
97
ensureInstance ( ) ;
99
98
100
- var format = options . raw ? 'text' : 'raw' ;
101
-
102
- // tinymce replaces "\r\n" to "\n", so we have to do the same on model value
99
+ // tinymce replaces '\r\n' to '\n', so we have to do the same on model value
100
+ // instance.getDoc() check is a guard against null value when destruction &
101
+ // recreation of instances happen
103
102
if ( tinyInstance &&
104
- tinyInstance . getContent ( { format : format } ) . trim ( ) !== ngModel . $viewValue . replace ( / \r \n / g, '\n' )
103
+ tinyInstance . getDoc ( ) &&
104
+ tinyInstance . getContent ( { format : options . format } ) . trim ( ) !== ngModel . $viewValue . replace ( / \r \n / g, '\n' )
105
105
) {
106
106
tinyInstance . setContent ( ngModel . $viewValue ) ;
107
107
}
@@ -123,16 +123,19 @@ angular.module('ui.tinymce', [])
123
123
}
124
124
} ) ;
125
125
126
+ // This block is because of TinyMCE not playing well with removal and
127
+ // recreation of instances, requiring instances to have different
128
+ // selectors in order to render new instances properly
126
129
scope . $on ( '$tinymce:refresh' , function ( e , id ) {
127
130
var eid = attrs . id ;
128
131
if ( angular . isUndefined ( id ) || id === eid ) {
129
132
var parentElement = element . parent ( ) ;
130
133
var clonedElement = element . clone ( ) ;
131
134
clonedElement . removeAttr ( 'id' ) ;
135
+ clonedElement . removeAttr ( 'style' ) ;
136
+ clonedElement . removeAttr ( 'aria-hidden' ) ;
132
137
tinymce . execCommand ( 'mceRemoveEditor' , false , eid ) ;
133
- $timeout ( function ( ) {
134
- parentElement . append ( $compile ( clonedElement ) ( scope ) ) ;
135
- } , 3000 ) ;
138
+ parentElement . append ( $compile ( clonedElement ) ( scope ) ) ;
136
139
}
137
140
} ) ;
138
141
0 commit comments