view-to-model-converters.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. import ModelDocumentFragment from '../treemodel/documentfragment.js';
  7. import ModelText from '../treemodel/text.js';
  8. /**
  9. * Contains {@link engine.treeView view} to {@link engine.treeModel model} converters for
  10. * {@link engine.treeController.ViewConversionDispatcher}.
  11. *
  12. * @namespace engine.treeController.viewToModel
  13. */
  14. /**
  15. * Function factory, creates a converter that converts {@link engine.treeView.DocumentFragment view document fragment} or
  16. * all children of {@link engine.treeView.Element} into {@link engine.treeModel.DocumentFragment model document fragment}.
  17. * This is the "entry-point" converter for view to model conversion. This converter starts the conversion of all "children"
  18. * of passed view document fragment. Those "children" {@link engine.treeView.Node view nodes} are then handled by other converters.
  19. *
  20. * This also a "default", last resort converter for all view elements that has not been converted by other converters.
  21. * When a view element is converted to the model and it does not have it's converter specified, all of that elements
  22. * children will be converted to {@link engine.treeModel.DocumentFragment} and returned.
  23. *
  24. * @external engine.treeController.viewToModel
  25. * @function engine.treeController.viewToModel.convertToModelFragment
  26. * @returns {Function} Universal converter for view {@link engine.treeView.DocumentFragment fragments} and
  27. * {@link engine.treeView.Element elements} that returns {@link engine.treeModel.DocumentFragment model fragment} with
  28. * children of converted view item.
  29. */
  30. export function convertToModelFragment() {
  31. return ( evt, data, consumable, conversionApi ) => {
  32. if ( !data.output && consumable.test( data.input ) ) {
  33. const convertedChildren = conversionApi.convertChildren( data.input, consumable, { context: data.context } );
  34. data.output = new ModelDocumentFragment( convertedChildren );
  35. }
  36. };
  37. }
  38. /**
  39. * Function factory, creates a converter that converts {@link engine.treeView.Text} to {@link engine.treeModel.Text}.
  40. *
  41. * @external engine.treeController.viewToModel
  42. * @function engine.treeController.viewToModel.convertText
  43. * @returns {Function} {@link engine.treeView.Text View text} converter.
  44. */
  45. export function convertText() {
  46. return ( evt, data, consumable ) => {
  47. if ( consumable.consume( data.input ) ) {
  48. data.output = new ModelText( data.input.data );
  49. }
  50. };
  51. }