actions.ts 7.4 KB

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