Source: ContrastComponent.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 {debounce} from 'frontend-js-web';
  15. import './ContrastSlider.es/';
  16. import {core} from 'metal';
  17. import Component from 'metal-component';
  18. import Soy from 'metal-soy';
  19. import componentTemplates from './ContrastComponent.soy';
  20. import './ContrastControls.soy';
  21. /**
  22. * Creates a Contrast component.
  23. */
  24. class ContrastComponent extends Component {
  25. /**
  26. * @inheritDoc
  27. */
  28. attached() {
  29. this.requestImageEditorPreview_ = debounce(
  30. this.requestImageEditorPreview,
  31. 50
  32. );
  33. this.cache_ = {};
  34. }
  35. /**
  36. * @inheritDoc
  37. */
  38. detached() {
  39. this.cache_ = {};
  40. }
  41. /**
  42. * Applies a contrast filter to the image.
  43. *
  44. * @param {ImageData} imageData The image data representation of the image.
  45. * @return {Promise} A promise that resolves when the webworker
  46. * finishes processing the image.
  47. */
  48. preview(imageData) {
  49. return this.process(imageData);
  50. }
  51. /**
  52. * Applies a contrast filter to the image.
  53. *
  54. * @param {ImageData} imageData The image data representation of the image.
  55. * @return {Promise} A promise that resolves when the webworker
  56. * finishes processing the image.
  57. */
  58. process(imageData) {
  59. const contrastValue = this.components.slider.value;
  60. let promise = this.cache_[contrastValue];
  61. if (!promise) {
  62. promise = this.spawnWorker_({
  63. contrastValue,
  64. imageData,
  65. });
  66. this.cache_[contrastValue] = promise;
  67. }
  68. return promise;
  69. }
  70. /**
  71. * Notifies the editor that this component wants to generate a different
  72. * preview version of the current image. It debounces the calls.
  73. */
  74. requestPreview() {
  75. this.requestImageEditorPreview_();
  76. }
  77. /**
  78. * Spawns a webworker to process the image in a different thread.
  79. *
  80. * @param {Object} message The image and contrast value.
  81. * @return {Promise} A promise that resolves when the webworker
  82. * finishes processing the image.
  83. */
  84. spawnWorker_(message) {
  85. return new Promise((resolve) => {
  86. const workerURI = this.modulePath + '/ContrastWorker.js';
  87. const processWorker = new Worker(workerURI);
  88. processWorker.onmessage = (event) => resolve(event.data);
  89. processWorker.postMessage(message);
  90. });
  91. }
  92. }
  93. /**
  94. * State definition.
  95. *
  96. * @static
  97. * @type {!Object}
  98. */
  99. ContrastComponent.STATE = {
  100. /**
  101. * Path of this module.
  102. *
  103. * @type {String}
  104. */
  105. modulePath: {
  106. validator: core.isString,
  107. },
  108. /**
  109. * Injected method that notifies the editor that this component wants to
  110. * generate a preview version of the image.
  111. *
  112. * @type {Function}
  113. */
  114. requestImageEditorPreview: {
  115. validator: core.isFunction,
  116. },
  117. };
  118. Soy.register(ContrastComponent, componentTemplates);
  119. export default ContrastComponent;