@summeruse/ol 0.5.1 → 0.6.0

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.
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { computed, createElementBlock, defineComponent, inject, onBeforeUnmount, onMounted, onUnmounted, openBlock, provide, ref, render, renderSlot, toValue, unref, watch } from "vue";
1
+ import { computed, createElementBlock, defineComponent, inject, onBeforeUnmount, onMounted, onUnmounted, openBlock, provide, ref, render, renderSlot, shallowRef, toValue, unref, watch } from "vue";
2
2
  import { Feature, Map as Map$1, Overlay, View } from "ol";
3
3
  import { Attribution, FullScreen, OverviewMap, Rotate, ScaleLine, Zoom } from "ol/control";
4
4
  import { DoubleClickZoom, DragPan, DragRotate, Draw, KeyboardPan, KeyboardZoom, Modify, MouseWheelZoom, PinchRotate, PinchZoom } from "ol/interaction";
@@ -9986,6 +9986,101 @@ function useGraticule(options) {
9986
9986
  return { showGraticule };
9987
9987
  }
9988
9988
 
9989
+ //#endregion
9990
+ //#region composables/useMapClick/index.ts
9991
+ /**
9992
+ * 通用的点击处理 Hook
9993
+ */
9994
+ function useMapClickHandler(options) {
9995
+ const { mapRef, items } = options;
9996
+ let currentMap;
9997
+ /** 按优先级和 hitTolerance 分组 */
9998
+ function groupConfigsByPriority(configs) {
9999
+ const sorted = [...configs].sort((a, b$1) => (b$1.priority ?? 0) - (a.priority ?? 0));
10000
+ const groups = [];
10001
+ let currentGroup = null;
10002
+ sorted.forEach((item) => {
10003
+ const tolerance = item.hitTolerance ?? 0;
10004
+ if (!currentGroup || currentGroup.tolerance !== tolerance) {
10005
+ currentGroup = {
10006
+ tolerance,
10007
+ items: [item]
10008
+ };
10009
+ groups.push(currentGroup);
10010
+ } else currentGroup.items.push(item);
10011
+ });
10012
+ return groups;
10013
+ }
10014
+ /** 创建点击事件处理器 */
10015
+ function handler(evt) {
10016
+ const map = evt.map;
10017
+ if (!map) return;
10018
+ const grouped = groupConfigsByPriority(toValue(items));
10019
+ let foundFeature;
10020
+ let foundLayer;
10021
+ for (const group of grouped) {
10022
+ map.forEachFeatureAtPixel(evt.pixel, (feature, layer) => {
10023
+ foundFeature = feature;
10024
+ foundLayer = layer;
10025
+ return true;
10026
+ }, { hitTolerance: group.tolerance });
10027
+ if (!foundFeature) continue;
10028
+ const context = {
10029
+ map,
10030
+ coordinate: evt.coordinate,
10031
+ pixel: evt.pixel,
10032
+ feature: foundFeature,
10033
+ layer: foundLayer
10034
+ };
10035
+ for (const config of group.items) {
10036
+ const visible = config.visible;
10037
+ if (typeof visible === "function" ? visible(context) : true) return {
10038
+ config,
10039
+ feature: foundFeature,
10040
+ layer: foundLayer
10041
+ };
10042
+ }
10043
+ }
10044
+ }
10045
+ /** 绑定事件 */
10046
+ function bindMapEvents(map) {
10047
+ map.on(options.type, handler);
10048
+ }
10049
+ /** 解绑事件 */
10050
+ function unbindMapEvents(map) {
10051
+ map.un(options.type, handler);
10052
+ }
10053
+ /** 监听 mapRef 变化 */
10054
+ watch(() => toValue(mapRef), (newMap, oldMap) => {
10055
+ if (oldMap) unbindMapEvents(oldMap);
10056
+ if (oldMap !== newMap) {
10057
+ currentMap = newMap;
10058
+ if (newMap) bindMapEvents(newMap);
10059
+ }
10060
+ }, { immediate: true });
10061
+ onBeforeUnmount(() => {
10062
+ if (currentMap) unbindMapEvents(currentMap);
10063
+ });
10064
+ }
10065
+ function useMapClick(mapRef, type) {
10066
+ const itemMap = shallowRef({});
10067
+ useMapClickHandler({
10068
+ mapRef,
10069
+ items: computed(() => Object.values(itemMap.value).flat()),
10070
+ type
10071
+ });
10072
+ const add$1 = (key$1, items) => {
10073
+ itemMap.value[key$1] = items;
10074
+ };
10075
+ const remove = (key$1) => {
10076
+ delete itemMap.value[key$1];
10077
+ };
10078
+ return {
10079
+ add: add$1,
10080
+ remove
10081
+ };
10082
+ }
10083
+
9989
10084
  //#endregion
9990
10085
  //#region composables/usePointermove/index.ts
