element-reconversion-demo.js 6.4 KB

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