瀏覽代碼

Merge pull request #51 from ckeditor/t/28

Fix: A single item in the mention dropdown will no longer lose highlight when pressing up or down arrow key.
Marek Lewandowski 6 年之前
父節點
當前提交
9b38dff16b
共有 2 個文件被更改,包括 43 次插入4 次删除
  1. 10 4
      packages/ckeditor5-mention/src/ui/mentionsview.js
  2. 33 0
      packages/ckeditor5-mention/tests/mentionui.js

+ 10 - 4
packages/ckeditor5-mention/src/ui/mentionsview.js

@@ -85,18 +85,24 @@ export default class MentionsView extends ListView {
 		}
 
 		const item = this.items.get( indexToGet );
-		item.highlight();
 
-		// Scroll the mentions view to the selected element.
-		if ( !this._isItemVisibleInScrolledArea( item ) ) {
-			this.element.scrollTop = item.element.offsetTop;
+		// Return early if item is already selected.
+		if ( this.selected === item ) {
+			return;
 		}
 
+		// Remove highlight of previously selected item.
 		if ( this.selected ) {
 			this.selected.removeHighlight();
 		}
 
+		item.highlight();
 		this.selected = item;
+
+		// Scroll the mentions view to the selected element.
+		if ( !this._isItemVisibleInScrolledArea( item ) ) {
+			this.element.scrollTop = item.element.offsetTop;
+		}
 	}
 
 	/**

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

@@ -916,6 +916,39 @@ describe( 'MentionUI', () => {
 							expectChildViewsIsOnState( [ true, false, false, false, false ] );
 						} );
 				} );
+
+				it( 'should not cycle with only one item in the list', () => {
+					setData( model, '<paragraph>foo []</paragraph>' );
+
+					const keyDownEvtData = {
+						keyCode: keyCodes.arrowdown,
+						preventDefault: sinon.spy(),
+						stopPropagation: sinon.spy()
+					};
+
+					const keyUpEvtData = {
+						keyCode: keyCodes.arrowdown,
+						preventDefault: sinon.spy(),
+						stopPropagation: sinon.spy()
+					};
+
+					model.change( writer => {
+						writer.insertText( '@T', doc.selection.getFirstPosition() );
+					} );
+
+					return waitForDebounce()
+						.then( () => {
+							expectChildViewsIsOnState( [ true ] );
+
+							fireKeyDownEvent( keyDownEvtData );
+
+							expectChildViewsIsOnState( [ true ] );
+
+							fireKeyDownEvent( keyUpEvtData );
+
+							expectChildViewsIsOnState( [ true ] );
+						} );
+				} );
 			} );
 
 			describe( 'on "execute" keys', () => {