8
0

model-selection-to-view-converters.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import ViewElement from '../view/element';
  6. import ViewRange from '../view/range';
  7. import viewWriter from '../view/writer';
  8. import { virtualSelectionDescriptorToAttribute } from './model-to-view-converters';
  9. /**
  10. * Contains {@link module:engine/model/selection~Selection model selection} to
  11. * {@link module:engine/view/selection~Selection view selection} converters for
  12. * {@link module:engine/conversion/modelconversiondispatcher~ModelConversionDispatcher}.
  13. *
  14. * @module engine/conversion/model-selection-to-view-converters
  15. */
  16. /**
  17. * Function factory, creates a converter that converts non-collapsed {@link module:engine/model/selection~Selection model selection} to
  18. * {@link module:engine/view/selection~Selection view selection}. The converter consumes appropriate value from `consumable` object
  19. * and maps model positions from selection to view positions.
  20. *
  21. * modelDispatcher.on( 'selection', convertRangeSelection() );
  22. *
  23. * @returns {Function} Selection converter.
  24. */
  25. export function convertRangeSelection() {
  26. return ( evt, data, consumable, conversionApi ) => {
  27. const selection = data.selection;
  28. if ( selection.isCollapsed ) {
  29. return;
  30. }
  31. if ( !consumable.consume( selection, 'selection' ) ) {
  32. return;
  33. }
  34. conversionApi.viewSelection.removeAllRanges();
  35. for ( const range of selection.getRanges() ) {
  36. const viewRange = conversionApi.mapper.toViewRange( range );
  37. conversionApi.viewSelection.addRange( viewRange, selection.isBackward );
  38. }
  39. };
  40. }
  41. /**
  42. * Function factory, creates a converter that converts collapsed {@link module:engine/model/selection~Selection model selection} to
  43. * {@link module:engine/view/selection~Selection view selection}. The converter consumes appropriate value from `consumable` object,
  44. * maps model selection position to view position and breaks {@link module:engine/view/attributeelement~AttributeElement attribute elements}
  45. * at the selection position.
  46. *
  47. * modelDispatcher.on( 'selection', convertCollapsedSelection() );
  48. *
  49. * Example of view state before and after converting collapsed selection:
  50. *
  51. * <p><strong>f^oo<strong>bar</p>
  52. * -> <p><strong>f</strong>^<strong>oo</strong>bar</p>
  53. *
  54. * By breaking attribute elements like `<strong>`, selection is in correct element. See also complementary
  55. * {@link module:engine/conversion/model-selection-to-view-converters~convertSelectionAttribute attribute converter}
  56. * for selection attributes,
  57. * which wraps collapsed selection into view elements. Those converters together ensure, that selection ends up in
  58. * appropriate attribute elements.
  59. *
  60. * See also {@link module:engine/conversion/model-selection-to-view-converters~clearAttributes} which does a clean-up
  61. * by merging attributes.
  62. *
  63. * @returns {Function} Selection converter.
  64. */
  65. export function convertCollapsedSelection() {
  66. return ( evt, data, consumable, conversionApi ) => {
  67. const selection = data.selection;
  68. if ( !selection.isCollapsed ) {
  69. return;
  70. }
  71. if ( !consumable.consume( selection, 'selection' ) ) {
  72. return;
  73. }
  74. const modelPosition = selection.getFirstPosition();
  75. const viewPosition = conversionApi.mapper.toViewPosition( modelPosition );
  76. const brokenPosition = viewWriter.breakAttributes( viewPosition );
  77. conversionApi.viewSelection.removeAllRanges();
  78. conversionApi.viewSelection.addRange( new ViewRange( brokenPosition, brokenPosition ) );
  79. };
  80. }
  81. /**
  82. * Function factory, creates a converter that converts {@link module:engine/model/selection~Selection model selection} attributes to
  83. * {@link module:engine/view/attributeelement~AttributeElement view attribute elements}. The converter works only for collapsed selection.
  84. * The converter consumes appropriate value from `consumable` object, maps model selection position to view position and
  85. * wraps that position into a view attribute element.
  86. *
  87. * The wrapping node depends on passed parameter. If {@link module:engine/view/element~Element} was passed, it will be cloned and
  88. * the copy will become the wrapping element. If `Function` is provided, it is passed all the parameters of the
  89. * {@link module:engine/conversion/modelconversiondispatcher~ModelConversionDispatcher#event:selectionAttribute selectionAttribute event}.
  90. * It's expected that the function returns a {@link module:engine/view/attributeelement~AttributeElement}.
  91. * The result of the function will be the wrapping element.
  92. *
  93. * modelDispatcher.on( 'selectionAttribute:italic', convertSelectionAttribute( new ViewAttributeElement( 'em' ) ) );
  94. *
  95. * function styleElementCreator( styleValue ) {
  96. * if ( styleValue == 'important' ) {
  97. * return new ViewAttributeElement( 'strong', { style: 'text-transform:uppercase;' } );
  98. * } else if ( styleValue == 'gold' ) {
  99. * return new ViewAttributeElement( 'span', { style: 'color:yellow;' } );
  100. * }
  101. * }
  102. * modelDispatcher.on( 'selectionAttribute:style', convertSelectionAttribute( styleCreator ) );
  103. *
  104. * **Note:** You can use the same `elementCreator` function for this converter factory
  105. * and {@link module:engine/conversion/model-to-view-converters~wrapRange}
  106. * model to view converter, as long as the `elementCreator` function uses only the first parameter (attribute value).
  107. *
  108. * modelDispatcher.on( 'selection', convertCollapsedSelection() );
  109. * modelDispatcher.on( 'selectionAttribute:italic', convertSelectionAttribute( new ViewAttributeElement( 'em' ) ) );
  110. * modelDispatcher.on( 'selectionAttribute:bold', convertSelectionAttribute( new ViewAttributeElement( 'strong' ) ) );
  111. *
  112. * Example of view states before and after converting collapsed selection:
  113. *
  114. * <p><em>f^oo</em>bar</p>
  115. * -> <p><em>f</em>^<em>oo</em>bar</p>
  116. * -> <p><em>f^oo</em>bar</p>
  117. *
  118. * Example of view state after converting collapsed selection. The scenario is: selection is inside bold text (`<strong>` element)
  119. * but it does not have bold attribute itself and has italic attribute instead (let's assume that user turned off bold and turned
  120. * on italic with selection collapsed):
  121. *
  122. * <p><strong>f^oo<strong>bar</p>
  123. * -> <p><strong>f</strong>^<strong>oo<strong>bar</p>
  124. * -> <p><strong>f</strong><em>^</em><strong>oo</strong>bar</p>
  125. *
  126. * In first example, nothing has changed, because first `<em>` element got broken by `convertCollapsedSelection()` converter,
  127. * but then it got wrapped-back by `convertSelectionAttribute()` converter. In second example, notice how `<strong>` element
  128. * is broken to prevent putting selection in it, since selection has no `bold` attribute.
  129. *
  130. * @param {module:engine/view/attributeelement~AttributeElement|Function} elementCreator View element,
  131. * or function returning a view element, which will be used for wrapping.
  132. * @returns {Function} Selection converter.
  133. */
  134. export function convertSelectionAttribute( elementCreator ) {
  135. return ( evt, data, consumable, conversionApi ) => {
  136. const viewElement = elementCreator instanceof ViewElement ?
  137. elementCreator.clone( true ) :
  138. elementCreator( data.value, data, data.selection, consumable, conversionApi );
  139. if ( !viewElement ) {
  140. return;
  141. }
  142. const consumableName = 'selectionAttribute:' + data.key;
  143. wrapCollapsedSelectionPosition( data.selection, conversionApi.viewSelection, viewElement, consumable, consumableName );
  144. };
  145. }
  146. /**
  147. * Performs similar conversion as {@link ~convertSelectionAttribute}, but depends on a marker name of a marker in which
  148. * collapsed selection is placed.
  149. *
  150. * modelDispatcher.on( 'selectionMarker:searchResult', convertSelectionMarker( { class: 'search' } ) );
  151. *
  152. * @see module:engine/conversion/model-selection-to-view-converters~convertSelectionAttribute
  153. * @param {module:engine/conversion/buildmodelconverter~VirtualSelectionDescriptor|Function} selectionDescriptor Virtual
  154. * selection descriptor object or function returning a descriptor object.
  155. * @returns {Function} Selection converter.
  156. */
  157. export function convertSelectionMarker( selectionDescriptor ) {
  158. return ( evt, data, consumable, conversionApi ) => {
  159. const descriptor = typeof selectionDescriptor == 'function' ?
  160. selectionDescriptor( data, consumable, conversionApi ) :
  161. selectionDescriptor;
  162. if ( !descriptor ) {
  163. return;
  164. }
  165. const viewElement = virtualSelectionDescriptorToAttribute( descriptor );
  166. const consumableName = 'selectionMarker:' + data.name;
  167. wrapCollapsedSelectionPosition( data.selection, conversionApi.viewSelection, viewElement, consumable, consumableName );
  168. };
  169. }
  170. // Helper function for `convertSelectionAttribute` and `convertSelectionMarker`, which perform similar task.
  171. function wrapCollapsedSelectionPosition( modelSelection, viewSelection, viewElement, consumable, consumableName ) {
  172. if ( !modelSelection.isCollapsed ) {
  173. return;
  174. }
  175. if ( !consumable.consume( modelSelection, consumableName ) ) {
  176. return;
  177. }
  178. let viewPosition = viewSelection.getFirstPosition();
  179. viewPosition = viewWriter.wrapPosition( viewPosition, viewElement );
  180. viewSelection.removeAllRanges();
  181. viewSelection.addRange( new ViewRange( viewPosition, viewPosition ) );
  182. }
  183. /**
  184. * Function factory, creates a converter that clears artifacts after the previous
  185. * {@link module:engine/model/selection~Selection model selection} conversion. It removes all empty
  186. * {@link module:engine/view/attributeelement~AttributeElement view attribute elements} and merge sibling attributes at all start and end
  187. * positions of all ranges.
  188. *
  189. * <p><strong>^</strong></p>
  190. * -> <p>^</p>
  191. *
  192. * <p><strong>foo</strong>^<strong>bar</strong>bar</p>
  193. * -> <p><strong>foo^bar<strong>bar</p>
  194. *
  195. * <p><strong>foo</strong><em>^</em><strong>bar</strong>bar</p>
  196. * -> <p><strong>foo^bar<strong>bar</p>
  197. *
  198. * This listener should be assigned before any converter for the new selection:
  199. *
  200. * modelDispatcher.on( 'selection', clearAttributes() );
  201. *
  202. * See {@link module:engine/conversion/model-selection-to-view-converters~convertCollapsedSelection}
  203. * which do the opposite by breaking attributes in the selection position.
  204. *
  205. * @returns {Function} Selection converter.
  206. */
  207. export function clearAttributes() {
  208. return ( evt, data, consumable, conversionApi ) => {
  209. for ( const range of conversionApi.viewSelection.getRanges() ) {
  210. // Not collapsed selection should not have artifacts.
  211. if ( range.isCollapsed ) {
  212. // Position might be in the node removed by the view writer.
  213. if ( range.end.parent.document ) {
  214. viewWriter.mergeAttributes( range.start );
  215. }
  216. }
  217. }
  218. conversionApi.viewSelection.removeAllRanges();
  219. };
  220. }
  221. /**
  222. * Function factory, creates a converter that clears fake selection marking after the previous
  223. * {@link module:engine/model/selection~Selection model selection} conversion.
  224. */
  225. export function clearFakeSelection() {
  226. return ( evt, data, consumable, conversionApi ) => conversionApi.viewSelection.setFake( false );
  227. }