This repository has been archived by the owner on Jun 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
Introduced EditorConfig#initialData. Made config param optional #30
Merged
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5b4414c
Introduced EditorConfig#initialData. Made `config` param optional.
scofalik 841b443
Docs: Rewritten docs for `ClassicEditor.create()`. Tests: Fixed manua…
scofalik 5c0b574
Docs: Typo.
scofalik d96fe67
Docs: More improvements.
scofalik 4621f40
Docs: More improvements.
scofalik 506c99d
Docs: More improvements.
scofalik c46d454
Docs: More improvements.
scofalik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
|
@@ -112,77 +113,89 @@ 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 <body> 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( '<p>Hello world!</p>' ) | ||
* .then( editor => { | ||
* console.log( 'Editor was initialized', editor ); | ||
* | ||
* // Append the toolbar to the <body> element. | ||
* document.body.appendChild( editor.ui.view.toolbar.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 ); | ||
* } ); | ||
* | ||
* **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: | ||
* 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: | ||
* | ||
* DecoupledEditor | ||
* .create( '<p>Editor data</p>' ) | ||
* .create( document.querySelector( '#editor' ), { | ||
* initialData: '<h2>Initial data</h2><p>Foo bar.</p>' | ||
* } ) | ||
* .then( editor => { | ||
* console.log( 'Editor was initialized', editor ); | ||
* | ||
* // Append the toolbar to the <body> element. | ||
* document.body.appendChild( editor.ui.view.toolbar.element ); | ||
* | ||
* // Append the editable to the <body> element. | ||
* document.body.appendChild( editor.ui.view.editable.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 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 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). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Decoupled also do not use |
||
* | ||
* 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. | ||
* 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. | ||
* | ||
* @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. | ||
*/ | ||
static create( sourceElementOrData, config ) { | ||
static create( sourceElementOrData, config = {} ) { | ||
return new Promise( resolve => { | ||
const editor = new this( sourceElementOrData, config ); | ||
|
||
|
@@ -192,9 +205,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 +224,7 @@ export default class DecoupledEditor extends Editor { | |
} | ||
|
||
mix( DecoupledEditor, DataApiMixin ); | ||
|
||
function getInitialData( sourceElementOrData ) { | ||
return isElement( sourceElementOrData ) ? getDataFromElement( sourceElementOrData ) : sourceElementOrData; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe you should change the order of these 2 sentences?