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

T/ckeditor5/1243: Unify widget insertion #1545

Merged
merged 16 commits into from
Sep 18, 2018
Merged

T/ckeditor5/1243: Unify widget insertion #1545

merged 16 commits into from
Sep 18, 2018

Conversation

jodator
Copy link
Contributor

@jodator jodator commented Sep 13, 2018

Suggested merge commit message (convention)

Other: The model.insertContent() accepts range and position. Closes ckeditor/ckeditor5#1243.


Additional information

Internal: Remove default parameter from `model.insertContent()` calls.
  • Above branch constellation is commited on t/1243 branch on ckeditor5 repo.

@jodator
Copy link
Contributor Author

jodator commented Sep 13, 2018

@pjasiun The code is ready to review. I'll need only to update BREAKING CHANGES entries in associated PRs.

@coveralls
Copy link

coveralls commented Sep 13, 2018

Coverage Status

Coverage remained the same at 100.0% when pulling 3cb0b59 on t/ckeditor5/1243 into ee8eeb3 on master.


if ( !selectable ) {
selection = model.document.selection;
} else if ( selectable instanceof Selection ) {
Copy link

Choose a reason for hiding this comment

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

or DocumentSelection

Copy link

Choose a reason for hiding this comment

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

Unfortunately DocumentSelection do not extend Selection, because it has limited API.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

my bad...

* @param {module:engine/model/selection~Selection|module:engine/model/documentselection~DocumentSelection|
* module:engine/model/position~Position|module:engine/model/element~Element|
* Iterable.<module:engine/model/range~Range>|module:engine/model/range~Range|null} [selectable]
* Selection into which the content should be inserted. If not provided the current model document selection will be used.
Copy link

Choose a reason for hiding this comment

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

You can add a note that if you use a selection object, it will be modified and you will be able to get the insertion selection.

@@ -47,6 +48,52 @@ describe( 'DataController utils', () => {
} );
Copy link

Choose a reason for hiding this comment

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

Tests should check if the selection was modified.

@jodator
Copy link
Contributor Author

jodator commented Sep 14, 2018

@pjasiun you can check the updates - I've added some examples to the docs but I'm not 100% sure if they are useful.

* // The selection contains a range to be selected - ie can be used to set selection.
* model.change( ( writer ) => {
* writer.setSelection( selection );
* } );
Copy link

Choose a reason for hiding this comment

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

I think these docs would be more useful for model.insertContent because this the API users will use.

* } );
*
* // The selection contains a range to be selected - ie can be used to set selection.
* model.change( ( writer ) => {
Copy link

Choose a reason for hiding this comment

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

Both commands should be in the same model.change block.

* const selection = new Selection( new Position( doc.getRoot(), [ 2 ] ), new Position( doc.getRoot(), [ 5 ] ) );
*
* editor.model.change( ( writer ) => {
* editor.model.insertContent( model, new Text( 'x' ), selection );
Copy link
Member

Choose a reason for hiding this comment

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

new Text() => writer.createText()

* editor.model.insertContent( model, new Text( 'x' ), selection );
*
* // The selection contains a range to be selected - ie can be used to set selection.
* writer.setSelection( selection );
Copy link
Member

Choose a reason for hiding this comment

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

// insertContent() modifies the passed selection instance so it can be used to set the document selection.
// Note: This is not necessary when you passed document selection to insertContent().

* Selection into which the content should be inserted.
* @param {module:engine/model/selection~Selection|module:engine/model/documentselection~DocumentSelection|
* module:engine/model/position~Position|module:engine/model/element~Element|
* Iterable.<module:engine/model/range~Range>|module:engine/model/range~Range|null} [selectable]
Copy link
Member

Choose a reason for hiding this comment

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

Wasn't it supposed to default to the document selection?

If yes, make sure to remove editor.model.document.selection from examples in this doc string.

Copy link
Member

Choose a reason for hiding this comment

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

I see that you made this change. So you also need to update these docs.

Copy link
Member

Choose a reason for hiding this comment

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

Still missing the default value in the documentation:

[selectable=model.document.selection]

Copy link
Member

Choose a reason for hiding this comment

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

Bonus – I think that we should define a typedef called Selectable. This param is repeated in many places (e.g. the writer and Selection()). This can be a followup of course.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah - I though about this also - I can make it here though.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

expect( selection.isEqual( selectionCopy ) ).to.be.true;

model.change( () => {
insertContent( model, new Text( 'x' ), selection );
Copy link
Member

Choose a reason for hiding this comment

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

Use the writer.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

To be honest I've copied tests from other cases so should we change all the tests then?

Copy link
Member

Choose a reason for hiding this comment

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

Hm... I think so. Just to make sure it's going to work with how other people will use it and to avoid confusing people who may see our tests.

* // Insert text replacing given selection instance.
* const selection = new Selection( new Position( doc.getRoot(), [ 2 ] ), new Position( doc.getRoot(), [ 5 ] ) );
*
* editor.model.change( ( writer ) => {
Copy link
Member

Choose a reason for hiding this comment

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

No parenthesis around writer.

@@ -298,13 +298,28 @@ export default class Model {
*
* editor.model.insertContent( modelFragment, editor.model.document.selection );
*
* If an instance of {module:engine/model/selection~Selection} is passed as `selectable` it will be modified
* to the insertion selection (equal to a range to be selected after insertion).
Copy link
Member

Choose a reason for hiding this comment

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

I'd propose something like:

If an instance of {module:engine/model/selection~Selection} is passed as selectable it will be moved to the target position (where the document selection should be moved after the insertion).

@@ -298,13 +298,29 @@ export default class Model {
*
* editor.model.insertContent( modelFragment, editor.model.document.selection );
Copy link

Choose a reason for hiding this comment

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

editor.model.document.selection should be removed, I believe.

* // Note: This is not necessary when you passed document selection to insertContent().
* writer.setSelection( selection );
* } );
*
Copy link

Choose a reason for hiding this comment

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

Sample with range/position is missing.

@jodator
Copy link
Contributor Author

jodator commented Sep 14, 2018

@Reinmar & @pjasiun I've updated the docs according to your comments. The remaing thing is with tests & @Reinmar's comment:

use writer

The added test were written similarly to other tests in that file so using writer there should update all the tests?

@Reinmar
Copy link
Member

Reinmar commented Sep 14, 2018

The added test were written similarly to other tests in that file so using writer there should update all the tests?

Hm... I think so. Just to make sure it's going to work with how other people will use it and to avoid confusing people who may see our tests.

@jodator
Copy link
Contributor Author

jodator commented Sep 14, 2018

@Reinmar - sure - I've updated the tests that were creating text inside model.change() blocks. So all issues are resolved.

* editor.model.insertContent( modelFragment, editor.model.document.selection );
* editor.model.insertContent( modelFragment );
*
* By default the method will use document selection but it can be also used with position, range or selection instances.
Copy link
Member

Choose a reason for hiding this comment

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

this method

the document selection

it can also be used with a position, range or selection instance

*
* By default the method will use document selection but it can be also used with position, range or selection instances.
*
* // Insert text at current document selection position.
Copy link
Member

Choose a reason for hiding this comment

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

the current

* // Insert text at current document selection position.
* editor.model.change( writer => {
* editor.model.insertContent( writer.createText( 'x' ) );
* // equal to editor.model.insertContent( model, writer.createText( 'x' ), editor.model.document.selection );
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 bit unnecessary and makes it more visually complex. Plus, there's a mistake (the model param).

* // equal to editor.model.insertContent( model, writer.createText( 'x' ), editor.model.document.selection );
* } );
*
* // Insert text at some position - model's selection will not be modified.
Copy link
Member

Choose a reason for hiding this comment

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

at a given position

document selection


there are two kinds of model selection - static selection and document selection; the one you refer to is called the document selection.

* it will be moved to the target position (where the document selection should be moved after the insertion).
*
* // Insert text replacing given selection instance.
* const selection = new Selection( new Position( doc.getRoot(), [ 2 ] ), new Position( doc.getRoot(), [ 5 ] ) );
Copy link
Member

Choose a reason for hiding this comment

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

Let's maybe use a more human readable method, like – Position.createAt( doc.getRoot(), x ).

Also, I don't think that Selection( pos, pos ) is supported. You need to create a Range. So I'd go with something simpel like:

const selection = new Selection( paragraph, 'in' );

Copy link
Member

@Reinmar Reinmar left a comment

Choose a reason for hiding this comment

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

LGTM

@pjasiun pjasiun merged commit bcdaaa9 into master Sep 18, 2018
@pjasiun pjasiun deleted the t/ckeditor5/1243 branch September 18, 2018 10:57
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