inserthtmlembedcommand.js 3.1 KB

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