-
Notifications
You must be signed in to change notification settings - Fork 40
T/ckeditor5/1243: Unify widget insertion #1545
Conversation
@pjasiun The code is ready to review. I'll need only to update BREAKING CHANGES entries in associated PRs. |
…ertContent()` needs to create `Selection` instance.
4045745
to
b91dc3b
Compare
src/model/utils/insertcontent.js
Outdated
|
||
if ( !selectable ) { | ||
selection = model.document.selection; | ||
} else if ( selectable instanceof Selection ) { |
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.
or DocumentSelection
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.
Unfortunately DocumentSelection
do not extend Selection
, because it has limited API.
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.
my bad...
src/model/model.js
Outdated
* @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. |
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.
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', () => { | |||
} ); |
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.
Tests should check if the selection was modified.
@pjasiun you can check the updates - I've added some examples to the docs but I'm not 100% sure if they are useful. |
src/model/utils/insertcontent.js
Outdated
* // The selection contains a range to be selected - ie can be used to set selection. | ||
* model.change( ( writer ) => { | ||
* writer.setSelection( selection ); | ||
* } ); |
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 these docs would be more useful for model.insertContent
because this the API users will use.
src/model/utils/insertcontent.js
Outdated
* } ); | ||
* | ||
* // The selection contains a range to be selected - ie can be used to set selection. | ||
* model.change( ( writer ) => { |
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.
Both commands should be in the same model.change
block.
src/model/model.js
Outdated
* 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 ); |
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.
new Text() => writer.createText()
src/model/model.js
Outdated
* 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 ); |
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.
// 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().
…tent()` from `insertContent()`.
6b10326
to
b00ca26
Compare
src/model/model.js
Outdated
* 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] |
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.
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.
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 see that you made this change. So you also need to update these docs.
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.
Still missing the default value in the documentation:
[selectable=model.document.selection]
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.
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.
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.
Yeah - I though about this also - I can make it here though.
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.
tests/model/utils/insertcontent.js
Outdated
expect( selection.isEqual( selectionCopy ) ).to.be.true; | ||
|
||
model.change( () => { | ||
insertContent( model, new Text( 'x' ), selection ); |
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.
Use the writer.
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.
To be honest I've copied tests from other cases so should we change all the tests 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.
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.
src/model/model.js
Outdated
* // Insert text replacing given selection instance. | ||
* const selection = new Selection( new Position( doc.getRoot(), [ 2 ] ), new Position( doc.getRoot(), [ 5 ] ) ); | ||
* | ||
* editor.model.change( ( writer ) => { |
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.
No parenthesis around writer
.
src/model/model.js
Outdated
@@ -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). |
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'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).
src/model/model.js
Outdated
@@ -298,13 +298,29 @@ export default class Model { | |||
* | |||
* editor.model.insertContent( modelFragment, editor.model.document.selection ); |
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.model.document.selection
should be removed, I believe.
src/model/model.js
Outdated
* // Note: This is not necessary when you passed document selection to insertContent(). | ||
* writer.setSelection( selection ); | ||
* } ); | ||
* |
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.
Sample with range/position is missing.
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. |
@Reinmar - sure - I've updated the tests that were creating text inside |
src/model/model.js
Outdated
* 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. |
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.
this method
the document selection
it can also be used with a position, range or selection instance
src/model/model.js
Outdated
* | ||
* 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. |
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 current
src/model/model.js
Outdated
* // 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 ); |
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 bit unnecessary and makes it more visually complex. Plus, there's a mistake (the model
param).
src/model/model.js
Outdated
* // 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. |
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.
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.
src/model/model.js
Outdated
* 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 ] ) ); |
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.
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' );
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.
LGTM
Suggested merge commit message (convention)
Other: The
model.insertContent()
accepts range and position. Closes ckeditor/ckeditor5#1243.Additional information
ckeditor5
repo.