浏览代码

Refactor code to make it clearer.

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

+ 29 - 14
packages/ckeditor5-restricted-editing/src/restrictededitingmodeediting.js

@@ -176,6 +176,7 @@ export default class RestrictedEditingModeEditing extends Plugin {
 	_setupRestrictions() {
 		const editor = this.editor;
 		const model = editor.model;
+		const selection = model.document.selection;
 		const viewDoc = editor.editing.view.document;
 
 		this.listenTo( model, 'deleteContent', restrictDeleteContent( editor ), { priority: 'high' } );
@@ -188,27 +189,23 @@ export default class RestrictedEditingModeEditing extends Plugin {
 			this.listenTo( inputCommand, 'execute', disallowInputExecForWrongRange( editor ), { priority: 'high' } );
 		}
 
-		// Block clipboard completely in restricted mode.
+		// Block clipboard outside exception marker on paste.
 		this.listenTo( viewDoc, 'clipboardInput', function( evt ) {
-			if ( !isRangeInsideSingleMarker( editor, model.document.selection.getFirstRange() ) ) {
+			if ( !isRangeInsideSingleMarker( editor, selection.getFirstRange() ) ) {
 				evt.stop();
 			}
-		}, { priority: 'highest' } );
+		}, { priority: 'high' } );
+
+		// Block clipboard outside exception marker on cut.
 		this.listenTo( viewDoc, 'clipboardOutput', ( evt, data ) => {
-			if ( data.method == 'cut' ) {
-				if ( !isRangeInsideSingleMarker( editor, model.document.selection.getFirstRange() ) ) {
-					evt.stop();
-				}
+			if ( data.method == 'cut' && !isRangeInsideSingleMarker( editor, selection.getFirstRange() ) ) {
+				evt.stop();
 			}
-		}, { priority: 'highest' } );
+		}, { priority: 'high' } );
 
 		const allowedAttributes = editor.config.get( 'restrictedEditing.allowedAttributes' );
-		model.schema.addAttributeCheck( ( context, attributeName ) => allowedAttributes.includes( attributeName ) );
-		model.schema.addChildCheck( ( context, childDefinition ) => {
-			if ( Array.from( context.getNames() ).includes( '$clipboardHolder' ) ) {
-				return childDefinition.name === '$text';
-			}
-		} );
+		model.schema.addAttributeCheck( onlyAllowAttributesFromList( allowedAttributes ) );
+		model.schema.addChildCheck( allowTextOnlyInClipboardHolder );
 	}
 
 	/**
@@ -397,3 +394,21 @@ function isRangeInsideSingleMarker( editor, range ) {
 
 	return markerAtStart && markerAtEnd && markerAtEnd === markerAtStart;
 }
+
+function onlyAllowAttributesFromList( allowedAttributes ) {
+	return ( context, attributeName ) => {
+		if ( isClipboardContext( context ) ) {
+			return allowedAttributes.includes( attributeName );
+		}
+	};
+}
+
+function allowTextOnlyInClipboardHolder( context, childDefinition ) {
+	if ( isClipboardContext( context ) ) {
+		return childDefinition.name === '$text';
+	}
+}
+
+function isClipboardContext( context ) {
+	return Array.from( context.getNames() ).includes( '$clipboardHolder' );
+}