index.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. import { getBaseItem } from "../util.ts";
  2. import { Transform } from "konva/lib/Util";
  3. import { CircleData, defaultStyle as circleDefaultStyle } from "../circle";
  4. import { InteractiveFix, InteractiveTo, MatResponseProps } from "../index.ts";
  5. import { DrawStore } from "@/core/store/index.ts";
  6. import { Pos } from "@/utils/math.ts";
  7. import { TableData } from "../table/index.ts";
  8. import { copy } from "@/utils/shared.ts";
  9. export {
  10. getMouseStyle,
  11. getSnapInfos,
  12. getSnapPoints,
  13. dataToConfig,
  14. } from "../circle";
  15. export { default as GroupComponent } from "./serial-group.vue";
  16. export { default as Component } from "./serial.vue";
  17. export { default as TempComponent } from "./temp-serial.vue";
  18. export const defaultTableStyle = {
  19. right: 44,
  20. top: 175,
  21. fontSize: 16,
  22. colHeight: 32,
  23. padding: 8,
  24. nameColWidth: 100,
  25. valueColWidth: 180,
  26. repColCount: 1,
  27. tableStrokeWidth: 1,
  28. };
  29. export const shapeName = "序号";
  30. export const addMode = "dot";
  31. export const defaultStyle = {
  32. ...circleDefaultStyle,
  33. strokeWidth: 2,
  34. };
  35. export const fixedStrokeOptions:number[] = [];
  36. export type SerialData = CircleData;
  37. export const getSerialFontW = (data: SerialData) => {
  38. const fontSize = data.fontSize || defaultStyle.fontSize;
  39. const pad = data.padding || 0;
  40. const len = (data.content?.length || 1) * 0.6;
  41. return fontSize * len + pad * 2;
  42. };
  43. export const getSerialFontSizeByFontW = (data: SerialData, w: number) => {
  44. const pad = data.padding || 0;
  45. const len = (data.content?.length || 1) * 0.6;
  46. return (w - pad * 2) / len;
  47. };
  48. export const getCurrentNdxRaw = (serials: SerialData[]) => {
  49. const cs = serials.map((item) => Number(item.content || 0));
  50. let max = Math.max(...cs, 0) + 1;
  51. let i = 1;
  52. for (; i < max; i++) {
  53. if (!cs.includes(i)) {
  54. break;
  55. }
  56. }
  57. return i.toString();
  58. };
  59. export const getCurrentNdx = (store: DrawStore) => {
  60. return getCurrentNdxRaw(store.getTypeItems("serial"));
  61. };
  62. export const interactiveToData: InteractiveTo<"serial"> = ({
  63. info,
  64. preset = {},
  65. store,
  66. ...args
  67. }) => {
  68. if (info.cur) {
  69. let item = {
  70. ...defaultStyle,
  71. fontSize: defaultStyle.fontSize,
  72. ...getBaseItem(),
  73. padding: 3,
  74. content: getCurrentNdx(store),
  75. ...preset,
  76. } as unknown as SerialData;
  77. return interactiveFixData({ ...args, info, store, data: item });
  78. }
  79. };
  80. export const interactiveFixData: InteractiveFix<"serial"> = ({
  81. data,
  82. info,
  83. }) => {
  84. data.mat = new Transform().translate(info.cur!.x, info.cur!.y).m;
  85. const radius = (getSerialFontW(data) * Math.sqrt(2)) / 2;
  86. data.radiusX = radius;
  87. data.radiusY = radius;
  88. return data;
  89. };
  90. export const delItemRaw = (
  91. table: TableData,
  92. data: SerialData[],
  93. item: SerialData
  94. ) => {
  95. const ndx = data.indexOf(item);
  96. const getPosition = (itemNdx: number) => {
  97. const colCount = table.content[0].length / 2;
  98. const rowNdx = Math.floor(itemNdx / colCount);
  99. const colNdx = itemNdx % colCount;
  100. return [rowNdx, colNdx * colCount, colCount] as const;
  101. };
  102. if (table && ~ndx) {
  103. let i = ndx + 1;
  104. let s = getPosition(i - 1);
  105. let r;
  106. let oldItem = { ...data[i - 1] };
  107. for (; i < data.length; i++) {
  108. r = s;
  109. s = getPosition(i);
  110. for (let j = 0; j < s[2]; j++) {
  111. Object.assign(table.content[r[0]][r[1] + j], table.content[s[0]][s[1] + j])
  112. // table.content[r[0]][r[1] + j] = copy(table.content[s[0]][s[1] + j]);
  113. // if (j === 0) {
  114. // table.content[r[0]][r[1] + j].content = oldItem.content!;
  115. // } else {
  116. // table.content[r[0]][r[1] + j].content =
  117. // table.content[s[0]][s[1] + j].content!;
  118. // }
  119. }
  120. // let cur = { ...data[i] };
  121. // data[i].content = oldItem.content;
  122. // const radius = (getSerialFontW(data[i]) * Math.sqrt(2)) / 2;
  123. // data[i].radiusX = radius;
  124. // data[i].radiusY = radius;
  125. // matResponse({
  126. // data: data[i],
  127. // mat: new Transform(data[i].mat),
  128. // increment: false,
  129. // });
  130. // store.setItem("serial", { id: data[i].id, value: { ...data[i] } });
  131. // oldItem = cur;
  132. }
  133. console.log(copy(table))
  134. for (let j = 0; j < s[2]; j++) {
  135. console.log({...table.content[s[0]][s[1] + j]})
  136. table.content[s[0]][s[1] + j].content = "";
  137. // if (j === 0) {
  138. // table.content[r[0]][r[1] + j].content = oldItem.content!;
  139. // } else {
  140. // table.content[r[0]][r[1] + j].content =
  141. // table.content[s[0]][s[1] + j].content!;
  142. // }
  143. }
  144. console.log(copy(table))
  145. const cols = table.content.flatMap((row) => {
  146. const cols = [];
  147. for (let i = 0; i < row.length; i += 2) {
  148. cols.push(row[i]);
  149. }
  150. return cols;
  151. });
  152. const delRowCount = Math.floor(
  153. cols.filter((item) => item.content === "").length / 2
  154. );
  155. console.log('delRowCount', delRowCount, cols.filter((item) => item.content === "").length)
  156. for (let i = 0; i < delRowCount; i++) {
  157. table.height -= table.content.pop()![0].height;
  158. }
  159. }
  160. };
  161. export const joinKey = "serial-table";
  162. export const delItem = (store: DrawStore, item: SerialData) => {
  163. const table = store
  164. .getTypeItems("table")
  165. .find((item) => item.key === joinKey)!;
  166. const data = store.getTypeItems("serial");
  167. delItemRaw(table, data, item);
  168. if (table) {
  169. if (data.length === 1) {
  170. store.delItem("table", table.id);
  171. } else {
  172. store.setItem("table", { id: table.id, value: table });
  173. }
  174. }
  175. store.delItem("serial", item.id);
  176. };
  177. export const matResponse = (
  178. { data, mat, increment }: MatResponseProps<"serial">,
  179. initRadius?: Pos
  180. ) => {
  181. if (!initRadius) {
  182. initRadius = {
  183. x: data.radiusX,
  184. y: data.radiusY,
  185. };
  186. }
  187. if (increment) {
  188. mat = mat.copy().multiply(new Transform(data.mat));
  189. }
  190. const dec = mat.decompose();
  191. data.radiusX = data.radiusY = dec.scaleY * initRadius.y;
  192. const w = (data.radiusX * 2) / Math.sqrt(2);
  193. data.fontSize = getSerialFontSizeByFontW(data, w);
  194. data.mat = new Transform().translate(dec.x, dec.y).rotate(0).m;
  195. return data;
  196. };
  197. export const getPredefine = (key: keyof CircleData) => {
  198. if (key === "fill") {
  199. return { canun: true };
  200. }
  201. };