index.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import { Pos } from "@/utils/math.ts";
  2. import { getMouseColors } from "@/utils/colors.ts";
  3. import { onlyId } from "@/utils/shared.ts";
  4. import { BaseItem, generateSnapInfos, getBaseItem } from "../util.ts";
  5. import { InteractiveFix, InteractiveTo, MatResponseProps } from "../index.ts";
  6. import { Transform } from "konva/lib/Util";
  7. import { ui18n } from "@/lang/index.ts";
  8. export { default as Component } from "./rectangle.vue";
  9. export { default as TempComponent } from "./temp-rectangle.vue";
  10. export const shapeName = ui18n.t('shape.rect.name');
  11. export const defaultStyle = {
  12. dash: [30, 0],
  13. fixed: true,
  14. strokeWidth: 5,
  15. stroke: "#000000",
  16. fontSize: 22,
  17. align: "center",
  18. fontStyle: "normal",
  19. fontColor: "#000000",
  20. };
  21. export const fixedStrokeOptions:number[] = [];
  22. export const getMouseStyle = (data: RectangleData) => {
  23. const fillStatus = data.fill && getMouseColors(data.fill);
  24. const strokeStatus = data.stroke && getMouseColors(data.stroke);
  25. const strokeWidth = data.strokeWidth;
  26. return {
  27. default: {
  28. fill: data.fill,
  29. stroke: data.stroke,
  30. strokeWidth,
  31. },
  32. hover: { fill: fillStatus && fillStatus.hover, stroke: strokeStatus && strokeStatus.hover },
  33. focus: { fill: fillStatus && fillStatus.hover, stroke: strokeStatus && strokeStatus.hover },
  34. press: { fill: fillStatus && fillStatus.press, stroke: strokeStatus && strokeStatus.press },
  35. select: {
  36. fill: fillStatus && fillStatus.select,
  37. stroke: strokeStatus && strokeStatus.select,
  38. },
  39. };
  40. };
  41. export const addMode = "area";
  42. export type RectangleData = Partial<typeof defaultStyle> &
  43. BaseItem & {
  44. id: string;
  45. attitude: number[];
  46. points: Pos[];
  47. createTime: number;
  48. zIndex: number;
  49. dash?: number[];
  50. stroke?: string;
  51. fill?: string;
  52. strokeWidth?: number;
  53. content?: string;
  54. };
  55. export const getSnapPoints = (data: RectangleData) => {
  56. return data.points;
  57. };
  58. export const getSnapInfos = (data: RectangleData) =>
  59. generateSnapInfos(getSnapPoints(data), true, false);
  60. export const interactiveToData: InteractiveTo<"rectangle"> = ({
  61. info,
  62. preset = {},
  63. ...args
  64. }) => {
  65. if (info.cur) {
  66. const item = {
  67. ...getBaseItem(),
  68. ...defaultStyle,
  69. fill: null,
  70. ...preset,
  71. id: onlyId(),
  72. createTime: Date.now(),
  73. zIndex: 0,
  74. } as unknown as RectangleData;
  75. return interactiveFixData({ ...args, info, data: item });
  76. }
  77. };
  78. export const interactiveFixData: InteractiveFix<"rectangle"> = ({
  79. data,
  80. info,
  81. }) => {
  82. if (info.cur) {
  83. const area = info.cur!;
  84. const width = area[1].x - area[0].x;
  85. const height = area[1].y - area[0].y;
  86. data.points = [
  87. info.cur[0],
  88. { x: info.cur[0].x + width, y: info.cur[0].y },
  89. { x: info.cur[0].x + width, y: info.cur[0].y + height },
  90. { x: info.cur[0].x, y: info.cur[0].y + height },
  91. ];
  92. data.attitude = [1, 0, 0, 1, 0, 0];
  93. }
  94. return data;
  95. };
  96. export const matResponse = ({
  97. data,
  98. mat,
  99. increment,
  100. }: MatResponseProps<"rectangle">) => {
  101. let transfrom: Transform;
  102. const attitude = new Transform(data.attitude);
  103. if (!increment) {
  104. const inverMat = attitude.copy().invert();
  105. transfrom = mat.copy().multiply(inverMat);
  106. } else {
  107. transfrom = mat;
  108. }
  109. data.points = data.points.map((v) => transfrom.point(v));
  110. data.attitude = transfrom.copy().multiply(attitude).m;
  111. return data;
  112. };
  113. export const getPredefine = (key: keyof RectangleData) => {
  114. if (["fill", "stroke"].includes(key)) {
  115. return { canun: true };
  116. }
  117. };