| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- /**
- * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
- */
- /**
- * @module horizontal-rule/horizontalrulecommand
- */
- import Command from '@ckeditor/ckeditor5-core/src/command';
- import { findOptimalInsertionPosition } from '@ckeditor/ckeditor5-widget/src/utils';
- /**
- * The insert a horizontal rule command.
- *
- * The command is registered by the {@link module:horizontal-rule/horizontalruleediting~HorizontalRuleEditing} as `'horizontalRule'`.
- *
- * To insert the horizontal rule at the current selection, execute the command:
- *
- * editor.execute( 'horizontalRule' );
- *
- * @extends module:core/command~Command
- */
- export default class HorizontalRuleCommand extends Command {
- /**
- * @inheritDoc
- */
- refresh() {
- this.isEnabled = isHorizontalRuleAllowed( this.editor.model );
- }
- /**
- * Executes the command.
- *
- * @fires execute
- */
- execute() {
- const model = this.editor.model;
- model.change( writer => {
- const modelElement = writer.createElement( 'horizontalRule' );
- model.insertContent( modelElement );
- } );
- }
- }
- // Checks if the `horizontalRule` element can be inserted at current model selection.
- //
- // @param {module:engine/model/model~Model} model
- // @returns {Boolean}
- function isHorizontalRuleAllowed( model ) {
- const schema = model.schema;
- const selection = model.document.selection;
- return isHorizontalRuleAllowedInParent( selection, schema, model ) &&
- !checkSelectionOnObject( selection, schema );
- }
- // Checks if horizontal rule is allowed by schema in optimal insertion parent.
- //
- // @param {module:engine/model/selection~Selection|module:engine/model/documentselection~DocumentSelection} selection
- // @param {module:engine/model/schema~Schema} schema
- // @param {module:engine/model/model~Model} model Model instance.
- // @returns {Boolean}
- function isHorizontalRuleAllowedInParent( selection, schema, model ) {
- const parent = getInsertHorizontalRuleParent( selection, model );
- return schema.checkChild( parent, 'horizontalRule' );
- }
- // Check if selection is on object.
- //
- // @param {module:engine/model/selection~Selection|module:engine/model/documentselection~DocumentSelection} selection
- // @param {module:engine/model/schema~Schema} schema
- // @returns {Boolean}
- function checkSelectionOnObject( selection, schema ) {
- const selectedElement = selection.getSelectedElement();
- return selectedElement && schema.isObject( selectedElement );
- }
- // Returns a node that will be used to insert horizontal rule with `model.insertContent` to check if horizontal rule can be placed there.
- //
- // @param {module:engine/model/selection~Selection|module:engine/model/documentselection~DocumentSelection} selection
- // @param {module:engine/model/model~Model} model Model instance.
- // @returns {module:engine/model/element~Element}
- function getInsertHorizontalRuleParent( selection, model ) {
- const insertAt = findOptimalInsertionPosition( selection, model );
- const parent = insertAt.parent;
- if ( parent.isEmpty && !parent.is( '$root' ) ) {
- return parent.parent;
- }
- return parent;
- }
|