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

Commit fce3edc

Browse files
author
Piotr Jasiun
authored
Merge pull request #85 from ckeditor/t/ckeditor5/1619
Feature: Introduced `EditorConfig#initialData`.
2 parents d68db77 + d1c26ab commit fce3edc

File tree

3 files changed

+94
-50
lines changed

3 files changed

+94
-50
lines changed

src/classiceditor.js

Lines changed: 49 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import ClassicEditorUIView from './classiceditoruiview';
1717
import getDataFromElement from '@ckeditor/ckeditor5-utils/src/dom/getdatafromelement';
1818
import mix from '@ckeditor/ckeditor5-utils/src/mix';
1919
import { isElement } from 'lodash-es';
20+
import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
2021

2122
/**
2223
* The {@glink builds/guides/overview#classic-editor classic editor} implementation.
@@ -92,9 +93,11 @@ export default class ClassicEditor extends Editor {
9293
}
9394

9495
/**
95-
* Creates a classic editor instance.
96+
* Creates a `ClassicEditor` instance.
9697
*
97-
* Creating an instance when using a {@glink builds/index CKEditor build}:
98+
* There are two general ways how the editor can be initialized.
99+
*
100+
* You can initialize the editor using an existing DOM element:
98101
*
99102
* ClassicEditor
100103
* .create( document.querySelector( '#editor' ) )
@@ -105,78 +108,80 @@ export default class ClassicEditor extends Editor {
105108
* console.error( err.stack );
106109
* } );
107110
*
108-
* Creating an instance when using CKEditor from source (make sure to specify the list of plugins to load and the toolbar):
111+
* The element's content will be used as the editor data and the element will be replaced by the editable element and the editor UI.
109112
*
110-
* import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classiceditor';
111-
* import Essentials from '@ckeditor/ckeditor5-essentials/src/essentials';
112-
* import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold';
113-
* import Italic from '@ckeditor/ckeditor5-basic-styles/src/italic';
114-
* import ...
113+
* Alternatively, you can initialize the editor by passing the initial data directly as a `String`.
114+
* In this case, the editor will render an element that must be inserted into the DOM for the editor to work properly:
115115
*
116116
* ClassicEditor
117-
* .create( document.querySelector( '#editor' ), {
118-
* plugins: [ Essentials, Bold, Italic, ... ],
119-
* toolbar: [ 'bold', 'italic', ... ]
120-
* } )
117+
* .create( '<p>Hello world!</p>' )
121118
* .then( editor => {
122119
* console.log( 'Editor was initialized', editor );
120+
*
121+
* // Initial data was provided so the editor UI element needs to be added manually to the DOM.
122+
* document.body.appendChild( editor.ui.element );
123123
* } )
124124
* .catch( err => {
125125
* console.error( err.stack );
126126
* } );
127127
*
128-
* Creating an instance when using initial data instead of a DOM element:
128+
* This lets you dynamically append the editor to your web page whenever it is convenient for you. You may use this method if your
129+
* web page content is generated on the client-side and the DOM structure is not ready at the moment when you initialize the editor.
129130
*
130-
* import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classiceditor';
131-
* import Essentials from '@ckeditor/ckeditor5-essentials/src/essentials';
132-
* import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold';
133-
* import Italic from '@ckeditor/ckeditor5-basic-styles/src/italic';
134-
* import ...
131+
* You can also mix those two ways by providing a DOM element to be used and passing the initial data through the config:
135132
*
136133
* ClassicEditor
137-
* .create( '<p>Hello world!</p>' )
134+
* .create( document.querySelector( '#editor' ), {
135+
* initialData: '<h2>Initial data</h2><p>Foo bar.</p>'
136+
* } )
138137
* .then( editor => {
139138
* console.log( 'Editor was initialized', editor );
140-
*
141-
* // Initial data was provided so `editor.element` needs to be added manually to the DOM.
142-
* document.body.appendChild( editor.element );
143139
* } )
144140
* .catch( err => {
145141
* console.error( err.stack );
146142
* } );
147143
*
144+
* This method can be used to initialize the editor on an existing element with specified content in case if your integration
145+
* makes it difficult to set the content of the source element.
146+
*
147+
* Note that an error will be thrown if you pass initial data both as the first parameter and also in the config.
148+
*
149+
* See also the {@link module:core/editor/editorconfig~EditorConfig editor configuration documentation} to learn more about
150+
* customizing plugins, toolbar and other.
151+
*
148152
* @param {HTMLElement|String} sourceElementOrData The DOM element that will be the source for the created editor
149153
* or the editor's initial data.
150154
*
151-
* If a source element is passed, then its contents will be automatically
152-
* {@link module:editor-classic/classiceditor~ClassicEditor#setData loaded} to the editor on startup
153-
* and the {@link module:core/editor/editorui~EditorUI#getEditableElement editor element} will replace the passed element in the DOM
154-
* (the original one will be hidden and the editor will be injected next to it).
155-
*
156-
* Moreover, the data will be set back to the source element once the editor is destroyed and
157-
* (if the element is a `<textarea>`) when a form in which this element is contained is submitted (which ensures
158-
* automatic integration with native web forms).
155+
* If a DOM element is passed, its content will be automatically loaded to the editor upon initialization
156+
* and the {@link module:editor-classic/classiceditorui~ClassicEditorUI#element editor element} will replace the passed element
157+
* in the DOM (the original one will be hidden and the editor will be injected next to it).
159158
*
160-
* If the data is passed, a detached editor will be created. It means that you need to insert it into the DOM manually (by accessing
161-
* it via the {@link module:editor-classic/classiceditorui~ClassicEditorUI#getEditableElement `editor.ui.getEditableElement()`} method).
159+
* Moreover, the editor data will be set back to the original element once the editor is destroyed and when a form, in which
160+
* this element is contained, is submitted (if the original element is a `<textarea>`). This ensures seamless integration with native
161+
* web forms.
162162
*
163-
* See the examples above to learn more.
163+
* If the initial data is passed, a detached editor will be created. In this case you need to insert it into the DOM manually.
164+
* It is available under {@link module:editor-classic/classiceditorui~ClassicEditorUI#element `editor.ui.element`} property.
164165
*
165-
* @param {module:core/editor/editorconfig~EditorConfig} config The editor configuration.
166-
* @returns {Promise} A promise resolved once the editor is ready.
167-
* The promise returns the created {@link module:editor-classic/classiceditor~ClassicEditor} instance.
166+
* @param {module:core/editor/editorconfig~EditorConfig} [config] The editor configuration.
167+
* @returns {Promise} A promise resolved once the editor is ready. The promise resolves with the created editor instance.
168168
*/
169-
static create( sourceElementOrData, config ) {
169+
static create( sourceElementOrData, config = {} ) {
170170
return new Promise( resolve => {
171171
const editor = new this( sourceElementOrData, config );
172172

173173
resolve(
174174
editor.initPlugins()
175175
.then( () => editor.ui.init( isElement( sourceElementOrData ) ? sourceElementOrData : null ) )
176176
.then( () => {
177-
const initialData = isElement( sourceElementOrData ) ?
178-
getDataFromElement( sourceElementOrData ) :
179-
sourceElementOrData;
177+
if ( !isElement( sourceElementOrData ) && config.initialData ) {
178+
throw new CKEditorError(
179+
'editor-create-initial-data: ' +
180+
'EditorConfig#initialData cannot be used together with initial data passed in Editor#create()'
181+
);
182+
}
183+
184+
const initialData = config.initialData || getInitialData( sourceElementOrData );
180185

181186
return editor.data.init( initialData );
182187
} )
@@ -189,3 +194,7 @@ export default class ClassicEditor extends Editor {
189194

190195
mix( ClassicEditor, DataApiMixin );
191196
mix( ClassicEditor, ElementApiMixin );
197+
198+
function getInitialData( sourceElementOrData ) {
199+
return isElement( sourceElementOrData ) ? getDataFromElement( sourceElementOrData ) : sourceElementOrData;
200+
}

tests/classiceditor.js

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

101-
it( 'allows to pass data to the constructor', () => {
102-
return ClassicEditor.create( '<p>Hello world!</p>', {
103-
plugins: [ Paragraph ]
104-
} ).then( editor => {
105-
expect( editor.getData() ).to.equal( '<p>Hello world!</p>' );
106-
} );
107-
} );
108-
109101
describe( 'ui', () => {
110102
it( 'creates the UI using BoxedEditorUI classes', () => {
111103
expect( editor.ui ).to.be.instanceof( ClassicEditorUI );
@@ -155,6 +147,49 @@ describe( 'ClassicEditor', () => {
155147
} );
156148
} );
157149

150+
it( 'should not require config object', () => {
151+
// Just being safe with `builtinPlugins` static property.
152+
class CustomClassicEditor extends ClassicEditor {}
153+
CustomClassicEditor.builtinPlugins = [ Paragraph, Bold ];
154+
155+
return CustomClassicEditor.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 ClassicEditor.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 ClassicEditor.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+
ClassicEditor.create( '<p>Hello world!</p>', {
186+
initialData: '<p>I am evil!</p>',
187+
plugins: [ Paragraph ]
188+
} ).catch( () => {
189+
done();
190+
} );
191+
} );
192+
158193
it( 'should have undefined the #sourceElement if editor was initialized with data', () => {
159194
return ClassicEditor
160195
.create( '<p>Foo.</p>', {

tests/manual/classiceditor-data.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ function initEditor() {
2828
.then( editor => {
2929
counter += 1;
3030
window.editors.push( editor );
31-
container.appendChild( editor.element );
31+
container.appendChild( editor.ui.element );
3232
} )
3333
.catch( err => {
3434
console.error( err.stack );
@@ -39,7 +39,7 @@ function destroyEditors() {
3939
window.editors.forEach( editor => {
4040
editor.destroy()
4141
.then( () => {
42-
editor.element.remove();
42+
editor.ui.element.remove();
4343
} );
4444
} );
4545
window.editors = [];

0 commit comments

Comments
 (0)