From 12226d7e44e597c551da27dbd9a48d718ce6def5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Go=C5=82aszewski?= Date: Mon, 8 Oct 2018 13:30:26 +0200 Subject: [PATCH 1/2] Remove Position, Range, Selection imports from engine/model. --- package.json | 4 ++-- src/utils.js | 12 ++++++------ src/widget.js | 15 ++++++--------- tests/utils.js | 14 +++++++------- 4 files changed, 21 insertions(+), 24 deletions(-) diff --git a/package.json b/package.json index c4f5228a..2f2bf2a1 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/utils.js b/src/utils.js index 9937b0a5..967cac15 100644 --- a/src/utils.js +++ b/src/utils.js @@ -9,7 +9,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'; @@ -254,13 +253,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; @@ -269,10 +269,10 @@ 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 ) ) { @@ -280,7 +280,7 @@ export function findOptimalInsertionPosition( selection ) { } // Otherwise return position before the block. - return ModelPosition.createBefore( firstBlock ); + return model.createPositionBefore( firstBlock ); } return selection.focus; diff --git a/src/widget.js b/src/widget.js index 1afe820c..32e6cc40 100644 --- a/src/widget.js +++ b/src/widget.js @@ -9,9 +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'; @@ -248,7 +245,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; @@ -301,7 +298,7 @@ export default class Widget extends Plugin { } model.change( writer => { - writer.setSelection( ModelRange.createIn( limitElement ) ); + writer.setSelection( writer.createRangeIn( limitElement ) ); } ); return true; @@ -328,7 +325,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; @@ -345,7 +342,7 @@ export default class Widget extends Plugin { */ _setSelectionOverElement( element ) { this.editor.model.change( writer => { - writer.setSelection( ModelRange.createOn( element ) ); + writer.setSelection( writer.createRangeOn( element ) ); } ); } @@ -365,11 +362,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; } diff --git a/tests/utils.js b/tests/utils.js index fe1a08e9..9869c9f2 100644 --- a/tests/utils.js +++ b/tests/utils.js @@ -366,7 +366,7 @@ describe( 'widget utils', () => { it( 'returns position after selected element', () => { setData( model, 'x[]y' ); - const pos = findOptimalInsertionPosition( doc.selection ); + const pos = findOptimalInsertionPosition( doc.selection, model ); expect( pos.path ).to.deep.equal( [ 2 ] ); } ); @@ -374,7 +374,7 @@ describe( 'widget utils', () => { it( 'returns position inside empty block', () => { setData( model, 'x[]y' ); - const pos = findOptimalInsertionPosition( doc.selection ); + const pos = findOptimalInsertionPosition( doc.selection, model ); expect( pos.path ).to.deep.equal( [ 1, 0 ] ); } ); @@ -382,7 +382,7 @@ describe( 'widget utils', () => { it( 'returns position before block if at the beginning of that block', () => { setData( model, 'x[]fooy' ); - const pos = findOptimalInsertionPosition( doc.selection ); + const pos = findOptimalInsertionPosition( doc.selection, model ); expect( pos.path ).to.deep.equal( [ 1 ] ); } ); @@ -390,7 +390,7 @@ describe( 'widget utils', () => { it( 'returns position before block if in the middle of that block', () => { setData( model, 'xf[]ooy' ); - const pos = findOptimalInsertionPosition( doc.selection ); + const pos = findOptimalInsertionPosition( doc.selection, model ); expect( pos.path ).to.deep.equal( [ 1 ] ); } ); @@ -398,7 +398,7 @@ describe( 'widget utils', () => { it( 'returns position after block if at the end of that block', () => { setData( model, 'xfoo[]y' ); - const pos = findOptimalInsertionPosition( doc.selection ); + const pos = findOptimalInsertionPosition( doc.selection, model ); expect( pos.path ).to.deep.equal( [ 2 ] ); } ); @@ -407,7 +407,7 @@ describe( 'widget utils', () => { it( 'returns position after block if at the end of that block (deeply nested)', () => { setData( model, 'xfoobar[]y' ); - const pos = findOptimalInsertionPosition( doc.selection ); + const pos = findOptimalInsertionPosition( doc.selection, model ); expect( pos.path ).to.deep.equal( [ 2 ] ); } ); @@ -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 ] ); } ); From fda03d5ec0d8754f23d78c5e71c29bcdb0a48f49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Go=C5=82aszewski?= Date: Mon, 15 Oct 2018 14:35:25 +0200 Subject: [PATCH 2/2] Remove Position, Range, Selection imports from view/model. --- src/utils.js | 3 +-- src/widget.js | 4 +--- tests/widget.js | 3 +-- 3 files changed, 3 insertions(+), 7 deletions(-) diff --git a/src/utils.js b/src/utils.js index 967cac15..2ad3e164 100644 --- a/src/utils.js +++ b/src/utils.js @@ -8,7 +8,6 @@ */ import HighlightStack from './highlightstack'; -import ViewPosition from '@ckeditor/ckeditor5-engine/src/view/position'; import IconView from '@ckeditor/ckeditor5-ui/src/icon/iconview'; import env from '@ckeditor/ckeditor5-utils/src/env'; @@ -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 ); } diff --git a/src/widget.js b/src/widget.js index 32e6cc40..b16d93d5 100644 --- a/src/widget.js +++ b/src/widget.js @@ -9,8 +9,6 @@ import Plugin from '@ckeditor/ckeditor5-core/src/plugin'; import MouseObserver from '@ckeditor/ckeditor5-engine/src/view/observer/mouseobserver'; -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'; @@ -413,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; } diff --git a/tests/widget.js b/tests/widget.js index cd0538a0..3221ba4e 100644 --- a/tests/widget.js +++ b/tests/widget.js @@ -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'; @@ -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' } ); }