11import Component from '@glimmer/component' ;
22import { service } from '@ember/service' ;
3- import { tracked } from '@glimmer/tracking' ;
4- import { restartableTask } from 'ember-concurrency' ;
3+ import { cached , tracked } from '@glimmer/tracking' ;
54import { loadFroalaEditor } from 'ilios-common/utils/load-froala-editor' ;
65import { guidFor } from '@ember/object/internals' ;
6+ import { modifier } from 'ember-modifier' ;
7+ import { TrackedAsyncData } from 'ember-async-data' ;
78
89export default class HtmlEditorComponent extends Component {
910 @service intl ;
10- @tracked editor = null ;
1111 @tracked editorId = null ;
1212 @tracked loadFinished = false ;
1313
14+ editor = null ;
1415 defaultButtons = {
1516 moreText : {
1617 buttons : [ 'bold' , 'italic' , 'subscript' , 'superscript' , 'formatOL' , 'formatUL' , 'insertLink' ] ,
@@ -27,6 +28,29 @@ export default class HtmlEditorComponent extends Component {
2728 this . editorId = guidFor ( this ) ;
2829 }
2930
31+ @cached
32+ get loadFroalaData ( ) {
33+ return new TrackedAsyncData ( loadFroalaEditor ( ) ) ;
34+ }
35+
36+ editorInserted = modifier ( ( element , [ options ] ) => {
37+ if ( ! this . editor ) {
38+ const { FroalaEditor } = this . loadFroalaData . value ;
39+ const component = this ;
40+ // getting the Froala instance inside its constructor callback
41+ // https://froala.com/wysiwyg-editor/examples/getHTML/
42+ this . editor = new FroalaEditor ( element , options , function ( ) {
43+ this . html . set ( component . args . content ) ;
44+ if ( component . args . autoFocus ) {
45+ this . events . focus ( ) ;
46+ }
47+ component . loadFinished = true ;
48+ } ) ;
49+ }
50+
51+ return true ;
52+ } ) ;
53+
3054 get options ( ) {
3155 return {
3256 key : 'Kb3A3pE2E2A1E4G4I4oCd2ZSb1XHi1Cb2a1KIWCWMJHXCLSwG1G1B2C1B1C7F6E1E4F4==' ,
@@ -73,26 +97,4 @@ export default class HtmlEditorComponent extends Component {
7397 this . editor = null ;
7498 }
7599 }
76- createEditor ( element , options ) {
77- return new Promise ( ( resolve ) => {
78- loadFroalaEditor ( ) . then ( ( { FroalaEditor } ) => {
79- new FroalaEditor ( element , options , function ( ) {
80- resolve ( this ) ;
81- } ) ;
82- } ) ;
83- } ) ;
84- }
85-
86- loadEditor = restartableTask ( async ( element , [ options ] ) => {
87- if ( ! this . editor ) {
88- this . editor = await this . createEditor ( element , options ) ;
89- this . editor . html . set ( this . args . content ) ;
90- if ( this . args . autofocus ) {
91- this . editor . events . focus ( ) ;
92- }
93- this . loadFinished = true ;
94- }
95-
96- return true ;
97- } ) ;
98100}
0 commit comments