actions.ts 7.3 KB

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