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

Commit b466e3f

Browse files
author
Piotr Jasiun
authored
Merge pull request #1357 from ckeditor/t/1304
Other: View selection is now split onto Selection and DocumentSelection. Closes #1304 . BREAKING CHANGE: Introduced view.DocumentSelection. It has protected API and can be modified only by view writer. Observers creating instance of selection (like SelectionObserver, MutationObserver) use view.Selection class now.
2 parents 7e88b96 + 0128e07 commit b466e3f

21 files changed

+1765
-236
lines changed

src/conversion/downcast-selection-converters.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@
55

66
/**
77
* Contains {@link module:engine/model/selection~Selection model selection} to
8-
* {@link module:engine/view/selection~Selection view selection} converters for
8+
* {@link module:engine/view/documentselection~DocumentSelection view selection} converters for
99
* {@link module:engine/conversion/downcastdispatcher~DowncastDispatcher}.
1010
*
1111
* @module engine/conversion/downcast-selection-converters
1212
*/
1313

1414
/**
1515
* Function factory, creates a converter that converts non-collapsed {@link module:engine/model/selection~Selection model selection} to
16-
* {@link module:engine/view/selection~Selection view selection}. The converter consumes appropriate value from `consumable` object
17-
* and maps model positions from selection to view positions.
16+
* {@link module:engine/view/documentselection~DocumentSelection view selection}. The converter consumes appropriate
17+
* value from `consumable` object and maps model positions from selection to view positions.
1818
*
1919
* modelDispatcher.on( 'selection', convertRangeSelection() );
2020
*
@@ -45,9 +45,9 @@ export function convertRangeSelection() {
4545

4646
/**
4747
* Function factory, creates a converter that converts collapsed {@link module:engine/model/selection~Selection model selection} to
48-
* {@link module:engine/view/selection~Selection view selection}. The converter consumes appropriate value from `consumable` object,
49-
* maps model selection position to view position and breaks {@link module:engine/view/attributeelement~AttributeElement attribute elements}
50-
* at the selection position.
48+
* {@link module:engine/view/documentselection~DocumentSelection view selection}. The converter consumes appropriate
49+
* value from `consumable` object, maps model selection position to view position and breaks
50+
* {@link module:engine/view/attributeelement~AttributeElement attribute elements} at the selection position.
5151
*
5252
* modelDispatcher.on( 'selection', convertCollapsedSelection() );
5353
*

src/conversion/upcast-selection-converters.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*/
55

