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

Commit

Permalink
Merge 84fa8ba into d00c24c
Browse files Browse the repository at this point in the history
  • Loading branch information
ma2ciek committed Jan 24, 2018
2 parents d00c24c + 84fa8ba commit f0c1912
Show file tree
Hide file tree
Showing 44 changed files with 2,129 additions and 1,942 deletions.
4 changes: 2 additions & 2 deletions src/controller/datacontroller.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,8 @@ export default class DataController {
this.model.enqueueChange( 'transparent', writer => {
// Clearing selection is a workaround for ticket #569 (LiveRange loses position after removing data from document).
// After fixing it this code should be removed.
this.model.document.selection.removeAllRanges();
this.model.document.selection.clearAttributes();
writer.setSelection( null );
writer.removeSelectionAttribute( this.model.document.selection.getAttributeKeys() );

writer.remove( ModelRange.createIn( modelRoot ) );
writer.insert( this.parse( data ), modelRoot );
Expand Down
12 changes: 6 additions & 6 deletions src/conversion/model-selection-to-view-converters.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
* For licensing, see LICENSE.md.
*/

import ViewRange from '../view/range';
import viewWriter from '../view/writer';

/**
Expand Down Expand Up @@ -35,12 +34,14 @@ export function convertRangeSelection() {
return;
}

conversionApi.viewSelection.removeAllRanges();
const viewRanges = [];

for ( const range of selection.getRanges() ) {
const viewRange = conversionApi.mapper.toViewRange( range );
conversionApi.viewSelection.addRange( viewRange, selection.isBackward );
viewRanges.push( viewRange );
}

conversionApi.viewSelection.setTo( viewRanges, selection.isBackward );
};
}

Expand Down Expand Up @@ -82,8 +83,7 @@ export function convertCollapsedSelection() {
const viewPosition = conversionApi.mapper.toViewPosition( modelPosition );
const brokenPosition = viewWriter.breakAttributes( viewPosition );

conversionApi.viewSelection.removeAllRanges();
conversionApi.viewSelection.addRange( new ViewRange( brokenPosition, brokenPosition ) );
conversionApi.viewSelection.setTo( brokenPosition );
};
}

Expand Down Expand Up @@ -122,7 +122,7 @@ export function clearAttributes() {
}
}
}
conversionApi.viewSelection.removeAllRanges();
conversionApi.viewSelection.setTo( null );
};
}

Expand Down
7 changes: 4 additions & 3 deletions src/conversion/model-to-view-converters.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import ViewAttributeElement from '../view/attributeelement';
import ViewText from '../view/text';
import ViewRange from '../view/range';
import viewWriter from '../view/writer';
import DocumentSelection from '../model/documentselection';

/**
* Contains model to view converters for
Expand Down Expand Up @@ -329,7 +330,7 @@ export function wrap( elementCreator ) {
return;
}

if ( data.item instanceof ModelSelection ) {
if ( data.item instanceof ModelSelection || data.item instanceof DocumentSelection ) {
// Selection attribute conversion.
viewWriter.wrap( conversionApi.viewSelection.getFirstRange(), newViewElement, conversionApi.viewSelection );
} else {
Expand Down Expand Up @@ -369,7 +370,7 @@ export function highlightText( highlightDescriptor ) {
return;
}

if ( !( data.item instanceof ModelSelection ) && !data.item.is( 'textProxy' ) ) {
if ( !( data.item instanceof ModelSelection || data.item instanceof DocumentSelection ) && !data.item.is( 'textProxy' ) ) {
return;
}

Expand All @@ -385,7 +386,7 @@ export function highlightText( highlightDescriptor ) {

const viewElement = createViewElementFromHighlightDescriptor( descriptor );

if ( data.item instanceof ModelSelection ) {
if ( data.item instanceof ModelSelection || data.item instanceof DocumentSelection ) {
viewWriter.wrap( conversionApi.viewSelection.getFirstRange(), viewElement, conversionApi.viewSelection );
} else {
const viewRange = conversionApi.mapper.toViewRange( data.range );
Expand Down
6 changes: 3 additions & 3 deletions src/conversion/view-selection-to-model-converters.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ export function convertSelectionChange( model, mapper ) {
ranges.push( mapper.toModelRange( viewRange ) );
}

modelSelection.setRanges( ranges, viewSelection.isBackward );
modelSelection.setTo( ranges, viewSelection.isBackward );

if ( !modelSelection.isEqual( model.document.selection ) ) {
model.change( () => {
model.document.selection.setTo( modelSelection );
model.change( writer => {
writer.setSelection( modelSelection );
} );
}
};
Expand Down
30 changes: 19 additions & 11 deletions src/dev-utils/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import ModelPosition from '../model/position';
import ModelConversionDispatcher from '../conversion/modelconversiondispatcher';
import ModelSelection from '../model/selection';
import ModelDocumentFragment from '../model/documentfragment';
import DocumentSelection from '../model/documentselection';

import ViewConversionDispatcher from '../conversion/viewconversiondispatcher';
import ViewSelection from '../view/selection';
Expand Down Expand Up @@ -46,7 +47,7 @@ import isPlainObject from '@ckeditor/ckeditor5-utils/src/lib/lodash/isPlainObjec
* @param {Object} [options]
* @param {Boolean} [options.withoutSelection=false] Whether to write the selection. When set to `true` selection will
* be not included in returned string.
* @param {Boolean} [options.rootName='main'] Name of the root from which data should be stringified. If not provided
* @param {String} [options.rootName='main'] Name of the root from which data should be stringified. If not provided
* default `main` name will be used.
* @returns {String} The stringified data.
*/
Expand Down Expand Up @@ -114,8 +115,8 @@ export function setData( model, data, options = {} ) {
writer.insert( modelDocumentFragment, modelRoot );

// Clean up previous document selection.
model.document.selection.clearAttributes();
model.document.selection.removeAllRanges();
writer.setSelection( null );
writer.removeSelectionAttribute( model.document.selection.getAttributeKeys() );

// Update document selection if specified.
if ( selection ) {
Expand All @@ -128,10 +129,12 @@ export function setData( model, data, options = {} ) {
ranges.push( new ModelRange( start, end ) );
}

model.document.selection.setRanges( ranges, selection.isBackward );
writer.setSelection( ranges, selection.isBackward );

if ( options.selectionAttributes ) {
model.document.selection.setAttributesTo( selection.getAttributes() );
for ( const [ key, value ] of selection.getAttributes() ) {
writer.setSelectionAttribute( key, value );
}
}
}
} );
Expand Down Expand Up @@ -180,12 +183,14 @@ export function stringify( node, selectionOrPositionOrRange = null ) {
// Get selection from passed selection or position or range if at least one is specified.
if ( selectionOrPositionOrRange instanceof ModelSelection ) {
selection = selectionOrPositionOrRange;
} else if ( selectionOrPositionOrRange instanceof DocumentSelection ) {
selection = selectionOrPositionOrRange;
} else if ( selectionOrPositionOrRange instanceof ModelRange ) {
selection = new ModelSelection();
selection.addRange( selectionOrPositionOrRange );
selection.setTo( selectionOrPositionOrRange );
} else if ( selectionOrPositionOrRange instanceof ModelPosition ) {
selection = new ModelSelection();
selection.addRange( new ModelRange( selectionOrPositionOrRange, selectionOrPositionOrRange ) );
selection.setTo( selectionOrPositionOrRange );
}

// Setup model to view converter.
Expand All @@ -198,7 +203,7 @@ export function stringify( node, selectionOrPositionOrRange = null ) {

modelToView.on( 'insert:$text', insertText() );
modelToView.on( 'attribute', wrap( ( value, data ) => {
if ( data.item instanceof ModelSelection || data.item.is( 'textProxy' ) ) {
if ( data.item instanceof ModelSelection || data.item instanceof DocumentSelection || data.item.is( 'textProxy' ) ) {
return new ViewAttributeElement( 'model-text-with-attributes', { [ data.attributeKey ]: stringifyAttributeValue( value ) } );
}
} ) );
Expand All @@ -216,7 +221,7 @@ export function stringify( node, selectionOrPositionOrRange = null ) {

// Convert model selection to view selection.
if ( selection ) {
modelToView.convertSelection( selection, [] );
modelToView.convertSelection( selection );
}

// Parse view to data string.
Expand Down Expand Up @@ -295,11 +300,14 @@ export function parse( data, schema, options = {} ) {

// Create new selection.
selection = new ModelSelection();
selection.setRanges( ranges, viewSelection.isBackward );
selection.setTo( ranges, viewSelection.isBackward );

// Set attributes to selection if specified.
if ( options.selectionAttributes ) {
selection.setAttributesTo( options.selectionAttributes );
for ( const key in options.selectionAttributes ) {
const value = options.selectionAttributes[ key ];
selection.setAttribute( key, value );
}
}
}

Expand Down
24 changes: 12 additions & 12 deletions src/dev-utils/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ setData._parse = parse;
* const b = new Element( 'b', null, text );
* const p = new Element( 'p', null, b );
* const selection = new Selection();
* selection.addRange( Range.createFromParentsAndOffsets( p, 0, p, 1 ) );
* selection.setTo( Range.createFromParentsAndOffsets( p, 0, p, 1 ) );
*
* stringify( p, selection ); // '<p>[<b>foobar</b>]</p>'
*
Expand All @@ -135,8 +135,7 @@ setData._parse = parse;
* const text = new Text( 'foobar' );
* const b = new Element( 'b', null, text );
* const p = new Element( 'p', null, b );
* const selection = new Selection();
* selection.addRange( Range.createFromParentsAndOffsets( text, 1, text, 5 ) );
* const selection = new Selection( [ Range.createFromParentsAndOffsets( text, 1, text, 5 ) ] );
*
* stringify( p, selection ); // '<p><b>f{ooba}r</b></p>'
*
Expand All @@ -148,8 +147,10 @@ setData._parse = parse;
*
* const text = new Text( 'foobar' );
* const selection = new Selection();
* selection.addRange( Range.createFromParentsAndOffsets( text, 0, text, 1 ) );
* selection.addRange( Range.createFromParentsAndOffsets( text, 3, text, 5 ) );
* selection.setTo( [
Range.createFromParentsAndOffsets( text, 0, text, 1 ) ),
* Range.createFromParentsAndOffsets( text, 3, text, 5 ) )
* ] );
*
* stringify( text, selection ); // '{f}oo{ba}r'
*
Expand Down Expand Up @@ -209,12 +210,12 @@ setData._parse = parse;
export function stringify( node, selectionOrPositionOrRange = null, options = {} ) {
let selection;

if ( selectionOrPositionOrRange instanceof Position ) {
selection = new Selection();
selection.addRange( new Range( selectionOrPositionOrRange, selectionOrPositionOrRange ) );
} else if ( selectionOrPositionOrRange instanceof Range ) {
if (
selectionOrPositionOrRange instanceof Position ||
selectionOrPositionOrRange instanceof Range
) {
selection = new Selection();
selection.addRange( selectionOrPositionOrRange );
selection.setTo( selectionOrPositionOrRange );
} else {
selection = selectionOrPositionOrRange;
}
Expand Down Expand Up @@ -332,8 +333,7 @@ export function parse( data, options = {} ) {

// When ranges are present - return object containing view, and selection.
if ( ranges.length ) {
const selection = new Selection();
selection.setRanges( ranges, !!options.lastRangeBackward );
const selection = new Selection( ranges, !!options.lastRangeBackward );

return {
view,
Expand Down
Loading

0 comments on commit f0c1912

Please sign in to comment.