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

Commit

Permalink
Merge ca11612 into 48bea53
Browse files Browse the repository at this point in the history
  • Loading branch information
scofalik committed Jan 24, 2020
2 parents 48bea53 + ca11612 commit 5e011dd
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/model/documentselection.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export default class DocumentSelection {

this._selection.delegate( 'change:range' ).to( this );
this._selection.delegate( 'change:attribute' ).to( this );
this._selection.delegate( 'change:marker' ).to( this );
}

/**
Expand Down Expand Up @@ -542,6 +543,17 @@ mix( DocumentSelection, EmitterMixin );
* @param {Array.<String>} attributeKeys Array containing keys of attributes that changed.
*/

/**
* Fired when selection marker(s) changed.
*
* @event change:marker
* @param {Boolean} directChange This is always set to `false` in case of `change:marker` event as there is no possibility
* to change markers directly through {@link module:engine/model/documentselection~DocumentSelection} API.
* See also {@link module:engine/model/documentselection~DocumentSelection#event:change:range} and
* {@link module:engine/model/documentselection~DocumentSelection#event:change:attribute}.
* @param {Array.<module:engine/model/markercollection~Marker>} oldMarkers Markers in which the selection was before the change.
*/

// `LiveSelection` is used internally by {@link module:engine/model/documentselection~DocumentSelection} and shouldn't be used directly.
//
// LiveSelection` is automatically updated upon changes in the {@link module:engine/model/document~Document document}
Expand Down Expand Up @@ -721,11 +733,13 @@ class LiveSelection extends Selection {
setTo( selectable, optionsOrPlaceOrOffset, options ) {
super.setTo( selectable, optionsOrPlaceOrOffset, options );
this._updateAttributes( true );
this._updateMarkers();
}

setFocus( itemOrPosition, offset ) {
super.setFocus( itemOrPosition, offset );
this._updateAttributes( true );
this._updateMarkers();
}

setAttribute( key, value ) {
Expand Down Expand Up @@ -830,6 +844,7 @@ class LiveSelection extends Selection {

_updateMarkers() {
const markers = [];
let changed = false;

for ( const marker of this._model.markers ) {
const markerRange = marker.getRange();
Expand All @@ -841,17 +856,27 @@ class LiveSelection extends Selection {
}
}

const oldMarkers = Array.from( this.markers );

for ( const marker of markers ) {
if ( !this.markers.has( marker ) ) {
this.markers.add( marker );

changed = true;
}
}

for ( const marker of Array.from( this.markers ) ) {
if ( !markers.includes( marker ) ) {
this.markers.remove( marker );

changed = true;
}
}

if ( changed ) {
this.fire( 'change:marker', { oldMarkers, directChange: false } );
}
}

// Updates this selection attributes according to its ranges and the {@link module:engine/model/document~Document model document}.
Expand Down
44 changes: 44 additions & 0 deletions tests/model/documentselection.js
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,50 @@ describe( 'DocumentSelection', () => {

expect( selection.markers.map( marker => marker.name ) ).to.have.members( [ 'marker' ] );
} );

it( 'should fire change:marker event when selection markers change', () => {
model.change( writer => {
const spy = sinon.spy();

model.document.selection.on( 'change:marker', spy );

writer.setSelection( writer.createRange(
writer.createPositionFromPath( root, [ 2, 1 ] ),
writer.createPositionFromPath( root, [ 2, 2 ] )
) );

expect( spy.called ).to.be.false;

writer.addMarker( 'marker-1', {
range: writer.createRange(
writer.createPositionFromPath( root, [ 2, 0 ] ),
writer.createPositionFromPath( root, [ 2, 5 ] )
),
usingOperation: false
} );

expect( spy.calledOnce ).to.be.true;

writer.addMarker( 'marker-2', {
range: writer.createRange(
writer.createPositionFromPath( root, [ 2, 0 ] ),
writer.createPositionFromPath( root, [ 2, 3 ] )
),
usingOperation: false
} );

expect( spy.calledTwice ).to.be.true;
spy.resetHistory();

writer.setSelection( writer.createPositionFromPath( root, [ 2, 6 ] ) );

expect( spy.calledOnce ).to.be.true;

writer.setSelection( writer.createPositionFromPath( root, [ 2, 2 ] ) );

expect( spy.calledTwice ).to.be.true;
} );
} );
} );

describe( 'destroy()', () => {
Expand Down

0 comments on commit 5e011dd

Please sign in to comment.