Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Remove Position, Range, Selection imports from engine/model. #56

Merged
merged 3 commits into from
Nov 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
"dependencies": {
"@ckeditor/ckeditor5-core": "^11.0.1",
"@ckeditor/ckeditor5-engine": "^11.0.0",
"@ckeditor/ckeditor5-utils": "^11.0.0",
"@ckeditor/ckeditor5-theme-lark": "^11.1.0",
"@ckeditor/ckeditor5-ui": "^11.1.0"
"@ckeditor/ckeditor5-ui": "^11.1.0",
"@ckeditor/ckeditor5-utils": "^11.0.0"
},
"devDependencies": {
"@ckeditor/ckeditor5-basic-styles": "^10.0.3",
Expand Down
15 changes: 7 additions & 8 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
*/

import HighlightStack from './highlightstack';
import ViewPosition from '@ckeditor/ckeditor5-engine/src/view/position';
import ModelPosition from '@ckeditor/ckeditor5-engine/src/model/position';
import IconView from '@ckeditor/ckeditor5-ui/src/icon/iconview';
import env from '@ckeditor/ckeditor5-utils/src/env';

Expand Down Expand Up @@ -254,13 +252,14 @@ export function toWidgetEditable( editable, writer ) {
*
* @param {module:engine/model/selection~Selection|module:engine/model/documentselection~DocumentSelection} selection
* The selection based on which the insertion position should be calculated.
* @param {module:engine/model/model~Model} model Model instance.
* @returns {module:engine/model/position~Position} The optimal position.
*/
export function findOptimalInsertionPosition( selection ) {
export function findOptimalInsertionPosition( selection, model ) {
const selectedElement = selection.getSelectedElement();

if ( selectedElement ) {
return ModelPosition.createAfter( selectedElement );
return model.createPositionAfter( selectedElement );
}

const firstBlock = selection.getSelectedBlocks().next().value;
Expand All @@ -269,18 +268,18 @@ export function findOptimalInsertionPosition( selection ) {
// If inserting into an empty block – return position in that block. It will get
// replaced with the image by insertContent(). #42.
if ( firstBlock.isEmpty ) {
return ModelPosition.createAt( firstBlock, 0 );
return model.createPositionAt( firstBlock, 0 );
}

const positionAfter = ModelPosition.createAfter( firstBlock );
const positionAfter = model.createPositionAfter( firstBlock );

// If selection is at the end of the block - return position after the block.
if ( selection.focus.isTouching( positionAfter ) ) {
return positionAfter;
}

// Otherwise return position before the block.
return ModelPosition.createBefore( firstBlock );
return model.createPositionBefore( firstBlock );
}

return selection.focus;
Expand Down Expand Up @@ -314,6 +313,6 @@ function addSelectionHandler( editable, writer ) {
} );

// Append the selection handler into the widget wrapper.
writer.insert( ViewPosition.createAt( editable, 0 ), selectionHandler );
writer.insert( writer.createPositionAt( editable, 0 ), selectionHandler );
writer.addClass( [ 'ck-widget_selectable' ], editable );
}
19 changes: 7 additions & 12 deletions src/widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@

import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
import MouseObserver from '@ckeditor/ckeditor5-engine/src/view/observer/mouseobserver';
import ModelRange from '@ckeditor/ckeditor5-engine/src/model/range';
import ModelSelection from '@ckeditor/ckeditor5-engine/src/model/selection';
import ModelElement from '@ckeditor/ckeditor5-engine/src/model/element';
import ViewEditableElement from '@ckeditor/ckeditor5-engine/src/view/editableelement';
import RootEditableElement from '@ckeditor/ckeditor5-engine/src/view/rooteditableelement';
import { isWidget, WIDGET_SELECTED_CLASS_NAME, getLabel } from './utils';
import { keyCodes, getCode, parseKeystroke } from '@ckeditor/ckeditor5-utils/src/keyboard';

Expand Down Expand Up @@ -248,7 +243,7 @@ export default class Widget extends Plugin {

const objectElement2 = this._getObjectElementNextToSelection( isForward );

if ( objectElement2 instanceof ModelElement && schema.isObject( objectElement2 ) ) {
if ( !!objectElement2 && schema.isObject( objectElement2 ) ) {
this._setSelectionOverElement( objectElement2 );

return true;
Expand Down Expand Up @@ -301,7 +296,7 @@ export default class Widget extends Plugin {
}

model.change( writer => {
writer.setSelection( ModelRange.createIn( limitElement ) );
writer.setSelection( writer.createRangeIn( limitElement ) );
} );

return true;
Expand All @@ -328,7 +323,7 @@ export default class Widget extends Plugin {
const widgetParent = editing.mapper.toModelElement( selectedElement.parent );

model.change( writer => {
writer.setSelection( ModelRange.createIn( widgetParent ) );
writer.setSelection( writer.createRangeIn( widgetParent ) );
} );

return true;
Expand All @@ -345,7 +340,7 @@ export default class Widget extends Plugin {
*/
_setSelectionOverElement( element ) {
this.editor.model.change( writer => {
writer.setSelection( ModelRange.createOn( element ) );
writer.setSelection( writer.createRangeOn( element ) );
} );
}

Expand All @@ -365,11 +360,11 @@ export default class Widget extends Plugin {

// Clone current selection to use it as a probe. We must leave default selection as it is so it can return
// to its current state after undo.
const probe = new ModelSelection( modelSelection );
const probe = model.createSelection( modelSelection );
model.modifySelection( probe, { direction: forward ? 'forward' : 'backward' } );
const objectElement = forward ? probe.focus.nodeBefore : probe.focus.nodeAfter;

if ( objectElement instanceof ModelElement && schema.isObject( objectElement ) ) {
if ( !!objectElement && schema.isObject( objectElement ) ) {
return objectElement;
}

Expand Down Expand Up @@ -416,7 +411,7 @@ function isSelectAllKeyCode( domEventData ) {
// @returns {Boolean}
function isInsideNestedEditable( element ) {
while ( element ) {
if ( element instanceof ViewEditableElement && !( element instanceof RootEditableElement ) ) {
if ( !!element && element.is( 'editableElement' ) && !element.is( 'rootElement' ) ) {
return true;
}

Expand Down
14 changes: 7 additions & 7 deletions tests/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -366,39 +366,39 @@ describe( 'widget utils', () => {
it( 'returns position after selected element', () => {
setData( model, '<paragraph>x</paragraph>[<image></image>]<paragraph>y</paragraph>' );

const pos = findOptimalInsertionPosition( doc.selection );
const pos = findOptimalInsertionPosition( doc.selection, model );

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 );
const pos = findOptimalInsertionPosition( doc.selection, model );

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 );
const pos = findOptimalInsertionPosition( doc.selection, model );

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 );
const pos = findOptimalInsertionPosition( doc.selection, model );

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 );
const pos = findOptimalInsertionPosition( doc.selection, model );

expect( pos.path ).to.deep.equal( [ 2 ] );
} );
Expand All @@ -407,7 +407,7 @@ describe( 'widget utils', () => {
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 );
const pos = findOptimalInsertionPosition( doc.selection, model );

expect( pos.path ).to.deep.equal( [ 2 ] );
} );
Expand All @@ -416,7 +416,7 @@ describe( 'widget utils', () => {
model.schema.extend( '$text', { allowIn: '$root' } );
setData( model, 'foo[]bar' );

const pos = findOptimalInsertionPosition( doc.selection );
const pos = findOptimalInsertionPosition( doc.selection, model );

expect( pos.path ).to.deep.equal( [ 3 ] );
} );
Expand Down
3 changes: 1 addition & 2 deletions tests/widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import MouseObserver from '@ckeditor/ckeditor5-engine/src/view/observer/mouseobs
import { downcastElementToElement } from '@ckeditor/ckeditor5-engine/src/conversion/downcast-converters';
import { toWidget } from '../src/utils';
import DomEventData from '@ckeditor/ckeditor5-engine/src/view/observer/domeventdata';
import ViewPosition from '@ckeditor/ckeditor5-engine/src/view/position';
import { setData as setModelData, getData as getModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
Expand Down Expand Up @@ -87,7 +86,7 @@ describe( 'Widget', () => {
view: ( modelItem, viewWriter ) => {
const b = viewWriter.createAttributeElement( 'b' );
const div = viewWriter.createContainerElement( 'div' );
viewWriter.insert( ViewPosition.createAt( div, 0 ), b );
viewWriter.insert( viewWriter.createPositionAt( div, 0 ), b );

return toWidget( div, viewWriter, { label: 'element label' } );
}
Expand Down