useMapEditor.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import { ref } from 'vue';
  2. import { defineStore } from 'pinia';
  3. import { WorkShopInfoItem } from '@/api/scene/scene';
  4. import { cloneDeep } from 'lodash-es';
  5. import { useGlobSetting } from '@/hooks/setting';
  6. import safeParse from '@/utils/safeParse';
  7. import urlJoin from 'url-join';
  8. import { resolve } from 'path';
  9. // import emptyImg from '@/assets/images/table/table-empty.png';
  10. export enum LabelPositionEnum {
  11. LEFT = 'left',
  12. RIGHT = 'right',
  13. TOP = 'top',
  14. }
  15. export type EditStyle = {
  16. x: number;
  17. y: number;
  18. // scale: number;
  19. scaleX: number;
  20. scaleY: number;
  21. thumbnail?: string;
  22. bgColor: string;
  23. fontSize: number;
  24. fontColor: string;
  25. posType: LabelPositionEnum;
  26. };
  27. export type MapWorkShopInfoItem = WorkShopInfoItem & EditStyle;
  28. export const useMapEditor = defineStore('home-map-ediotr', () => {
  29. const globSetting = useGlobSetting();
  30. const bgImg = ref('');
  31. const mapWidth = ref(0);
  32. const mapHeight = ref(0);
  33. const addedShops = ref<MapWorkShopInfoItem[]>([]);
  34. const showShops = ref<MapWorkShopInfoItem[]>([]);
  35. const activeShopId = ref<number>();
  36. /** konva elements refs */
  37. const bgImage = ref<HTMLImageElement>(new Image());
  38. const clearBg = () => {
  39. bgImage.value.src = '';
  40. };
  41. const addBg = () => {
  42. return new Promise((resolve) => {
  43. const imgUrl = urlJoin(globSetting.imgUrl!, bgImg.value);
  44. const tempImg = new Image();
  45. tempImg.onload = () => {
  46. mapWidth.value = tempImg.width;
  47. mapHeight.value = tempImg.height;
  48. bgImage.value = tempImg;
  49. };
  50. tempImg.src = imgUrl;
  51. resolve(null);
  52. });
  53. };
  54. const addShop = (shop: MapWorkShopInfoItem) => {
  55. activeShopId.value = shop.id;
  56. addedShops.value.push(cloneDeep(shop));
  57. showShops.value.push(cloneDeep(shop));
  58. };
  59. const calcLayout = (json: string) => {
  60. const mapJson = safeParse(json);
  61. const mapData = mapJson.children[0].children.filter((item) => item.className === 'Group');
  62. const shopListAdded = addedShops.value.map((item, index) => {
  63. item.x = mapData[index].attrs.x;
  64. item.y = mapData[index].attrs.y;
  65. item.scaleX = mapData[index].attrs.scaleX || 1;
  66. item.scaleY = mapData[index].attrs.scaleY || 1;
  67. return item;
  68. });
  69. const layout = {
  70. bgInfo: {
  71. width: mapWidth.value,
  72. height: mapHeight.value,
  73. img: bgImg.value,
  74. },
  75. shopList: shopListAdded,
  76. };
  77. return JSON.stringify(layout);
  78. };
  79. const createMap = (layout) => {
  80. bgImg.value = layout.bgInfo.img;
  81. addBg().then(() => {
  82. mapWidth.value = layout.bgInfo.width;
  83. mapHeight.value = layout.bgInfo.height;
  84. addedShops.value = cloneDeep(layout.shopList);
  85. showShops.value = cloneDeep(layout.shopList);
  86. });
  87. };
  88. const deleteShop = () => {
  89. addedShops.value = addedShops.value.filter((item) => item.id !== Number(activeShopId.value));
  90. showShops.value = showShops.value.filter((item) => item.id !== Number(activeShopId.value));
  91. activeShopId.value = undefined;
  92. };
  93. const resetMap = () => {
  94. bgImg.value = '';
  95. mapWidth.value = 0;
  96. mapHeight.value = 0;
  97. addedShops.value = [];
  98. showShops.value = [];
  99. activeShopId.value = undefined;
  100. clearBg();
  101. };
  102. return {
  103. bgImg,
  104. mapWidth,
  105. mapHeight,
  106. addedShops,
  107. showShops,
  108. activeShopId,
  109. bgImage,
  110. addShop,
  111. addBg,
  112. calcLayout,
  113. createMap,
  114. deleteShop,
  115. resetMap,
  116. };
  117. });
  118. export default useMapEditor;