Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

stretch.ts 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import Command from './command'
  2. import history from '../history'
  3. import { StretchType, Data, Corner } from 'types'
  4. import {
  5. deepClone,
  6. getCommonBounds,
  7. getPage,
  8. getSelectedShapes,
  9. } from 'utils/utils'
  10. import { getShapeUtils } from 'state/shape-utils'
  11. export default function stretchCommand(data: Data, type: StretchType): void {
  12. const { currentPageId } = data
  13. const initialShapes = getSelectedShapes(data).map((shape) => deepClone(shape))
  14. const snapshot = Object.fromEntries(
  15. initialShapes.map((shape) => [
  16. shape.id,
  17. {
  18. initialShape: shape,
  19. initialBounds: getShapeUtils(shape).getBounds(shape),
  20. },
  21. ])
  22. )
  23. const commonBounds = getCommonBounds(
  24. ...initialShapes.map((shape) => getShapeUtils(shape).getBounds(shape))
  25. )
  26. history.execute(
  27. data,
  28. new Command({
  29. name: 'stretched_shapes',
  30. category: 'canvas',
  31. do(data) {
  32. const { shapes } = getPage(data, currentPageId)
  33. switch (type) {
  34. case StretchType.Horizontal: {
  35. Object.values(snapshot).forEach(
  36. ({ initialShape, initialBounds }) => {
  37. const newBounds = { ...initialBounds }
  38. newBounds.minX = commonBounds.minX
  39. newBounds.width = commonBounds.width
  40. newBounds.maxX = commonBounds.maxX
  41. const shape = shapes[initialShape.id]
  42. getShapeUtils(shape).transform(shape, newBounds, {
  43. type: Corner.TopLeft,
  44. scaleX: newBounds.width / initialBounds.width,
  45. scaleY: 1,
  46. initialShape,
  47. transformOrigin: [0.5, 0.5],
  48. })
  49. }
  50. )
  51. break
  52. }
  53. case StretchType.Vertical: {
  54. Object.values(snapshot).forEach(
  55. ({ initialShape, initialBounds }) => {
  56. const newBounds = { ...initialBounds }
  57. newBounds.minY = commonBounds.minY
  58. newBounds.height = commonBounds.height
  59. newBounds.maxY = commonBounds.maxY
  60. const shape = shapes[initialShape.id]
  61. getShapeUtils(shape).transform(shape, newBounds, {
  62. type: Corner.TopLeft,
  63. scaleX: 1,
  64. scaleY: newBounds.height / initialBounds.height,
  65. initialShape,
  66. transformOrigin: [0.5, 0.5],
  67. })
  68. }
  69. )
  70. }
  71. }
  72. },
  73. undo(data) {
  74. const { shapes } = getPage(data, currentPageId)
  75. initialShapes.forEach((shape) => (shapes[shape.id] = shape))
  76. },
  77. })
  78. )
  79. }