|
@@ -13,6 +13,8 @@ import ElementReplacer from '@ckeditor/ckeditor5-utils/src/elementreplacer';
|
|
|
import InlineEditableUIView from '@ckeditor/ckeditor5-ui/src/editableui/inline/inlineeditableuiview';
|
|
import InlineEditableUIView from '@ckeditor/ckeditor5-ui/src/editableui/inline/inlineeditableuiview';
|
|
|
import getDataFromElement from '@ckeditor/ckeditor5-utils/src/dom/getdatafromelement';
|
|
import getDataFromElement from '@ckeditor/ckeditor5-utils/src/dom/getdatafromelement';
|
|
|
import mix from '@ckeditor/ckeditor5-utils/src/mix';
|
|
import mix from '@ckeditor/ckeditor5-utils/src/mix';
|
|
|
|
|
+import { isElement } from 'lodash-es';
|
|
|
|
|
+import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* A simplified classic editor. Useful for testing features.
|
|
* A simplified classic editor. Useful for testing features.
|
|
@@ -24,51 +26,66 @@ export default class ClassicTestEditor extends Editor {
|
|
|
/**
|
|
/**
|
|
|
* @inheritDoc
|
|
* @inheritDoc
|
|
|
*/
|
|
*/
|
|
|
- constructor( element, config ) {
|
|
|
|
|
|
|
+ constructor( sourceElementOrData, config ) {
|
|
|
super( config );
|
|
super( config );
|
|
|
|
|
|
|
|
- // The element on which the editor has been initialized.
|
|
|
|
|
- this.element = element;
|
|
|
|
|
|
|
+ if ( isElement( sourceElementOrData ) ) {
|
|
|
|
|
+ this.sourceElement = sourceElementOrData;
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
// Use the HTML data processor in this editor.
|
|
// Use the HTML data processor in this editor.
|
|
|
this.data.processor = new HtmlDataProcessor();
|
|
this.data.processor = new HtmlDataProcessor();
|
|
|
|
|
|
|
|
|
|
+ // Create the ("main") root element of the model tree.
|
|
|
|
|
+ this.model.document.createRoot();
|
|
|
|
|
+
|
|
|
this.ui = new ClassicTestEditorUI( this, new BoxedEditorUIView( this.locale ) );
|
|
this.ui = new ClassicTestEditorUI( this, new BoxedEditorUIView( this.locale ) );
|
|
|
|
|
|
|
|
// Expose properties normally exposed by the ClassicEditorUI.
|
|
// Expose properties normally exposed by the ClassicEditorUI.
|
|
|
this.ui.view.editable = new InlineEditableUIView( this.ui.view.locale, this.editing.view );
|
|
this.ui.view.editable = new InlineEditableUIView( this.ui.view.locale, this.editing.view );
|
|
|
-
|
|
|
|
|
- // Create the ("main") root element of the model tree.
|
|
|
|
|
- this.model.document.createRoot();
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* @inheritDoc
|
|
* @inheritDoc
|
|
|
*/
|
|
*/
|
|
|
destroy() {
|
|
destroy() {
|
|
|
|
|
+ if ( this.sourceElement ) {
|
|
|
|
|
+ this.updateSourceElement();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
this.ui.destroy();
|
|
this.ui.destroy();
|
|
|
|
|
|
|
|
return super.destroy();
|
|
return super.destroy();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * @inheritDoc
|
|
|
|
|
|
|
+ * @param {HTMLElement|String} sourceElementOrData The DOM element that will be the source for the created editor
|
|
|
|
|
+ * or the editor's initial data.
|
|
|
|
|
+ * @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( element, config = {} ) {
|
|
|
|
|
|
|
+ static create( sourceElementOrData, config = {} ) {
|
|
|
return new Promise( resolve => {
|
|
return new Promise( resolve => {
|
|
|
- const editor = new this( element, config );
|
|
|
|
|
|
|
+ const editor = new this( sourceElementOrData, config );
|
|
|
|
|
|
|
|
resolve(
|
|
resolve(
|
|
|
editor.initPlugins()
|
|
editor.initPlugins()
|
|
|
// Simulate EditorUI.init() (e.g. like in ClassicEditorUI). The ui#view
|
|
// Simulate EditorUI.init() (e.g. like in ClassicEditorUI). The ui#view
|
|
|
// should be rendered after plugins are initialized.
|
|
// should be rendered after plugins are initialized.
|
|
|
- .then( () => editor.ui.init( element ) )
|
|
|
|
|
|
|
+ .then( () => editor.ui.init( isElement( sourceElementOrData ) ? sourceElementOrData : null ) )
|
|
|
.then( () => editor.editing.view.attachDomRoot( editor.ui.getEditableElement() ) )
|
|
.then( () => editor.editing.view.attachDomRoot( editor.ui.getEditableElement() ) )
|
|
|
- .then( () => editor.data.init( config.initialData || getDataFromElement( element ) ) )
|
|
|
|
|
.then( () => {
|
|
.then( () => {
|
|
|
- editor.state = 'ready';
|
|
|
|
|
- editor.fire( 'ready' );
|
|
|
|
|
|
|
+ if ( !isElement( sourceElementOrData ) && config.initialData ) {
|
|
|
|
|
+ // Documented in core/editor/editorconfig.jsdoc.
|
|
|
|
|
+ throw new CKEditorError(
|
|
|
|
|
+ 'editor-create-initial-data: ' +
|
|
|
|
|
+ 'The config.initialData option cannot be used together with initial data passed in Editor.create().'
|
|
|
|
|
+ );
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ editor.data.init( config.initialData || getInitialData( sourceElementOrData ) );
|
|
|
} )
|
|
} )
|
|
|
|
|
+ .then( () => editor.fire( 'ready' ) )
|
|
|
.then( () => editor )
|
|
.then( () => editor )
|
|
|
);
|
|
);
|
|
|
} );
|
|
} );
|
|
@@ -104,7 +121,12 @@ class ClassicTestEditorUI extends EditorUI {
|
|
|
return this._view;
|
|
return this._view;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- init( element ) {
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Initializes the UI.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param {HTMLElement|null} replacementElement The DOM element that will be the source for the created editor.
|
|
|
|
|
+ */
|
|
|
|
|
+ init( replacementElement ) {
|
|
|
const view = this.view;
|
|
const view = this.view;
|
|
|
const editable = view.editable;
|
|
const editable = view.editable;
|
|
|
const editingView = this.editor.editing.view;
|
|
const editingView = this.editor.editing.view;
|
|
@@ -118,7 +140,9 @@ class ClassicTestEditorUI extends EditorUI {
|
|
|
|
|
|
|
|
this._editableElements.set( 'main', view.editable.element );
|
|
this._editableElements.set( 'main', view.editable.element );
|
|
|
|
|
|
|
|
- this._elementReplacer.replace( element, view.element );
|
|
|
|
|
|
|
+ if ( replacementElement ) {
|
|
|
|
|
+ this._elementReplacer.replace( replacementElement, view.element );
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
this.fire( 'ready' );
|
|
this.fire( 'ready' );
|
|
|
}
|
|
}
|
|
@@ -137,3 +161,7 @@ class ClassicTestEditorUI extends EditorUI {
|
|
|
|
|
|
|
|
mix( ClassicTestEditor, DataApiMixin );
|
|
mix( ClassicTestEditor, DataApiMixin );
|
|
|
mix( ClassicTestEditor, ElementApiMixin );
|
|
mix( ClassicTestEditor, ElementApiMixin );
|
|
|
|
|
+
|
|
|
|
|
+function getInitialData( sourceElementOrData ) {
|
|
|
|
|
+ return isElement( sourceElementOrData ) ? getDataFromElement( sourceElementOrData ) : sourceElementOrData;
|
|
|
|
|
+}
|