menu.ts 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import { ShapeType, shapeNames, PresetAdd } from '@/index.ts'
  2. import { v4 as uuid } from 'uuid'
  3. export type MenuItem = {
  4. icon: string,
  5. name: string,
  6. value?: string,
  7. children?: MenuItem[]
  8. payload?: PresetAdd
  9. }
  10. const genItem = <T extends ShapeType>(type: T, preset: PresetAdd<T>['preset'] = {}) => ({
  11. name: shapeNames[type],
  12. value: uuid(),
  13. payload: {type, preset}
  14. })
  15. export const getItem = (value: string, queryMenus = menus): MenuItem | undefined => {
  16. for (const menu of queryMenus) {
  17. const eqItem = menu.value === value
  18. ? menu
  19. : menu.children?.length ? getItem(value, menu.children) : void 0
  20. if (eqItem) return eqItem
  21. }
  22. }
  23. const eqPayload = (p1?: PresetAdd, p2?: PresetAdd) => {
  24. if (!p2 || !p1 || p1.type !== p2.type) return false;
  25. return !p1.preset || !p2.preset || toRaw(p1.preset) === toRaw(p2.preset)
  26. }
  27. export const getValue = (payload: PresetAdd, queryMenus = menus): string | undefined => {
  28. for (const menu of queryMenus) {
  29. const eqItem = eqPayload(menu.payload, payload)
  30. ? menu.value
  31. : menu.children?.length ? getValue(payload, menu.children) : void 0
  32. if (eqItem) return eqItem
  33. }
  34. }
  35. import svg1 from '../../assets/icons/vue.svg'
  36. import svg2 from '../../assets/icons/BedsideCupboard.svg'
  37. import { toRaw } from "vue";
  38. export const menus: MenuItem[] = [
  39. {
  40. icon: '',
  41. name: '绘制',
  42. value: uuid(),
  43. children: [
  44. {
  45. icon: '',
  46. ...genItem('line')
  47. },
  48. {
  49. icon: '',
  50. ...genItem('arrow')
  51. },
  52. {
  53. icon: '',
  54. ...genItem('rectangle')
  55. },
  56. {
  57. icon: '',
  58. ...genItem('circle')
  59. },
  60. {
  61. icon: '',
  62. ...genItem('triangle')
  63. },
  64. {
  65. icon: '',
  66. ...genItem('polygon')
  67. },
  68. ]
  69. },
  70. {
  71. icon: '',
  72. name: '图例',
  73. value: uuid(),
  74. children: [
  75. {
  76. icon: '',
  77. ...genItem('icon', { url: svg1, width: 500, height: 500 }),
  78. name: 'vue'
  79. },
  80. {
  81. icon: '',
  82. ...genItem('icon', { url: svg2, width: 300, height: 300, stroke: 'red', strokeWidth: 1, strokeScaleEnabled: false }),
  83. name: '自定义'
  84. }
  85. ]
  86. },
  87. {
  88. icon: '',
  89. ...genItem('text')
  90. },
  91. ]