8
0

horizontalrulecommand.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /**
  6. * @module horizontal-rule/horizontalrulecommand
  7. */
  8. import Command from '@ckeditor/ckeditor5-core/src/command';
  9. import { findOptimalInsertionPosition } from '@ckeditor/ckeditor5-widget/src/utils';
  10. /**
  11. * The insert a horizontal rule command.
  12. *
  13. * The command is registered by the {@link module:horizontal-rule/horizontalruleediting~HorizontalRuleEditing} as `'horizontalRule'`.
  14. *
  15. * To insert the horizontal rule at the current selection, execute the command:
  16. *
  17. * editor.execute( 'horizontalRule' );
  18. *
  19. * @extends module:core/command~Command
  20. */
  21. export default class HorizontalRuleCommand extends Command {
  22. /**
  23. * @inheritDoc
  24. */
  25. refresh() {
  26. this.isEnabled = isHorizontalRuleAllowed( this.editor.model );
  27. }
  28. /**
  29. * Executes the command.
  30. *
  31. * @fires execute
  32. */
  33. execute() {
  34. const model = this.editor.model;
  35. model.change( writer => {
  36. const modelElement = writer.createElement( 'horizontalRule' );
  37. model.insertContent( modelElement );
  38. } );
  39. }
  40. }
  41. // Checks if the `horizontalRule` element can be inserted at current model selection.
  42. //
  43. // @param {module:engine/model/model~Model} model
  44. // @returns {Boolean}
  45. function isHorizontalRuleAllowed( model ) {
  46. const schema = model.schema;
  47. const selection = model.document.selection;
  48. return isHorizontalRuleAllowedInParent( selection, schema, model ) &&
  49. !checkSelectionOnObject( selection, schema );
  50. }
  51. // Checks if horizontal rule is allowed by schema in optimal insertion parent.
  52. //
  53. // @param {module:engine/model/selection~Selection|module:engine/model/documentselection~DocumentSelection} selection
  54. // @param {module:engine/model/schema~Schema} schema
  55. // @param {module:engine/model/model~Model} model Model instance.
  56. // @returns {Boolean}
  57. function isHorizontalRuleAllowedInParent( selection, schema, model ) {
  58. const parent = getInsertHorizontalRuleParent( selection, model );
  59. return schema.checkChild( parent, 'horizontalRule' );
  60. }
  61. // Check if selection is on object.
  62. //
  63. // @param {module:engine/model/selection~Selection|module:engine/model/documentselection~DocumentSelection} selection
  64. // @param {module:engine/model/schema~Schema} schema
  65. // @returns {Boolean}
  66. function checkSelectionOnObject( selection, schema ) {
  67. const selectedElement = selection.getSelectedElement();
  68. return selectedElement && schema.isObject( selectedElement );
  69. }
  70. // Returns a node that will be used to insert horizontal rule with `model.insertContent` to check if horizontal rule can be placed there.
  71. //
  72. // @param {module:engine/model/selection~Selection|module:engine/model/documentselection~DocumentSelection} selection
  73. // @param {module:engine/model/model~Model} model Model instance.
  74. // @returns {module:engine/model/element~Element}
  75. function getInsertHorizontalRuleParent( selection, model ) {
  76. const insertAt = findOptimalInsertionPosition( selection, model );
  77. const parent = insertAt.parent;
  78. if ( parent.isEmpty && !parent.is( '$root' ) ) {
  79. return parent.parent;
  80. }
  81. return parent;
  82. }