|
|
@@ -3,10 +3,12 @@
|
|
|
* For licensing, see LICENSE.md.
|
|
|
*/
|
|
|
|
|
|
-import { isImageType } from '../src/utils';
|
|
|
+import { isImageType, findOptimalInsertionPosition } from '../src/utils';
|
|
|
+import Document from '@ckeditor/ckeditor5-engine/src/model/document';
|
|
|
+import { setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
|
|
|
|
|
|
-describe( 'utils', () => {
|
|
|
- describe( 'isImageType', () => {
|
|
|
+describe( 'upload utils', () => {
|
|
|
+ describe( 'isImageType()', () => {
|
|
|
it( 'should return true for png mime type', () => {
|
|
|
expect( isImageType( { type: 'image/png' } ) ).to.be.true;
|
|
|
} );
|
|
|
@@ -28,4 +30,82 @@ describe( 'utils', () => {
|
|
|
expect( isImageType( { type: 'video/mpeg' } ) ).to.be.false;
|
|
|
} );
|
|
|
} );
|
|
|
+
|
|
|
+ describe( 'findOptimalInsertionPosition()', () => {
|
|
|
+ let doc;
|
|
|
+
|
|
|
+ beforeEach( () => {
|
|
|
+ doc = new Document();
|
|
|
+
|
|
|
+ doc.createRoot();
|
|
|
+
|
|
|
+ doc.schema.registerItem( 'paragraph', '$block' );
|
|
|
+ doc.schema.registerItem( 'image' );
|
|
|
+ doc.schema.registerItem( 'span' );
|
|
|
+
|
|
|
+ doc.schema.allow( { name: 'image', inside: '$root' } );
|
|
|
+ doc.schema.objects.add( 'image' );
|
|
|
+
|
|
|
+ doc.schema.allow( { name: 'span', inside: 'paragraph' } );
|
|
|
+ doc.schema.allow( { name: '$text', inside: 'span' } );
|
|
|
+ } );
|
|
|
+
|
|
|
+ it( 'returns position after selected element', () => {
|
|
|
+ setData( doc, '<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( doc, '<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( doc, '<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( doc, '<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( doc, '<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( doc, '<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', () => {
|
|
|
+ doc.schema.allow( { name: '$text', inside: '$root' } );
|
|
|
+ setData( doc, 'foo[]bar' );
|
|
|
+
|
|
|
+ const pos = findOptimalInsertionPosition( doc.selection );
|
|
|
+
|
|
|
+ expect( pos.path ).to.deep.equal( [ 3 ] );
|
|
|
+ } );
|
|
|
+ } );
|
|
|
} );
|