useCreateElement.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import { useStore } from 'vuex'
  2. import { MutationTypes } from '@/store'
  3. import { createRandomCode } from '@/utils/common'
  4. import { getImageSize } from '@/utils/image'
  5. import { VIEWPORT_SIZE, VIEWPORT_ASPECT_RATIO } from '@/configs/canvas'
  6. import { ChartType, PPTElement } from '@/types/slides'
  7. import { ShapePoolItem } from '@/configs/shapes'
  8. import { LinePoolItem } from '@/configs/lines'
  9. import {
  10. DEFAULT_IMAGE,
  11. DEFAULT_TEXT,
  12. DEFAULT_SHAPE,
  13. DEFAULT_LINE,
  14. DEFAULT_CHART,
  15. } from '@/configs/element'
  16. import useHistorySnapshot from '@/hooks/useHistorySnapshot'
  17. interface CommonElementPosition {
  18. top: number;
  19. left: number;
  20. width: number;
  21. height: number;
  22. }
  23. interface LineElementPosition {
  24. top: number;
  25. left: number;
  26. start: [number, number];
  27. end: [number, number];
  28. }
  29. export default () => {
  30. const store = useStore()
  31. const { addHistorySnapshot } = useHistorySnapshot()
  32. const createElement = (element: PPTElement) => {
  33. store.commit(MutationTypes.ADD_ELEMENT, element)
  34. store.commit(MutationTypes.SET_ACTIVE_ELEMENT_ID_LIST, [element.id])
  35. addHistorySnapshot()
  36. }
  37. const createImageElement = (src: string) => {
  38. getImageSize(src).then(({ width, height }) => {
  39. const scale = width / height
  40. if(scale < VIEWPORT_ASPECT_RATIO && width > VIEWPORT_SIZE) {
  41. width = VIEWPORT_SIZE
  42. height = width * scale
  43. }
  44. else if(height > VIEWPORT_SIZE * VIEWPORT_ASPECT_RATIO) {
  45. height = VIEWPORT_SIZE * VIEWPORT_ASPECT_RATIO
  46. width = height / scale
  47. }
  48. createElement({
  49. ...DEFAULT_IMAGE,
  50. type: 'image',
  51. id: createRandomCode(),
  52. src,
  53. width,
  54. height,
  55. })
  56. })
  57. }
  58. const createChartElement = (chartType: ChartType) => {
  59. createElement({
  60. ...DEFAULT_CHART,
  61. type: 'chart',
  62. id: createRandomCode(),
  63. chartType,
  64. })
  65. }
  66. const createTableElement = (rowCount: number, colCount: number) => {
  67. console.log(rowCount, colCount)
  68. }
  69. const createTextElement = (position: CommonElementPosition) => {
  70. const { left, top, width, height } = position
  71. createElement({
  72. ...DEFAULT_TEXT,
  73. type: 'text',
  74. id: createRandomCode(),
  75. left,
  76. top,
  77. width,
  78. height,
  79. })
  80. }
  81. const createShapeElement = (position: CommonElementPosition, data: ShapePoolItem) => {
  82. const { left, top, width, height } = position
  83. createElement({
  84. ...DEFAULT_SHAPE,
  85. type: 'shape',
  86. id: createRandomCode(),
  87. left,
  88. top,
  89. width,
  90. height,
  91. viewBox: data.viewBox,
  92. path: data.path,
  93. })
  94. }
  95. const createLineElement = (position: LineElementPosition, data: LinePoolItem) => {
  96. const { left, top, start, end } = position
  97. createElement({
  98. ...DEFAULT_LINE,
  99. type: 'line',
  100. id: createRandomCode(),
  101. left,
  102. top,
  103. start,
  104. end,
  105. points: data.points,
  106. })
  107. }
  108. return {
  109. createImageElement,
  110. createChartElement,
  111. createTableElement,
  112. createTextElement,
  113. createShapeElement,
  114. createLineElement,
  115. }
  116. }