yio-map 0.15.1 → 0.16.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/yio-map.js CHANGED
@@ -765,6 +765,18 @@ var LayerControl = class extends Control {
765
765
  }, 150);
766
766
  }
767
767
  };
768
+ // src/controls/selectableFeatures.js
769
+ /**
770
+ * The selectable features of the content map at a pixel: those with an `id` property.
771
+ * They are reported in `userSelect`; other content features (e.g. a fill without `id`) are not selectable.
772
+ * @param {import('ol/Map.js').default} map
773
+ * @param {import('ol/layer/Group.js').default} contentLayer
774
+ * @param {import('ol/pixel.js').Pixel} pixel
775
+ * @returns {Array<import('ol/Feature.js').FeatureLike>}
776
+ */
777
+ function getSelectableFeaturesAtPixel(map, contentLayer, pixel) {
778
+ return map.getFeaturesAtPixel(pixel, { layerFilter: (layer) => contentLayer.getLayers().getArray().includes(layer) }).filter((feature) => feature.get("id") !== void 0);
779
+ }
768
780
  // src/controls/UserSelectInteraction.js
769
781
  /**
770
782
  * @typedef {Object} Options
@@ -796,8 +808,7 @@ var UserSelectInteraction = class extends Interaction {
796
808
  */
797
809
  #handlePointerMove(event) {
798
810
  const map = this.getMap();
799
- const contentLayer = this.#yioMap._getContentLayer();
800
- if (map.getFeaturesAtPixel(event.pixel, { layerFilter: (layer) => contentLayer.getLayers().getArray().includes(layer) }).filter((feature) => feature.get("id") !== void 0).length) map.getTargetElement().classList.add("cursor-pointer");
811
+ if (getSelectableFeaturesAtPixel(map, this.#yioMap._getContentLayer(), event.pixel).length) map.getTargetElement().classList.add("cursor-pointer");
801
812
  else map.getTargetElement().classList.remove("cursor-pointer");
802
813
  }
803
814
  /**
@@ -805,10 +816,8 @@ var UserSelectInteraction = class extends Interaction {
805
816
  * @returns {boolean} Propageate the event further
806
817
  */
807
818
  #handleClick(event) {
808
- const map = this.getMap();
809
- const contentLayer = this.#yioMap._getContentLayer();
810
819
  const userSelectSource = this.#layer.getSource();
811
- const features = map.getFeaturesAtPixel(event.pixel, { layerFilter: (layer) => contentLayer.getLayers().getArray().includes(layer) }).filter((feature) => feature.get("id") !== void 0);
820
+ const features = getSelectableFeaturesAtPixel(this.getMap(), this.#yioMap._getContentLayer(), event.pixel);
812
821
  userSelectSource.clear();
813
822
  userSelectSource.addFeatures(features.map(
814
823
  /** @param {import('ol/render/Feature.js').default} featureOrRenderFeature */
@@ -850,11 +859,29 @@ var defaultStyles = [new Style({
850
859
  fill,
851
860
  stroke
852
861
  })];
853
- var pinStyle = new Style({ image: new Icon({
854
- opacity: 1,
855
- src: "data:image/svg+xml;utf8,\n <svg aria-hidden=\"true\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" fill=\"%23dc4b6f\" viewBox=\"0 0 24 24\">\n <path fill-rule=\"evenodd\" d=\"M11.906 1.994a8.002 8.002 0 0 1 8.09 8.421 7.996 7.996 0 0 1-1.297 3.957.996.996 0 0 1-.133.204l-.108.129c-.178.243-.37.477-.573.699l-5.112 6.224a1 1 0 0 1-1.545 0L5.982 15.26l-.002-.002a18.146 18.146 0 0 1-.309-.38l-.133-.163a.999.999 0 0 1-.13-.202 7.995 7.995 0 0 1 6.498-12.518ZM15 9.997a3 3 0 1 1-5.999 0 3 3 0 0 1 5.999 0Z\" clip-rule=\"evenodd\" />\n </svg>\n",
856
- anchor: [.5, 1]
857
- }) });
862
+ var PIN_PATH = "M11.906 1.994a8.002 8.002 0 0 1 8.09 8.421 7.996 7.996 0 0 1-1.297 3.957.996.996 0 0 1-.133.204l-.108.129c-.178.243-.37.477-.573.699l-5.112 6.224a1 1 0 0 1-1.545 0L5.982 15.26l-.002-.002a18.146 18.146 0 0 1-.309-.38l-.133-.163a.999.999 0 0 1-.13-.202 7.995 7.995 0 0 1 6.498-12.518ZM15 9.997a3 3 0 1 1-5.999 0 3 3 0 0 1 5.999 0Z";
863
+ /** Fill of the pin on a plain point (empty map, or content features that aren't selectable) */
864
+ var PIN_COLOR = "#dc4b6f";
865
+ /** Fill of the pin on a selectable feature: the blue of the lot highlight outline, so it reads as "selected" */
866
+ var PIN_COLOR_SELECTABLE = "#1D4ED8";
867
+ /**
868
+ * A map pin icon with a thin white outline, so it stays visible on dark maps (orthophoto) and on symbols of a
869
+ * similar color.
870
+ * @param {string} color
871
+ * @returns {Style}
872
+ */
873
+ function createPinStyle(color) {
874
+ const svg = `<svg aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"><path fill-rule="evenodd" clip-rule="evenodd" fill="${color}" stroke="#ffffff" stroke-width="1.25" stroke-linejoin="round" paint-order="stroke" d="${PIN_PATH}"/></svg>`;
875
+ return new Style({ image: new Icon({
876
+ opacity: 1,
877
+ src: "data:image/svg+xml;utf8," + encodeURIComponent(svg),
878
+ anchor: [.5, 1]
879
+ }) });
880
+ }
881
+ /** The pin on a plain point */
882
+ var pinStyle = createPinStyle(PIN_COLOR);
883
+ /** The pin on a selectable feature (with `enableSelect`) */
884
+ var pinSelectableStyle = createPinStyle(PIN_COLOR_SELECTABLE);
858
885
  // src/controls/UserEditInteraction.js
859
886
  /**
860
887
  * @typedef {Object} Options
@@ -1157,33 +1184,42 @@ var UserPinInteraction = class extends Interaction {
1157
1184
  }
1158
1185
  /**
1159
1186
  * @param {Array<number>} coordinate - coordinate in EPSG:3857
1187
+ * @param {boolean} [selectable] - whether the pin marks a selectable feature
1160
1188
  */
1161
- #placePin(coordinate) {
1189
+ #placePin(coordinate, selectable = false) {
1162
1190
  const source = this.#layer.getSource();
1163
1191
  source.clear();
1164
1192
  const feature = new Feature(new Point(coordinate));
1165
- feature.setStyle(pinStyle);
1193
+ feature.setStyle(selectable ? pinSelectableStyle : pinStyle);
1166
1194
  source.addFeature(feature);
1167
1195
  this.#yioMap.dispatchEvent(new CustomEvent("pin", {
1168
1196
  composed: true,
1169
1197
  bubbles: true,
1170
- detail: { coordinate: toLonLat(coordinate) }
1198
+ detail: {
1199
+ coordinate: toLonLat(coordinate),
1200
+ selectable
1201
+ }
1171
1202
  }));
1172
1203
  }
