actions.ts 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. import { DrawItem, shapeNames, ShapeType } from "@/index";
  2. import { v4 as uuid } from "uuid";
  3. import { getAMapInfo } from "../../dialog/basemap";
  4. import { getImageSize } from "@/utils/shape";
  5. import { selectAI } from "../../dialog/ai";
  6. import { drawPlatformResource } from "../../platform/platform-draw";
  7. import { selectFile } from "@/utils/dom";
  8. import { getImage } from "@/utils/resource";
  9. import { Draw } from "../container/use-draw";
  10. import { MenuItem } from "./menu";
  11. import { copy } from "@/utils/shared";
  12. import { ElMessage } from "element-plus";
  13. export type PresetAdd<T extends ShapeType = ShapeType> = {
  14. type: T;
  15. preset?: Partial<DrawItem<T>>;
  16. };
  17. const genDrawItem = <T extends ShapeType>(
  18. type: T,
  19. preset: PresetAdd<T>["preset"] = {}
  20. ) => ({
  21. name: shapeNames[type],
  22. value: uuid(),
  23. payload: { type, preset },
  24. });
  25. export const draw: MenuItem = {
  26. icon: "line_d",
  27. name: "绘制",
  28. value: uuid(),
  29. children: [
  30. { icon: "line", ...genDrawItem('sequentLine') },
  31. { icon: "line", ...genDrawItem('line') },
  32. { icon: "arrows", ...genDrawItem("arrow") },
  33. { icon: "rectangle", ...genDrawItem("rectangle") },
  34. { icon: "circle", ...genDrawItem("circle") },
  35. { icon: "triangulartriangular", ...genDrawItem("triangle") },
  36. { icon: "line", ...genDrawItem("polygon") },
  37. ],
  38. };
  39. export const text: MenuItem = {
  40. icon: "text",
  41. ...genDrawItem("text", { content: "文本" }),
  42. };
  43. export const table: MenuItem = {
  44. icon: "table",
  45. ...genDrawItem("table", {}),
  46. };
  47. export const serial: MenuItem = {
  48. icon: "order_no",
  49. ...genDrawItem("serial", { content: "1" }),
  50. };
  51. export const imp: MenuItem = {
  52. icon: 'import',
  53. name: "导入",
  54. value: uuid(),
  55. children: [
  56. {
  57. value: uuid(),
  58. icon: "scene_i",
  59. name: "场景",
  60. handler: async (draw: Draw) => {
  61. const aiData = await selectAI();
  62. drawPlatformResource(aiData, draw);
  63. }
  64. },
  65. {
  66. value: uuid(),
  67. icon: "local_i",
  68. name: "本地",
  69. handler: async (draw: Draw) => {
  70. const files = await selectFile(false, 'image/jpeg,image/png')
  71. const url = await window.platform.uploadResourse(files[0])
  72. const image = await getImage(url)
  73. ElMessage.warning('请在画图面板中选择放置位置,鼠标右键取消')
  74. draw.enterDrawShape('image', {
  75. width: image.width,
  76. height: image.height,
  77. url
  78. }, true)
  79. }
  80. }
  81. ]
  82. }
  83. export const getPaperConfig = (p: number[], scale: number) => {
  84. const pad = 5 * scale;
  85. const size = { width: p[0] * scale, height: p[1] * scale };
  86. const margin = [pad, pad, pad, pad * 5];
  87. return {size, margin}
  88. }
  89. export const paperConfigs = {
  90. 'a4': { size: [297, 210], scale: 3.8 },
  91. 'a3': { size: [420, 297], scale: 2.5 }
  92. }
  93. const setPaper = (draw: Draw, p: number[], scale: number) => {
  94. const { size, margin } = getPaperConfig(p, scale)
  95. // draw.config.size = size;
  96. draw.viewer.setViewSize(size)
  97. draw.config.back = { color: "#fff", opacity: 1 };
  98. draw.config.border = {
  99. margin: margin,
  100. lineWidth: 1,
  101. color: "#000",
  102. opacity: 1,
  103. };
  104. draw.config.margin = margin;
  105. };
  106. export const paper = {
  107. icon: "drawing",
  108. name: "纸张",
  109. type: 'sub-menu-horizontal',
  110. value: uuid(),
  111. children: [
  112. // {
  113. // value: uuid(),
  114. // icon: "A4_v",
  115. // key: 'a4',
  116. // name: "A4竖版",
  117. // handler: (draw: Draw) => setPaper(draw, [210, 297], 2.8),
  118. // },
  119. {
  120. value: uuid(),
  121. icon: "A4_h",
  122. key: 'a4',
  123. name: "A4横版",
  124. handler: (draw: Draw) => setPaper(draw, paperConfigs.a4.size, paperConfigs.a4.scale),
  125. },
  126. // {
  127. // value: uuid(),
  128. // icon: "A3_v",
  129. // key: 'a3',
  130. // name: "A3竖版",
  131. // handler: (draw: Draw) => setPaper(draw, [297, 450], 1.8),
  132. // },
  133. {
  134. value: uuid(),
  135. icon: "A3_h",
  136. key: 'a3',
  137. name: "A3横版",
  138. handler: (draw: Draw) => setPaper(draw, paperConfigs.a3.size, paperConfigs.a3.scale),
  139. },
  140. ],
  141. };
  142. export const dbImage: MenuItem = {
  143. value: uuid(),
  144. icon: "",
  145. name: "底图",
  146. children: [
  147. {
  148. value: uuid(),
  149. icon: "",
  150. name: "高德地图",
  151. handler: async (draw: Draw) => {
  152. const info = await getAMapInfo();
  153. const size = await getImageSize(info.blob);
  154. let proportion = { scale: info.ratio * 100, unit: "mm" };
  155. if (info.ratio > 1000) {
  156. proportion = { scale: info.ratio / 1000, unit: "km" };
  157. } else if (info.ratio > 1) {
  158. proportion = { scale: info.ratio, unit: "m" };
  159. }
  160. const url = await window.platform.uploadResourse(new File([info.blob], 'map.png'))
  161. draw.history.onceTrack(() => {
  162. draw.addShape(
  163. "image",
  164. {
  165. ...size,
  166. url,
  167. zIndex: -1,
  168. },
  169. { x: window.innerWidth / 2, y: window.innerHeight / 2 },
  170. true
  171. );
  172. draw.store.setConfig({ proportion });
  173. });
  174. },
  175. },
  176. ],
  177. };
  178. export const test: MenuItem = {
  179. value: uuid(),
  180. icon: "debugger",
  181. name: "测试",
  182. type: 'sub-menu-horizontal',
  183. children: [
  184. {
  185. value: uuid(),
  186. icon: "",
  187. name: "视图恢复",
  188. handler: (draw: Draw) => {
  189. draw.viewer.setViewMat( [1, 0, 0, 1, 0, 0])
  190. },
  191. },
  192. {
  193. value: uuid(),
  194. icon: "",
  195. name: "切换栅栏显示",
  196. handler: (draw: Draw) => {
  197. draw.config.showGrid = !draw.config.showGrid;
  198. },
  199. },
  200. {
  201. value: uuid(),
  202. icon: "",
  203. name: "切换标注线显示",
  204. handler: (draw: Draw) => {
  205. draw.config.showLabelLine = !draw.config.showLabelLine;
  206. },
  207. },
  208. {
  209. value: uuid(),
  210. icon: "",
  211. name: "切换指南针显示",
  212. handler: (draw: Draw) => {
  213. draw.config.showCompass = !draw.config.showCompass;
  214. },
  215. },
  216. {
  217. value: uuid(),
  218. icon: "",
  219. name: "切换组件大小显示",
  220. handler: (draw: Draw) => {
  221. draw.config.showComponentSize = !draw.config.showComponentSize;
  222. },
  223. },
  224. {
  225. value: uuid(),
  226. icon: "",
  227. name: "碰撞检测",
  228. handler: (draw: Draw) => {
  229. draw.toggleHit();
  230. },
  231. },
  232. {
  233. value: uuid(),
  234. icon: "",
  235. name: "获取当前数据",
  236. handler: (draw: Draw) => {
  237. console.log(copy(draw.store.$state))
  238. },
  239. }
  240. ],
  241. };