9991
10086
  function usePointermove({ mapRef, items, forceUpdate = false }) {
@@ -10010,15 +10105,22 @@ function usePointermove({ mapRef, items, forceUpdate = false }) {
10010
10105
  let currentMap;
10011
10106
  let viewport;
10012
10107
  let originalCursor = "";
10013
- /** 查找匹配的 tooltip 配置 */
10014
- function findMatchingPointermove(params$1) {
10015
- const tooltips = toValue(items);
10016
- tooltips.sort((a, b$1) => (b$1.priority ?? 0) - (a.priority ?? 0));
10017
- return tooltips.find((item) => {
10018
- const shouldShow = item.visible;
10019
- if (typeof shouldShow === "function") return shouldShow(params$1);
10020
- return shouldShow ?? true;
10108
+ /** 按优先级排序,然后按相邻相同 tolerance 分组 */
10109
+ function groupItemsByPriorityWithToleranceMerge(tooltips) {
10110
+ const sortedByPriority = [...tooltips].sort((a, b$1) => (b$1.priority ?? 0) - (a.priority ?? 0));
10111
+ const groups = [];
10112
+ let currentGroup = null;
10113
+ sortedByPriority.forEach((item) => {
10114
+ const tolerance = item.hitTolerance ?? 0;
10115
+ if (!currentGroup || currentGroup.tolerance !== tolerance) {
10116
+ currentGroup = {
10117
+ tolerance,
10118
+ items: [item]
10119
+ };
10120
+ groups.push(currentGroup);
10121
+ } else currentGroup.items.push(item);
10021
10122
  });
10123
+ return groups;
10022
10124
  }
10023
10125
  /** 显示提示 */
10024
10126
  function show(evt) {
@@ -10028,39 +10130,51 @@ function usePointermove({ mapRef, items, forceUpdate = false }) {
10028
10130
  originalCoordinate.value = _coordinate;
10029
10131
  coordinate.value = _coordinate;
10030
10132
  let pixel = evt.pixel;
10133
+ const groupedItems = groupItemsByPriorityWithToleranceMerge(toValue(items));
10134
+ const { top, left } = viewport.getBoundingClientRect();
10135
+ let matchedPointermove;
10031
10136
  let foundFeature;
10032
10137
  let foundLayer;
10033
- currentMap.forEachFeatureAtPixel(pixel, (feature$1, layer) => {
10034
- foundFeature = feature$1;
10035
- foundLayer = layer;
10036
- return true;
10037
- });
10038
- if (!foundFeature) {
10138
+ for (const group of groupedItems) {
10139
+ currentMap.forEachFeatureAtPixel(pixel, (feature$1, layer) => {
10140
+ foundFeature = feature$1;
10141
+ foundLayer = layer;
10142
+ return true;
10143
+ }, { hitTolerance: group.tolerance });
10144
+ if (!foundFeature) {
10145
+ foundFeature = void 0;
10146
+ foundLayer = void 0;
10147
+ continue;
10148
+ }
10149
+ const params$1 = {
10150
+ map: currentMap,
10151
+ position: {
10152
+ x: pixel[0] + left,
10153
+ y: pixel[1] + top
10154
+ },
10155
+ coordinate: _coordinate,
10156
+ feature: foundFeature,
10157
+ layer: foundLayer
10158
+ };
10159
+ for (const item of group.items) {
10160
+ const shouldShow = item.visible;
10161
+ if (typeof shouldShow === "function" ? shouldShow(params$1) : shouldShow ?? true) {
10162
+ matchedPointermove = item;
10163
+ break;
10164
+ }
10165
+ }
10166
+ if (matchedPointermove) break;
10167
+ foundFeature = void 0;
10168
+ foundLayer = void 0;
10169
+ }
10170
+ if (!foundFeature || !matchedPointermove) {
10039
10171
  hide();
10040
10172
  return;
10041
10173
  }
10042
10174
  if (!forceUpdate && feature.value && feature.value.getId() === foundFeature.getId()) return;
10043
10175
  feature.value = foundFeature;
10044
- const { top, left } = viewport.getBoundingClientRect();
10045
- const params$1 = {
10046
- map: currentMap,
10047
- position: {
10048
- x: pixel[0] + left,
10049
- y: pixel[1] + top
10050
- },
10051
- coordinate: _coordinate,
10052
- feature: foundFeature,
10053
- layer: foundLayer
10054
- };
10055
- const matchedPointermove = findMatchingPointermove(params$1);
10056
- if (matchedPointermove) {
10057
- const { content: _content, cursor: _cursor, visible: _visible, fixedFeatureCenter: _fixedFeatureCenter, offset: _offset, priority: _priority, ...rest } = matchedPointermove;
10058
- option.value = { ...rest };
10059
- }
10060
- if (!matchedPointermove) {
10061
- hide();
10062
- return;
10063
- }
10176
+ const { content: _content, cursor: _cursor, visible: _visible, fixedFeatureCenter: _fixedFeatureCenter, offset: _offset, priority: _priority, hitTolerance: _hitTolerance, originalIndex, ...rest } = matchedPointermove;
10177
+ option.value = { ...rest };
10064
10178
  offset.value = {
10065
10179
  x: matchedPointermove.offset?.x ?? 0,
10066
10180
  y: matchedPointermove.offset?.y ?? 0
@@ -10075,9 +10189,21 @@ function usePointermove({ mapRef, items, forceUpdate = false }) {
10075
10189
  originalPosition.value.x = pixel[0] + left;
10076
10190
  originalPosition.value.y = pixel[1] + top;
10077
10191
  const tooltipContent = matchedPointermove.content;
10078
- content.value = typeof tooltipContent === "function" ? () => tooltipContent(params$1) : tooltipContent;
10192
+ content.value = typeof tooltipContent === "function" ? () => tooltipContent({
10193
+ map: currentMap,
10194
+ coordinate: coordinate.value,
10195
+ position: position.value,
10196
+ feature: foundFeature,
10197
+ layer: foundLayer
10198
+ }) : tooltipContent;
10079
10199
  const cursor = matchedPointermove.cursor;
10080
- const cursorStyle = typeof cursor === "function" ? cursor(params$1) : cursor;
10200
+ const cursorStyle = typeof cursor === "function" ? cursor({
10201
+ map: currentMap,
10202
+ coordinate: coordinate.value,
10203
+ position: position.value,
10204
+ feature: foundFeature,
10205
+ layer: foundLayer
10206
+ }) : cursor;
10081
10207
  if (cursorStyle !== void 0 && cursorStyle !== viewport.style.cursor) viewport.style.cursor = cursorStyle;
10082
10208
  visible.value = true;
10083
10209
  }
@@ -10145,4 +10271,4 @@ function useSwitchBaseLayer(data) {
10145
10271
  }
10146
10272
 
10147
10273
  //#endregion
10148
- export { EPSG_3395, EPSG_3857, EPSG_3857ExtentToEPSG_4326, EPSG_3857ToEPSG_4326, EPSG_4326, EPSG_4326ExtentToEPSG_3857, EPSG_4326ToEPSG_3857, ONE_NM, ol_map_default as OlMap, angleToRotation, createBingLayer, createCanvasLayer, createCircle, createCircleFeature, createCircleStyle, createFeature, createHeatmapLayer, createLineString, createLineStringFeature, createMultiLineString, createMultiLineStringFeature, createMultiPoint, createMultiPointFeature, createMultiPolygon, createMultiPolygonFeature, createOpenStreetMapLayer, createPMTilesLayer, createPoint, createPointFeature, createPolygon, createPolygonFeature, createStyle, createTextStyle, createTianDiTuLayer, createTianDiTuUrl, createTileGrid, createVectorLayer, createVectorSource, createWebGLVectorLayer, createXYZLayer, formatAngle, formatRotation, kmToNauticalMiles, nauticalMilesToKm, olMapInjectionKey, lib_default as proj4, registerEPSG_3395, rotationToAngle, useContextmenu, useDrawLineString, useDrawPolygon, useGraticule, useOlMap, usePointermove, useSwitchBaseLayer };
10274
+ export { EPSG_3395, EPSG_3857, EPSG_3857ExtentToEPSG_4326, EPSG_3857ToEPSG_4326, EPSG_4326, EPSG_4326ExtentToEPSG_3857, EPSG_4326ToEPSG_3857, ONE_NM, ol_map_default as OlMap, angleToRotation, createBingLayer, createCanvasLayer, createCircle, createCircleFeature, createCircleStyle, createFeature, createHeatmapLayer, createLineString, createLineStringFeature, createMultiLineString, createMultiLineStringFeature, createMultiPoint, createMultiPointFeature, createMultiPolygon, createMultiPolygonFeature, createOpenStreetMapLayer, createPMTilesLayer, createPoint, createPointFeature, createPolygon, createPolygonFeature, createStyle, createTextStyle, createTianDiTuLayer, createTianDiTuUrl, createTileGrid, createVectorLayer, createVectorSource, createWebGLVectorLayer, createXYZLayer, formatAngle, formatRotation, kmToNauticalMiles, nauticalMilesToKm, olMapInjectionKey, lib_default as proj4, registerEPSG_3395, rotationToAngle, useContextmenu, useDrawLineString, useDrawPolygon, useGraticule, useMapClick, useMapClickHandler, useOlMap, usePointermove, useSwitchBaseLayer };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@summeruse/ol",
3
3
  "type": "module",
4
- "version": "0.5.1",
4
+ "version": "0.6.0",
5
5
  "description": "",
6
6
  "author": "finalsummer",
7
7
  "license": "ISC",