-
Notifications
You must be signed in to change notification settings - Fork 84
Allow to pass initial data to the editor constructor #73
Conversation
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.
I improved a little the manual test. Except that, everything looks and works fine.
src/classiceditor.js
Outdated
* @param {HTMLElement|String} elementOrData The DOM element that will be the source for the created editor | ||
* or editor's initial data. | ||
* | ||
* If an element is passed, then it contents will be automatically |
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.
its contents
src/classiceditor.js
Outdated
* | ||
* If an element is passed, then it contents will be automatically | ||
* {@link module:editor-classic/classiceditor~ClassicEditor#setData loaded} to the editor on startup | ||
* and the editor element will replace the passed element in the DOM (the original one will be hidden and editor |
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.
"editor element" -> link to the new property
src/classiceditor.js
Outdated
* and the editor element will replace the passed element in the DOM (the original one will be hidden and editor | ||
* will be injected next to it). | ||
* | ||
* Moreover, the data will be set back to the original element once the editor is destroyed and |
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.
original => source
…itorWithUI interface (added editor.element property).
src/classiceditor.js
Outdated
/** | ||
* An HTML element that represents the whole editor (with UI). See the {@link module:core/editor/editorwithui~EditorWithUI} interface. | ||
* | ||
* @returns {HTMLElement|null} |
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.
Can it be a null
here? At what point it's a null
?
src/classiceditor.js
Outdated
@@ -79,15 +83,24 @@ export default class ClassicEditor extends Editor { | |||
attachToForm( this ); | |||
} | |||
|
|||
/** | |||
* An HTML element that represents the whole editor (with UI). See the {@link module:core/editor/editorwithui~EditorWithUI} interface. |
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.
Do we have to redefine this API doc at all?
editor.fire( 'uiReady' ); | ||
} ) | ||
.then( () => editor.editing.view.attachDomRoot( editor.ui.view.editableElement ) ) | ||
.then( () => editor.data.init( getDataFromElement( element ) ) ) | ||
.then( () => { |
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.
It'd be easier to read if it was written like this:
const initialData = isElement( sourceElementOrData ) ? getDataFromElement( sourceElementOrData ) : sourceElementOrData;
editor.data.init( initialData );
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.
The point is – what differs these two scenarios isn't how or whether you call editor.data.init()
. It's how you retrieve this data and from where.
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.
Note that the code you committed isn't equal to what I proposed:
editor.data.init(
isElement( sourceElementOrData ) ? getDataFromElement( sourceElementOrData ) : sourceElementOrData
);
In this case, we lose the information what do we call init()
with. It makes reading the code harder.
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.
BTW, editor.data.init()
may return a promise so this change breaks the promise chain – make sure to add a test for that.
src/classiceditor.js
Outdated
editor.data.init( getDataFromElement( editor.sourceElement ) ); | ||
} else { | ||
editor.data.init( sourceElementOrData ); | ||
editor.sourceElement = editor.ui.view.element; |
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.
I think it's a mistake. Probably a leftover.
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.
BTW, how did tests pass with this? There must be a missing test.
@Reinmar, would you like to check it once again? |
Here you are :D |
Fixed. |
I'll be pedantic – it's still not the same as what I proposed. I named the variable It's not critical here. It's not even a place when one may have some bigger doubts. But did you think about this when changing the name? Cause that should've been intentional and I don't think it was. |
When the name is |
That is the worst possible reason ;) Try formatting that piece of code differently (split into multiple lines if needed). |
Suggested merge commit message (convention)
Feature: Editor can be created with initial data passed to the constructor. Closes ckeditor/ckeditor5#2230.
BREAKING CHANGE:
ClassicEditor#element
is now available asClassicEditor#sourceElement
. See ckeditor/ckeditor5#2882.Requires: ckeditor/ckeditor5-core#129