Source: ResizeComponent.es.js

  1. /**
  2. * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
  3. *
  4. * This library is free software; you can redistribute it and/or modify it under
  5. * the terms of the GNU Lesser General Public License as published by the Free
  6. * Software Foundation; either version 2.1 of the License, or (at your option)
  7. * any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  12. * details.
  13. */
  14. import {core} from 'metal';
  15. import Component from 'metal-component';
  16. import Soy from 'metal-soy';
  17. import componentTemplates from './ResizeComponent.soy';
  18. import './ResizeControls.soy';
  19. /**
  20. * Creates a Resize component.
  21. */
  22. class ResizeComponent extends Component {
  23. /**
  24. * @inheritDoc
  25. */
  26. attached() {
  27. this.getImageEditorImageData().then((imageData) => {
  28. this.imageWidth = imageData.width;
  29. this.imageHeight = imageData.height;
  30. this.imageRatio_ = this.imageWidth / this.imageHeight;
  31. this.imageHeightInput_ = this.element.querySelector(
  32. '#' + this.ref + 'Height'
  33. );
  34. this.imageWidthInput_ = this.element.querySelector(
  35. '#' + this.ref + 'Width'
  36. );
  37. this.lockProportions = true;
  38. });
  39. }
  40. /**
  41. * Executes the resize operation to get the final version of the image.
  42. *
  43. * @param {ImageData} imageData The image data representation of the image.
  44. * @return {Promise} A promise that resolves with the resized
  45. * image data representation.
  46. */
  47. process(imageData) {
  48. return Promise.resolve(this.resizeImageData_(imageData));
  49. }
  50. /**
  51. * Resizes image data to the user's selected width and height values.
  52. *
  53. * @param {ImageData} imageData The original image data.
  54. * @return {ImageData} The resized image data with the component width and
  55. * height values specified by the user.
  56. */
  57. resizeImageData_(imageData) {
  58. const rawCanvas = document.createElement('canvas');
  59. rawCanvas.width = imageData.width;
  60. rawCanvas.height = imageData.height;
  61. rawCanvas.getContext('2d').putImageData(imageData, 0, 0);
  62. const canvas = document.createElement('canvas');
  63. canvas.width = this.imageWidth;
  64. canvas.height = this.imageHeight;
  65. const context = canvas.getContext('2d');
  66. context.drawImage(rawCanvas, 0, 0, this.imageWidth, this.imageHeight);
  67. return context.getImageData(0, 0, this.imageWidth, this.imageHeight);
  68. }
  69. /**
  70. * Keeps the width/height ratio in sync when <code>lockProportions</code> is
  71. * <code>true</code>.
  72. *
  73. * @param {InputEvent} event The input event.
  74. */
  75. syncDimensions(event) {
  76. const newValue = parseInt(event.delegateTarget.value, 10);
  77. if (event.delegateTarget === this.imageWidthInput_) {
  78. this.imageWidth = newValue;
  79. if (this.lockProportions) {
  80. this.imageHeight = parseInt(newValue / this.imageRatio_, 10);
  81. this.imageHeightInput_.value = this.imageHeight;
  82. }
  83. }
  84. else {
  85. this.imageHeight = newValue;
  86. if (this.lockProportions) {
  87. this.imageWidth = parseInt(newValue * this.imageRatio_, 10);
  88. this.imageWidthInput_.value = this.imageWidth;
  89. }
  90. }
  91. }
  92. /**
  93. * Toggles the value of the <code>lockProportions</code> attribute. When
  94. * enabled, changes in one of the dimensions cascades changes to the other
  95. * to keep the original image ratio.
  96. */
  97. toggleLockProportions() {
  98. this.lockProportions = !this.lockProportions;
  99. }
  100. }
  101. /**
  102. * State definition.
  103. *
  104. * @static
  105. * @type {!Object}
  106. */
  107. ResizeComponent.STATE = {
  108. /**
  109. * Injected helper that retrieves the editor image data.
  110. *
  111. * @type {Function}
  112. */
  113. getImageEditorImageData: {
  114. validator: core.isFunction,
  115. },
  116. };
  117. Soy.register(ResizeComponent, componentTemplates);
  118. export default ResizeComponent;