瀏覽代碼

Fix: Add helper to handle custom keydown behaviour in restricted editing

panr 5 年之前
父節點
當前提交
7c0458f4b6
共有 1 個文件被更改,包括 34 次插入0 次删除
  1. 34 0
      packages/ckeditor5-restricted-editing/src/restrictededitingmodeediting.js

+ 34 - 0
packages/ckeditor5-restricted-editing/src/restrictededitingmodeediting.js

@@ -85,6 +85,9 @@ export default class RestrictedEditingModeEditing extends Plugin {
 		editor.keystrokes.set( 'Tab', getCommandExecuter( editor, 'goToNextRestrictedEditingException' ) );
 		editor.keystrokes.set( 'Shift+Tab', getCommandExecuter( editor, 'goToPreviousRestrictedEditingException' ) );
 
+		// Handle custom keydown behaviour.
+		this.listenTo( editingView.document, 'keydown', ( ...args ) => onKeyDown( editor, ...args ), { priority: 'high' } );
+
 		editingView.change( writer => {
 			for ( const root of editingView.document.roots ) {
 				writer.addClass( 'ck-restricted-editing_mode_restricted', root );
@@ -308,6 +311,37 @@ function getCommandExecuter( editor, commandName ) {
 	};
 }
 
+// Helper for handling custom keydown behaviour.
+export function onKeyDown( editor, eventInfo, evtData ) {
+	const model = editor.model;
+	const selection = model.document.selection;
+	const ctrlA = evtData.ctrlKey && evtData.keyCode === 65;
+
+	// Ctrl+A handler.
+	// If collapsed selection is inside a restricted editing exception, select text only within the exception.
+	//
+	// Note: Second Ctrl+A will select the entire text in the editor.
+	if ( ctrlA ) {
+		const marker = getMarkerAtPosition( editor, selection.focus );
+
+		if ( marker ) {
+			const { start: { offset: selecitonStart }, end: { offset: selecitonEnd } } = selection.getFirstRange();
+			const { start: { offset: markerStart }, end: { offset: markerEnd } } = marker.getRange();
+
+			const selectionIsInsideException = selecitonStart > markerStart && selecitonEnd < markerEnd;
+
+			if ( selectionIsInsideException || selection.isCollapsed ) {
+				evtData.preventDefault();
+				evtData.stopPropagation();
+
+				model.change( writer => {
+					writer.setSelection( marker.getRange() );
+				} );
+			}
+		}
+	}
+}
+
 // 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: