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

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