瀏覽代碼

Created the getTypeAroundFakeCaretPosition() helper.

Aleksander Nowodzinski 5 年之前
父節點
當前提交
8534b16905

+ 19 - 1
packages/ckeditor5-widget/src/widgettypearound/utils.js

@@ -10,6 +10,12 @@
 import { isWidget } from '../utils';
 
 /**
+ * The name of the type around model selection attribute responsible for
+ * displaying a "fake caret" next to a selected widget.
+ */
+export const TYPE_AROUND_SELECTION_ATTRIBUTE = 'widget-type-around';
+
+/**
  * Checks if an element is a widget that qualifies to get the type around UI.
  *
  * @param {module:engine/view/element~Element} viewElement
@@ -37,7 +43,7 @@ export function getClosestTypeAroundDomButton( domElement ) {
  * clicked by the user.
  *
  * @param {HTMLElement} domElement
- * @returns {String} Either `'before'` or `'after'`.
+ * @returns {'before'|'after'} Position of the button.
  */
 export function getTypeAroundButtonPosition( domElement ) {
 	return domElement.classList.contains( 'ck-widget__type-around__button_before' ) ? 'before' : 'after';
@@ -55,3 +61,15 @@ export function getClosestWidgetViewElement( domElement, domConverter ) {
 
 	return domConverter.mapDomToView( widgetDomElement );
 }
+
+/**
+ * For the passed selection instance, it returns the position of the "fake caret" displayed next to a widget.
+ *
+ * **Note**: If the "fake caret" is not currently displayed, `null` is returned.
+ *
+ * @param {module:engine/model/selection~Selection|module:engine/model/documentselection~DocumentSelection} selection
+ * @returns {'before'|'after'|null} Position of the fake caret or `null` when none is preset.
+ */
+export function getTypeAroundFakeCaretPosition( selection ) {
+	return selection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE );
+}

+ 36 - 0
packages/ckeditor5-widget/tests/widgettypearound/utils.js

@@ -0,0 +1,36 @@
+/**
+ * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
+ */
+
+import Selection from '@ckeditor/ckeditor5-engine/src/model/selection';
+import {
+	TYPE_AROUND_SELECTION_ATTRIBUTE,
+	getTypeAroundFakeCaretPosition
+} from '../../src/widgettypearound/utils';
+
+describe( 'widget type around utils', () => {
+	let selection;
+
+	beforeEach( () => {
+		selection = new Selection();
+	} );
+
+	describe( 'getTypeAroundFakeCaretPosition()', () => {
+		it( 'should return "before" if the model selection attribute is "before"', () => {
+			selection.setAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE, 'before' );
+
+			expect( getTypeAroundFakeCaretPosition( selection ) ).to.equal( 'before' );
+		} );
+
+		it( 'should return "after" if the model selection attribute is "after"', () => {
+			selection.setAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE, 'after' );
+
+			expect( getTypeAroundFakeCaretPosition( selection ) ).to.equal( 'after' );
+		} );
+
+		it( 'should return undefined if the model selection attribute is not set', () => {
+			expect( getTypeAroundFakeCaretPosition( selection ) ).to.be.undefined;
+		} );
+	} );
+} );