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

Commit

Permalink
Merge pull request #634 from ckeditor/t/259
Browse files Browse the repository at this point in the history
Handling attributes, ranges and their changes in (Live)Selection.
  • Loading branch information
Reinmar authored Oct 18, 2016
2 parents 94746d5 + a5d31ab commit a94116f
Show file tree
Hide file tree
Showing 17 changed files with 926 additions and 263 deletions.
8 changes: 4 additions & 4 deletions src/conversion/view-selection-to-model-converters.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ export function convertSelectionChange( modelDocument, mapper ) {
return ( evt, data ) => {
modelDocument.enqueueChanges( () => {
const viewSelection = data.newSelection;

modelDocument.selection.removeAllRanges();
const ranges = [];

for ( let viewRange of viewSelection.getRanges() ) {
const modelRange = mapper.toModelRange( viewRange );
modelDocument.selection.addRange( modelRange, viewSelection.isBackward );
ranges.push( mapper.toModelRange( viewRange ) );
}

modelDocument.selection.setRanges( ranges, viewSelection.isBackward );
} );
};
}
5 changes: 4 additions & 1 deletion src/dev-utils/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,10 @@ export function setData( document, data, options = {} ) {
}

document.selection.setRanges( ranges, selection.isBackward );
document.selection.setAttributesTo( selection.getAttributes() );

if ( options.selectionAttributes ) {
document.selection.setAttributesTo( selection.getAttributes() );
}
}
} );
}
Expand Down
33 changes: 24 additions & 9 deletions src/model/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import deltas from './delta/basic-deltas.js'; // jshint ignore:line
import transformations from './delta/basic-transformations.js'; // jshint ignore:line

import Range from './range.js';
import Position from './position.js';
import RootElement from './rootelement.js';
import Batch from './batch.js';
import History from './history.js';
Expand Down Expand Up @@ -103,15 +105,6 @@ export default class Document {
*/
this._roots = new Map();

// Add events that will update selection attributes.
this.selection.on( 'change:range', () => {
this.selection._updateAttributes();
} );

this.on( 'changesDone', () => {
this.selection._updateAttributes();
} );

// Add events that will ensure selection correctness.
this.selection.on( 'change:range', () => {
for ( let range of this.selection.getRanges() ) {
Expand Down Expand Up @@ -323,6 +316,28 @@ export default class Document {
return this.graveyard;
}

/**
* Returns a default range for this selection. The default range is a collapsed range that starts and ends
* at the beginning of this selection's document's {@link engine.model.Document#_getDefaultRoot default root}.
*
* @protected
* @returns {engine.model.Range}
*/
_getDefaultRange() {
const defaultRoot = this._getDefaultRoot();

// Find the first position where the selection can be put.
for ( let position of Range.createIn( defaultRoot ).getPositions() ) {
if ( this.schema.check( { name: '$text', inside: position } ) ) {
return new Range( position, position );
}
}

const position = new Position( defaultRoot, [ 0 ] );

return new Range( position, position );
}

/**
* Checks whether given {@link engine.model.Range range} is a valid range for
* {@link engine.model.Document#selection document's selection}.
Expand Down
15 changes: 13 additions & 2 deletions src/model/liverange.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ export default class LiveRange extends Range {
* @param {engine.model.Range} range
* @returns {engine.model.LiveRange}
*/

/**
* Fired when `LiveRange` instance is changed due to changes on {@link engine.model.Document}.
*
* @event engine.model.LiveRange#change
*/
}

/**
Expand Down Expand Up @@ -150,8 +156,13 @@ function transform( type, range, position ) {
break;
}

this.start = updated.start;
this.end = updated.end;
// If anything changed, update the range and fire an event.
if ( !updated.start.isEqual( this.start ) || !updated.end.isEqual( this.end ) ) {
this.start = updated.start;
this.end = updated.end;

this.fire( 'change' );
}
}

mix( LiveRange, EmitterMixin );
Loading

0 comments on commit a94116f

Please sign in to comment.