index.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. 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. readonly?: boolean
  36. notdel?: boolean
  37. };
  38. export type TableData = Partial<typeof defaultStyle> &
  39. BaseItem & {
  40. notaddRow?: boolean
  41. notaddCol?: boolean
  42. mat: number[];
  43. content: TableCollData[][];
  44. width: number;
  45. height: number;
  46. };
  47. export const getMouseStyle = (data: TableData) => {
  48. const strokeStatus = getMouseColors(data.stroke || defaultStyle.stroke);
  49. return {
  50. default: { stroke: strokeStatus.pub },
  51. hover: { stroke: strokeStatus.hover },
  52. press: { stroke: strokeStatus.press },
  53. };
  54. };
  55. export const getSnapPoints = (data: TableData) => {
  56. const tf = new Transform(data.mat);
  57. const points = getRectSnapPoints(data.width, data.height, 0, 0)
  58. .map((v) => tf.point(v));
  59. return points
  60. };
  61. export const getSnapInfos = (data: TableData) => {
  62. return generateSnapInfos(getSnapPoints(data), true, false);
  63. };
  64. export const interactiveToData: InteractiveTo<"table"> = ({
  65. info,
  66. preset = {},
  67. ...args
  68. }) => {
  69. if (info.cur) {
  70. const item = {
  71. ...defaultStyle,
  72. ...getBaseItem(),
  73. ...preset,
  74. } as unknown as TableData;
  75. return interactiveFixData({ ...args, info, data: item });
  76. }
  77. };
  78. export const autoCollWidth = 100;
  79. export const autoCollHeight = 50;
  80. export const interactiveFixData: InteractiveFix<"table"> = ({
  81. data,
  82. info,
  83. notdraw
  84. }) => {
  85. if (info.cur) {
  86. const area = info.cur!;
  87. const origin = {
  88. x: Math.min(area[0].x, area[1].x),
  89. y: Math.min(area[0].y, area[1].y),
  90. }
  91. data.width = Math.abs(area[0].x - area[1].x)
  92. data.height = Math.abs(area[0].y - area[1].y)
  93. if (!notdraw || !(data.content?.length && data.content[0].length)) {
  94. const colNum = Math.floor(data.width / autoCollWidth) || 1;
  95. const rawNum = Math.floor(data.height / autoCollHeight) || 1;
  96. const temp = data.content?.[0]?.[0] || {
  97. content: ""
  98. };
  99. data.content = Array.from({ length: rawNum }, () =>
  100. Array.from({ length: colNum }, () => ({
  101. ...temp,
  102. width: data.width / colNum,
  103. height: data.height / rawNum,
  104. padding: 8
  105. }))
  106. );
  107. } else {
  108. const colHeight = data.height / data.content.length
  109. const colWidth = data.width / data.content[0].length
  110. data.content.forEach(row => {
  111. row.forEach(col => {
  112. col.width = colWidth
  113. col.height = colHeight
  114. col.padding = 8
  115. console.log(col.content)
  116. })
  117. })
  118. }
  119. data.mat = new Transform().translate(origin.x, origin.y).m;
  120. }
  121. return data;
  122. };