8
0
Просмотр исходного кода

Extend exception marker on spell checking change.

Maciej Gołaszewski 6 лет назад
Родитель
Сommit
b17533ebba

+ 11 - 11
packages/ckeditor5-restricted-editing/src/restrictededitingmode/converters.js

@@ -103,9 +103,9 @@ export function extendMarkerOnTypingPostFixer( editor ) {
 		let changeApplied = false;
 
 		for ( const change of editor.model.document.differ.getChanges() ) {
-			if ( change.type == 'insert' && change.name == '$text' && change.length === 1 ) {
-				changeApplied = _tryExtendMarkerStart( editor, change.position, writer ) || changeApplied;
-				changeApplied = _tryExtendMarkedEnd( editor, change.position, writer ) || changeApplied;
+			if ( change.type == 'insert' && change.name == '$text' ) {
+				changeApplied = _tryExtendMarkerStart( editor, change.position, change.length, writer ) || changeApplied;
+				changeApplied = _tryExtendMarkedEnd( editor, change.position, change.length, writer ) || changeApplied;
 			}
 		}
 
@@ -159,13 +159,13 @@ export function upcastHighlightToMarker( config ) {
 	} );
 }
 
-// Extend marker if typing detected on marker's start position.
-function _tryExtendMarkerStart( editor, position, writer ) {
-	const markerAtStart = getMarkerAtPosition( editor, position.getShiftedBy( 1 ) );
+// Extend marker if change detected on marker's start position.
+function _tryExtendMarkerStart( editor, position, length, writer ) {
+	const markerAtStart = getMarkerAtPosition( editor, position.getShiftedBy( length ) );
 
-	if ( markerAtStart && markerAtStart.getStart().isEqual( position.getShiftedBy( 1 ) ) ) {
+	if ( markerAtStart && markerAtStart.getStart().isEqual( position.getShiftedBy( length ) ) ) {
 		writer.updateMarker( markerAtStart, {
-			range: writer.createRange( markerAtStart.getStart().getShiftedBy( -1 ), markerAtStart.getEnd() )
+			range: writer.createRange( markerAtStart.getStart().getShiftedBy( -length ), markerAtStart.getEnd() )
 		} );
 
 		return true;
@@ -174,13 +174,13 @@ function _tryExtendMarkerStart( editor, position, writer ) {
 	return false;
 }
 
-// Extend marker if typing detected on marker's end position.
-function _tryExtendMarkedEnd( editor, position, writer ) {
+// Extend marker if change detected on marker's end position.
+function _tryExtendMarkedEnd( editor, position, length, writer ) {
 	const markerAtEnd = getMarkerAtPosition( editor, position );
 
 	if ( markerAtEnd && markerAtEnd.getEnd().isEqual( position ) ) {
 		writer.updateMarker( markerAtEnd, {
-			range: writer.createRange( markerAtEnd.getStart(), markerAtEnd.getEnd().getShiftedBy( 1 ) )
+			range: writer.createRange( markerAtEnd.getStart(), markerAtEnd.getEnd().getShiftedBy( length ) )
 		} );
 
 		return true;

+ 60 - 2
packages/ckeditor5-restricted-editing/tests/restrictededitingmodeediting.js

@@ -315,6 +315,64 @@ describe( 'RestrictedEditingModeEditing', () => {
 			expect( markerRange.isEqual( expectedRange ) ).to.be.true;
 		} );
 
+		it( 'should retain marker on non-typing change at the marker boundary (start)', () => {
+			setModelData( model, '<paragraph>foo bar[] baz</paragraph>' );
+			const firstParagraph = model.document.getRoot().getChild( 0 );
+			addExceptionMarker( 4, 7, firstParagraph );
+
+			model.change( writer => {
+				editor.execute( 'delete', {
+					selection: writer.createSelection( writer.createRange(
+						writer.createPositionAt( firstParagraph, 4 ),
+						writer.createPositionAt( firstParagraph, 6 )
+					) )
+				} );
+				editor.execute( 'input', {
+					text: 'XX',
+					range: writer.createRange( writer.createPositionAt( firstParagraph, 4 ) )
+				} );
+			} );
+
+			assertEqualMarkup( getModelData( model ), '<paragraph>foo XX[]r baz</paragraph>' );
+
+			const markerRange = editor.model.markers.get( 'restrictedEditingException:1' ).getRange();
+			const expectedRange = model.createRange(
+				model.createPositionAt( firstParagraph, 4 ),
+				model.createPositionAt( firstParagraph, 7 )
+			);
+
+			expect( markerRange.isEqual( expectedRange ) ).to.be.true;
+		} );
+
+		it( 'should retain marker on non-typing change at marker boundary (end)', () => {
+			setModelData( model, '<paragraph>foo bar[] baz</paragraph>' );
+			const firstParagraph = model.document.getRoot().getChild( 0 );
+			addExceptionMarker( 4, 7, firstParagraph );
+
+			model.change( writer => {
+				editor.execute( 'delete', {
+					selection: writer.createSelection( writer.createRange(
+						writer.createPositionAt( firstParagraph, 5 ),
+						writer.createPositionAt( firstParagraph, 7 )
+					) )
+				} );
+				editor.execute( 'input', {
+					text: 'XX',
+					range: writer.createRange( writer.createPositionAt( firstParagraph, 5 ) )
+				} );
+			} );
+
+			assertEqualMarkup( getModelData( model ), '<paragraph>foo bXX[] baz</paragraph>' );
+
+			const markerRange = editor.model.markers.get( 'restrictedEditingException:1' ).getRange();
+			const expectedRange = model.createRange(
+				model.createPositionAt( firstParagraph, 4 ),
+				model.createPositionAt( firstParagraph, 7 )
+			);
+
+			expect( markerRange.isEqual( expectedRange ) ).to.be.true;
+		} );
+
 		it( 'should not move collapsed marker to $graveyard', () => {
 			setModelData( model, '<paragraph>foo b[]ar baz</paragraph>' );
 			const firstParagraph = model.document.getRoot().getChild( 0 );
@@ -418,7 +476,7 @@ describe( 'RestrictedEditingModeEditing', () => {
 		} );
 
 		it( 'should work with document selection', () => {
-			setModelData( model, '<paragraph>foo [bar] baz</paragraph>' );
+			setModelData( model, '<paragraph>f[oo bar] baz</paragraph>' );
 			const firstParagraph = model.document.getRoot().getChild( 0 );
 
 			addExceptionMarker( 2, 'end', firstParagraph );
@@ -427,7 +485,7 @@ describe( 'RestrictedEditingModeEditing', () => {
 				model.deleteContent( model.document.selection );
 			} );
 
-			assertEqualMarkup( getModelData( model ), '<paragraph>foo [] baz</paragraph>' );
+			assertEqualMarkup( getModelData( model, { withoutSelection: true } ), '<paragraph>fo baz</paragraph>' );
 		} );
 	} );