mrxy-yk 1.5.0 → 1.6.0-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,7 @@
1
1
  import { ElDialog, ElForm, ElFormItem, ElAutocomplete, ElInput, ElButton, ElCheckbox } from "element-plus/es";
2
- import { defineComponent, onMounted, watch, nextTick, computed, ref, onUnmounted, openBlock, createBlock, withCtx, createVNode, createElementVNode, toDisplayString, createElementBlock, createCommentVNode, createTextVNode } from "vue";
2
+ import { defineComponent, onMounted, watch, nextTick, computed, ref, shallowRef, onUnmounted, openBlock, createBlock, withCtx, createVNode, createElementVNode, toDisplayString, createElementBlock, createCommentVNode, createTextVNode } from "vue";
3
3
  import { ElMessage } from "element-plus";
4
- import { AMapLoader } from "../../utils/amap/index.js";
4
+ import { useAMapClassicHook } from "../../utils/amap/index.js";
5
5
  import { AMapConfig } from "../../config/index.js";
6
6
  const _hoisted_1 = { class: "autocomplete-subtitle" };
7
7
  const _hoisted_2 = { key: 0 };
@@ -30,14 +30,14 @@ var _sfc_main = /* @__PURE__ */ defineComponent({
30
30
  const mapKey = props.mapKey || AMapConfig.mapKey;
31
31
  const mapSecret = props.mapSecret || AMapConfig.mapSecret;
32
32
  watch(() => props.modelValue, (nv) => {
33
- if (nv && !map) {
33
+ if (nv && !aMapHook.value.map) {
34
34
  nextTick(() => {
35
35
  loadAMap();
36
36
  });
37
37
  } else {
38
38
  placeSearch.clear();
39
39
  showMarker(props.lnglat, props.address);
40
- emits("open", map);
40
+ emits("open", aMapHook.value.map);
41
41
  }
42
42
  });
43
43
  const changeDialogCustomer = computed({
@@ -63,28 +63,27 @@ var _sfc_main = /* @__PURE__ */ defineComponent({
63
63
  emits("update:address", pickupForm.value.address);
64
64
  emits("update:modelValue", false);
65
65
  };
66
- let AMap = null;
67
- let map = null;
68
- const coordinatePickupRef = ref();
69
- function loadAMap() {
70
- AMapLoader({
66
+ const coordinatePickupRef = shallowRef();
67
+ const aMapHook = useAMapClassicHook({
68
+ el: coordinatePickupRef,
69
+ mapStyle: props.mapStyle,
70
+ loaderOptions: {
71
71
  key: mapKey,
72
72
  secret: mapSecret,
73
- el: coordinatePickupRef.value,
74
- plugins: ["AMap.PlaceSearch", "AMap.AutoComplete", "AMap.Geocoder"],
75
- mapStyle: props.mapStyle
76
- }).then((res) => {
77
- AMap = res.AMap;
78
- map = res.map;
79
- satelliteLayer = new AMap["TileLayer"].Satellite();
80
- map.on("click", clickHandler);
81
- autoComplete = new AMap["Autocomplete"]({ city: "全国" });
82
- placeSearch = new AMap["PlaceSearch"]({ map, city: "全国", autoFitView: true });
83
- placeSearch.on("markerClick", placeMarkerClick);
84
- geocoder = new AMap["Geocoder"]();
85
- showMarker(props.lnglat, props.address);
86
- emits("open", { map, AMap });
87
- });
73
+ plugins: ["AMap.PlaceSearch", "AMap.AutoComplete", "AMap.Geocoder"]
74
+ }
75
+ }, loadAMap);
76
+ function loadAMap() {
77
+ const AMap = aMapHook.value.AMap;
78
+ const map = aMapHook.value.map;
79
+ satelliteLayer = new AMap["TileLayer"].Satellite();
80
+ map.on("click", clickHandler);
81
+ autoComplete = new AMap["Autocomplete"]({ city: "全国" });
82
+ placeSearch = new AMap["PlaceSearch"]({ map, city: "全国", autoFitView: true });
83
+ placeSearch.on("markerClick", placeMarkerClick);
84
+ geocoder = new AMap["Geocoder"]();
85
+ showMarker(props.lnglat, props.address);
86
+ emits("open", { map, AMap });
88
87
  }
89
88
  const clickHandler = async (e) => {
90
89
  const { lng, lat } = e.lnglat;
@@ -93,6 +92,7 @@ var _sfc_main = /* @__PURE__ */ defineComponent({
93
92
  showMarker(lnglat, address);
94
93
  };
95
94
  onUnmounted(function() {
95
+ const map = aMapHook.value.map;
96
96
  if (map) {
97
97
  map.off("click", clickHandler);
98
98
  map.destroy();
@@ -166,6 +166,7 @@ var _sfc_main = /* @__PURE__ */ defineComponent({
166
166
  }
167
167
  let marker = null;
168
168
  function showMarker(lnglat, address) {
169
+ const { map, AMap } = aMapHook.value;
169
170
  if (lnglat) {
170
171
  const position = lnglat.split(",");
171
172
  if (marker) {
@@ -185,6 +186,7 @@ var _sfc_main = /* @__PURE__ */ defineComponent({
185
186
  let satelliteLayer = null;
186
187
  const isSatelliteLayer = ref(false);
187
188
  function setSatelliteLayer() {
189
+ const { map } = aMapHook.value;
188
190
  if (isSatelliteLayer.value) {
189
191
  map.add(satelliteLayer);
190
192
  } else {
package/dist/index.js CHANGED
@@ -9,7 +9,7 @@ import { useEmptyHooks } from "./components/empty/hooks/index.js";
9
9
  import { MatchUnit } from "./utils/match-unit/index.js";
10
10
  import { TransitionFade } from "./utils/transition-fade/index.js";
11
11
  import { echarts } from "./utils/echarts/index.js";
12
- import { AMapLoader, newAMapInfoWindow, newAMapMap, newAMapMarkerCluster, useAMapLoader } from "./utils/amap/index.js";
12
+ import { newAMapInfoWindow, newAMapMap, newAMapMarkerCluster, useAMapClassicHook, useAMapLoader } from "./utils/amap/index.js";
13
13
  import { StorageUtil } from "./utils/storage/index.js";
14
14
  import { Pages } from "./entity/Pages.js";
15
15
  import { BigFloat } from "./entity/BigFloat.js";
@@ -49,7 +49,6 @@ var index = {
49
49
  }
50
50
  };
51
51
  export {
52
- AMapLoader,
53
52
  AMapUtil,
54
53
  ArrayUtil,
55
54
  BigFloat,
@@ -105,6 +104,7 @@ export {
105
104
  positiveNumberRule,
106
105
  positiveNumberTag,
107
106
  required,
107
+ useAMapClassicHook,
108
108
  useAMapLoader,
109
109
  useEChartsRef,
110
110
  useEcBarOption,
@@ -1,4 +1,4 @@
1
- import { AMapClassProxy, LoaderOptions, LoaderOptions1, AMapResolve } from './type';
1
+ import { AMapClassProxy, LoaderOptions, UseAMapHookExample, UseAMapHookOptions } from './type';
2
2
  import { AMap } from './@type/index';
3
3
  import { TemplateRef, ShallowRef } from 'vue';
4
4
  export * from './utils';
@@ -35,4 +35,10 @@ export declare function newAMapMarkerCluster(options: {
35
35
  * @param AMap { AMap } AMap存在时可以传入跳过`setAMap`过程
36
36
  */
37
37
  export declare function newAMapInfoWindow(options: ConstructorParameters<typeof AMap.InfoWindow>[0], AMap?: AMap): AMapClassProxy<AMap.InfoWindow>;
38
- export declare function AMapLoader(options: LoaderOptions1): Promise<AMapResolve>;
38
+ /**
39
+ * 常用高德地图配套功能包含地图(Map),聚合标记点(MarkerCluster),信息窗口(InfoWindow)
40
+ * 返回的hook实例需要在onMounted之后才能使用
41
+ * @param options
42
+ * @param callback
43
+ */
44
+ export declare function useAMapClassicHook(options: UseAMapHookOptions, callback?: (res: ShallowRef<UseAMapHookExample>) => void): ShallowRef<UseAMapHookExample>;
@@ -1,11 +1,11 @@
1
- import _AMapLoader from "@amap/amap-jsapi-loader";
1
+ import AMapLoader from "@amap/amap-jsapi-loader";
2
2
  import { AMapConfig } from "../../config/index.js";
3
- import { toValue } from "vue";
3
+ import { shallowRef, onMounted, useTemplateRef, onBeforeUnmount, toValue } from "vue";
4
4
  async function useAMapLoader(options) {
5
5
  const o = options || {};
6
6
  const win = window;
7
7
  win._AMapSecurityConfig = { securityJsCode: o.secret || AMapConfig.mapSecret };
8
- return await _AMapLoader.load({
8
+ return await AMapLoader.load({
9
9
  key: o.key || AMapConfig.mapKey,
10
10
  version: "2.0",
11
11
  plugins: o.plugins,
@@ -62,26 +62,56 @@ function newAMapInfoWindow(options, AMap) {
62
62
  return new AMap2.InfoWindow(o);
63
63
  }, AMap);
64
64
  }
65
- async function AMapLoader(options) {
65
+ function useAMapClassicHook(options, callback) {
66
66
  const o = options;
67
- const win = window;
68
- win._AMapSecurityConfig = { securityJsCode: o.secret || AMapConfig.mapSecret };
69
- const aMap = await _AMapLoader.load({
70
- key: o.key || AMapConfig.mapKey,
71
- version: "2.0",
72
- plugins: o.plugins,
73
- ...o.loadOption
67
+ const retRes = shallowRef({});
68
+ let map = null;
69
+ onMounted(async () => {
70
+ const AMap = await useAMapLoader({
71
+ plugins: o.plugins,
72
+ ...o?.loaderOptions
73
+ });
74
+ let elRef = shallowRef();
75
+ if (typeof o.el === "string") {
76
+ elRef = useTemplateRef(o.el);
77
+ } else if (o.el) {
78
+ elRef = o.el;
79
+ }
80
+ map = newAMapMap({
81
+ el: elRef.value,
82
+ mapStyle: o.mapStyle,
83
+ opts: o.mapOpts
84
+ }, AMap);
85
+ map.destroy();
86
+ const markerCluster = newAMapMarkerCluster({
87
+ map,
88
+ markerClusterOptions: Object.assign({
89
+ renderMarker: o.renderMarker,
90
+ renderClusterMarker: o.renderClusterMarker
91
+ }, o.markerClusterOpts)
92
+ }, AMap);
93
+ const infoWindow = newAMapInfoWindow({
94
+ isCustom: true,
95
+ content: o.infoWindowContent?.value || void 0,
96
+ ...o?.infoWindowOpts
97
+ }, AMap);
98
+ retRes.value = {
99
+ map,
100
+ markerCluster,
101
+ infoWindow,
102
+ AMap
103
+ };
104
+ callback?.(retRes);
74
105
  });
75
- const map = new aMap.Map(o.el, {
76
- mapStyle: o.mapStyle,
77
- ...o.mapOption
106
+ onBeforeUnmount(() => {
107
+ map.destroy();
78
108
  });
79
- return { AMap: aMap, map };
109
+ return retRes;
80
110
  }
81
111
  export {
82
- AMapLoader,
83
112
  newAMapInfoWindow,
84
113
  newAMapMap,
85
114
  newAMapMarkerCluster,
115
+ useAMapClassicHook,
86
116
  useAMapLoader
87
117
  };
@@ -2,7 +2,7 @@ import { AMap, AMapType } from './@type/AMap';
2
2
  import { LoadOption } from './@type';
3
3
  import { MapOptions } from './@type/Map';
4
4
  import { RenderMarkerCallbackParams } from './@type/MarkerCluster';
5
- type PluginsType = 'AMap.MarkerCluster' | 'AMap.Weather' | string
5
+ type PluginsType = 'AMap.MarkerCluster' | 'AMap.Weather' | 'AMap.AutoComplete' | 'AMap.Geocoder' | string
6
6
 
7
7
  export type LoaderOptions = {
8
8
  // key 高德地图key(缺省使用全局配置)
@@ -19,13 +19,20 @@ export type AMapClassProxy<T> = T & { setAMap (AMap: AMap): void }
19
19
 
20
20
  // region useAMapHook
21
21
  export type UseAMapHookOptions = {
22
- el: ShallowRef<HTMLDivElement>
22
+ // 加载选项
23
+ plugins?: PluginsType[]
24
+ loaderOptions?: LoaderOptions
25
+ // 地图
26
+ el: ShallowRef<HTMLDivElement> | string
23
27
  mapStyle?: string
28
+ mapOpts?: ConstructorParameters<typeof AMap.Map>[1]
29
+ // 聚合标记
24
30
  renderMarker? (e: RenderMarkerCallbackParams): void
25
31
  renderClusterMarker? (count: number, marker: AMap.Marker): void
26
- infoWindowContent: ShallowRef<HTMLDivElement>
27
- mapOpts?: ConstructorParameters<typeof AMap.Map>[1]
28
32
  markerClusterOpts?: ConstructorParameters<typeof AMap.MarkerCluster>[2]
33
+ // 信息窗体
34
+ infoWindowContent?: ShallowRef<HTMLDivElement>
35
+ infoWindowOpts?: ConstructorParameters<typeof AMap.InfoWindow>[0]
29
36
  }
30
37
 
31
38
  export type UseAMapHookExample = {
@@ -35,22 +42,3 @@ export type UseAMapHookExample = {
35
42
  AMap: AMap
36
43
  }
37
44
  // endregion
38
-
39
- export type LoaderOptions1 = {
40
- // key 高德地图key(缺省使用全局配置)
41
- key?: string,
42
- // secret 高德地图secret(缺省使用全局配置)
43
- secret?: string,
44
- // el 地图容器元素
45
- el: HTMLDivElement,
46
- // plugins 插件列表
47
- plugins?: string[],
48
- // mapStyle 地图样式
49
- mapStyle?: string,
50
- // loadOption 加载选项
51
- loadOption?: LoadOption,
52
- // mapOption 地图选项
53
- mapOption?: MapOptions
54
- }
55
-
56
- export type AMapResolve = { AMap: AMapType, map: AMap.Map }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mrxy-yk",
3
- "version": "1.5.0",
3
+ "version": "1.6.0-beta.2",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "description": "A collection of Vue 3 components and utilities",