useMapEditor.ts 3.2 KB

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