Source: BrightnessComponent.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 './BrightnessSlider.es';
  16. import {core} from 'metal';
  17. import Component from 'metal-component';
  18. import Soy from 'metal-soy';
  19. import componentTemplates from './BrightnessComponent.soy';
  20. import './BrightnessControls.soy';
  21. /**
  22. * Creates a Brightness component.
  23. */
  24. class BrightnessComponent 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 brightness 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 brightness 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 brightnessValue = this.components.slider.value;
  60. let promise = this.cache_[brightnessValue];
  61. if (!promise) {
  62. promise = this.spawnWorker_({
  63. brightnessValue,
  64. imageData,
  65. });
  66. this.cache_[brightnessValue] = promise;
  67. }
  68. return promise;
  69. }
  70. /**
  71. * Notifies the editor that this component wants to generate
  72. * a different preview version of the current image. It debounces
  73. * the calls.
  74. */
  75. requestPreview() {
  76. this.requestImageEditorPreview_();
  77. }
  78. /**
  79. * Spawns a webworker to process the image in a different thread.
  80. *
  81. * @param {Object} message The image and brightness value.
  82. * @return {Promise} A promise that resolves when the webworker
  83. * finishes processing the image.
  84. */
  85. spawnWorker_(message) {
  86. return new Promise((resolve) => {
  87. const workerURI = this.modulePath + '/BrightnessWorker.js';
  88. const processWorker = new Worker(workerURI);
  89. processWorker.onmessage = (event) => resolve(event.data);
  90. processWorker.postMessage(message);
  91. });
  92. }
  93. }
  94. /**
  95. * State definition.
  96. *
  97. * @static
  98. * @type {!Object}
  99. */
  100. BrightnessComponent.STATE = {
  101. /**
  102. * Path of this module.
  103. *
  104. * @type {String}
  105. */
  106. modulePath: {
  107. validator: core.isString,
  108. },
  109. /**
  110. * Injected method that notifies the editor that the component wants to
  111. * generate a preview version of the image.
  112. *
  113. * @type {Function}
  114. */
  115. requestImageEditorPreview: {
  116. validator: core.isFunction,
  117. },
  118. };
  119. Soy.register(BrightnessComponent, componentTemplates);
  120. export default BrightnessComponent;