@aibee/crc-bmap 0.0.91 → 0.0.92

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/lib/bmap.esm.js CHANGED
@@ -904,9 +904,11 @@ import { EventDispatcher as EventDispatcher2, Vector3 as Vector35 } from "three"
904
904
 
905
905
  // src/elements/overlay.ts
906
906
  import { Box3 as Box32, EventDispatcher, Vector3 as Vector34 } from "three";
907
+ import { debounce } from "lodash";
907
908
  var defaultOptions2 = {
908
909
  autoUpdate: true,
909
- appendToBody: false
910
+ appendToBody: false,
911
+ autoChangePlacement: false
910
912
  };
911
913
  var Overlay = class extends EventDispatcher {
912
914
  constructor(context, options = {}) {
@@ -918,8 +920,14 @@ var Overlay = class extends EventDispatcher {
918
920
  __publicField(this, "clientPos", { x: 0, y: 0 });
919
921
  __publicField(this, "visible", true);
920
922
  __publicField(this, "options");
923
+ __publicField(this, "placement", "top");
924
+ __publicField(this, "observer", null);
921
925
  __publicField(this, "onUpdate", () => {
922
- this.updatePosition();
926
+ if (this.options.autoChangePlacement) {
927
+ this.usePlacement();
928
+ } else {
929
+ this.updatePosition();
930
+ }
923
931
  });
924
932
  this.options = __spreadValues(__spreadValues({}, defaultOptions2), options);
925
933
  this.registryEvent();
@@ -930,13 +938,175 @@ var Overlay = class extends EventDispatcher {
930
938
  this.context.container.appendChild(this.div);
931
939
  }
932
940
  }
941
+ initObserver() {
942
+ const observer = new MutationObserver(
943
+ debounce(() => {
944
+ this.usePlacement();
945
+ }, 100)
946
+ );
947
+ observer.observe(this.div, {
948
+ childList: true,
949
+ subtree: true,
950
+ attributes: true
951
+ });
952
+ this.observer = observer;
953
+ }
954
+ getPlacementPosition() {
955
+ const { max, min } = new Box32().setFromObject(this.element);
956
+ const centerX = (max.x + min.x) / 2;
957
+ const centerY = (max.y + min.y) / 2;
958
+ return {
959
+ left: new Vector34(min.x, centerY, max.z),
960
+ leftTop: new Vector34(min.x, max.y, max.z),
961
+ top: new Vector34(centerX, max.y, max.z),
962
+ rightTop: new Vector34(max.x, max.y, max.z),
963
+ right: new Vector34(max.x, centerY, max.z),
964
+ rightBottom: new Vector34(max.x, min.y, max.z),
965
+ bottom: new Vector34(centerX, min.y, max.z),
966
+ leftBottom: new Vector34(min.x, min.y, max.z),
967
+ center: new Vector34(centerX, centerY, max.z)
968
+ };
969
+ }
970
+ getPlacementScreenPosition() {
971
+ const { width, height } = this.context.clientSize;
972
+ const positions = this.getPlacementPosition();
973
+ return Object.keys(positions).reduce((obj, key) => {
974
+ const { x, y } = vector3ToDevice(
975
+ positions[key],
976
+ this.context.camera,
977
+ width,
978
+ height
979
+ );
980
+ obj[key] = { x, y };
981
+ return obj;
982
+ }, {});
983
+ }
984
+ // 检查是不是溢出了context.container
985
+ // @return true 表示没有溢出
986
+ checkOverflow(max, min) {
987
+ const { width, height } = this.context.clientSize;
988
+ return min.x > 0 && max.x < width && min.y < height && max.y > 0;
989
+ }
990
+ // 计算适用的placement
991
+ getPlacement() {
992
+ const { clientWidth, clientHeight } = this.div;
993
+ const positions = this.getPlacementScreenPosition();
994
+ const halfWidth = clientWidth / 2;
995
+ const halfHeight = clientHeight / 2;
996
+ const placements = [
997
+ {
998
+ type: "center",
999
+ getBox(x, y) {
1000
+ return {
1001
+ max: { x: halfWidth + x, y: y - clientHeight },
1002
+ min: { x: x - halfWidth, y }
1003
+ };
1004
+ }
1005
+ },
1006
+ {
1007
+ type: "left",
1008
+ getBox(x, y) {
1009
+ return {
1010
+ max: { x, y: y - halfHeight },
1011
+ min: { x: x - clientWidth, y: y + halfHeight }
1012
+ };
1013
+ }
1014
+ },
1015
+ {
1016
+ type: "leftTop",
1017
+ getBox(x, y) {
1018
+ return {
1019
+ max: { x, y: y - clientHeight },
1020
+ min: { x: x - clientWidth, y }
1021
+ };
1022
+ }
1023
+ },
1024
+ {
1025
+ type: "top",
1026
+ getBox(x, y) {
1027
+ return {
1028
+ max: { x: x + halfWidth, y: y - clientHeight },
1029
+ min: { x: x - halfWidth, y }
1030
+ };
1031
+ }
1032
+ },
1033
+ {
1034
+ type: "rightTop",
1035
+ getBox(x, y) {
1036
+ return {
1037
+ max: { x: x + clientWidth, y: y - clientHeight },
1038
+ min: { x, y }
1039
+ };
1040
+ }
1041
+ },
1042
+ {
1043
+ type: "right",
1044
+ getBox(x, y) {
1045
+ return {
1046
+ max: { x: x + clientWidth, y: y - halfHeight },
1047
+ min: { x, y: y + halfHeight }
1048
+ };
1049
+ }
1050
+ },
1051
+ {
1052
+ type: "rightBottom",
1053
+ getBox(x, y) {
1054
+ return {
1055
+ max: { x: x + clientWidth, y },
1056
+ min: { x, y: y + clientHeight }
1057
+ };
1058
+ }
1059
+ },
1060
+ {
1061
+ type: "bottom",
1062
+ getBox(x, y) {
1063
+ return {
1064
+ max: { x: x + halfWidth, y },
1065
+ min: { x: x - halfWidth, y: y + clientHeight }
1066
+ };
1067
+ }
1068
+ },
1069
+ {
1070
+ type: "leftBottom",
1071
+ getBox(x, y) {
1072
+ return {
1073
+ max: { x, y },
1074
+ min: { x: x - clientWidth, y: y + halfHeight }
1075
+ };
1076
+ }
1077
+ }
1078
+ ];
1079
+ for (let i = 0; i < placements.length; i++) {
1080
+ const placement = placements[i];
1081
+ const position = positions[placement.type];
1082
+ const { max, min } = placement.getBox(position.x, position.y);
1083
+ if (this.checkOverflow(max, min)) {
1084
+ return {
1085
+ type: placement.type,
1086
+ position
1087
+ };
1088
+ }
1089
+ }
1090
+ return {
1091
+ type: "center",
1092
+ position: positions.center
1093
+ };
1094
+ }
933
1095
  initDiv() {
934
1096
  const div = document.createElement("div");
935
1097
  div.style.position = "absolute";
936
1098
  return div;
937
1099
  }
1100
+ usePlacement() {
1101
+ const placement = this.getPlacement();
1102
+ this.div.className = `overlay_${placement.type}`;
1103
+ this._updatePosition(placement.position.x, placement.position.y);
1104
+ }
938
1105
  bindElement(element) {
939
1106
  this.element = element;
1107
+ if (this.options.autoChangePlacement) {
1108
+ this.initObserver();
1109
+ }
940
1110
  this.onUpdate();
941
1111
  }
942
1112
  unBindElement() {
@@ -969,14 +1139,9 @@ var Overlay = class extends EventDispatcher {
969
1139
  const { width, height } = this.context.clientSize;
970
1140
  return x >= 0 && x <= width && y >= 0 && y <= height;
971
1141
  }
972
- updatePosition(force = false) {
973
- const vector = this.getPosition();
974
- const { width, height, x: clientX, y: clientY } = this.context.clientSize;
975
- const { x, y } = vector3ToDevice(vector, this.context.camera, width, height);
976
- if (this.clientPos.x === x && this.clientPos.y === y && !force) {
977
- return;
978
- }
1142
+ _updatePosition(x, y) {
979
1143
  this.clientPos = { x, y };
1144
+ const { width, height, x: clientX, y: clientY } = this.context.clientSize;
980
1145
  if (this.options.appendToBody) {
981
1146
  this.div.style.left = `${clientX}px`;
982
1147
  this.div.style.top = `${clientY + height}px`;
@@ -987,6 +1152,20 @@ var Overlay = class extends EventDispatcher {
987
1152
  this.dispatchEvent({ type: "update-position", x, y, width, height });
988
1153
  }
989
1154
  }
1155
+ updatePosition(force = false) {
1156
+ const vector = this.getPosition();
1157
+ const { width, height, x: clientX, y: clientY } = this.context.clientSize;
1158
+ const { x, y } = vector3ToDevice(
1159
+ vector,
1160
+ this.context.camera,
1161
+ width,
1162
+ height
1163
+ );
1164
+ if (this.clientPos.x === x && this.clientPos.y === y && !force) {
1165
+ return;
1166
+ }
1167
+ this._updatePosition(x, y);
1168
+ }
990
1169
  registryEvent() {
991
1170
  this.context.addEventListener("update", this.onUpdate);
992
1171
  }
@@ -994,10 +1173,11 @@ var Overlay = class extends EventDispatcher {
994
1173
  this.context.removeEventListener("update", this.onUpdate);
995
1174
  }
996
1175
  dispose() {
997
- var _a;
1176
+ var _a, _b;
998
1177
  this.unRegistryEvent();
999
1178
  this.unBindElement();
1000
- (_a = this.div) == null ? void 0 : _a.remove();
1179
+ (_a = this.observer) == null ? void 0 : _a.disconnect();
1180
+ (_b = this.div) == null ? void 0 : _b.remove();
1001
1181
  this.div = null;
1002
1182
  }
1003
1183
  };
@@ -1353,7 +1533,7 @@ var GraphicLayer = class extends Layer {
1353
1533
  };
1354
1534
 
1355
1535
  // src/layer/poi-layer.ts
1356
- import { debounce } from "lodash";
1536
+ import { debounce as debounce2 } from "lodash";
1357
1537
  var PoiLayer = class extends Layer {
1358
1538
  constructor(context) {
1359
1539
  super(context);
@@ -1366,7 +1546,7 @@ var PoiLayer = class extends Layer {
1366
1546
  });
1367
1547
  });
1368
1548
  this.registryEvent();
1369
- this.debounceCollisionDetection = debounce(this.collisionDetection, 10);
1549
+ this.debounceCollisionDetection = debounce2(this.collisionDetection, 10);
1370
1550
  }
1371
1551
  clear(force = false) {
1372
1552
  this.pois.forEach((item) => {
@@ -3207,7 +3387,7 @@ function getConfig(config) {
3207
3387
  }
3208
3388
 
3209
3389
  // src/bmap.ts
3210
- import { debounce as debounce2 } from "lodash";
3390
+ import { debounce as debounce3 } from "lodash";
3211
3391
  var MapTypePolar = /* @__PURE__ */ ((MapTypePolar2) => {
3212
3392
  MapTypePolar2[MapTypePolar2["D2"] = 0] = "D2";
3213
3393
  MapTypePolar2[MapTypePolar2["D3"] = 1.1] = "D3";
@@ -3579,7 +3759,7 @@ var BMap = class extends EventDispatcher7 {
3579
3759
  window.addEventListener("keydown", this.onKeydown);
3580
3760
  window.addEventListener("keyup", this.onKeyUp);
3581
3761
  if (this.config.resizeObserver) {
3582
- const observe = new ResizeObserver(debounce2(this.resize, 5));
3762
+ const observe = new ResizeObserver(debounce3(this.resize, 5));
3583
3763
  observe.observe(this.container);
3584
3764
  this.observe = observe;
3585
3765
  }