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

Commit 59e97b5

Browse files
author
Piotr Jasiun
authored
Merge pull request #50 from ckeditor/t/ckeditor5/1619
Feature: Introduced `EditorConfig#initialData`.
2 parents 95a3b03 + 453d519 commit 59e97b5

File tree

4 files changed

+92
-47
lines changed

4 files changed

+92
-47
lines changed

src/inlineeditor.js

Lines changed: 47 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import setDataInElement from '@ckeditor/ckeditor5-utils/src/dom/setdatainelement
1818
import getDataFromElement from '@ckeditor/ckeditor5-utils/src/dom/getdatafromelement';
1919
import mix from '@ckeditor/ckeditor5-utils/src/mix';
2020
import { isElement } from 'lodash-es';
21+
import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
2122

2223
/**
2324
* The {@glink builds/guides/overview#inline-editor inline editor} implementation.
@@ -99,9 +100,11 @@ export default class InlineEditor extends Editor {
99100
}
100101

101102
/**
102-
* Creates an inline editor instance.
103+
* Creates an `InlineEditor` instance.
103104
*
104-
* Creating an instance when using a {@glink builds/index CKEditor build}:
105+
* There are two general ways how the editor can be initialized.
106+
*
107+
* You can initialize the editor using an existing DOM element:
105108
*
106109
* InlineEditor
107110
* .create( document.querySelector( '#editor' ) )
@@ -112,60 +115,60 @@ export default class InlineEditor extends Editor {
112115
* console.error( err.stack );
113116
* } );
114117
*
115-
* Creating an instance when using CKEditor from source (make sure to specify the list of plugins to load and the toolbar):
118+
* The element's content will be used as the editor data and the element will become the editable element.
116119
*
117-
* import InlineEditor from '@ckeditor/ckeditor5-editor-inline/src/inlineeditor';
118-
* import Essentials from '@ckeditor/ckeditor5-essentials/src/essentials';
119-
* import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold';
120-
* import Italic from '@ckeditor/ckeditor5-basic-styles/src/italic';
121-
* import ...
120+
* Alternatively, you can initialize the editor by passing the initial data directly as a `String`.
121+
* In this case, the editor will render an element that must be inserted into the DOM for the editor to work properly:
122122
*
123123
* InlineEditor
124-
* .create( document.querySelector( '#editor' ), {
125-
* plugins: [ Essentials, Bold, Italic, ... ],
126-
* toolbar: [ 'bold', 'italic', ... ]
127-
* } )
124+
* .create( '<p>Hello world!</p>' )
128125
* .then( editor => {
129126
* console.log( 'Editor was initialized', editor );
127+
*
128+
* // Initial data was provided so the editor UI element needs to be added manually to the DOM.
129+
* document.body.appendChild( editor.ui.element );
130130
* } )
131131
* .catch( err => {
132132
* console.error( err.stack );
133133
* } );
134134
*
135-
* Creating an instance when using the initial data instead of a DOM element:
135+
* This lets you dynamically append the editor to your web page whenever it is convenient for you. You may use this method if your
136+
* web page content is generated on the client-side and the DOM structure is not ready at the moment when you initialize the editor.
136137
*
137-
* import InlineEditor from '@ckeditor/ckeditor5-editor-inline/src/inlineeditor';
138-
* import Essentials from '@ckeditor/ckeditor5-essentials/src/essentials';
139-
* import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold';
140-
* import Italic from '@ckeditor/ckeditor5-basic-styles/src/italic';
141-
* import ...
138+
* You can also mix those two ways by providing a DOM element to be used and passing the initial data through the config:
142139
*
143140
* InlineEditor
144-
* .create( '<p>Hello world!</p>' )
141+
* .create( document.querySelector( '#editor' ), {
142+
* initialData: '<h2>Initial data</h2><p>Foo bar.</p>'
143+
* } )
145144
* .then( editor => {
146145
* console.log( 'Editor was initialized', editor );
147-
*
148-
* // Initial data was provided so `editor.element` needs to be added manually to the DOM.
149-
* document.body.appendChild( editor.element );
150146
* } )
151147
* .catch( err => {
152148
* console.error( err.stack );
153149
* } );
154150
*
151+
* This method can be used to initialize the editor on an existing element with specified content in case if your integration
152+
* makes it difficult to set the content of the source element.
153+
*
154+
* Note that an error will be thrown if you pass initial data both as the first parameter and also in the config.
155+
*
156+
* See also the {@link module:core/editor/editorconfig~EditorConfig editor configuration documentation} to learn more about
157+
* customizing plugins, toolbar and other.
158+
*
155159
* @param {HTMLElement|String} sourceElementOrData The DOM element that will be the source for the created editor
156-
* (on which the editor will be initialized) or the initial data for the editor.
160+
* or the editor's initial data.
157161
*
158-
* If a source element is passed, then its contents will be automatically
159-
* {@link module:editor-inline/inlineeditor~InlineEditor#setData loaded} to the editor on startup and the element
160-
* itself will be used as the editor's editable element.
162+
* If a DOM element is passed, its content will be automatically loaded to the editor upon initialization.
163+
* Moreover, the editor data will be set back to the original element once the editor is destroyed.
161164
*
162-
* If data is provided, then `editor.element` will be created automatically and needs to be added
163-
* to the DOM manually.
164-
* @param {module:core/editor/editorconfig~EditorConfig} config The editor configuration.
165-
* @returns {Promise} A promise resolved once the editor is ready.
166-
* The promise returns the created {@link module:editor-inline/inlineeditor~InlineEditor} instance.
165+
* If the initial data is passed, a detached editor will be created. In this case you need to insert it into the DOM manually.
166+
* It is available under {@link module:editor-inline/inlineeditorui~InlineEditorUI#element `editor.ui.element`} property.
167+
*
168+
* @param {module:core/editor/editorconfig~EditorConfig} [config] The editor configuration.
169+
* @returns {Promise} A promise resolved once the editor is ready. The promise resolves with the created editor instance.
167170
*/
168-
static create( sourceElementOrData, config ) {
171+
static create( sourceElementOrData, config = {} ) {
169172
return new Promise( resolve => {
170173
const editor = new this( sourceElementOrData, config );
171174

@@ -175,9 +178,14 @@ export default class InlineEditor extends Editor {
175178
editor.ui.init();
176179
} )
177180
.then( () => {
178-
const initialData = isElement( sourceElementOrData ) ?
179-
getDataFromElement( sourceElementOrData ) :
180-
sourceElementOrData;
181+
if ( !isElement( sourceElementOrData ) && config.initialData ) {
182+
throw new CKEditorError(
183+
'editor-create-initial-data: ' +
184+
'EditorConfig#initialData cannot be used together with initial data passed in Editor#create()'
185+
);
186+
}
187+
188+
const initialData = config.initialData || getInitialData( sourceElementOrData );
181189

182190
return editor.data.init( initialData );
183191
} )
@@ -190,3 +198,7 @@ export default class InlineEditor extends Editor {
190198

191199
mix( InlineEditor, DataApiMixin );
192200
mix( InlineEditor, ElementApiMixin );
201+
202+
function getInitialData( sourceElementOrData ) {
203+
return isElement( sourceElementOrData ) ? getDataFromElement( sourceElementOrData ) : sourceElementOrData;
204+
}

tests/inlineeditor.js

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,6 @@ describe( 'InlineEditor', () => {
9999
} );
100100
} );
101101

102-
it( 'allows to pass data to the constructor', () => {
103-
return InlineEditor.create( '<p>Hello world!</p>', {
104-
plugins: [ Paragraph ]
105-
} ).then( editor => {
106-
expect( editor.getData() ).to.equal( '<p>Hello world!</p>' );
107-
} );
108-
} );
109-
110102
it( 'should have undefined the #sourceElement if editor was initialized with data', () => {
111103
return InlineEditor.create( '<p>Hello world!</p>', {
112104
plugins: [ Paragraph ]
@@ -155,6 +147,49 @@ describe( 'InlineEditor', () => {
155147
expect( editor.getData() ).to.equal( '<p><strong>foo</strong> bar</p>' );
156148
} );
157149

150+
it( 'should not require config object', () => {
151+
// Just being safe with `builtinPlugins` static property.
152+
class CustomInlineEditor extends InlineEditor {}
153+
CustomInlineEditor.builtinPlugins = [ Paragraph, Bold ];
154+
155+
return CustomInlineEditor.create( editorElement )
156+
.then( newEditor => {
157+
expect( newEditor.getData() ).to.equal( '<p><strong>foo</strong> bar</p>' );
158+
159+
return newEditor.destroy();
160+
} );
161+
} );
162+
163+
it( 'allows to pass data to the constructor', () => {
164+
return InlineEditor.create( '<p>Hello world!</p>', {
165+
plugins: [ Paragraph ]
166+
} ).then( editor => {
167+
expect( editor.getData() ).to.equal( '<p>Hello world!</p>' );
168+
169+
editor.destroy();
170+
} );
171+
} );
172+
173+
it( 'initializes with config.initialData', () => {
174+
return InlineEditor.create( editorElement, {
175+
initialData: '<p>Hello world!</p>',
176+
plugins: [ Paragraph ]
177+
} ).then( editor => {
178+
expect( editor.getData() ).to.equal( '<p>Hello world!</p>' );
179+
180+
editor.destroy();
181+
} );
182+
} );
183+
184+
it( 'throws if initial data is passed in Editor#create and config.initialData is also used', done => {
185+
InlineEditor.create( '<p>Hello world!</p>', {
186+
initialData: '<p>I am evil!</p>',
187+
plugins: [ Paragraph ]
188+
} ).catch( () => {
189+
done();
190+
} );
191+
} );
192+
158193
// #25
159194
it( 'creates an instance of a InlineEditor child class', () => {
160195
// Fun fact: Remove the next 3 lines and you'll get a lovely inf loop due to two

tests/manual/inlineeditor-data.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ function initEditor() {
2121
.then( editor => {
2222
counter += 1;
2323
window.editors.push( editor );
24-
container.appendChild( editor.element );
24+
container.appendChild( editor.ui.element );
2525
} )
2626
.catch( err => {
2727
console.error( err.stack );
@@ -32,7 +32,7 @@ function destroyEditors() {
3232
window.editors.forEach( editor => {
3333
editor.destroy()
3434
.then( () => {
35-
editor.element.remove();
35+
editor.ui.element.remove();
3636
} );
3737
} );
3838
window.editors = [];

tests/manual/inlineeditor.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,5 +63,3 @@ function destroyEditors() {
6363

6464
document.getElementById( 'initEditors' ).addEventListener( 'click', initEditors );
6565
document.getElementById( 'destroyEditors' ).addEventListener( 'click', destroyEditors );
66-
67-
initEditors();

0 commit comments

Comments
 (0)