8
0
Quellcode durchsuchen

Fix typing at the beginning of a marker.

Maciej Gołaszewski vor 6 Jahren
Ursprung
Commit
cd120f8e74

+ 28 - 9
packages/ckeditor5-restricted-editing/src/restrictededitingediting.js

@@ -73,15 +73,8 @@ export default class RestrictedEditingEditing extends Plugin {
 
 			for ( const change of editor.model.document.differ.getChanges() ) {
 				if ( change.type == 'insert' && change.name == '$text' && change.length === 1 ) {
-					const marker = this._getMarker( change.position );
-
-					if ( marker.getEnd().isEqual( change.position ) ) {
-						writer.updateMarker( marker, {
-							range: writer.createRange( marker.getStart(), marker.getEnd().getShiftedBy( 1 ) )
-						} );
-
-						changeApplied = true;
-					}
+					changeApplied = this._tryExtendMarkedEnd( change, writer, changeApplied ) || changeApplied;
+					changeApplied = this._tryExtendMarkerStart( change, writer, changeApplied ) || changeApplied;
 				}
 			}
 
@@ -89,6 +82,32 @@ export default class RestrictedEditingEditing extends Plugin {
 		} );
 	}
 
+	_tryExtendMarkerStart( change, writer, changeApplied ) {
+		const markerAtStart = this._getMarker( change.position.getShiftedBy( 1 ) );
+
+		if ( markerAtStart && markerAtStart.getStart().isEqual( change.position.getShiftedBy( 1 ) ) ) {
+			writer.updateMarker( markerAtStart, {
+				range: writer.createRange( markerAtStart.getStart().getShiftedBy( -1 ), markerAtStart.getEnd() )
+			} );
+
+			changeApplied = true;
+		}
+		return changeApplied;
+	}
+
+	_tryExtendMarkedEnd( change, writer, changeApplied ) {
+		const markerAtEnd = this._getMarker( change.position );
+
+		if ( markerAtEnd && markerAtEnd.getEnd().isEqual( change.position ) ) {
+			writer.updateMarker( markerAtEnd, {
+				range: writer.createRange( markerAtEnd.getStart(), markerAtEnd.getEnd().getShiftedBy( 1 ) )
+			} );
+
+			changeApplied = true;
+		}
+		return changeApplied;
+	}
+
 	_checkCommands() {
 		const editor = this.editor;
 		const selection = editor.model.document.selection;

+ 25 - 0
packages/ckeditor5-restricted-editing/tests/restrictededitingediting.js

@@ -224,6 +224,31 @@ describe( 'RestrictedEditingEditing', () => {
 
 			expect( markerRange.isEqual( expectedRange ) ).to.be.true;
 		} );
+
+		it( 'should extend maker when typing on the marker boundary (start)', () => {
+			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
+				} );
+			} );
+
+			editor.execute( 'input', { text: 'X' } );
+
+			assertEqualMarkup( getModelData( model ), '<paragraph>foo X[]bar baz</paragraph>' );
+			const markerRange = editor.model.markers.get( 'restricted-editing-exception:1' ).getRange();
+
+			const expectedRange = model.createRange(
+				model.createPositionAt( firstParagraph, 4 ),
+				model.createPositionAt( firstParagraph, 8 )
+			);
+
+			expect( markerRange.isEqual( expectedRange ) ).to.be.true;
+		} );
 	} );
 
 	describe( 'commands behavior', () => {