index.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 { InteractiveFix, InteractiveTo } from "../index.ts";
  11. import { Size } from "@/utils/math.ts";
  12. export { default as Component } from "./table.vue";
  13. export { default as TempComponent } from "./temp-table.vue";
  14. export const shapeName = "表格";
  15. export const defaultStyle = {
  16. stroke: themeMouseColors.theme,
  17. strokeWidth: 1,
  18. fontSize: 16,
  19. align: "center",
  20. fontStyle: "normal",
  21. fontColor: themeMouseColors.theme,
  22. };
  23. export const defaultCollData = {
  24. fontFamily: "Calibri",
  25. fontSize: 16,
  26. align: "center",
  27. fontStyle: "normal",
  28. fontColor: themeMouseColors.theme,
  29. };
  30. export const addMode = "area";
  31. export type TableCollData = Partial<typeof defaultCollData> & Size & {
  32. content: string;
  33. padding: number
  34. readonly?: boolean
  35. notdel?: boolean
  36. };
  37. export type TableData = Partial<typeof defaultStyle> &
  38. BaseItem & Size & {
  39. notaddRow?: boolean
  40. notaddCol?: boolean
  41. mat: number[];
  42. content: TableCollData[][];
  43. };
  44. export const getMouseStyle = (data: TableData) => {
  45. const strokeStatus = getMouseColors(data.stroke || defaultStyle.stroke);
  46. return {
  47. default: { stroke: strokeStatus.pub },
  48. hover: { stroke: strokeStatus.hover },
  49. press: { stroke: strokeStatus.press },
  50. select: { select: strokeStatus.select },
  51. };
  52. };
  53. export const getSnapPoints = (data: TableData) => {
  54. const tf = new Transform(data.mat);
  55. const points = getRectSnapPoints(data.width, data.height, 0, 0)
  56. .map((v) => tf.point(v));
  57. return points
  58. };
  59. export const getSnapInfos = (data: TableData) => {
  60. return generateSnapInfos(getSnapPoints(data), true, false);
  61. };
  62. export const interactiveToData: InteractiveTo<"table"> = ({
  63. info,
  64. preset = {},
  65. ...args
  66. }) => {
  67. if (info.cur) {
  68. const item = {
  69. ...defaultStyle,
  70. ...getBaseItem(),
  71. ...preset,
  72. } as unknown as TableData;
  73. return interactiveFixData({ ...args, info, data: item });
  74. }
  75. };
  76. export const autoCollWidth = 100;
  77. export const autoCollHeight = 50;
  78. export const interactiveFixData: InteractiveFix<"table"> = ({
  79. data,
  80. info,
  81. notdraw
  82. }) => {
  83. if (info.cur) {
  84. const area = info.cur!;
  85. const origin = {
  86. x: Math.min(area[0].x, area[1].x),
  87. y: Math.min(area[0].y, area[1].y),
  88. }
  89. data.width = Math.abs(area[0].x - area[1].x)
  90. data.height = Math.abs(area[0].y - area[1].y)
  91. if (!notdraw || !(data.content?.length && data.content[0].length)) {
  92. const colNum = Math.floor(data.width / autoCollWidth) || 1;
  93. const rawNum = Math.floor(data.height / autoCollHeight) || 1;
  94. const temp = data.content?.[0]?.[0] || {
  95. content: ""
  96. };
  97. data.content = Array.from({ length: rawNum }, () =>
  98. Array.from({ length: colNum }, () => ({
  99. ...temp,
  100. width: data.width / colNum,
  101. height: data.height / rawNum,
  102. padding: 8
  103. }))
  104. );
  105. } else {
  106. const colHeight = data.height / data.content.length
  107. const colWidth = data.width / data.content[0].length
  108. data.content.forEach(row => {
  109. row.forEach(col => {
  110. col.width = colWidth
  111. col.height = colHeight
  112. col.padding = 8
  113. console.log(col.content)
  114. })
  115. })
  116. }
  117. data.mat = new Transform().translate(origin.x, origin.y).m;
  118. }
  119. return data;
  120. };