index.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import { Transform } from "konva/lib/Util";
  2. import { themeMouseColors } from "@/constant/help-style.ts";
  3. import {
  4. BaseItem,
  5. generateSnapInfos,
  6. getBaseItem,
  7. getRectSnapPoints,
  8. } from "../util.ts";
  9. import { getMouseColors } from "@/utils/colors.ts";
  10. import { AddMessage } from "@/core/hook/use-draw.ts";
  11. export { default as Component } from "./table.vue";
  12. export { default as TempComponent } from "./temp-table.vue";
  13. export const shapeName = "表格";
  14. export const defaultStyle = {
  15. stroke: themeMouseColors.theme,
  16. strokeWidth: 1,
  17. fontSize: 16,
  18. align: "center",
  19. fontStyle: "normal",
  20. fontColor: themeMouseColors.theme,
  21. };
  22. export const defaultCollData = {
  23. fontFamily: "Calibri",
  24. fontSize: 16,
  25. align: "center",
  26. fontStyle: "normal",
  27. fontColor: themeMouseColors.theme,
  28. };
  29. export const addMode = "area";
  30. export type TableCollData = Partial<typeof defaultCollData> & {
  31. content: string;
  32. width: number;
  33. height: number;
  34. padding: number
  35. };
  36. export type TableData = Partial<typeof defaultStyle> &
  37. BaseItem & {
  38. mat: number[];
  39. content: TableCollData[][];
  40. width: number;
  41. height: number;
  42. };
  43. export const getMouseStyle = (data: TableData) => {
  44. const strokeStatus = getMouseColors(data.stroke || defaultStyle.stroke);
  45. return {
  46. default: { stroke: strokeStatus.pub },
  47. hover: { stroke: strokeStatus.hover },
  48. press: { stroke: strokeStatus.press },
  49. };
  50. };
  51. export const getSnapPoints = (data: TableData) => {
  52. const tf = new Transform(data.mat);
  53. const points = getRectSnapPoints(data.width, data.height, 0, 0)
  54. .map((v) => tf.point(v));
  55. return points
  56. };
  57. export const getSnapInfos = (data: TableData) => {
  58. return generateSnapInfos(getSnapPoints(data), true, false);
  59. };
  60. export const interactiveToData = (
  61. info: AddMessage<"table">,
  62. preset: Partial<TableData> = {}
  63. ): TableData | undefined => {
  64. if (info.cur) {
  65. const item = {
  66. ...defaultStyle,
  67. ...getBaseItem(),
  68. ...preset,
  69. } as unknown as TableData;
  70. return interactiveFixData(item, info);
  71. }
  72. };
  73. const autoCollWidth = 100;
  74. const autoCollHeight = 50;
  75. export const interactiveFixData = (
  76. data: TableData,
  77. info: AddMessage<"table">
  78. ) => {
  79. if (info.cur) {
  80. const area = info.cur!;
  81. const origin = {
  82. x: Math.min(area[0].x, area[1].x),
  83. y: Math.min(area[0].y, area[1].y),
  84. }
  85. data.width = Math.abs(area[0].x - area[1].x)
  86. data.height = Math.abs(area[0].y - area[1].y)
  87. const colNum = Math.floor(data.width / autoCollWidth) || 1;
  88. const rawNum = Math.floor(data.height / autoCollHeight) || 1;
  89. const temp = data.content?.[0]?.[0] || {
  90. content: ""
  91. };
  92. data.content = Array.from({ length: rawNum }, () =>
  93. Array.from({ length: colNum }, () => ({
  94. ...temp,
  95. width: data.width / colNum,
  96. height: data.height / rawNum,
  97. padding: 8
  98. }))
  99. );
  100. data.mat = new Transform().translate(origin.x, origin.y).m;
  101. }
  102. return data;
  103. };