浏览代码

Refactor command toggling methods.

Maciej Gołaszewski 6 年之前
父节点
当前提交
494524f36d
共有 1 个文件被更改,包括 21 次插入24 次删除
  1. 21 24
      packages/ckeditor5-restricted-editing/src/restrictededitingmodeediting.js

+ 21 - 24
packages/ckeditor5-restricted-editing/src/restrictededitingmodeediting.js

@@ -203,37 +203,14 @@ export default class RestrictedEditingModeEditing extends Plugin {
 	_enableCommands( marker ) {
 		const editor = this.editor;
 
-		const exceptionDisable = [];
-
 		const commands = this._getCommandNamesToToggle( editor, this._allowedInException )
 			.filter( name => this._allowedInException.has( name ) )
-			.filter( name => {
-				const selection = editor.model.document.selection;
-				const markerRange = marker.getRange();
-
-				if ( name == 'delete' && markerRange.start.isEqual( selection.focus ) ) {
-					exceptionDisable.push( name );
-
-					return false;
-				}
-
-				if ( name == 'forwardDelete' && selection.isCollapsed && markerRange.end.isEqual( selection.focus ) ) {
-					exceptionDisable.push( name );
-
-					return false;
-				}
-
-				return true;
-			} )
+			.filter( filterDeleteCommandsOnMarkerBoundaries( editor.model.document.selection, marker.getRange() ) )
 			.map( name => editor.commands.get( name ) );
 
 		for ( const command of commands ) {
 			command.clearForceDisabled( 'RestrictedEditingMode' );
 		}
-
-		for ( const command of exceptionDisable.map( name => editor.commands.get( name ) ) ) {
-			command.forceDisabled( 'RestrictedEditingMode' );
-		}
 	}
 
 	/**
@@ -276,3 +253,23 @@ function getCommandExecuter( editor, commandName ) {
 		cancel();
 	};
 }
+
+// Additional filtering rule for enabling "delete" and "forwardDelete" commands if selection is on range boundaries:
+//
+// Does not allow to enable command when selection focus is:
+// - is on marker start - "delete" - to prevent removing content before marker
+// - is on marker end - "forwardDelete" - to prevent removing content after marker
+function filterDeleteCommandsOnMarkerBoundaries( selection, markerRange ) {
+	return name => {
+		if ( name == 'delete' && markerRange.start.isEqual( selection.focus ) ) {
+			return false;
+		}
+
+		// Only for collapsed selection - non-collapsed seleciton that extends over a marker is handled elsewhere.
+		if ( name == 'forwardDelete' && selection.isCollapsed && markerRange.end.isEqual( selection.focus ) ) {
+			return false;
+		}
+
+		return true;
+	};
+}