Explorar o código

Fix usage of markers.

Maciej Gołaszewski %!s(int64=6) %!d(string=hai) anos
pai
achega
af117d63ee

+ 7 - 9
packages/ckeditor5-mention/src/mentionui.js

@@ -329,14 +329,10 @@ export default class MentionUI extends Plugin {
 			const start = focus.getShiftedBy( -matchedTextLength );
 			const end = focus.getShiftedBy( -feedText.length );
 
-			const markerRange = editor.model.createRange( start, end );
+			if ( !editor.model.markers.has( 'mention' ) ) {
+				const markerRange = editor.model.createRange( start, end );
 
-			let mentionMarker;
-
-			if ( editor.model.markers.has( 'mention' ) ) {
-				mentionMarker = editor.model.markers.get( 'mention' );
-			} else {
-				mentionMarker = editor.model.change( writer => writer.addMarker( 'mention', {
+				editor.model.change( writer => writer.addMarker( 'mention', {
 					range: markerRange,
 					usingOperation: false,
 					affectsData: false
@@ -380,8 +376,10 @@ export default class MentionUI extends Plugin {
 						this._items.add( { item, marker } );
 					}
 
-					if ( this._items.length ) {
-						this._showUI( mentionMarker );
+					const markerMarker = editor.model.markers.get( 'mention' );
+
+					if ( this._items.length && markerMarker ) {
+						this._showUI( markerMarker );
 					} else {
 						// Do not show empty mention UI.
 						this._hideUIAndRemoveMarker();

+ 38 - 0
packages/ckeditor5-mention/tests/mentionui.js

@@ -962,6 +962,44 @@ describe( 'MentionUI', () => {
 					expect( data.title ).to.equal( 'Requesting feed failed' );
 				} );
 			} );
+
+			it( 'should not fail if marker was removed', () => {
+				setData( model, '<paragraph>foo []</paragraph>' );
+				const selectFirstMentionSpy = sinon.spy( mentionsView, 'selectFirst' );
+
+				model.change( writer => {
+					writer.insertText( '#', doc.selection.getFirstPosition() );
+				} );
+
+				sinon.assert.notCalled( feedCallbackStub );
+
+				// Increase the response time to extend the debounce time out.
+				feedCallbackTimeout = 500;
+
+				return Promise.resolve()
+				// .then( waitForDebounce )
+					.then( waitForDebounce )
+					.then( wait( 20 ) )
+					.then( () => {
+						model.change( writer => {
+							writer.setSelection( doc.getRoot().getChild( 0 ), 2 );
+						} );
+					} )
+					.then( wait( 20 ) )
+					.then( () => {
+						feedCallbackTimeout = 1000;
+						model.change( writer => {
+							writer.setSelection( doc.getRoot().getChild( 0 ), 'end' );
+						} );
+					} )
+					.then( wait( 500 ) )
+					.then( () => {
+						expect( panelView.isVisible, 'panel is visible' ).to.be.true;
+						// If there were any errors this will not get called.
+						// The errors might come from unhandled promise rejections errors.
+						sinon.assert.calledOnce( selectFirstMentionSpy );
+					} );
+			} );
 		} );
 	} );