-
Notifications
You must be signed in to change notification settings - Fork 3.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Move selection methods to writer #4221
Comments
I reviewed the current selection API and how it is used, and we should simplify it. I propose to introduce a new set of methods: setRange( range );
setRange( range, isBackward );
setRange( [ range1, range2 ], isBackward );
setRange( position );
setRange( item, offset ); // same as https://github.com/ckeditor/ckeditor5-engine/blob/a8e0f3732d2ef6dbc2e604103edb393df8e6b89f/src/model/position.js#L662
setRange( item, 'end' ); // setRange( item, 'before' ); / setRange( item, 'after' );
setRange( null ); // removes all ranges
setRange( selection )
setFocus( itemOrPosition, offset )
removeAttribute( key );
setAttribute( key, value );
setAttribute( { key1: value1, key2: value2 ); Old API to new API porting: addRange( range, isBackward = false ) -> // get ranges as array, add range to the array, set array as ranges
removeAllRanges() -> setRange( null );
setRanges( newRanges, isLastBackward = false ) -> setRange( newRanges, isLastBackward = false );
setTo( selectable ) -> setRange( selection )
setIn( element ) -> setRange( Range.createIn( element ) )
setOn( item ) -> setRange( Range.createOn( item ) );
setCollapsedAt( itemOrPosition, offset ) -> setRange( itemOrPosition, offset )
collapseToStart() -> setRange( selection.getFirstPosition() );
collapseToEnd() -> setRange( selection.getLastPosition() );
moveFocusTo( itemOrPosition, offset ) -> setFocus( itemOrPosition, offset )
removeAttribute( key ) -> removeAttribute( key ); // same
setAttribute( key, value ) -> setAttribute( key, value ); // same
setAttributesTo( attrs ) -> // for ( attr ) removeAttribute; setAttribute( newAttrs );
refreshAttributes() -> // private method The new API should be available for the
writer.setSelection -> selection.setRange
writer.setSelectionFocus -> selection.setFocus
setSelectionAttribute -> selection.setAttribute
removeSelectionAttribute -> selection.removeAttribute You can also remove Note that if during refactoring tests it appears that there is a missing method, feel free to add it. However, think whether that API isn't only needed for tests, are there a least 2 real (not tests) use cases for such additional method. |
The setRange( range );
setRange( range, isBackward );
setRange( [ range1, range2 ], isBackward );
setRange( position );
setRange( item, offset );
setRange( item, 'end' );
setRange( null );
setRange( selection ) In only two cases (per 8) you set it by a range. In most cases you just set it to a given location. Note that the fact that internally selection stores a range (which isn't even true – it keeps live ranges) is an implementation detail. By having a method called Therefore, I'd override one thing in PJ's proposal and call this method setTo( range );
setTo( range, isBackward );
setTo( [ range1, range2 ], isBackward );
setTo( position );
setTo( item, offset );
setTo( item, 'end' );
setTo( null );
setTo( selection ) However, since we have |
There's one more thing which I don't like in this proposal, but I don't want to block it so I moved the discussion to #742. |
I'd stick with the |
I understand that in |
I'm unsure about overloading methods in JSDoc. We haven't done that so far so there would be no consistency and it might not work well (e.g. what about links to methods?). Additionally, I'd be worried that people who're not used to classical OOP (I'm not) would not think to check all forms of The other option is to document it like this:
This will be rather understandable. If properly formatted it will be even more understandable than what we usually have because it would contain examples and examples are gold. However, being understandable for human doesn't make it understandable for machines. I'm sure that many people want to convert JSDoc notation to TypeScripts's definitions and with However, taken all this, I'd not risk overloading methods in JSDoc. |
Yep, it was just my finding and this approach have some downvotes, e.g. two same HTML's ids for same methods, probably lower readability.
I agree, but e.g. I'll stick with the original proposal as it might have fewer cons. |
I'd add setters to the |
The idea behind calling it On the other hand, if it will change attributes to these in the focus position, it makes sense to call it However, So I'm fine with
Yea, I was very excited that we can do it, but then agreed with you that it will be problematic. |
One more question. Should tests to the |
I would test actually document selection. These tests do not need to be as precise as document selection tests but should check if the proper method was called to do what it suppose to do. I believe it is safer to have integration tests in such cases. They will tell us if these methods do what they should do, and should work, even if we will change the implementation in the future. |
Hmm. That makes some sense. I was just surprised that tests to the |
Other: Moved selection methods to `Writer`, introduced `LiveSelection`. Closes #1209.
We decided that writer should be the only proper way to modify model and it should include selection changes. We had plenty of bugs because of the selection changes out of the
enqueueChanges
block, and it should be fixed.At this point we have following methods to modify document selection:
We should use this occasion to rethink if we can limit this list. Then selection methods should be changed to protected (and starts from underscore) and proper writter methods should be added.
At the same time the question is if only document selection modification should be protected or any selection.
The text was updated successfully, but these errors were encountered: