From 3197cecd3892a966581ba13a9780bb9ae5a9c637 Mon Sep 17 00:00:00 2001 From: Szymon Cofalik Date: Tue, 19 Mar 2019 18:06:05 +0100 Subject: [PATCH 1/7] Introduced EditorConfig#initialData. Made `config` param optional. --- src/classiceditor.js | 36 ++++++++++++++++++++++++----- tests/classiceditor.js | 51 +++++++++++++++++++++++++++++++++++------- 2 files changed, 73 insertions(+), 14 deletions(-) diff --git a/src/classiceditor.js b/src/classiceditor.js index aff5389..b8433cf 100644 --- a/src/classiceditor.js +++ b/src/classiceditor.js @@ -17,6 +17,7 @@ import ClassicEditorUIView from './classiceditoruiview'; import getDataFromElement from '@ckeditor/ckeditor5-utils/src/dom/getdatafromelement'; import mix from '@ckeditor/ckeditor5-utils/src/mix'; import { isElement } from 'lodash-es'; +import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror'; /** * The {@glink builds/guides/overview#classic-editor classic editor} implementation. @@ -125,7 +126,8 @@ export default class ClassicEditor extends Editor { * console.error( err.stack ); * } ); * - * Creating an instance when using initial data instead of a DOM element: + * Creating an instance when using initial data instead of a DOM element. + * The editor will then render an editable element that must be inserted into the DOM for the editor to work properly: * * import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classiceditor'; * import Essentials from '@ckeditor/ckeditor5-essentials/src/essentials'; @@ -145,6 +147,19 @@ export default class ClassicEditor extends Editor { * console.error( err.stack ); * } ); * + * Creating an instance on an existing DOM element using external initial content (specified in config): + * + * ClassicEditor + * .create( document.querySelector( '#editor' ), { + * initialData: '

Initial data

Foo bar.

