createElement.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import { createRandomCode } from '@/utils/common'
  2. import { getImageSize } from '@/utils/image'
  3. import { VIEWPORT_SIZE, VIEWPORT_ASPECT_RATIO } from '@/configs/canvas'
  4. import { TableElementCell } from '@/types/slides'
  5. import {
  6. DEFAULT_IMAGE,
  7. DEFAULT_TEXT,
  8. DEFAULT_SHAPE,
  9. DEFAULT_LINE,
  10. DEFAULT_CHART,
  11. DEFAULT_TABLE,
  12. } from '@/configs/defaultElement'
  13. interface CommonElementPosition {
  14. top: number;
  15. left: number;
  16. width: number;
  17. height: number;
  18. }
  19. interface LineElementPosition {
  20. top: number;
  21. left: number;
  22. start: [number, number];
  23. end: [number, number];
  24. }
  25. export const insertImage = (imgUrl: string) => {
  26. getImageSize(imgUrl).then(({ width, height }) => {
  27. const scale = width / height
  28. if(scale < VIEWPORT_ASPECT_RATIO && width > VIEWPORT_SIZE) {
  29. width = VIEWPORT_SIZE
  30. height = width * scale
  31. }
  32. else if(height > VIEWPORT_SIZE * VIEWPORT_ASPECT_RATIO) {
  33. height = VIEWPORT_SIZE * VIEWPORT_ASPECT_RATIO
  34. width = height / scale
  35. }
  36. return {
  37. ...DEFAULT_IMAGE,
  38. elId: createRandomCode(),
  39. imgUrl,
  40. width,
  41. height,
  42. }
  43. })
  44. }
  45. export const insertChart = (chartType: string, data: Object) => {
  46. return {
  47. ...DEFAULT_CHART,
  48. elId: createRandomCode(),
  49. chartType,
  50. data,
  51. }
  52. }
  53. export const insertTable = (rowCount: number, colCount: number) => {
  54. const row: TableElementCell[] = new Array(colCount).fill({ colspan: 1, rowspan: 1, content: '' })
  55. const data: TableElementCell[][] = new Array(rowCount).fill(row)
  56. const DEFAULT_CELL_WIDTH = 80
  57. const DEFAULT_CELL_HEIGHT = 35
  58. const DEFAULT_BORDER_WIDTH = 2
  59. const colSizes: number[] = new Array(colCount).fill(DEFAULT_CELL_WIDTH)
  60. const rowSizes: number[] = new Array(rowCount).fill(DEFAULT_CELL_HEIGHT)
  61. return {
  62. ...DEFAULT_TABLE,
  63. elId: createRandomCode(),
  64. width: colCount * DEFAULT_CELL_WIDTH + DEFAULT_BORDER_WIDTH,
  65. height: rowCount * DEFAULT_CELL_HEIGHT + DEFAULT_BORDER_WIDTH,
  66. colSizes,
  67. rowSizes,
  68. data,
  69. }
  70. }
  71. export const insertText = (position: CommonElementPosition) => {
  72. const { left, top, width, height } = position
  73. return {
  74. ...DEFAULT_TEXT,
  75. elId: createRandomCode(),
  76. left,
  77. top,
  78. width,
  79. height,
  80. }
  81. }
  82. export const insertShape = (position: CommonElementPosition, svgCode: string) => {
  83. const { left, top, width, height } = position
  84. return {
  85. ...DEFAULT_SHAPE,
  86. elId: createRandomCode(),
  87. left,
  88. top,
  89. width,
  90. height,
  91. svgCode,
  92. }
  93. }
  94. export const insertLine = (position: LineElementPosition, marker: [string, string], lineType: string) => {
  95. const { left, top, start, end } = position
  96. return {
  97. ...DEFAULT_LINE,
  98. elId: createRandomCode(),
  99. left,
  100. top,
  101. start,
  102. end,
  103. marker,
  104. lineType,
  105. }
  106. }