-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
utils.js
70 lines (62 loc) · 2.18 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/**
* @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
/**
* @module restricted-editing/restrictededitingmode/utils
*/
/**
* Returns a single "restricted-editing-exception" marker at a given position. Contrary to
* {@link module:engine/model/markercollection~MarkerCollection#getMarkersAtPosition}, it returnd a marker also when the postion is
* equal to one of the marker's start or end positions.
*
* @param {module:core/editor/editor~Editor} editor
* @param {module:engine/model/position~Position} position
* @returns {module:engine/model/markercollection~Marker|undefined} marker
*/
export function getMarkerAtPosition( editor, position ) {
for ( const marker of editor.model.markers ) {
const markerRange = marker.getRange();
if ( isPositionInRangeBoundaries( markerRange, position ) ) {
if ( marker.name.startsWith( 'restrictedEditingException:' ) ) {
return marker;
}
}
}
}
/**
* Checks if the position is fully contained in the range. Positions equal to range start or end are considered "in".
*
* @param {module:engine/model/range~Range} range
* @param {module:engine/model/position~Position} position
* @returns {Boolean}
*/
export function isPositionInRangeBoundaries( range, position ) {
return (
range.containsPosition( position ) ||
range.end.isEqual( position ) ||
range.start.isEqual( position )
);
}
/**
* Checks if the selection is fully contained in the marker. Positions on marker boundaries are considered "in".
*
* <marker>[]foo</marker> -> true
* <marker>f[oo]</marker> -> true
* <marker>f[oo</marker> ba]r -> false
* <marker>foo</marker> []bar -> false
*
* @param {module:engine/model/selection~Selection} selection
* @param {module:engine/model/markercollection~Marker} marker
* @returns {Boolean}
*/
export function isSelectionInMarker( selection, marker ) {
if ( !marker ) {
return false;
}
const markerRange = marker.getRange();
if ( selection.isCollapsed ) {
return isPositionInRangeBoundaries( markerRange, selection.focus );
}
return markerRange.containsRange( selection.getFirstRange(), true );
}