Source: ImageEditorHistoryEntry.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. /**
  15. * Image Editor History Entry
  16. *
  17. * This class models a step in the image edition process. It stores the
  18. * Image data at a given point in time so it can be later recovered for
  19. * undo/redo purposes or other visualization needs.
  20. * @review
  21. */
  22. class ImageEditorHistoryEntry {
  23. /**
  24. * Constructor
  25. * @review
  26. */
  27. constructor(image) {
  28. this.dataPromise_ = new Promise((resolve) => {
  29. // Preemtively fetch the imageData when all we have is the image url
  30. if (image.url && !image.data) {
  31. this.loadData_(image.url).then((imageData) =>
  32. resolve(imageData)
  33. );
  34. }
  35. else {
  36. resolve(image.data);
  37. }
  38. });
  39. }
  40. /**
  41. * Fetches an ImageData for a given image url
  42. * @param {String} imageURL The image url to load
  43. * @protected
  44. * @review
  45. */
  46. loadData_(imageURL) {
  47. return new Promise((resolve) => {
  48. const bufferImage = new Image();
  49. bufferImage.onload = () => {
  50. const bufferCanvas = document.createElement('canvas');
  51. const bufferContext = bufferCanvas.getContext('2d');
  52. const height = bufferImage.height;
  53. const width = bufferImage.width;
  54. bufferCanvas.width = width;
  55. bufferCanvas.height = height;
  56. bufferContext.drawImage(bufferImage, 0, 0, width, height);
  57. resolve(bufferContext.getImageData(0, 0, width, height));
  58. };
  59. bufferImage.src = imageURL;
  60. });
  61. }
  62. /**
  63. * Fetches the stored ImageData of this history entry
  64. * @return {Promise} A promise that will resolve with the stored ImageData value
  65. * @review
  66. */
  67. getImageData() {
  68. return this.dataPromise_;
  69. }
  70. }
  71. export default ImageEditorHistoryEntry;