From 5b4414cf5892f4910e0f8e19a56e56ea08b97aa5 Mon Sep 17 00:00:00 2001 From: Szymon Cofalik Date: Tue, 19 Mar 2019 18:08:27 +0100 Subject: [PATCH 1/7] Introduced EditorConfig#initialData. Made `config` param optional. --- src/decouplededitor.js | 40 +++++++++++++++++++++++++++++++++------- tests/decouplededitor.js | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 7 deletions(-) diff --git a/src/decouplededitor.js b/src/decouplededitor.js index 06e65b6..6191418 100644 --- a/src/decouplededitor.js +++ b/src/decouplededitor.js @@ -16,6 +16,7 @@ import getDataFromElement from '@ckeditor/ckeditor5-utils/src/dom/getdatafromele import setDataInElement from '@ckeditor/ckeditor5-utils/src/dom/setdatainelement'; 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#document-editor decoupled editor} implementation. @@ -151,8 +152,8 @@ export default class DecoupledEditor extends Editor { * console.error( err.stack ); * } ); * - * **Note**: It is possible to create the editor out of a pure data string. The editor will then render - * an editable element that must be inserted into the DOM for the editor to work properly: + * 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: * * DecoupledEditor * .create( '

Editor data

' ) @@ -169,6 +170,22 @@ export default class DecoupledEditor extends Editor { * console.error( err.stack ); * } ); * + * Creating an instance on an existing DOM element using external initial content (specified in config): + * + * DecoupledEditor + * .create( document.querySelector( '#editor' ), { + * initialData: '

Initial data

Foo bar.

' + * } ) + * .then( editor => { + * // Append the toolbar to the element. + * document.body.appendChild( editor.ui.view.toolbar.element ); + * + * 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 * (on which the editor will be initialized) or initial data for the editor. * @@ -178,11 +195,11 @@ export default class DecoupledEditor extends Editor { * * If data is provided, then `editor.ui.view.editable.element` will be created automatically and needs to be added * to the DOM manually. - * @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-decoupled/decouplededitor~DecoupledEditor} instance. */ - static create( sourceElementOrData, config ) { + static create( sourceElementOrData, config = {} ) { return new Promise( resolve => { const editor = new this( sourceElementOrData, config ); @@ -192,9 +209,14 @@ export default class DecoupledEditor extends Editor { editor.ui.init(); } ) .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 ); } ) @@ -206,3 +228,7 @@ export default class DecoupledEditor extends Editor { } mix( DecoupledEditor, DataApiMixin ); + +function getInitialData( sourceElementOrData ) { + return isElement( sourceElementOrData ) ? getDataFromElement( sourceElementOrData ) : sourceElementOrData; +} diff --git a/tests/decouplededitor.js b/tests/decouplededitor.js index 611a283..aa94503 100644 --- a/tests/decouplededitor.js +++ b/tests/decouplededitor.js @@ -114,6 +114,26 @@ describe( 'DecoupledEditor', () => { test( () => editableElement ); } ); + it( 'initializes with config.initialData', () => { + return DecoupledEditor.create( document.createElement( 'div' ), { + 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 => { + DecoupledEditor.create( '

Hello world!

', { + initialData: '

I am evil!

', + plugins: [ Paragraph ] + } ).catch( () => { + done(); + } ); + } ); + function test( getElementOrData ) { it( 'creates an instance which inherits from the DecoupledEditor', () => { return DecoupledEditor @@ -139,6 +159,19 @@ describe( 'DecoupledEditor', () => { } ); } ); + it( 'should not require config object', () => { + // Just being safe with `builtinPlugins` static property. + class CustomDecoupledEditor extends DecoupledEditor {} + CustomDecoupledEditor.builtinPlugins = [ Paragraph, Bold ]; + + return CustomDecoupledEditor.create( getElementOrData() ) + .then( newEditor => { + expect( newEditor.getData() ).to.equal( '

foo bar

