8
0

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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import ViewElement from '../view/element.js';
  6. import ViewRange from '../view/range.js';
  7. import viewWriter from '../view/writer.js';
  8. /**
  9. * Contains {@link engine.model.Selection model selection} to {@link engine.view.Selection view selection} converters for
  10. * {@link engine.conversion.ModelConversionDispatcher}.
  11. *
  12. * @namespace engine.conversion.modelSelectionToView
  13. */
  14. /**
  15. * Function factory, creates a converter that converts non-collapsed {@link engine.model.Selection model selection} to
  16. * {@link engine.view.Selection view selection}. The converter consumes appropriate value from `consumable` object
  17. * and maps model positions from selection to view positions.
  18. *
  19. * modelDispatcher.on( 'selection', convertRangeSelection() );
  20. *
  21. * @external engine.conversion.modelSelectionToView
  22. * @function engine.conversion.modelSelectionToView.convertRangeSelection
  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 ( let 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 engine.model.Selection model selection} to
  43. * {@link engine.view.Selection view selection}. The converter consumes appropriate value from `consumable` object,
  44. * maps model selection position to view position and breaks {@link engine.view.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 engine.conversion.modelSelectionToView.convertSelectionAttribute attribute converter} for selection attributes,
  56. * which wraps collapsed selection into view elements. Those converters together ensure, that selection ends up in
  57. * appropriate attribute elements.
  58. *
  59. * See also {@link engine.conversion.modelSelectionToView.clearAttributes} which does a clean-up by merging attributes.
  60. *
  61. * @external engine.conversion.modelSelectionToView
  62. * @function engine.conversion.modelSelectionToView.convertCollapsedSelection
  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.breakAt( 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 engine.model.Selection model selection} attributes to
  83. * {@link engine.view.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 engine.view.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 engine.conversion.ModelConversionDispatcher#event:selectionAttribute selectionAttribute event}. It's expected that
  90. * the function returns a {@link engine.view.AttributeElement}. The result of the function will be the wrapping element.
  91. *
  92. * modelDispatcher.on( 'selectionAttribute:italic', convertSelectionAttribute( new ViewAttributeElement( 'em' ) ) );
  93. *
  94. * function styleElementCreator( styleValue ) {
  95. * if ( styleValue == 'important' ) {
  96. * return new ViewAttributeElement( 'strong', { style: 'text-transform:uppercase;' } );
  97. * } else if ( styleValue == 'gold' ) {
  98. * return new ViewAttributeElement( 'span', { style: 'color:yellow;' } );
  99. * }
  100. * }
  101. * modelDispatcher.on( 'selectionAttribute:style', convertSelectionAttribute( styleCreator ) );
  102. *
  103. * **Note:** You can use the same `elementCreator` function for this converter factory and {@link engine.conversion.modelToView.wrap}
  104. * model to view converter, as long as the `elementCreator` function uses only the first parameter (attribute value).
  105. *
  106. * modelDispatcher.on( 'selection', convertCollapsedSelection() );
  107. * modelDispatcher.on( 'selectionAttribute:italic', convertSelectionAttribute( new ViewAttributeElement( 'em' ) ) );
  108. * modelDispatcher.on( 'selectionAttribute:bold', convertSelectionAttribute( new ViewAttributeElement( 'strong' ) ) );
  109. *
  110. * Example of view states before and after converting collapsed selection:
  111. *
  112. * <p><em>f^oo</em>bar</p>
  113. * -> <p><em>f</em>^<em>oo</em>bar</p>
  114. * -> <p><em>f^oo</em>bar</p>
  115. *
  116. * Example of view state after converting collapsed selection. The scenario is: selection is inside bold text (`<strong>` element)
  117. * but it does not have bold attribute itself and has italic attribute instead (let's assume that user turned off bold and turned
  118. * on italic with selection collapsed):
  119. *
  120. * <p><strong>f^oo<strong>bar</p>
  121. * -> <p><strong>f</strong>^<strong>oo<strong>bar</p>
  122. * -> <p><strong>f</strong><em>^</em><strong>oo</strong>bar</p>
  123. *
  124. * In first example, nothing has changed, because first `<em>` element got broken by `convertCollapsedSelection()` converter,
  125. * but then it got wrapped-back by `convertSelectionAttribute()` converter. In second example, notice how `<strong>` element
  126. * is broken to prevent putting selection in it, since selection has no `bold` attribute.
  127. *
  128. * @external engine.conversion.modelSelectionToView
  129. * @function engine.conversion.modelSelectionToView.convertCollapsedSelection
  130. * @param {engine.view.AttributeElement|Function} elementCreator View element, or function returning a view element, which will
  131. * be used for wrapping.
  132. * @returns {Function} Selection converter.
  133. */
  134. export function convertSelectionAttribute( elementCreator ) {
  135. return ( evt, data, consumable, conversionApi ) => {
  136. const selection = data.selection;
  137. if ( !selection.isCollapsed ) {
  138. return;
  139. }
  140. if ( !consumable.consume( selection, 'selectionAttribute:' + data.key ) ) {
  141. return;
  142. }
  143. let viewPosition = conversionApi.viewSelection.getFirstPosition();
  144. conversionApi.viewSelection.removeAllRanges();
  145. const viewElement = elementCreator instanceof ViewElement ?
  146. elementCreator.clone( true ) :
  147. elementCreator( data.value, data, selection, consumable, conversionApi );
  148. viewPosition = viewWriter.wrapPosition( viewPosition, viewElement );
  149. conversionApi.viewSelection.addRange( new ViewRange( viewPosition, viewPosition ) );
  150. };
  151. }
  152. /**
  153. * Function factory, creates a converter that clears artifacts after the previous
  154. * {@link engine.model.Selection model selection} conversion. It removes all empty
  155. * {@link engine.view.AttributeElement view attribute elements} and merge sibling attributes at all start and end
  156. * positions of all ranges.
  157. *
  158. * <p><strong>^</strong></p>
  159. * -> <p>^</p>
  160. *
  161. * <p><strong>foo</strong>^<strong>bar</strong>bar</p>
  162. * -> <p><strong>foo^bar<strong>bar</p>
  163. *
  164. * <p><strong>foo</strong><em>^</em><strong>bar</strong>bar</p>
  165. * -> <p><strong>foo^bar<strong>bar</p>
  166. *
  167. * This listener should be assigned before any converter for the new selection:
  168. *
  169. * modelDispatcher.on( 'selection', clearAttributes() );
  170. *
  171. * See {@link engine.conversion.modelSelectionToView.convertCollapsedSelection} which do the opposite by breaking
  172. * attributes in the selection position.
  173. *
  174. * @external engine.conversion.modelSelectionToView
  175. * @function engine.conversion.modelSelectionToView.clearAttributes
  176. * @returns {Function} Selection converter.
  177. */
  178. export function clearAttributes() {
  179. return ( evt, data, consumable, conversionApi ) => {
  180. for ( let range of conversionApi.viewSelection.getRanges() ) {
  181. // Not collapsed selection should not have artifacts.
  182. if ( range.isCollapsed ) {
  183. // Position might be in the node removed by the view writer.
  184. if ( range.end.parent.document ) {
  185. viewWriter.mergeAt( range.start );
  186. }
  187. }
  188. }
  189. conversionApi.viewSelection.removeAllRanges();
  190. };
  191. }