|
|
@@ -16,11 +16,14 @@ import {
|
|
|
getLabel,
|
|
|
toWidgetEditable,
|
|
|
setHighlightHandling,
|
|
|
+ findOptimalInsertionPosition,
|
|
|
WIDGET_CLASS_NAME
|
|
|
} from '../src/utils';
|
|
|
import UIElement from '@ckeditor/ckeditor5-engine/src/view/uielement';
|
|
|
import env from '@ckeditor/ckeditor5-utils/src/env';
|
|
|
import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
|
|
|
+import Model from '@ckeditor/ckeditor5-engine/src/model/model';
|
|
|
+import { setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
|
|
|
|
|
|
describe( 'widget utils', () => {
|
|
|
let element, writer, viewDocument;
|
|
|
@@ -337,4 +340,85 @@ describe( 'widget utils', () => {
|
|
|
expect( addSpy.secondCall.args[ 1 ] ).to.equal( secondDescriptor );
|
|
|
} );
|
|
|
} );
|
|
|
+
|
|
|
+ describe( 'findOptimalInsertionPosition()', () => {
|
|
|
+ let model, doc;
|
|
|
+
|
|
|
+ beforeEach( () => {
|
|
|
+ model = new Model();
|
|
|
+ doc = model.document;
|
|
|
+
|
|
|
+ doc.createRoot();
|
|
|
+
|
|
|
+ model.schema.register( 'paragraph', { inheritAllFrom: '$block' } );
|
|
|
+ model.schema.register( 'image' );
|
|
|
+ model.schema.register( 'span' );
|
|
|
+
|
|
|
+ model.schema.extend( 'image', {
|
|
|
+ allowIn: '$root',
|
|
|
+ isObject: true
|
|
|
+ } );
|
|
|
+
|
|
|
+ model.schema.extend( 'span', { allowIn: 'paragraph' } );
|
|
|
+ model.schema.extend( '$text', { allowIn: 'span' } );
|
|
|
+ } );
|
|
|
+
|
|
|
+ it( 'returns position after selected element', () => {
|
|
|
+ setData( model, '<paragraph>x</paragraph>[<image></image>]<paragraph>y</paragraph>' );
|
|
|
+
|
|
|
+ const pos = findOptimalInsertionPosition( doc.selection );
|
|
|
+
|
|
|
+ expect( pos.path ).to.deep.equal( [ 2 ] );
|
|
|
+ } );
|
|
|
+
|
|
|
+ it( 'returns position inside empty block', () => {
|
|
|
+ setData( model, '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>y</paragraph>' );
|
|
|
+
|
|
|
+ const pos = findOptimalInsertionPosition( doc.selection );
|
|
|
+
|
|
|
+ expect( pos.path ).to.deep.equal( [ 1, 0 ] );
|
|
|
+ } );
|
|
|
+
|
|
|
+ it( 'returns position before block if at the beginning of that block', () => {
|
|
|
+ setData( model, '<paragraph>x</paragraph><paragraph>[]foo</paragraph><paragraph>y</paragraph>' );
|
|
|
+
|
|
|
+ const pos = findOptimalInsertionPosition( doc.selection );
|
|
|
+
|
|
|
+ expect( pos.path ).to.deep.equal( [ 1 ] );
|
|
|
+ } );
|
|
|
+
|
|
|
+ it( 'returns position before block if in the middle of that block', () => {
|
|
|
+ setData( model, '<paragraph>x</paragraph><paragraph>f[]oo</paragraph><paragraph>y</paragraph>' );
|
|
|
+
|
|
|
+ const pos = findOptimalInsertionPosition( doc.selection );
|
|
|
+
|
|
|
+ expect( pos.path ).to.deep.equal( [ 1 ] );
|
|
|
+ } );
|
|
|
+
|
|
|
+ it( 'returns position after block if at the end of that block', () => {
|
|
|
+ setData( model, '<paragraph>x</paragraph><paragraph>foo[]</paragraph><paragraph>y</paragraph>' );
|
|
|
+
|
|
|
+ const pos = findOptimalInsertionPosition( doc.selection );
|
|
|
+
|
|
|
+ expect( pos.path ).to.deep.equal( [ 2 ] );
|
|
|
+ } );
|
|
|
+
|
|
|
+ // Checking if isTouching() was used.
|
|
|
+ it( 'returns position after block if at the end of that block (deeply nested)', () => {
|
|
|
+ setData( model, '<paragraph>x</paragraph><paragraph>foo<span>bar[]</span></paragraph><paragraph>y</paragraph>' );
|
|
|
+
|
|
|
+ const pos = findOptimalInsertionPosition( doc.selection );
|
|
|
+
|
|
|
+ expect( pos.path ).to.deep.equal( [ 2 ] );
|
|
|
+ } );
|
|
|
+
|
|
|
+ it( 'returns selection focus if not in a block', () => {
|
|
|
+ model.schema.extend( '$text', { allowIn: '$root' } );
|
|
|
+ setData( model, 'foo[]bar' );
|
|
|
+
|
|
|
+ const pos = findOptimalInsertionPosition( doc.selection );
|
|
|
+
|
|
|
+ expect( pos.path ).to.deep.equal( [ 3 ] );
|
|
|
+ } );
|
|
|
+ } );
|
|
|
} );
|