瀏覽代碼

Enable commands on range boundaries.

Maciej Gołaszewski 6 年之前
父節點
當前提交
a4abb0725c

+ 26 - 6
packages/ckeditor5-restricted-editing/src/restrictededitingediting.js

@@ -74,13 +74,11 @@ export default class RestrictedEditingEditing extends Plugin {
 
 			const marker = this._getMarker( editor, selection );
 
-			if ( !marker || !marker.getRange().containsRange( selection.getFirstRange(), true ) ) {
+			if ( isSelectionInExceptionMarker( marker, selection ) ) {
+				this._enableCommands( editor );
+			} else {
 				this._disableCommands( editor );
-
-				return;
 			}
-
-			this._enableCommands( editor );
 		} );
 	}
 
@@ -88,7 +86,7 @@ export default class RestrictedEditingEditing extends Plugin {
 		for ( const marker of this.editor.model.markers ) {
 			const markerRange = marker.getRange();
 
-			if ( markerRange.containsPosition( selection.focus ) || markerRange.end.isEqual( selection.focus ) ) {
+			if ( isPositionInRangeOrOnRangeBoundary( markerRange, selection.focus ) ) {
 				if ( marker.name.startsWith( 'restricted-editing-exception:' ) ) {
 					return marker;
 				}
@@ -155,3 +153,25 @@ function upcastHighlightToMarker( config ) {
 		data.modelCursor = data.modelRange.end;
 	} );
 }
+
+function isSelectionInExceptionMarker( marker, selection ) {
+	if ( !marker ) {
+		return false;
+	}
+
+	const markerRange = marker.getRange();
+
+	if ( selection.isCollapsed ) {
+		return isPositionInRangeOrOnRangeBoundary( markerRange, selection.focus );
+	}
+
+	return markerRange.containsRange( selection.getFirstRange(), true );
+}
+
+function isPositionInRangeOrOnRangeBoundary( range, position ) {
+	return (
+		range.containsPosition( position ) ||
+		range.end.isEqual( position ) ||
+		range.start.isEqual( position )
+	);
+}

+ 18 - 2
packages/ckeditor5-restricted-editing/tests/restrictededitingediting.js

@@ -275,7 +275,7 @@ describe( 'RestrictedEditingEditing', () => {
 				editor.commands.add( 'input', buildFakeCommand( editor ) );
 			} );
 
-			it( 'should be disabled outside exception marker', () => {
+			it( 'should be disabled when caret is outside exception marker', () => {
 				model.change( writer => {
 					writer.setSelection( firstParagraph, 1 );
 				} );
@@ -283,7 +283,7 @@ describe( 'RestrictedEditingEditing', () => {
 				expect( editor.commands.get( 'input' ).isEnabled ).to.be.false;
 			} );
 
-			it( 'should be enabled inside exception marker', () => {
+			it( 'should be enabled when caret is inside exception marker (not touching boundaries)', () => {
 				model.change( writer => {
 					writer.setSelection( firstParagraph, 5 );
 				} );
@@ -291,6 +291,22 @@ describe( 'RestrictedEditingEditing', () => {
 				expect( editor.commands.get( 'input' ).isEnabled ).to.be.true;
 			} );
 
+			it( 'should be enabled when caret is inside exception marker (start boundary)', () => {
+				model.change( writer => {
+					writer.setSelection( firstParagraph, 4 );
+				} );
+
+				expect( editor.commands.get( 'input' ).isEnabled ).to.be.true;
+			} );
+
+			it( 'should be enabled when caret is inside exception marker (end boundary)', () => {
+				model.change( writer => {
+					writer.setSelection( firstParagraph, 7 );
+				} );
+
+				expect( editor.commands.get( 'input' ).isEnabled ).to.be.true;
+			} );
+
 			it( 'should be disabled for non-collapsed selection that expands over exception marker', () => {
 				model.change( writer => {
 					writer.setSelection( writer.createRange(