|
@@ -1,5 +1,5 @@
|
|
|
/**
|
|
/**
|
|
|
- * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
|
|
|
|
|
|
|
+ * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
|
|
|
* For licensing, see LICENSE.md.
|
|
* For licensing, see LICENSE.md.
|
|
|
*/
|
|
*/
|
|
|
|
|
|
|
@@ -8,7 +8,6 @@
|
|
|
*/
|
|
*/
|
|
|
|
|
|
|
|
import Command from '@ckeditor/ckeditor5-core/src/command';
|
|
import Command from '@ckeditor/ckeditor5-core/src/command';
|
|
|
-import Position from '@ckeditor/ckeditor5-engine/src/model/position';
|
|
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* Enter command. It is used by the {@link module:enter/enter~Enter Enter feature} to handle the <kbd>Enter</kbd> key.
|
|
* Enter command. It is used by the {@link module:enter/enter~Enter Enter feature} to handle the <kbd>Enter</kbd> key.
|
|
@@ -20,68 +19,67 @@ export default class EnterCommand extends Command {
|
|
|
* @inheritDoc
|
|
* @inheritDoc
|
|
|
*/
|
|
*/
|
|
|
execute() {
|
|
execute() {
|
|
|
- const doc = this.editor.document;
|
|
|
|
|
- const batch = doc.batch();
|
|
|
|
|
|
|
+ const model = this.editor.model;
|
|
|
|
|
+ const doc = model.document;
|
|
|
|
|
|
|
|
- doc.enqueueChanges( () => {
|
|
|
|
|
- enterBlock( this.editor.data, batch, doc.selection, doc.schema );
|
|
|
|
|
-
|
|
|
|
|
- this.fire( 'afterExecute', { batch } );
|
|
|
|
|
|
|
+ model.change( writer => {
|
|
|
|
|
+ enterBlock( this.editor.model, writer, doc.selection, model.schema );
|
|
|
|
|
+ this.fire( 'afterExecute', { writer } );
|
|
|
} );
|
|
} );
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// Creates a new block in the way that the <kbd>Enter</kbd> key is expected to work.
|
|
// Creates a new block in the way that the <kbd>Enter</kbd> key is expected to work.
|
|
|
//
|
|
//
|
|
|
-// @param {engine.controller.DataController} dataController
|
|
|
|
|
-// @param {module:engine/model/batch~Batch} batch A batch to which the deltas will be added.
|
|
|
|
|
|
|
+// @param {module:engine/model~Model} model
|
|
|
|
|
+// @param {module:engine/model/writer~Writer} writer
|
|
|
// @param {module:engine/model/selection~Selection} selection Selection on which the action should be performed.
|
|
// @param {module:engine/model/selection~Selection} selection Selection on which the action should be performed.
|
|
|
// @param {module:engine/model/schema~Schema} schema
|
|
// @param {module:engine/model/schema~Schema} schema
|
|
|
-function enterBlock( dataController, batch, selection, schema ) {
|
|
|
|
|
|
|
+function enterBlock( model, writer, selection, schema ) {
|
|
|
const isSelectionEmpty = selection.isCollapsed;
|
|
const isSelectionEmpty = selection.isCollapsed;
|
|
|
const range = selection.getFirstRange();
|
|
const range = selection.getFirstRange();
|
|
|
const startElement = range.start.parent;
|
|
const startElement = range.start.parent;
|
|
|
const endElement = range.end.parent;
|
|
const endElement = range.end.parent;
|
|
|
|
|
|
|
|
// Don't touch the roots and other limit elements.
|
|
// Don't touch the roots and other limit elements.
|
|
|
- if ( schema.limits.has( startElement.name ) || schema.limits.has( endElement.name ) ) {
|
|
|
|
|
|
|
+ if ( schema.isLimit( startElement ) || schema.isLimit( endElement ) ) {
|
|
|
// Delete the selected content but only if inside a single limit element.
|
|
// Delete the selected content but only if inside a single limit element.
|
|
|
// Abort, when crossing limit elements boundary (e.g. <limit1>x[x</limit1>donttouchme<limit2>y]y</limit2>).
|
|
// Abort, when crossing limit elements boundary (e.g. <limit1>x[x</limit1>donttouchme<limit2>y]y</limit2>).
|
|
|
// This is an edge case and it's hard to tell what should actually happen because such a selection
|
|
// This is an edge case and it's hard to tell what should actually happen because such a selection
|
|
|
// is not entirely valid.
|
|
// is not entirely valid.
|
|
|
if ( !isSelectionEmpty && startElement == endElement ) {
|
|
if ( !isSelectionEmpty && startElement == endElement ) {
|
|
|
- dataController.deleteContent( selection, batch );
|
|
|
|
|
|
|
+ model.deleteContent( selection );
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if ( isSelectionEmpty ) {
|
|
if ( isSelectionEmpty ) {
|
|
|
- splitBlock( batch, selection, range.start );
|
|
|
|
|
|
|
+ splitBlock( writer, selection, range.start );
|
|
|
} else {
|
|
} else {
|
|
|
const leaveUnmerged = !( range.start.isAtStart && range.end.isAtEnd );
|
|
const leaveUnmerged = !( range.start.isAtStart && range.end.isAtEnd );
|
|
|
const isContainedWithinOneElement = ( startElement == endElement );
|
|
const isContainedWithinOneElement = ( startElement == endElement );
|
|
|
|
|
|
|
|
- dataController.deleteContent( selection, batch, { leaveUnmerged } );
|
|
|
|
|
|
|
+ model.deleteContent( selection, { leaveUnmerged } );
|
|
|
|
|
|
|
|
if ( leaveUnmerged ) {
|
|
if ( leaveUnmerged ) {
|
|
|
// Partially selected elements.
|
|
// Partially selected elements.
|
|
|
//
|
|
//
|
|
|
// <h>x[xx]x</h> -> <h>x^x</h> -> <h>x</h><h>^x</h>
|
|
// <h>x[xx]x</h> -> <h>x^x</h> -> <h>x</h><h>^x</h>
|
|
|
if ( isContainedWithinOneElement ) {
|
|
if ( isContainedWithinOneElement ) {
|
|
|
- splitBlock( batch, selection, selection.focus );
|
|
|
|
|
|
|
+ splitBlock( writer, selection, selection.focus );
|
|
|
}
|
|
}
|
|
|
// Selection over multiple elements.
|
|
// Selection over multiple elements.
|
|
|
//
|
|
//
|
|
|
// <h>x[x</h><p>y]y<p> -> <h>x^</h><p>y</p> -> <h>x</h><p>^y</p>
|
|
// <h>x[x</h><p>y]y<p> -> <h>x^</h><p>y</p> -> <h>x</h><p>^y</p>
|
|
|
else {
|
|
else {
|
|
|
- selection.setCollapsedAt( endElement );
|
|
|
|
|
|
|
+ writer.setSelection( endElement, 0 );
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-function splitBlock( batch, selection, splitPos ) {
|
|
|
|
|
|
|
+function splitBlock( writer, selection, splitPos ) {
|
|
|
const oldElement = splitPos.parent;
|
|
const oldElement = splitPos.parent;
|
|
|
const newElement = new oldElement.constructor( oldElement.name, oldElement.getAttributes() );
|
|
const newElement = new oldElement.constructor( oldElement.name, oldElement.getAttributes() );
|
|
|
|
|
|
|
@@ -89,15 +87,15 @@ function splitBlock( batch, selection, splitPos ) {
|
|
|
// If the split is at the end of element, instead of splitting, just create a clone of position's parent
|
|
// If the split is at the end of element, instead of splitting, just create a clone of position's parent
|
|
|
// element and insert it after split element. The result is the same but less operations are done
|
|
// element and insert it after split element. The result is the same but less operations are done
|
|
|
// and it's more semantically correct (when it comes to operational transformation).
|
|
// and it's more semantically correct (when it comes to operational transformation).
|
|
|
- batch.insert( Position.createAfter( splitPos.parent ), newElement );
|
|
|
|
|
|
|
+ writer.insert( newElement, splitPos.parent, 'after' );
|
|
|
} else if ( splitPos.isAtStart ) {
|
|
} else if ( splitPos.isAtStart ) {
|
|
|
// If the split is at the start of element, instead of splitting, just create a clone of position's parent
|
|
// If the split is at the start of element, instead of splitting, just create a clone of position's parent
|
|
|
// element and insert it before split element. The result is the same but less operations are done
|
|
// element and insert it before split element. The result is the same but less operations are done
|
|
|
// and it's more semantically correct (when it comes to operational transformation).
|
|
// and it's more semantically correct (when it comes to operational transformation).
|
|
|
- batch.insert( Position.createBefore( splitPos.parent ), newElement );
|
|
|
|
|
|
|
+ writer.insert( newElement, splitPos.parent, 'before' );
|
|
|
} else {
|
|
} else {
|
|
|
- batch.split( splitPos );
|
|
|
|
|
|
|
+ writer.split( splitPos );
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- selection.setCollapsedAt( splitPos.parent.nextSibling );
|
|
|
|
|
|
|
+ writer.setSelection( splitPos.parent.nextSibling, 0 );
|
|
|
}
|
|
}
|