index.ts 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import { Pos } from "@/draw/utils/math.ts";
  2. import { flatPositions } from "@/draw/utils/shared.ts";
  3. import { InteractiveAction, InteractiveMessage } from "../../hook/use-interactive.ts";
  4. import { LineConfig } from "konva/lib/shapes/Line";
  5. export { default as Component } from "./line.vue";
  6. export const shapeName = "线段";
  7. export const defaultStyle = {
  8. stroke: "#000",
  9. strokeWidth: 10,
  10. };
  11. export const addMode = 'dots'
  12. export const style = {
  13. default: defaultStyle,
  14. focus: defaultStyle,
  15. hover: defaultStyle,
  16. };
  17. export type LineData = Partial<typeof defaultStyle> & { points: Pos[] };
  18. export const dataToConfig = (data: LineData): LineConfig => ({
  19. ...defaultStyle,
  20. ...data,
  21. points: flatPositions(data.points),
  22. });
  23. export const interactiveToData = (
  24. info: InteractiveMessage,
  25. preset: Partial<LineData> = {}
  26. ): LineData | undefined => {
  27. if (info.dot) {
  28. return interactiveFixData({ ...preset, points: [] }, info);
  29. }
  30. };
  31. export const interactiveFixData = (
  32. data: LineData,
  33. info: InteractiveMessage
  34. ) => {
  35. if (info.action === InteractiveAction.delete) {
  36. data.points.pop()
  37. } else {
  38. data.points[info.ndx!] = info.dot!
  39. }
  40. return data;
  41. };