Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Allow to pass initial data to the editor constructor #73

Merged
merged 16 commits into from Jul 3, 2018
Merged

Conversation

szymonkups
Copy link
Contributor

@szymonkups szymonkups commented May 15, 2018

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 as ClassicEditor#sourceElement. See ckeditor/ckeditor5#2882.

Requires: ckeditor/ckeditor5-core#129

@szymonkups szymonkups requested a review from Reinmar May 15, 2018 15:19
@coveralls
Copy link

coveralls commented May 15, 2018

Coverage Status

Coverage decreased (-6.3%) to 93.75% when pulling 46d2f71 on t/72 into ae98cfd on master.

Copy link
Member

@pomek pomek left a 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.

@Reinmar
Copy link
Member

Reinmar commented Jun 6, 2018

* @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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

its contents

*
* 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
Copy link
Member

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

* 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
Copy link
Member

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).
/**
* An HTML element that represents the whole editor (with UI). See the {@link module:core/editor/editorwithui~EditorWithUI} interface.
*
* @returns {HTMLElement|null}
Copy link
Member

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?

@@ -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.
Copy link
Member

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( () => {
Copy link
Member

@Reinmar Reinmar Jun 26, 2018

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 );

Copy link
Member

@Reinmar Reinmar Jun 26, 2018

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.

Copy link
Member

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.

Copy link
Member

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.

editor.data.init( getDataFromElement( editor.sourceElement ) );
} else {
editor.data.init( sourceElementOrData );
editor.sourceElement = editor.ui.view.element;
Copy link
Member

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.

Copy link
Member

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.

@pomek
Copy link
Member

pomek commented Jun 29, 2018

@Reinmar, would you like to check it once again?

@Reinmar
Copy link
Member

Reinmar commented Jun 29, 2018

Here you are :D

@pomek
Copy link
Member

pomek commented Jul 2, 2018

Fixed.

@Reinmar
Copy link
Member

Reinmar commented Jul 2, 2018

I'll be pedantic – it's still not the same as what I proposed. I named the variable initialData on purpose to make it clear that it's not just any data, but the data you load initially. Compared to a simple data it brings more information on what's happening. Just like, not every key in an object is just a key. They are command names, element names, plugin names, keystrokes, whatever.

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.

@pomek
Copy link
Member

pomek commented Jul 2, 2018

When the name is initialData, length of the line is bigger than 140 and it requires additional eslint rules... That's the reason why I'm trying "smuggle" other name.

@Reinmar
Copy link
Member

Reinmar commented Jul 2, 2018

That is the worst possible reason ;) Try formatting that piece of code differently (split into multiple lines if needed).

@Reinmar Reinmar merged commit 09cebc6 into master Jul 3, 2018
@Reinmar Reinmar deleted the t/72 branch July 3, 2018 07:11
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
4 participants