瀏覽代碼

Enable editing inside exception marker.

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

+ 21 - 1
packages/ckeditor5-restricted-editing/src/restrictedediting.js

@@ -51,7 +51,27 @@ export default class RestrictedEditing extends Plugin {
 			} )
 		} );
 
-		for ( const command of this.editor.commands.commands() ) {
+		this._disableCommands( editor );
+
+		const selection = editor.model.document.selection;
+		this.listenTo( selection, 'change', () => {
+			const marker = Array.from( editor.model.markers.getMarkersAtPosition( selection.focus ) )
+				.find( marker => marker.name.startsWith( 'restricted-editing-exception:' ) );
+
+			if ( !marker ) {
+				this._disableCommands( editor );
+
+				return;
+			}
+
+			for ( const command of editor.commands.commands() ) {
+				command.clearForceDisabled( 'RestrictedMode' );
+			}
+		} );
+	}
+
+	_disableCommands( editor ) {
+		for ( const command of editor.commands.commands() ) {
 			command.forceDisabled( 'RestrictedMode' );
 		}
 	}

+ 22 - 2
packages/ckeditor5-restricted-editing/tests/restrictedediting.js

@@ -180,11 +180,31 @@ describe( 'RestrictedEditing', () => {
 		} );
 
 		it( 'should block user typing outside exception markers', () => {
-			setModelData( model, '<paragraph>foo[]bar</paragraph>' );
+			setModelData( model, '<paragraph>foo []bar baz</paragraph>' );
 
 			editor.execute( 'input', { text: 'X' } );
 
-			assertEqualMarkup( getModelData( model ), '<paragraph>foo[]bar</paragraph>' );
+			assertEqualMarkup( getModelData( model ), '<paragraph>foo []bar baz</paragraph>' );
+		} );
+
+		it( 'should not block user typing inside exception marker', () => {
+			setModelData( model, '<paragraph>[]foo bar baz</paragraph>' );
+			const firstParagraph = model.document.getRoot().getChild( 0 );
+
+			model.change( writer => {
+				writer.addMarker( 'restricted-editing-exception:1', {
+					range: writer.createRange( writer.createPositionAt( firstParagraph, 4 ), writer.createPositionAt( firstParagraph, 7 ) ),
+					usingOperation: true,
+					affectsData: true
+				} );
+			} );
+
+			model.change( writer => {
+				writer.setSelection( firstParagraph, 5 );
+			} );
+			editor.execute( 'input', { text: 'X' } );
+
+			assertEqualMarkup( getModelData( model ), '<paragraph>foo bX[]ar baz</paragraph>' );
 		} );
 	} );
 } );