1173
1204
  /**
1174
1205
  * @param {import('ol').MapBrowserEvent} event
1175
1206
  */
1176
1207
  #handleClick(event) {
1177
- const map = this.getMap();
1178
- const contentLayer = this.#yioMap._getContentLayer();
1179
- if (map.getFeaturesAtPixel(event.pixel, { layerFilter: (layer) => contentLayer.getLayers().getArray().includes(layer) }).length === 0) this.#placePin(event.coordinate);
1208
+ const selectable = !!this.#yioMap.enableSelect && getSelectableFeaturesAtPixel(this.getMap(), this.#yioMap._getContentLayer(), event.pixel).length > 0;
1209
+ this.#placePin(event.coordinate, selectable);
1210
+ }
1211
+ /**
1212
+ * Show the pin as a plain point, e.g. when the selection is switched off. The pin stays where it is.
1213
+ */
1214
+ resetSelectable() {
1215
+ for (const feature of this.#layer.getSource().getFeatures()) feature.setStyle(pinStyle);
1180
1216
  }
1181
1217
  setActive(active) {
1182
1218
  if (!active) this.#layer.getSource().clear();
1183
1219
  super.setActive(active);
1184
1220
  }
1185
1221
  /**
1186
- * Set the pin to a specific location.
1222
+ * Set the pin to a specific location, as a plain point.
1187
1223
  * @param {Array<number>} coordinate - coordinate in EPSG:3857
1188
1224
  */
1189
1225
  setPin(coordinate) {
@@ -1209,7 +1245,7 @@ var package_default = {
1209
1245
  "type": "git",
1210
1246
  "url": "https://github.com/yiogmbh/yio-map"
1211
1247
  },
1212
- version: "0.15.1",
1248
+ version: "0.16.0",
1213
1249
  type: "module",
1214
1250
  files: ["dist"],
1215
1251
  exports: { ".": {
@@ -1828,6 +1864,7 @@ var YioMap = class extends i {
1828
1864
  set enableSelect(value) {
1829
1865
  this.#enableUserSelect = value;
1830
1866
  this.#userSelectInteraction.setActive(!!this.#enableUserSelect);
1867
+ if (!this.#enableUserSelect) this.#userPinInteraction.resetSelectable();
1831
1868
  }
1832
1869
  get enablePinning() {
1833
1870
  return this.#enablePinning;