8
0

utils.js 2.2 KB

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