extending-content-custom-figure-attributes.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals ClassicEditor, console, window, document */
  6. import { CS_CONFIG } from '@ckeditor/ckeditor5-cloud-services/tests/_utils/cloud-services-config';
  7. /**
  8. * Plugin that converts custom attributes for elements that are wrapped in <figure> in the view.
  9. */
  10. class CustomFigureAttributes {
  11. /**
  12. * Plugin's constructor - receives editor instance on creation.
  13. */
  14. constructor( editor ) {
  15. // Save reference to the editor.
  16. this.editor = editor;
  17. }
  18. /**
  19. * Setups conversion and extends table & image features schema.
  20. *
  21. * Schema extending must be done in the “afterInit()” call because plugins define their schema in “init()“.
  22. */
  23. afterInit() {
  24. const editor = this.editor;
  25. // Define on which elements the CSS classes should be preserved:
  26. setupCustomClassConversion( 'img', 'image', editor );
  27. setupCustomClassConversion( 'table', 'table', editor );
  28. editor.conversion.for( 'upcast' ).add( upcastCustomClasses( 'figure' ), { priority: 'low' } );
  29. // Define custom attributes that should be preserved.
  30. setupCustomAttributeConversion( 'img', 'image', 'id', editor );
  31. setupCustomAttributeConversion( 'table', 'table', 'id', editor );
  32. }
  33. }
  34. /**
  35. * Sets up a conversion that preservers classes on <img> and <table> elements.
  36. */
  37. function setupCustomClassConversion( viewElementName, modelElementName, editor ) {
  38. // The 'customClass' attribute will store custom classes from the data in the model so schema definitions allow this attribute.
  39. editor.model.schema.extend( modelElementName, { allowAttributes: [ 'customClass' ] } );
  40. // Define upcast converters for the <img> and <table> elements with a "low" priority so they are run after the default converters.
  41. editor.conversion.for( 'upcast' ).add( upcastCustomClasses( viewElementName ), { priority: 'low' } );
  42. // Define downcast converters for a model element with a "low" priority so they are run after the default converters.
  43. editor.conversion.for( 'downcast' ).add( downcastCustomClasses( modelElementName ), { priority: 'low' } );
  44. }
  45. /**
  46. * Sets up a conversion for a custom attribute on view elements contained inside a <figure>.
  47. *
  48. * This method:
  49. * - Adds proper schema rules.
  50. * - Adds an upcast converter.
  51. * - Adds a downcast converter.
  52. */
  53. function setupCustomAttributeConversion( viewElementName, modelElementName, viewAttribute, editor ) {
  54. // Extend the schema to store an attribute in the model.
  55. const modelAttribute = `custom${ viewAttribute }`;
  56. editor.model.schema.extend( modelElementName, { allowAttributes: [ modelAttribute ] } );
  57. editor.conversion.for( 'upcast' ).add( upcastAttribute( viewElementName, viewAttribute, modelAttribute ) );
  58. editor.conversion.for( 'downcast' ).add( downcastAttribute( modelElementName, viewElementName, viewAttribute, modelAttribute ) );
  59. }
  60. /**
  61. * Creates an upcast converter that will pass all classes from the view element to the model element.
  62. */
  63. function upcastCustomClasses( elementName ) {
  64. return dispatcher => dispatcher.on( `element:${ elementName }`, ( evt, data, conversionApi ) => {
  65. const viewItem = data.viewItem;
  66. const modelRange = data.modelRange;
  67. const modelElement = modelRange && modelRange.start.nodeAfter;
  68. if ( !modelElement ) {
  69. return;
  70. }
  71. // The upcast conversion picks up classes from the base element and from the <figure> element so it should be extensible.
  72. const currentAttributeValue = modelElement.getAttribute( 'customClass' ) || [];
  73. currentAttributeValue.push( ...viewItem.getClassNames() );
  74. conversionApi.writer.setAttribute( 'customClass', currentAttributeValue, modelElement );
  75. } );
  76. }
  77. /**
  78. * Creates a downcast converter that adds classes defined in the `customClass` attribute to a given view element.
  79. *
  80. * This converter expects that the view element is nested in a <figure> element.
  81. */
  82. function downcastCustomClasses( modelElementName ) {
  83. return dispatcher => dispatcher.on( `insert:${ modelElementName }`, ( evt, data, conversionApi ) => {
  84. const modelElement = data.item;
  85. const viewFigure = conversionApi.mapper.toViewElement( modelElement );
  86. if ( !viewFigure ) {
  87. return;
  88. }
  89. // The code below assumes that classes are set on the <figure> element...
  90. conversionApi.writer.addClass( modelElement.getAttribute( 'customClass' ), viewFigure );
  91. // ... but if you prefer the classes to be passed to the <img> element, find the view element inside the <figure>:
  92. //
  93. // const viewElement = findViewChild( viewFigure, viewElementName, conversionApi );
  94. //
  95. // conversionApi.writer.addClass( modelElement.getAttribute( 'customClass' ), viewElement );
  96. } );
  97. }
  98. /**
  99. * Helper method that searches for a given view element in all children of the model element.
  100. *
  101. * @param {module:engine/view/item~Item} viewElement
  102. * @param {String} viewElementName
  103. * @param {module:engine/conversion/downcastdispatcher~DowncastConversionApi} conversionApi
  104. * @return {module:engine/view/item~Item}
  105. */
  106. function findViewChild( viewElement, viewElementName, conversionApi ) {
  107. const viewChildren = Array.from( conversionApi.writer.createRangeIn( viewElement ).getItems() );
  108. return viewChildren.find( item => item.is( 'element', viewElementName ) );
  109. }
  110. /**
  111. * Returns the custom attribute upcast converter.
  112. */
  113. function upcastAttribute( viewElementName, viewAttribute, modelAttribute ) {
  114. return dispatcher => dispatcher.on( `element:${ viewElementName }`, ( evt, data, conversionApi ) => {
  115. const viewItem = data.viewItem;
  116. const modelRange = data.modelRange;
  117. const modelElement = modelRange && modelRange.start.nodeAfter;
  118. if ( !modelElement ) {
  119. return;
  120. }
  121. conversionApi.writer.setAttribute( modelAttribute, viewItem.getAttribute( viewAttribute ), modelElement );
  122. } );
  123. }
  124. /**
  125. * Returns the custom attribute downcast converter.
  126. */
  127. function downcastAttribute( modelElementName, viewElementName, viewAttribute, modelAttribute ) {
  128. return dispatcher => dispatcher.on( `insert:${ modelElementName }`, ( evt, data, conversionApi ) => {
  129. const modelElement = data.item;
  130. const viewFigure = conversionApi.mapper.toViewElement( modelElement );
  131. const viewElement = findViewChild( viewFigure, viewElementName, conversionApi );
  132. if ( !viewElement ) {
  133. return;
  134. }
  135. conversionApi.writer.setAttribute( viewAttribute, modelElement.getAttribute( modelAttribute ), viewElement );
  136. } );
  137. }
  138. ClassicEditor
  139. .create( document.querySelector( '#snippet-custom-figure-attributes' ), {
  140. cloudServices: CS_CONFIG,
  141. extraPlugins: [ CustomFigureAttributes ],
  142. toolbar: {
  143. viewportTopOffset: window.getViewportTopOffsetConfig()
  144. }
  145. } )
  146. .then( editor => {
  147. window.editor = editor;
  148. } )
  149. .catch( err => {
  150. console.error( err.stack );
  151. } );