attributeconverters.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import AttributeElement from '../view/attributeelement';
  6. import buildModelConverter from './buildmodelconverter';
  7. import { defineConverter, parseDefinition } from './utils';
  8. /**
  9. * @param {String} attributeName
  10. * @param {} definition Converter definition
  11. * @param dispatchers
  12. */
  13. export function modelAttributeToView( attributeName, definition, dispatchers ) {
  14. const { model: attributeValue, viewDefinition } = parseDefinition( definition );
  15. buildModelConverter()
  16. .for( ...dispatchers )
  17. .fromAttribute( attributeName )
  18. .toElement( value => {
  19. if ( value != attributeValue ) {
  20. return;
  21. }
  22. return AttributeElement.fromViewDefinition( viewDefinition );
  23. } );
  24. }
  25. export function viewToModelAttribute( attributeName, definition, dispatchers ) {
  26. const { model: attributeValue, viewDefinitions } = parseDefinition( definition );
  27. const converter = defineConverter( dispatchers, viewDefinitions );
  28. converter.toAttribute( () => ( {
  29. key: attributeName,
  30. value: attributeValue
  31. } ) );
  32. }