/**
* @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
/**
* @module enter/shiftentercommand
*/
import Command from '@ckeditor/ckeditor5-core/src/command';
import { getCopyOnEnterAttributes } from './utils';
/**
* ShiftEnter command. It is used by the {@link module:enter/shiftenter~ShiftEnter ShiftEnter feature} to handle
* the Shift+Enter keystroke.
*
* @extends module:core/command~Command
*/
export default class ShiftEnterCommand extends Command {
/**
* @inheritDoc
*/
execute() {
const model = this.editor.model;
const doc = model.document;
model.change( writer => {
softBreakAction( model, writer, doc.selection );
this.fire( 'afterExecute', { writer } );
} );
}
refresh() {
const model = this.editor.model;
const doc = model.document;
this.isEnabled = isEnabled( model.schema, doc.selection );
}
}
// Checks whether the ShiftEnter command should be enabled in the specified selection.
//
// @param {module:engine/model/schema~Schema} schema
// @param {module:engine/model/selection~Selection|module:engine/model/documentselection~DocumentSelection} selection
function isEnabled( schema, selection ) {
// At this moment it is okay to support single range selections only.
// But in the future we may need to change that.
if ( selection.rangeCount > 1 ) {
return false;
}
const anchorPos = selection.anchor;
// Check whether the break element can be inserted in the current selection anchor.
if ( !anchorPos || !schema.checkChild( anchorPos, 'softBreak' ) ) {
return false;
}
const range = selection.getFirstRange();
const startElement = range.start.parent;
const endElement = range.end.parent;
// Do not modify the content if selection is cross-limit elements.
if ( ( isInsideLimitElement( startElement, schema ) || isInsideLimitElement( endElement, schema ) ) && startElement !== endElement ) {
return false;
}
return true;
}
// Creates a break in the way that the Shift+Enter keystroke is expected to work.
//
// @param {module:engine/model~Model} model
// @param {module:engine/model/writer~Writer} writer
// @param {module:engine/model/selection~Selection|module:engine/model/documentselection~DocumentSelection} selection
// Selection on which the action should be performed.
function softBreakAction( model, writer, selection ) {
const isSelectionEmpty = selection.isCollapsed;
const range = selection.getFirstRange();
const startElement = range.start.parent;
const endElement = range.end.parent;
const isContainedWithinOneElement = ( startElement == endElement );
if ( isSelectionEmpty ) {
const attributesToCopy = getCopyOnEnterAttributes( model.schema, selection.getAttributes() );
insertBreak( model, writer, range.end );
writer.removeSelectionAttribute( selection.getAttributeKeys() );
writer.setSelectionAttribute( attributesToCopy );
} else {
const leaveUnmerged = !( range.start.isAtStart && range.end.isAtEnd );
model.deleteContent( selection, { leaveUnmerged } );
// Selection within one element:
//
//
^x
y]y
->
y
->^y
// // We chose not to insert a line break in this case because: // // * it's not a very common scenario, // * it actually surprised me when I saw the "expected behavior" in real life. // // It's ok if the user will need to be more specific where they want the` element is inside a limit element: // - <$root>
Text.
$root> => false // - <$root>Text