66
/**
7-
* Contains {@link module:engine/view/selection~Selection view selection}
7+
* Contains {@link module:engine/view/documentselection~DocumentSelection view selection}
88
* to {@link module:engine/model/selection~Selection model selection} conversion helpers.
99
*
1010
* @module engine/conversion/upcast-selection-converters
@@ -13,8 +13,8 @@
1313
import ModelSelection from '../model/selection';
1414

1515
/**
16-
* Function factory, creates a callback function which converts a {@link module:engine/view/selection~Selection view selection} taken
17-
* from the {@link module:engine/view/document~Document#event:selectionChange} event
16+
* Function factory, creates a callback function which converts a {@link module:engine/view/selection~Selection
17+
* view selection} taken from the {@link module:engine/view/document~Document#event:selectionChange} event
1818
* and sets in on the {@link module:engine/model/document~Document#selection model}.
1919
*
2020
* **Note**: because there is no view selection change dispatcher nor any other advanced view selection to model

src/dev-utils/view.js

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import View from '../view/view';
1515
import ViewDocumentFragment from '../view/documentfragment';
1616
import XmlDataProcessor from '../dataprocessor/xmldataprocessor';
1717
import ViewElement from '../view/element';
18-
import Selection from '../view/selection';
18+
import DocumentSelection from '../view/documentselection';
1919
import Range from '../view/range';
2020
import Position from '../view/position';
2121
import AttributeElement from '../view/attributeelement';
@@ -125,8 +125,8 @@ setData._parse = parse;
125125
*
126126
* stringify( fragment ); // '<p style="color:red;"></p><b name="test">foobar</b>'
127127
*
128-
* Additionally, a {@link module:engine/view/selection~Selection selection} instance can be provided. Ranges from the selection
129-
* will then be included in output data.
128+
* Additionally, a {@link module:engine/view/documentselection~DocumentSelection selection} instance can be provided.
129+
* Ranges from the selection will then be included in output data.
130130
* If a range position is placed inside the element node, it will be represented with `[` and `]`:
131131
*
132132
* const text = new Text( 'foobar' );
@@ -163,9 +163,9 @@ setData._parse = parse;
163163
* stringify( text, selection ); // '{f}oo{ba}r'
164164
*
165165
* A {@link module:engine/view/range~Range range} or {@link module:engine/view/position~Position position} instance can be provided
166-
* instead of the {@link module:engine/view/selection~Selection selection} instance. If a range instance is provided, it will be
167-
* converted to a selection containing this range. If a position instance is provided, it will be converted to a selection
168-
* containing one range collapsed at this position.
166+
* instead of the {@link module:engine/view/documentselection~DocumentSelection selection} instance. If a range instance
167+
* is provided, it will be converted to a selection containing this range. If a position instance is provided, it will
168+
* be converted to a selection containing one range collapsed at this position.
169169
*
170170
* const text = new Text( 'foobar' );
171171
* const range = Range.createFromParentsAndOffsets( text, 0, text, 1 );
@@ -206,7 +206,7 @@ setData._parse = parse;
206206
*
207207
* @param {module:engine/view/text~Text|module:engine/view/element~Element|module:engine/view/documentfragment~DocumentFragment}
208208
* node The node to stringify.
209-
* @param {module:engine/view/selection~Selection|module:engine/view/position~Position|module:engine/view/range~Range}
209+
* @param {module:engine/view/documentselection~DocumentSelection|module:engine/view/position~Position|module:engine/view/range~Range}
210210
* [selectionOrPositionOrRange = null ]
211211
* A selection instance whose ranges will be included in the returned string data. If a range instance is provided, it will be
212212
* converted to a selection containing this range. If a position instance is provided, it will be converted to a selection
@@ -231,7 +231,7 @@ export function stringify( node, selectionOrPositionOrRange = null, options = {}
231231
selectionOrPositionOrRange instanceof Position ||
232232
selectionOrPositionOrRange instanceof Range
233233
) {
234-
selection = new Selection( selectionOrPositionOrRange );
234+
selection = new DocumentSelection( selectionOrPositionOrRange );
235235
} else {
236236
selection = selectionOrPositionOrRange;
237237
}
@@ -257,7 +257,7 @@ export function stringify( node, selectionOrPositionOrRange = null, options = {}
257257
* parse( '<b>foo</b><i>bar</i>' ); // Returns a document fragment with two child elements.
258258
*
259259
* The method can parse multiple {@link module:engine/view/range~Range ranges} provided in string data and return a
260-
* {@link module:engine/view/selection~Selection selection} instance containing these ranges. Ranges placed inside
260+
* {@link module:engine/view/documentselection~DocumentSelection selection} instance containing these ranges. Ranges placed inside
261261
* {@link module:engine/view/text~Text text} nodes should be marked using `{` and `}` brackets:
262262
*
263263
* const { text, selection } = parse( 'f{ooba}r' );
@@ -278,8 +278,9 @@ export function stringify( node, selectionOrPositionOrRange = null, options = {}
278278
* In the example above, the first range (`{fo}`) will be added to the selection as the second one, the second range (`{ar}`) will be
279279
* added as the third and the third range (`{ba}`) will be added as the first one.
280280
*
281-
* If the selection's last range should be added as a backward one (so the {@link module:engine/view/selection~Selection#anchor selection
282-
* anchor} is represented by the `end` position and {@link module:engine/view/selection~Selection#focus selection focus} is
281+
* If the selection's last range should be added as a backward one
282+
* (so the {@link module:engine/view/documentselection~DocumentSelection#anchor selection anchor} is represented
283+
* by the `end` position and {@link module:engine/view/documentselection~DocumentSelection#focus selection focus} is
283284
* represented by the `start` position), use the `lastRangeBackward` flag:
284285
*
285286
* const { root, selection } = parse( `{foo}bar{baz}`, { lastRangeBackward: true } );
@@ -298,11 +299,11 @@ export function stringify( node, selectionOrPositionOrRange = null, options = {}
298299
* @param {String} data An HTML-like string to be parsed.
299300
* @param {Object} options
300301
* @param {Array.<Number>} [options.order] An array with the order of parsed ranges added to the returned
301-
* {@link module:engine/view/selection~Selection Selection} instance. Each element should represent the desired position of each range in
302-
* the selection instance. For example: `[2, 3, 1]` means that the first range will be placed as the second, the second as the third and
303-
* the third as the first.
302+
* {@link module:engine/view/documentselection~DocumentSelection Selection} instance. Each element should represent the
303+
* desired position of each range in the selection instance. For example: `[2, 3, 1]` means that the first range will be
304+
* placed as the second, the second as the third and the third as the first.
304305
* @param {Boolean} [options.lastRangeBackward=false] If set to `true`, the last range will be added as backward to the returned
305-
* {@link module:engine/view/selection~Selection selection} instance.
306+
* {@link module:engine/view/documentselection~DocumentSelection selection} instance.
306307
* @param {module:engine/view/element~Element|module:engine/view/documentfragment~DocumentFragment}
307308
* [options.rootElement=null] The default root to use when parsing elements.
308309
* When set to `null`, the root element will be created automatically. If set to
@@ -351,7 +352,7 @@ export function parse( data, options = {} ) {
351352

352353
// When ranges are present - return object containing view, and selection.
353354
if ( ranges.length ) {
354-
const selection = new Selection( ranges, { backward: !!options.lastRangeBackward } );
355+
const selection = new DocumentSelection( ranges, { backward: !!options.lastRangeBackward } );
355356

356357
return {
357358
view,
@@ -594,7 +595,8 @@ class ViewStringify {
594595
* Creates a view stringify instance.
595596
*
596597
* @param root
597-
* @param {module:engine/view/selection~Selection} selection A selection whose ranges should also be converted to a string.
598+
* @param {module:engine/view/documentselection~DocumentSelection} selection A selection whose ranges
599+
* should also be converted to a string.
598600
* @param {Object} options An options object.
599601
* @param {Boolean} [options.showType=false] When set to `true`, the type of elements will be printed (`<container:p>`
600602
* instead of `<p>`, `<attribute:b>` instead of `<b>` and `<empty:img>` instead of `<img>`).

src/view/document.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77
* @module engine/view/document
88
*/
99

10-
import Selection from './selection';
10+
import DocumentSelection from './documentselection';
1111
import Collection from '@ckeditor/ckeditor5-utils/src/collection';
1212
import mix from '@ckeditor/ckeditor5-utils/src/mix';
1313
import ObservableMixin from '@ckeditor/ckeditor5-utils/src/observablemixin';
1414

1515
/**
1616
* Document class creates an abstract layer over the content editable area, contains a tree of view elements and
17-
* {@link module:engine/view/selection~Selection view selection} associated with this document.
17+
* {@link module:engine/view/documentselection~DocumentSelection view selection} associated with this document.
1818
*
1919
* @mixes module:utils/observablemixin~ObservableMixin
2020
*/
@@ -27,9 +27,9 @@ export default class Document {
2727
* Selection done on this document.
2828
*
2929
* @readonly
30-
* @member {module:engine/view/selection~Selection} module:engine/view/document~Document#selection
30+
* @member {module:engine/view/documentselection~DocumentSelection} module:engine/view/document~Document#selection
3131
*/
32-
this.selection = new Selection();
32+
this.selection = new DocumentSelection();
3333

3434
/**
3535
* Roots of the view tree. Collection of the {module:engine/view/element~Element view elements}.

0 commit comments

Comments
 (0)