8
0

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

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