' ); + + return newEditor.destroy(); + } ); + } ); + // https://github.com/ckeditor/ckeditor5-editor-classic/issues/53 it( 'creates an instance of a DecoupledEditor child class', () => { class CustomDecoupledEditor extends DecoupledEditor {} From 841b443e877762a77003e1d0e389dd5383246013 Mon Sep 17 00:00:00 2001 From: Szymon Cofalik Date: Mon, 25 Mar 2019 18:12:43 +0100 Subject: [PATCH 2/7] Docs: Rewritten docs for `ClassicEditor.create()`. Tests: Fixed manual test. --- src/decouplededitor.js | 80 ++++++++++++++++++++---------------------- 1 file changed, 38 insertions(+), 42 deletions(-) diff --git a/src/decouplededitor.js b/src/decouplededitor.js index 6191418..67dbb6c 100644 --- a/src/decouplededitor.js +++ b/src/decouplededitor.js @@ -113,91 +113,87 @@ export default class DecoupledEditor extends Editor { } /** - * Creates a decoupled editor instance. + * Creates a `DecoupledEditor` instance. * - * Creating an instance when using a {@glink builds/index CKEditor 5 build}: + * There are two general ways how the editor can be initialized. Remember that `DecoupledEditor` do not append the toolbar element + * to your web page so you have to do it manually after the editor has been initialized. + * + * You can initialize the editor using an existing DOM element: * * DecoupledEditor * .create( document.querySelector( '#editor' ) ) * .then( editor => { + * console.log( 'Editor was initialized', editor ); + * * // Append the toolbar to the element. * document.body.appendChild( editor.ui.view.toolbar.element ); - * - * 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): + * The element's content will be used as the editor data. The editable element will replace the source element on your web page. * - * import DecoupledEditor from '@ckeditor/ckeditor5-editor-decoupled/src/decouplededitor'; - * 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, you will have to manually append to your web page both the toolbar element and the editable element. * * DecoupledEditor - * .create( document.querySelector( '#editor' ), { - * plugins: [ Essentials, Bold, Italic, ... ], - * toolbar: [ 'bold', 'italic', ... ] - * } ) + * .create( '

Hello world!

' ) * .then( editor => { - * // Append the toolbar to the element. - * document.body.appendChild( editor.ui.view.toolbar.element ); - * * console.log( 'Editor was initialized', editor ); - * } ) - * .catch( err => { - * 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: * - * DecoupledEditor - * .create( '

Editor data

' ) - * .then( editor => { * // Append the toolbar to the element. * document.body.appendChild( editor.ui.view.toolbar.element ); * - * // Append the editable to the element. - * document.body.appendChild( editor.ui.view.editable.element ); - * - * console.log( 'Editor was initialized', editor ); + * // 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: * * DecoupledEditor * .create( document.querySelector( '#editor' ), { * initialData: '

Initial data

Foo bar.

