image.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. const colCache = require('../utils/col-cache');
  2. const Anchor = require('./anchor');
  3. class Image {
  4. constructor(worksheet, model) {
  5. this.worksheet = worksheet;
  6. this.model = model;
  7. }
  8. get model() {
  9. switch (this.type) {
  10. case 'background':
  11. return {
  12. type: this.type,
  13. imageId: this.imageId,
  14. };
  15. case 'image':
  16. return {
  17. type: this.type,
  18. imageId: this.imageId,
  19. hyperlinks: this.range.hyperlinks,
  20. range: {
  21. tl: this.range.tl.model,
  22. br: this.range.br && this.range.br.model,
  23. ext: this.range.ext,
  24. editAs: this.range.editAs,
  25. },
  26. };
  27. default:
  28. throw new Error('Invalid Image Type');
  29. }
  30. }
  31. set model({type, imageId, range, hyperlinks}) {
  32. this.type = type;
  33. this.imageId = imageId;
  34. if (type === 'image') {
  35. if (typeof range === 'string') {
  36. const decoded = colCache.decode(range);
  37. this.range = {
  38. tl: new Anchor(this.worksheet, {col: decoded.left, row: decoded.top}, -1),
  39. br: new Anchor(this.worksheet, {col: decoded.right, row: decoded.bottom}, 0),
  40. editAs: 'oneCell',
  41. };
  42. } else {
  43. this.range = {
  44. tl: new Anchor(this.worksheet, range.tl, 0),
  45. br: range.br && new Anchor(this.worksheet, range.br, 0),
  46. ext: range.ext,
  47. editAs: range.editAs,
  48. hyperlinks: hyperlinks || range.hyperlinks,
  49. };
  50. }
  51. }
  52. }
  53. }
  54. module.exports = Image;