' + * } ) + * .then( editor => { + * console.log( 'Editor was initialized', editor ); + * } ) + * .catch( err => { + * console.error( err.stack ); + * } ); + * * @param {HTMLElement|String} sourceElementOrData The DOM element that will be the source for the created editor * or the editor's initial data. * @@ -162,11 +177,11 @@ export default class ClassicEditor extends Editor { * * See the examples above to learn more. * - * @param {module:core/editor/editorconfig~EditorConfig} config The editor configuration. + * @param {module:core/editor/editorconfig~EditorConfig} [config] The editor configuration. * @returns {Promise} A promise resolved once the editor is ready. * The promise returns the created {@link module:editor-classic/classiceditor~ClassicEditor} instance. */ - static create( sourceElementOrData, config ) { + static create( sourceElementOrData, config = {} ) { return new Promise( resolve => { const editor = new this( sourceElementOrData, config ); @@ -174,9 +189,14 @@ export default class ClassicEditor extends Editor { editor.initPlugins() .then( () => editor.ui.init( isElement( sourceElementOrData ) ? sourceElementOrData : null ) ) .then( () => { - const initialData = isElement( sourceElementOrData ) ? - getDataFromElement( sourceElementOrData ) : - sourceElementOrData; + if ( !isElement( sourceElementOrData ) && config.initialData ) { + throw new CKEditorError( + 'editor-create-initial-data: ' + + 'EditorConfig#initialData cannot be used together with initial data passed in Editor#create()' + ); + } + + const initialData = config.initialData || getInitialData( sourceElementOrData ); return editor.data.init( initialData ); } ) @@ -189,3 +209,7 @@ export default class ClassicEditor extends Editor { mix( ClassicEditor, DataApiMixin ); mix( ClassicEditor, ElementApiMixin ); + +function getInitialData( sourceElementOrData ) { + return isElement( sourceElementOrData ) ? getDataFromElement( sourceElementOrData ) : sourceElementOrData; +} diff --git a/tests/classiceditor.js b/tests/classiceditor.js index c543385..48c3ca5 100644 --- a/tests/classiceditor.js +++ b/tests/classiceditor.js @@ -98,14 +98,6 @@ describe( 'ClassicEditor', () => { } ); } ); - it( 'allows to pass data to the constructor', () => { - return ClassicEditor.create( '

Hello world!

', { - plugins: [ Paragraph ] - } ).then( editor => { - expect( editor.getData() ).to.equal( '

Hello world!

' ); - } ); - } ); - describe( 'ui', () => { it( 'creates the UI using BoxedEditorUI classes', () => { expect( editor.ui ).to.be.instanceof( ClassicEditorUI ); @@ -155,6 +147,49 @@ describe( 'ClassicEditor', () => { } ); } ); + it( 'should not require config object', () => { + // Just being safe with `builtinPlugins` static property. + class CustomClassicEditor extends ClassicEditor {} + CustomClassicEditor.builtinPlugins = [ Paragraph, Bold ]; + + return CustomClassicEditor.create( editorElement ) + .then( newEditor => { + expect( newEditor.getData() ).to.equal( '

foo bar

' ); + + return newEditor.destroy(); + } ); + } ); + + it( 'allows to pass data to the constructor', () => { + return ClassicEditor.create( '

Hello world!

', { + plugins: [ Paragraph ] + } ).then( editor => { + expect( editor.getData() ).to.equal( '

Hello world!

' ); + + editor.destroy(); + } ); + } ); + + it( 'initializes with config.initialData', () => { + return ClassicEditor.create( editorElement, { + initialData: '

Hello world!

', + plugins: [ Paragraph ] + } ).then( editor => { + expect( editor.getData() ).to.equal( '

Hello world!

' ); + + editor.destroy(); + } ); + } ); + + it( 'throws if initial data is passed in Editor#create and config.initialData is also used', done => { + ClassicEditor.create( '

Hello world!

', { + initialData: '

I am evil!

', + plugins: [ Paragraph ] + } ).catch( () => { + done(); + } ); + } ); + it( 'should have undefined the #sourceElement if editor was initialized with data', () => { return ClassicEditor .create( '

Foo.

', { From 3f1f21f94c4e4191dfa92bab307aa13389036ab7 Mon Sep 17 00:00:00 2001 From: Szymon Cofalik Date: Mon, 25 Mar 2019 18:08:57 +0100 Subject: [PATCH 2/7] Docs: Rewritten docs for `ClassicEditor.create()`. Tests: Fixed manual test. --- src/classiceditor.js | 72 ++++++++++++------------------ tests/manual/classiceditor-data.js | 4 +- 2 files changed, 31 insertions(+), 45 deletions(-) diff --git a/src/classiceditor.js b/src/classiceditor.js index b8433cf..f04134d 100644 --- a/src/classiceditor.js +++ b/src/classiceditor.js @@ -93,32 +93,14 @@ export default class ClassicEditor extends Editor { } /** - * Creates a classic editor instance. + * Creates a `ClassicEditor` instance. * - * Creating an instance when using a {@glink builds/index CKEditor build}: + * There are two general ways how the editor can be initialized. * - * ClassicEditor - * .create( document.querySelector( '#editor' ) ) - * .then( editor => { - * console.log( 'Editor was initialized', editor ); - * } ) - * .catch( err => { - * console.error( err.stack ); - * } ); - * - * Creating an instance when using CKEditor from source (make sure to specify the list of plugins to load and the toolbar): - * - * import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classiceditor'; - * import Essentials from '@ckeditor/ckeditor5-essentials/src/essentials'; - * import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold'; - * import Italic from '@ckeditor/ckeditor5-basic-styles/src/italic'; - * import ... + * You can initialize the editor using an existing DOM element: * * ClassicEditor - * .create( document.querySelector( '#editor' ), { - * plugins: [ Essentials, Bold, Italic, ... ], - * toolbar: [ 'bold', 'italic', ... ] - * } ) + * .create( document.querySelector( '#editor' ) ) * .then( editor => { * console.log( 'Editor was initialized', editor ); * } ) @@ -126,28 +108,27 @@ export default class ClassicEditor extends Editor { * console.error( err.stack ); * } ); * - * Creating an instance when using initial data instead of a DOM element. - * The editor will then render an editable element that must be inserted into the DOM for the editor to work properly: + * This is the most convenient and common way to initialize the editor. The element's content will be used as the editor data. * - * import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classiceditor'; - * import Essentials from '@ckeditor/ckeditor5-essentials/src/essentials'; - * import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold'; - * import Italic from '@ckeditor/ckeditor5-basic-styles/src/italic'; - * import ... + * Alternatively, you can initialize the editor by passing the initial data directly as a `String`. + * In this case, the editor will render an element that must be inserted into the DOM for the editor to work properly: * * ClassicEditor * .create( '

Hello world!

' ) * .then( editor => { * console.log( 'Editor was initialized', editor ); * - * // Initial data was provided so `editor.element` needs to be added manually to the DOM. - * document.body.appendChild( editor.element ); + * // Initial data was provided so the editor UI element needs to be added manually to the DOM. + * document.body.appendChild( editor.ui.element ); * } ) * .catch( err => { * console.error( err.stack ); * } ); * - * Creating an instance on an existing DOM element using external initial content (specified in config): + * This let's you dynamically append the editor to your web page whenever it is convenient for you. You may use this method if your + * web page content is generated on the client-side and the DOM structure is not ready at the moment when you initialize the editor. + * + * You can also mix those two ways by providing a DOM element to be used and passing the initial data through the config: * * ClassicEditor * .create( document.querySelector( '#editor' ), { @@ -160,26 +141,31 @@ export default class ClassicEditor extends Editor { * console.error( err.stack ); * } ); * + * This method can be used to initialize the editor on an existing element with specified content in case if your integration + * makes it difficult to set the content of the source element. + * + * Note that an error will be thrown if you pass initial data both as the first parameter and also in the config. + * + * See also the {@link module:core/editor/editorconfig~EditorConfig editor configuration documentation} to learn more about + * customizing plugins, toolbar and other. + * * @param {HTMLElement|String} sourceElementOrData The DOM element that will be the source for the created editor * or the editor's initial data. * - * If a source element is passed, then its contents will be automatically - * {@link module:editor-classic/classiceditor~ClassicEditor#setData loaded} to the editor on startup + * If a DOM element is passed, its content will be automatically + * {@link module:editor-classic/classiceditor~ClassicEditor#setData loaded} to the editor upon initialization * and the {@link module:core/editor/editorui~EditorUI#getEditableElement editor element} will replace the passed element in the DOM * (the original one will be hidden and the editor will be injected next to it). * - * Moreover, the data will be set back to the source element once the editor is destroyed and - * (if the element is a `