' * } ) * .then( editor => { + * console.log( 'Editor was initialized', editor ); + * * // Append the toolbar to the element. * document.body.appendChild( editor.ui.view.toolbar.element ); - * - * console.log( 'Editor was initialized', editor ); * } ) * .catch( err => { * 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 - * (on which the editor will be initialized) or initial data for the editor. + * or the editor's initial data. + * + * If a DOM element is passed, its content will be automatically + * {@link module:editor-decoupled/decouplededitor~DecoupledEditor#setData loaded} to the editor upon initialization + * and the {@link module:core/editor/editorui~EditorUI#getEditableElement editable element} will replace the passed element in the DOM + * (the original element will be hidden and the editor will be injected next to it). + * + * Moreover, the editor data will be set back to the original element once the editor is destroyed. * - * If a source element is passed, then its contents will be automatically - * {@link module:editor-decoupled/decouplededitor~DecoupledEditor#setData loaded} to the editor on startup and the element - * itself will be used as the editor's editable element. + * If the initial data is passed, a detached editor will be created. In this case you need to insert it into the DOM manually. + * It is available under {@link module:editor-classic/classiceditorui~DecoupledEditorUI#element `editor.ui.element`} property. * - * If data is provided, then `editor.ui.view.editable.element` will be created automatically and needs to be added - * to the DOM manually. * @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-decoupled/decouplededitor~DecoupledEditor} instance. + * @returns {Promise} A promise resolved once the editor is ready. The promise resolves with the created editor instance. */ static create( sourceElementOrData, config = {} ) { return new Promise( resolve => { From 5c0b574713cae2974968c279cb49395aa89de433 Mon Sep 17 00:00:00 2001 From: Szymon Cofalik Date: Mon, 25 Mar 2019 18:16:56 +0100 Subject: [PATCH 3/7] Docs: Typo. --- src/decouplededitor.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/decouplededitor.js b/src/decouplededitor.js index 67dbb6c..7bd232c 100644 --- a/src/decouplededitor.js +++ b/src/decouplededitor.js @@ -152,7 +152,7 @@ export default class DecoupledEditor extends Editor { * console.error( err.stack ); * } ); * - * 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 + * This lets 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: From d96fe67f56b3bc92b07a144236d7f22616b14107 Mon Sep 17 00:00:00 2001 From: Szymon Cofalik Date: Tue, 26 Mar 2019 17:45:06 +0100 Subject: [PATCH 4/7] Docs: More improvements. --- src/decouplededitor.js | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/decouplededitor.js b/src/decouplededitor.js index 7bd232c..cd766d6 100644 --- a/src/decouplededitor.js +++ b/src/decouplededitor.js @@ -115,8 +115,10 @@ export default class DecoupledEditor extends Editor { /** * Creates a `DecoupledEditor` instance. * - * There are two general ways how the editor can be initialized. Remember that `DecoupledEditor` do not append the toolbar element - * to your web page so you have to do it manually after the editor has been initialized. + * Remember that `DecoupledEditor` do not append the toolbar element to your web page so you have to do it manually after the editor + * has been initialized. + * + * There are two general ways how the editor can be initialized. * * You can initialize the editor using an existing DOM element: * @@ -183,10 +185,7 @@ export default class DecoupledEditor extends Editor { * or the editor's initial data. * * If a DOM element is passed, its content will be automatically - * {@link module:editor-decoupled/decouplededitor~DecoupledEditor#setData loaded} to the editor upon initialization - * and the {@link module:core/editor/editorui~EditorUI#getEditableElement editable element} will replace the passed element in the DOM - * (the original element will be hidden and the editor will be injected next to it). - * + * {@link module:editor-decoupled/decouplededitor~DecoupledEditor#initData loaded} to the editor upon initialization. * Moreover, the editor data will be set back to the original element once the editor is destroyed. * * If the initial data is passed, a detached editor will be created. In this case you need to insert it into the DOM manually. From 4621f402cbe29970c839fa63670439f90f49eee8 Mon Sep 17 00:00:00 2001 From: Szymon Cofalik Date: Wed, 27 Mar 2019 10:26:24 +0100 Subject: [PATCH 5/7] Docs: More improvements. --- src/decouplededitor.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/decouplededitor.js b/src/decouplededitor.js index cd766d6..521a6df 100644 --- a/src/decouplededitor.js +++ b/src/decouplededitor.js @@ -184,12 +184,11 @@ export default class DecoupledEditor extends Editor { * @param {HTMLElement|String} sourceElementOrData The DOM element that will be the source for the created editor * or the editor's initial data. * - * If a DOM element is passed, its content will be automatically - * {@link module:editor-decoupled/decouplededitor~DecoupledEditor#initData loaded} to the editor upon initialization. + * If a DOM element is passed, its content will be automatically loaded to the editor upon initialization. * Moreover, the editor data will be set back to the original element once the editor is destroyed. * * If the initial data is passed, a detached editor will be created. In this case you need to insert it into the DOM manually. - * It is available under {@link module:editor-classic/classiceditorui~DecoupledEditorUI#element `editor.ui.element`} property. + * It is available under {@link module:editor-decoupled/decouplededitorui~DecoupledEditorUI#element `editor.ui.element`} property. * * @param {module:core/editor/editorconfig~EditorConfig} [config] The editor configuration. * @returns {Promise} A promise resolved once the editor is ready. The promise resolves with the created editor instance. From 506c99d292f542df2a073cceca3d7ea9f72cce69 Mon Sep 17 00:00:00 2001 From: Szymon Cofalik Date: Wed, 27 Mar 2019 11:24:44 +0100 Subject: [PATCH 6/7] Docs: More improvements. --- src/decouplededitor.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/decouplededitor.js b/src/decouplededitor.js index 521a6df..96a37d7 100644 --- a/src/decouplededitor.js +++ b/src/decouplededitor.js @@ -134,7 +134,7 @@ export default class DecoupledEditor extends Editor { * console.error( err.stack ); * } ); * - * The element's content will be used as the editor data. The editable element will replace the source element on your web page. + * The element's content will be used as the editor data and it will become the editable element. * * Alternatively, you can initialize the editor by passing the initial data directly as a `String`. * In this case, you will have to manually append to your web page both the toolbar element and the editable element. From c46d454c83dec2c878228d7d787cbf9e54212ae2 Mon Sep 17 00:00:00 2001 From: Szymon Cofalik Date: Wed, 27 Mar 2019 11:32:02 +0100 Subject: [PATCH 7/7] Docs: More improvements. --- src/decouplededitor.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/decouplededitor.js b/src/decouplededitor.js index 96a37d7..62400a8 100644 --- a/src/decouplededitor.js +++ b/src/decouplededitor.js @@ -134,7 +134,7 @@ export default class DecoupledEditor extends Editor { * console.error( err.stack ); * } ); * - * The element's content will be used as the editor data and it will become the editable element. + * The element's content will be used as the editor data and the element will become the editable element. * * Alternatively, you can initialize the editor by passing the initial data directly as a `String`. * In this case, you will have to manually append to your web page both the toolbar element and the editable element.