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

Commit e7f8467

Browse files
authored
Merge pull request #1585 from ckeditor/t/1555
Other: Moved `Position`, `Range` and `Selection` factories from those classes to the model/view writers and `Model`/`View` instances. Previously, those factories were available as static methods of the `Position`, `Range` and `Selection` classes which meant that you needed to import those classes to your plugin's code to create new instances. That required your package to depend on `@ckeditor/ckeditor5-engine` and was not very useful in general. After this change, you can create instances of those classes without importing anything. See the "Breaking changes" section for more details. Closes #1555. BREAKING CHANGE: The model `Position.createAt()` method was removed from the public API. Use `writer.createPositionAt()` instead. This method is also available on the `Model` instance. BREAKING CHANGE: The model `Position.createAfter()` method was removed from the public API. Use `writer.createPositionAfter()` instead. This method is also available on the `Model` instance. BREAKING CHANGE: The model `Position.createBefore()` method was removed from the public API. Use `writer.createPositionBefore()` instead. This method is also available on the `Model` instance. BREAKING CHANGE: The model `Position.createFromPosition()` method was removed. Use `writer.createPositionAt( position )` to create a new position instance. This method is also available on the `Model` instance. BREAKING CHANGE: The model `Position.createFromParentAndOffset()` method was removed. Use `writer.createPositionAt( parent, offset )` instead. This method is also available on the `Model` instance. BREAKING CHANGE: The model `Range.createIn()` method was removed from the public API. Use `writer.createRangeIn()` instead. This method is also available on the `Model` instance. BREAKING CHANGE: The model `Range.createOn()` method was removed from the public API. Use `writer.createRangeOn()` instead. This method is also available on the `Model` instance. BREAKING CHANGE: The model `Range.createFromRange()` method was removed from the public API. BREAKING CHANGE: The model `Range.createFromParentsAndOffsets()` method was removed from the public API. BREAKING CHANGE: The model `Range.createFromPositionAndShift()` method was removed from the public API. BREAKING CHANGE: The model `Range.createCollapsedAt()` method removed method was removed. Use `writer.createRange( position )` to create collapsed range. This method is also available on the `Model` instance. BREAKING CHANGE: The model `Range.createFromRanges()` method was removed from the public API. BREAKING CHANGE: The view `Position.createAt()` method was removed from the public API. Use `writer.createPositionAt()` instead. This method is also available on the `View` instance. BREAKING CHANGE: The view `Position.createAfter()` method was removed from the public API. Use `writer.createPositionAfter()` instead. This method is also available on the `View` instance. BREAKING CHANGE: The view `Position.createBefore()` method was removed from the public API. Use `writer.createPositionBefore()` instead. This method is also available on the `View` instance. BREAKING CHANGE: The view `Position.createFromPosition()` method was removed. Use `writer.createPositionAt( position )` to create a new position instance. This method is also available on the `View` instance. BREAKING CHANGE: The view `Range.createIn()` method was removed from the public API. Use `writer.createRangeIn()` instead. This method is also available on the `View` instance. BREAKING CHANGE: The view `Range.createOn()` method was removed from the public API. Use `writer.createRangeOn()` instead. This method is also available on the `View` instance. BREAKING CHANGE: The view `Range.createFromRange()` method was removed from the public API. BREAKING CHANGE: The view `Range.createFromPositionAndShift()` method was removed from the public API. BREAKING CHANGE: The view `Range.createFromParentsAndOffsets()` method was removed from the public API. BREAKING CHANGE: The view `Range.createCollapsedAt()` method removed method was removed. Use `writer.createRange( position )` to create a collapsed range. This method is also available on the `View` instance.
2 parents a15667a + 15dd8c5 commit e7f8467

File tree

118 files changed

+2922
-1870
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

118 files changed

+2922
-1870
lines changed

