瀏覽代碼

Feature: Remove entire exception when collapsed selection is inside text with restricted attribute

panr 6 年之前
父節點
當前提交
dbab950552

+ 21 - 3
packages/ckeditor5-restricted-editing/src/restrictededitingexceptioncommand.js

@@ -38,10 +38,28 @@ export default class RestrictedEditingExceptionCommand extends Command {
 			const ranges = model.schema.getValidRanges( selection.getRanges(), 'restrictedEditingException' );
 
 			if ( selection.isCollapsed ) {
-				if ( valueToSet ) {
-					writer.setSelectionAttribute( 'restrictedEditingException', true );
-				} else {
+				const position = selection.getFirstPosition();
+
+				// When selection is inside restricted text
+				if ( selection.hasAttribute( 'restrictedEditingException' ) ) {
+					// Find the full resticted range
+					const isSameRestrictedException = value => {
+						return value.item.hasAttribute( 'restrictedEditingException' ) &&
+						value.item.getAttribute( 'restrictedEditingException' ) === this.value;
+					};
+
+					const restrictedEditingExceptionStart = position.getLastMatchingPosition( isSameRestrictedException,
+						{ direction: 'backward' } );
+					const restrictedEditingExceptionEnd = position.getLastMatchingPosition( isSameRestrictedException );
+
+					const restrictedEditingExceptionRange = writer.createRange( restrictedEditingExceptionStart,
+						restrictedEditingExceptionEnd );
+
+					writer.removeAttribute( 'restrictedEditingException', restrictedEditingExceptionRange );
 					writer.removeSelectionAttribute( 'restrictedEditingException' );
+				} else if ( valueToSet ) {
+					// Set attribute on selection with unset attribute
+					writer.setSelectionAttribute( 'restrictedEditingException', valueToSet );
 				}
 			} else {
 				for ( const range of ranges ) {

+ 9 - 0
packages/ckeditor5-restricted-editing/tests/restrictededitingexceptioncommand.js

@@ -124,6 +124,15 @@ describe( 'RestrictedEditingExceptionCommand', () => {
 
 				expect( model.document.selection.hasAttribute( 'restrictedEditingException' ) ).to.be.false;
 			} );
+
+			it( 'should remove entire selection attribute when inside restricted text', () => {
+				setData( model, '<p>abc<$text restrictedEditingException="true">foo[]bar</$text>baz</p>' );
+
+				command.execute();
+
+				expect( model.document.selection.hasAttribute( 'restrictedEditingException' ) ).to.be.false;
+				expect( getData( model ) ).to.equal( '<p>abcfoo[]barbaz</p>' );
+			} );
 		} );
 
 		describe( 'non-collapsed selection', () => {