@@ -17,6 +17,7 @@ import InlineEditorUIView from './inlineeditoruiview';
17
17
import setDataInElement from '@ckeditor/ckeditor5-utils/src/dom/setdatainelement' ;
18
18
import getDataFromElement from '@ckeditor/ckeditor5-utils/src/dom/getdatafromelement' ;
19
19
import mix from '@ckeditor/ckeditor5-utils/src/mix' ;
20
+ import isElement from '@ckeditor/ckeditor5-utils/src/lib/lodash/isElement' ;
20
21
21
22
/**
22
23
* The {@glink builds/guides/overview#inline-editor inline editor} implementation.
@@ -53,24 +54,34 @@ export default class InlineEditor extends Editor {
53
54
* {@link module:editor-inline/inlineeditor~InlineEditor.create `InlineEditor.create()`} method instead.
54
55
*
55
56
* @protected
56
- * @param {HTMLElement } element The DOM element that will be the source for the created editor
57
- * (on which the editor will be initialized).
57
+ * @param {HTMLElement|String } sourceElementOrData The DOM element that will be the source for the created editor
58
+ * (on which the editor will be initialized) or initial data for the editor. For more information see
59
+ * {@link module:editor-inline/inlineeditor~InlineEditor.create `InlineEditor.create()`}.
58
60
* @param {module:core/editor/editorconfig~EditorConfig } config The editor configuration.
59
61
*/
60
- constructor ( element , config ) {
62
+ constructor ( sourceElementOrData , config ) {
61
63
super ( config ) ;
62
64
63
- this . element = element ;
64
-
65
65
this . data . processor = new HtmlDataProcessor ( ) ;
66
66
67
67
this . model . document . createRoot ( ) ;
68
68
69
- this . ui = new InlineEditorUI ( this , new InlineEditorUIView ( this . locale , element ) ) ;
69
+ if ( isElement ( sourceElementOrData ) ) {
70
+ this . sourceElement = sourceElementOrData ;
71
+ }
72
+
73
+ this . ui = new InlineEditorUI ( this , new InlineEditorUIView ( this . locale , this . sourceElement ) ) ;
70
74
71
75
attachToForm ( this ) ;
72
76
}
73
77
78
+ /**
79
+ * @inheritDoc
80
+ */
81
+ get element ( ) {
82
+ return this . ui . view . editable . element ;
83
+ }
84
+
74
85
/**
75
86
* Destroys the editor instance, releasing all resources used by it.
76
87
*
@@ -86,11 +97,15 @@ export default class InlineEditor extends Editor {
86
97
this . ui . destroy ( ) ;
87
98
88
99
return super . destroy ( )
89
- . then ( ( ) => setDataInElement ( this . element , data ) ) ;
100
+ . then ( ( ) => {
101
+ if ( this . sourceElement ) {
102
+ setDataInElement ( this . sourceElement , data ) ;
103
+ }
104
+ } ) ;
90
105
}
91
106
92
107
/**
93
- * Creates a inline editor instance.
108
+ * Creates an inline editor instance.
94
109
*
95
110
* Creating instance when using {@glink builds/index CKEditor build}:
96
111
*
@@ -123,23 +138,56 @@ export default class InlineEditor extends Editor {
123
138
* console.error( err.stack );
124
139
* } );
125
140
*
126
- * @param {HTMLElement } element The DOM element that will be the source for the created editor
127
- * (on which the editor will be initialized).
141
+ * Creating instance when using initial data instead of a DOM element:
142
+ *
143
+ * import InlineEditor from '@ckeditor/ckeditor5-editor-inline/src/inlineeditor';
144
+ * import Essentials from '@ckeditor/ckeditor5-essentials/src/essentials';
145
+ * import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold';
146
+ * import Italic from '@ckeditor/ckeditor5-basic-styles/src/italic';
147
+ * import ...
148
+ *
149
+ * InlineEditor
150
+ * .create( '<p>Hello world!</p>' )
151
+ * .then( editor => {
152
+ * console.log( 'Editor was initialized', editor );
153
+ *
154
+ * // Initial data was provided so `editor.element` needs to be added manually to the DOM.
155
+ * document.body.appendChild( editor.element );
156
+ * } )
157
+ * .catch( err => {
158
+ * console.error( err.stack );
159
+ * } );
160
+ *
161
+ * @param {HTMLElement|String } sourceElementOrData The DOM element that will be the source for the created editor
162
+ * (on which the editor will be initialized) or initial data for the editor.
163
+ *
164
+ * If a source element is passed, then its contents will be automatically
165
+ * {@link module:editor-classic/inlineeditor~InlineEditor#setData loaded} to the editor on startup and the element
166
+ * itself will be used as the editor's editable element.
167
+ *
168
+ * If a data is provided, then `editor.element` will be created automatically and needs to be added
169
+ * manually to the DOM.
128
170
* @param {module:core/editor/editorconfig~EditorConfig } config The editor configuration.
129
171
* @returns {Promise } A promise resolved once the editor is ready.
130
172
* The promise returns the created {@link module:editor-inline/inlineeditor~InlineEditor} instance.
131
173
*/
132
- static create ( element , config ) {
174
+ static create ( sourceElementOrData , config ) {
133
175
return new Promise ( resolve => {
134
- const editor = new this ( element , config ) ;
176
+ const editor = new this ( sourceElementOrData , config ) ;
135
177
136
178
resolve (
137
179
editor . initPlugins ( )
138
180
. then ( ( ) => {
139
181
editor . ui . init ( ) ;
140
182
editor . fire ( 'uiReady' ) ;
141
183
} )
142
- . then ( ( ) => editor . data . init ( getDataFromElement ( element ) ) )
184
+ . then ( ( ) => {
185
+ const initialData = isElement ( sourceElementOrData ) ?
186
+ getDataFromElement ( sourceElementOrData ) :
187
+ sourceElementOrData ;
188
+
189
+ return editor . data . init ( initialData ) ;
190
+ } )
143
191
. then ( ( ) => {
144
192
editor . fire ( 'dataReady' ) ;
145
193
editor . fire ( 'ready' ) ;
0 commit comments