element-reconversion-demo.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals ClassicEditor, toWidget, toWidgetEditable, console, window, prompt, document */
  6. import { CS_CONFIG } from '@ckeditor/ckeditor5-cloud-services/tests/_utils/cloud-services-config';
  7. import createElement from '@ckeditor/ckeditor5-utils/src/dom/createelement';
  8. const getTypeFromViewElement = viewElement => {
  9. for ( const type of [ 'info', 'warning' ] ) {
  10. if ( viewElement.hasClass( `info-box-${ type }` ) ) {
  11. return type;
  12. }
  13. }
  14. return 'info';
  15. };
  16. const upcastInfoBox = ( viewElement, { writer } ) => {
  17. const complexInfoBox = writer.createElement( 'complexInfoBox' );
  18. const type = getTypeFromViewElement( viewElement );
  19. writer.setAttribute( 'infoBoxType', type, complexInfoBox );
  20. writer.setAttribute( 'infoBoxURL', 'https://ckeditor.com', complexInfoBox );
  21. return complexInfoBox;
  22. };
  23. function addActionButton( text, callback, domElement, editor ) {
  24. const domDocument = domElement.ownerDocument;
  25. const button = createElement( domDocument, 'button', {}, [ text ] );
  26. button.addEventListener( 'click', () => {
  27. editor.model.change( callback );
  28. } );
  29. domElement.appendChild( button );
  30. return button;
  31. }
  32. const renderActionsView = ( editor, modelElement ) => function( domElement ) {
  33. addActionButton( 'Set URL', writer => {
  34. // eslint-disable-next-line no-alert
  35. const newURL = prompt( 'Set URL', modelElement.getAttribute( 'infoBoxURL' ) || '' );
  36. writer.setAttribute( 'infoBoxURL', newURL, modelElement );
  37. }, domElement, editor );
  38. const currentType = modelElement.getAttribute( 'infoBoxType' );
  39. const newType = currentType === 'info' ? 'warning' : 'info';
  40. addActionButton( `Change to ${ newType }`, writer => {
  41. writer.setAttribute( 'infoBoxType', newType, modelElement );
  42. }, domElement, editor );
  43. const childCount = modelElement.childCount;
  44. const addButton = addActionButton( 'Add content box', writer => {
  45. writer.insertElement( 'complexInfoBoxContent', modelElement, 'end' );
  46. }, domElement, editor );
  47. if ( childCount > 4 ) {
  48. addButton.setAttribute( 'disabled', 'disabled' );
  49. }
  50. const removeButton = addActionButton( 'Remove content box', writer => {
  51. writer.remove( modelElement.getChild( childCount - 1 ) );
  52. }, domElement, editor );
  53. if ( childCount < 3 ) {
  54. removeButton.setAttribute( 'disabled', 'disabled' );
  55. }
  56. };
  57. const downcastInfoBox = editor => {
  58. return ( modelElement, { writer, consumable, mapper } ) => {
  59. const complexInfoBoxView = writer.createContainerElement( 'div', {
  60. class: 'info-box'
  61. } );
  62. const type = modelElement.getAttribute( 'infoBoxType' ) || 'info';
  63. writer.addClass( `info-box-${ type }`, complexInfoBoxView );
  64. for ( const child of modelElement.getChildren() ) {
  65. const childView = writer.createEditableElement( 'div' );
  66. if ( child.is( 'element', 'complexInfoBoxTitle' ) ) {
  67. writer.addClass( 'info-box-title', childView );
  68. } else {
  69. writer.addClass( 'info-box-content', childView );
  70. }
  71. consumable.consume( child, 'insert' );
  72. mapper.bindElements( child, childView );
  73. toWidgetEditable( childView, writer );
  74. writer.insert( writer.createPositionAt( complexInfoBoxView, 'end' ), childView );
  75. }
  76. const urlBox = writer.createRawElement( 'div', {
  77. class: 'info-box-url'
  78. }, function( domElement ) {
  79. domElement.innerText = `URL: "${ modelElement.getAttribute( 'infoBoxURL' ) }"`;
  80. } );
  81. writer.insert( writer.createPositionAt( complexInfoBoxView, 'end' ), urlBox );
  82. const actionsView = writer.createRawElement( 'div', {
  83. class: 'info-box-actions',
  84. contenteditable: 'false', // Prevent editing of the element:
  85. 'data-cke-ignore-events': 'true' // Allows using custom UI elements inside editing view.
  86. }, renderActionsView( editor, modelElement ) );
  87. writer.insert( writer.createPositionAt( complexInfoBoxView, 'end' ), actionsView );
  88. return toWidget( complexInfoBoxView, writer, {
  89. widgetLabel: 'Complex Info Box'
  90. } );
  91. };
  92. };
  93. class ComplexInfoBox {
  94. constructor( editor ) {
  95. this._defineSchema( editor );
  96. this._defineConversion( editor );
  97. }
  98. _defineConversion( editor ) {
  99. const conversion = editor.conversion;
  100. conversion.for( 'upcast' )
  101. .elementToElement( {
  102. view: { name: 'div', classes: [ 'info-box' ] },
  103. model: upcastInfoBox
  104. } )
  105. .elementToElement( {
  106. view: { name: 'div', classes: [ 'info-box-title' ] },
  107. model: 'complexInfoBoxTitle'
  108. } )
  109. .elementToElement( {
  110. view: { name: 'div', classes: [ 'info-box-content' ] },
  111. model: 'complexInfoBoxContent'
  112. } );
  113. // The downcast conversion must be split as you need a widget in the editing pipeline.
  114. conversion.for( 'downcast' ).elementToElement( {
  115. model: 'complexInfoBox',
  116. view: downcastInfoBox( editor ),
  117. triggerBy: {
  118. attributes: [ 'infoBoxType', 'infoBoxURL' ],
  119. children: [ 'complexInfoBoxContent' ]
  120. }
  121. } );
  122. }
  123. _defineSchema( editor ) {
  124. const schema = editor.model.schema;
  125. // The main element with attributes for type and URL:
  126. schema.register( 'complexInfoBox', {
  127. allowWhere: '$block',
  128. allowContentOf: '$root',
  129. isObject: true,
  130. allowAttributes: [ 'infoBoxType', 'infoBoxURL' ]
  131. } );
  132. // A text-only title.
  133. schema.register( 'complexInfoBoxTitle', {
  134. isLimit: true,
  135. allowIn: 'complexInfoBox'
  136. } );
  137. schema.extend( '$text', { allowIn: 'complexInfoBoxTitle' } );
  138. schema.addAttributeCheck( context => {
  139. if ( context.endsWith( 'complexInfoBoxTitle $text' ) ) {
  140. return false;
  141. }
  142. } );
  143. // A content which can have any content allowed in $root.
  144. schema.register( 'complexInfoBoxContent', {
  145. isLimit: true,
  146. allowIn: 'complexInfoBox',
  147. allowContentOf: '$root'
  148. } );
  149. }
  150. }
  151. ClassicEditor
  152. .create( document.querySelector( '#editor-element-reconversion' ), {
  153. cloudServices: CS_CONFIG,
  154. extraPlugins: [ ComplexInfoBox ],
  155. image: {
  156. toolbar: [ 'imageStyle:full', 'imageStyle:side', '|', 'imageTextAlternative' ]
  157. },
  158. table: {
  159. contentToolbar: [
  160. 'tableColumn',
  161. 'tableRow',
  162. 'mergeTableCells'
  163. ]
  164. },
  165. toolbar: {
  166. items: [ 'heading', '|', 'bold', 'italic' ],
  167. viewportTopOffset: window.getViewportTopOffsetConfig()
  168. }
  169. } )
  170. .then( editor => {
  171. window.editor = editor;
  172. } )
  173. .catch( err => {
  174. console.error( err );
  175. } );