src/controller/datacontroller.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ export default class DataController {
149149
this.mapper.clearBindings();
150150

151151
// First, convert elements.
152-
const modelRange = ModelRange.createIn( modelElementOrFragment );
152+
const modelRange = ModelRange._createIn( modelElementOrFragment );
153153

154154
const viewDocumentFragment = new ViewDocumentFragment();
155155

@@ -227,7 +227,7 @@ export default class DataController {
227227
writer.setSelection( null );
228228
writer.removeSelectionAttribute( this.model.document.selection.getAttributeKeys() );
229229

230-
writer.remove( ModelRange.createIn( modelRoot ) );
230+
writer.remove( writer.createRangeIn( modelRoot ) );
231231
writer.insert( this.parse( data, modelRoot ), modelRoot, 0 );
232232
} );
233233
}
@@ -297,7 +297,7 @@ function _getMarkersRelativeToElement( element ) {
297297
return [];
298298
}
299299

300-
const elementRange = ModelRange.createIn( element );
300+
const elementRange = ModelRange._createIn( element );
301301

302302
for ( const marker of doc.model.markers ) {
303303
const intersection = elementRange.getIntersection( marker.getRange() );

src/conversion/downcast-converters.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import ModelSelection from '../model/selection';
88
import ModelElement from '../model/element';
99

1010
import ViewAttributeElement from '../view/attributeelement';
11-
import ViewRange from '../view/range';
1211
import DocumentSelection from '../model/documentselection';
1312

1413
import { cloneDeep } from 'lodash-es';
@@ -535,14 +534,14 @@ export function remove() {
535534
const modelEnd = data.position.getShiftedBy( data.length );
536535
const viewEnd = conversionApi.mapper.toViewPosition( modelEnd, { isPhantom: true } );
537536

538-
const viewRange = new ViewRange( viewStart, viewEnd );
537+
const viewRange = conversionApi.writer.createRange( viewStart, viewEnd );
539538

540539
// Trim the range to remove in case some UI elements are on the view range boundaries.
541540
const removed = conversionApi.writer.remove( viewRange.getTrimmed() );
542541

543542
// After the range is removed, unbind all view elements from the model.
544543
// Range inside view document fragment is used to unbind deeply.
545-
for ( const child of ViewRange.createIn( removed ).getItems() ) {
544+
for ( const child of conversionApi.writer.createRangeIn( removed ).getItems() ) {
546545
conversionApi.mapper.unbindViewElement( child );
547546
}
548547
};
@@ -628,7 +627,7 @@ export function removeUIElement() {
628627
conversionApi.mapper.unbindElementsFromMarkerName( data.markerName );
629628

630629
for ( const element of elements ) {
631-
conversionApi.writer.clear( ViewRange.createOn( element ), element );
630+
conversionApi.writer.clear( conversionApi.writer.createRangeOn( element ), element );
632631
}
633632

634633
conversionApi.writer.clearClonedElementsGroup( data.markerName );
@@ -903,7 +902,7 @@ export function highlightElement( highlightDescriptor ) {
903902
conversionApi.consumable.consume( data.item, evt.name );
904903

905904
// Consume all children nodes.
906-
for ( const value of ModelRange.createIn( data.item ) ) {
905+
for ( const value of ModelRange._createIn( data.item ) ) {
907906
conversionApi.consumable.consume( value.item, evt.name );
908907
}
909908

@@ -965,7 +964,7 @@ export function removeHighlight( highlightDescriptor ) {
965964

966965
for ( const element of elements ) {
967966
if ( element.is( 'attributeElement' ) ) {
968-
conversionApi.writer.unwrap( ViewRange.createOn( element ), viewHighlightElement );
967+
conversionApi.writer.unwrap( conversionApi.writer.createRangeOn( element ), viewHighlightElement );
969968
} else {
970969
// if element.is( 'containerElement' ).
971970
element.getCustomProperty( 'removeHighlight' )( element, descriptor.id, conversionApi.writer );

src/conversion/downcastdispatcher.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ export default class DowncastDispatcher {
131131
// Convert changes that happened on model tree.
132132
for ( const entry of differ.getChanges() ) {
133133
if ( entry.type == 'insert' ) {
134-
this.convertInsert( Range.createFromPositionAndShift( entry.position, entry.length ), writer );
134+
this.convertInsert( Range._createFromPositionAndShift( entry.position, entry.length ), writer );
135135
} else if ( entry.type == 'remove' ) {
136136
this.convertRemove( entry.position, entry.length, entry.name, writer );
137137
} else {
@@ -166,7 +166,7 @@ export default class DowncastDispatcher {
166166
// Fire a separate insert event for each node and text fragment contained in the range.
167167
for ( const value of range ) {
168168
const item = value.item;
169-
const itemRange = Range.createFromPositionAndShift( value.previousPosition, value.length );
169+
const itemRange = Range._createFromPositionAndShift( value.previousPosition, value.length );
170170
const data = {
171171
item,
172172
range: itemRange
@@ -226,7 +226,7 @@ export default class DowncastDispatcher {
226226
// Create a separate attribute event for each node in the range.
227227
for ( const value of range ) {
228228
const item = value.item;
229-
const itemRange = Range.createFromPositionAndShift( value.previousPosition, value.length );
229+
const itemRange = Range._createFromPositionAndShift( value.previousPosition, value.length );
230230
const data = {
231231
item,
232232
range: itemRange,
@@ -343,7 +343,7 @@ export default class DowncastDispatcher {
343343
continue;
344344
}
345345

346-
const data = { item, range: Range.createOn( item ), markerName, markerRange };
346+
const data = { item, range: Range._createOn( item ), markerName, markerRange };
347347

348348
this.fire( eventName, data, this.conversionApi );
349349
}

src/conversion/mapper.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ export default class Mapper {
101101

102102
const modelOffset = this._toModelOffset( data.viewPosition.parent, data.viewPosition.offset, viewBlock );
103103

104-
data.modelPosition = ModelPosition.createFromParentAndOffset( modelParent, modelOffset );
104+
data.modelPosition = ModelPosition._createAt( modelParent, modelOffset );
105105
}, { priority: 'low' } );
106106
}
107107

src/conversion/upcast-converters.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import Matcher from '../view/matcher';
77

88
import ModelRange from '../model/range';
9-
import ModelPosition from '../model/position';
109

1110
import { cloneDeep } from 'lodash-es';
1211

@@ -358,20 +357,20 @@ function _prepareToElementConverter( config ) {
358357
conversionApi.writer.insert( modelElement, splitResult.position );
359358

360359
// Convert children and insert to element.
361-
const childrenResult = conversionApi.convertChildren( data.viewItem, ModelPosition.createAt( modelElement, 0 ) );
360+
const childrenResult = conversionApi.convertChildren( data.viewItem, conversionApi.writer.createPositionAt( modelElement, 0 ) );
362361

363362
// Consume appropriate value from consumable values list.
364363
conversionApi.consumable.consume( data.viewItem, match.match );
365364

366365
// Set conversion result range.
367366
data.modelRange = new ModelRange(
368367
// Range should start before inserted element
369-
ModelPosition.createBefore( modelElement ),
368+
conversionApi.writer.createPositionBefore( modelElement ),
370369
// Should end after but we need to take into consideration that children could split our
371370
// element, so we need to move range after parent of the last converted child.
372371
// before: <allowed>[]</allowed>
373372
// after: <allowed>[<converted><child></child></converted><child></child><converted>]</converted></allowed>
374-
ModelPosition.createAfter( childrenResult.modelCursor.parent )
373+
conversionApi.writer.createPositionAfter( childrenResult.modelCursor.parent )
375374
);
376375

377376
// Now we need to check where the modelCursor should be.
@@ -380,7 +379,7 @@ function _prepareToElementConverter( config ) {
380379
// before: <allowed><notAllowed>[]</notAllowed></allowed>
381380
// after: <allowed><notAllowed></notAllowed><converted></converted><notAllowed>[]</notAllowed></allowed>
382381
if ( splitResult.cursorParent ) {
383-
data.modelCursor = ModelPosition.createAt( splitResult.cursorParent, 0 );
382+
data.modelCursor = conversionApi.writer.createPositionAt( splitResult.cursorParent, 0 );
384383

385384
// Otherwise just continue after inserted element.
386385
} else {
@@ -602,7 +601,7 @@ export function convertText() {
602601

603602
conversionApi.writer.insert( text, data.modelCursor );
604603

605-
data.modelRange = ModelRange.createFromPositionAndShift( data.modelCursor, text.offsetSize );
604+
data.modelRange = ModelRange._createFromPositionAndShift( data.modelCursor, text.offsetSize );
606605
data.modelCursor = data.modelRange.end;
607606
}
608607
}

src/conversion/upcastdispatcher.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,16 @@ import mix from '@ckeditor/ckeditor5-utils/src/mix';
7171
* conversionApi.writer.insert( paragraph, splitResult.position );
7272
*
7373
* // Convert children to paragraph.
74-
* const { modelRange } = conversionApi.convertChildren( data.viewItem, Position.createAt( paragraph, 0 ) );
74+
* const { modelRange } = conversionApi.convertChildren(
75+
* data.viewItem,
76+
* conversionApi.writer.createPositionAt( paragraph, 0 )
77+
* );
7578
*
7679
* // Set as conversion result, attribute converters may use this property.
77-
* data.modelRange = new Range( Position.createBefore( paragraph ), modelRange.end );
80+
* data.modelRange = conversionApi.writer.createRange(
81+
* conversionApi.writer.createPositionBefore( paragraph ),
82+
* modelRange.end
83+
* );
7884
*
7985
* // Continue conversion inside paragraph.
8086
* data.modelCursor = data.modelRange.end;
@@ -372,7 +378,7 @@ function extractMarkersFromModelFragment( modelItem, writer ) {
372378
const markers = new Map();
373379

374380
// Create ModelTreeWalker.
375-
const range = ModelRange.createIn( modelItem ).getItems();
381+
const range = ModelRange._createIn( modelItem ).getItems();
376382

377383
// Walk through DocumentFragment and collect marker elements.
378384
for ( const item of range ) {
@@ -385,14 +391,14 @@ function extractMarkersFromModelFragment( modelItem, writer ) {
385391
// Walk through collected marker elements store its path and remove its from the DocumentFragment.
386392
for ( const markerElement of markerElements ) {
387393
const markerName = markerElement.getAttribute( 'data-name' );
388-
const currentPosition = ModelPosition.createBefore( markerElement );
394+
const currentPosition = writer.createPositionBefore( markerElement );
389395

390396
// When marker of given name is not stored it means that we have found the beginning of the range.
391397
if ( !markers.has( markerName ) ) {
392-
markers.set( markerName, new ModelRange( ModelPosition.createFromPosition( currentPosition ) ) );
398+
markers.set( markerName, new ModelRange( currentPosition.clone() ) );
393399
// Otherwise is means that we have found end of the marker range.
394400
} else {
395-
markers.get( markerName ).end = ModelPosition.createFromPosition( currentPosition );
401+
markers.get( markerName ).end = currentPosition.clone();
396402
}
397403

398404
// Remove marker element from DocumentFragment.
@@ -419,7 +425,7 @@ function createContextTree( contextDefinition, writer ) {
419425
writer.append( current, position );
420426
}
421427

422-
position = ModelPosition.createAt( current, 0 );
428+
position = ModelPosition._createAt( current, 0 );
423429
}
424430

425431
return position;

src/dev-utils/enableenginedebug.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ function enableLoggingTools() {
311311
} );
312312

313313
sandbox.mock( DetachOperation.prototype, 'toString', function() {
314-
const range = ModelRange.createFromPositionAndShift( this.sourcePosition, this.howMany );
314+
const range = ModelRange._createFromPositionAndShift( this.sourcePosition, this.howMany );
315315
const nodes = Array.from( range.getItems() );
316316
const nodeString = nodes.length > 1 ? `[ ${ nodes.length } ]` : nodes[ 0 ];
317317

@@ -330,7 +330,7 @@ function enableLoggingTools() {
330330
} );
331331

332332
sandbox.mock( MoveOperation.prototype, 'toString', function() {
333-
const range = ModelRange.createFromPositionAndShift( this.sourcePosition, this.howMany );
333+
const range = ModelRange._createFromPositionAndShift( this.sourcePosition, this.howMany );
334334

335335
return getClassName( this ) + `( ${ this.baseVersion } ): ${ range } -> ${ this.targetPosition }`;
336336
} );

src/dev-utils/model.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ export function setData( model, data, options = {} ) {
115115

116116
model.change( writer => {
117117
// Replace existing model in document by new one.
118-
writer.remove( ModelRange.createIn( modelRoot ) );
118+
writer.remove( writer.createRangeIn( modelRoot ) );
119119
writer.insert( modelDocumentFragment, modelRoot );
120120

121121
// Clean up previous document selection.
@@ -169,16 +169,16 @@ export function stringify( node, selectionOrPositionOrRange = null, markers = nu
169169

170170
// Create a range witch wraps passed node.
171171
if ( node instanceof RootElement || node instanceof ModelDocumentFragment ) {
172-
range = ModelRange.createIn( node );
172+
range = model.createRangeIn( node );
173173
} else {
174174
// Node is detached - create new document fragment.
175175
if ( !node.parent ) {
176176
const fragment = new ModelDocumentFragment( node );
177-
range = ModelRange.createIn( fragment );
177+
range = model.createRangeIn( fragment );
178178
} else {
179179
range = new ModelRange(
180-
ModelPosition.createBefore( node ),
181-
ModelPosition.createAfter( node )
180+
model.createPositionBefore( node ),
181+
model.createPositionAfter( node )
182182
);
183183
}
184184
}
@@ -391,9 +391,9 @@ function convertToModelElement() {
391391

392392
conversionApi.mapper.bindElements( element, data.viewItem );
393393

394-
conversionApi.convertChildren( data.viewItem, ModelPosition.createAt( element, 0 ) );
394+
conversionApi.convertChildren( data.viewItem, ModelPosition._createAt( element, 0 ) );
395395

396-
data.modelRange = ModelRange.createOn( element );
396+
data.modelRange = ModelRange._createOn( element );
397397
data.modelCursor = data.modelRange.end;
398398

399399
evt.stop();
@@ -420,7 +420,7 @@ function convertToModelText( withAttributes = false ) {
420420

421421
conversionApi.writer.insert( node, data.modelCursor );
422422

423-
data.modelRange = ModelRange.createFromPositionAndShift( data.modelCursor, node.offsetSize );
423+
data.modelRange = ModelRange._createFromPositionAndShift( data.modelCursor, node.offsetSize );
424424
data.modelCursor = data.modelRange.end;
425425

426426
evt.stop();

src/dev-utils/view.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ setData._parse = parse;
138138
* const b = new Element( 'b', null, text );
139139
* const p = new Element( 'p', null, b );
140140
* const selection = new Selection(
141-
* Range.createFromParentsAndOffsets( p, 0, p, 1 )
141+
* Range._createFromParentsAndOffsets( p, 0, p, 1 )
142142
* );
143143
*
144144
* stringify( p, selection ); // '<p>[<b>foobar</b>]</p>'
@@ -148,7 +148,7 @@ setData._parse = parse;
148148
* const text = new Text( 'foobar' );
149149
* const b = new Element( 'b', null, text );
150150
* const p = new Element( 'p', null, b );
151-
* const selection = new Selection( Range.createFromParentsAndOffsets( text, 1, text, 5 ) );
151+
* const selection = new Selection( Range._createFromParentsAndOffsets( text, 1, text, 5 ) );
152152
*
153153
* stringify( p, selection ); // '<p><b>f{ooba}r</b></p>'
154154
*
@@ -161,8 +161,8 @@ setData._parse = parse;
161161
*
162162
* const text = new Text( 'foobar' );
163163
* const selection = new Selection( [
164-
* Range.createFromParentsAndOffsets( text, 0, text, 1 ) ),
165-
* Range.createFromParentsAndOffsets( text, 3, text, 5 ) )
164+
* Range._createFromParentsAndOffsets( text, 0, text, 1 ) ),
165+
* Range._createFromParentsAndOffsets( text, 3, text, 5 ) )
166166
* ] );
167167
*
168168
* stringify( text, selection ); // '{f}oo{ba}r'
@@ -173,7 +173,7 @@ setData._parse = parse;
173173
* be converted to a selection containing one range collapsed at this position.
174174
*
175175
* const text = new Text( 'foobar' );
176-
* const range = Range.createFromParentsAndOffsets( text, 0, text, 1 );
176+
* const range = Range._createFromParentsAndOffsets( text, 0, text, 1 );
177177
* const position = new Position( text, 3 );
178178
*
179179
* stringify( text, range ); // '{f}oobar'

0 commit comments

Comments
 (0)