Skip to content

Commit 2a44017

Browse files
committed
refactor: handle generic event pass-through without warnings
provide debug mode capability
1 parent d98803d commit 2a44017

21 files changed

+321
-187
lines changed

docs/.vitepress/theme/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export default {
77
async enhanceApp({ app }) {
88
if (!import.meta.env.SSR) {
99
const plugin = await import("../../../src");
10-
app.use(plugin);
10+
app.use(plugin, { debug: true });
1111
}
1212
},
1313
};

docs/get-started.md

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ npm install vue3-openlayers@next # try out the latest version (next channel)
1818

1919
To use `vue3-openlayers` in your application, you can import all components or just what you really need.
2020

21-
```js
21+
```ts
2222
import { createApp } from "vue";
2323
import App from "./App.vue";
2424

@@ -27,7 +27,23 @@ import "vue3-openlayers/styles.css"; // vue3-openlayers version < 1.0.0-*
2727
import "vue3-openlayers/dist/vue3-openlayers.css"; // vue3-openlayers version >= 1.0.0-*
2828

2929
const app = createApp(App);
30-
app.use(OpenLayersMap);
30+
app.use(OpenLayersMap /* options */);
3131

3232
app.mount("#app");
3333
```
34+
35+
## Debug Mode
36+
37+
You can activate the `debug` mode, to log events receiving from OpenLayers and props passed to OpenLayers on the console.
38+
39+
```ts
40+
import OpenLayersMap, {
41+
type Vue3OpenlayersGlobalOptions,
42+
} from "vue3-openlayers";
43+
// ...
44+
45+
const options: Vue3OpenlayersGlobalOptions = {
46+
debug: true,
47+
};
48+
app.use(OpenLayersMap, options);
49+
```

src/components/layers/OlLayerGroup.vue

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,20 @@ import LayerGroup, { type Options } from "ol/layer/Group";
1010
import type Map from "ol/Map";
1111
import usePropsAsObjectProperties from "@/composables/usePropsAsObjectProperties";
1212
import { layersCommonDefaultProps } from "@/components/layers/LayersCommonProps";
13-
import eventGateway from "@/helpers/eventGateway";
13+
import { useOpenLayersEvents } from "@/composables/useOpenLayersEvents";
14+
15+
// prevent warnings caused by event pass-through via useOpenLayersEvents composable
16+
defineOptions({
17+
inheritAttrs: false,
18+
});
1419
1520
const props = withDefaults(defineProps<Options>(), layersCommonDefaultProps);
16-
const emit = defineEmits([]);
1721
1822
const map = inject<Map>("map");
1923
const { properties } = usePropsAsObjectProperties(props);
2024
2125
const layerGroup = new LayerGroup(properties);
22-
eventGateway(emit, layerGroup, [
26+
useOpenLayersEvents(layerGroup, [
2327
"change",
2428
"change:extend",
2529
"change:layers",

src/components/sources/OlSourceBingMaps.vue

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,15 @@ import type { Ref } from "vue";
88
import { inject, watch, onMounted, onUnmounted, computed } from "vue";
99
import type TileLayer from "ol/layer/Tile";
1010
import usePropsAsObjectProperties from "@/composables/usePropsAsObjectProperties";
11-
import eventGateway, { IMAGE_SOURCE_EVENTS } from "@/helpers/eventGateway";
11+
import {
12+
IMAGE_SOURCE_EVENTS,
13+
useOpenLayersEvents,
14+
} from "@/composables/useOpenLayersEvents";
15+
16+
// prevent warnings caused by event pass-through via useOpenLayersEvents composable
17+
defineOptions({
18+
inheritAttrs: false,
19+
});
1220
1321
const props = withDefaults(
1422
defineProps<Omit<Options, "key"> & { apiKey: string }>(),
@@ -24,22 +32,20 @@ const props = withDefaults(
2432
wrapX: true,
2533
}
2634
);
27-
const emit = defineEmits([]);
2835
2936
const layer = inject<Ref<TileLayer<BingMaps>> | null>("tileLayer");
3037
3138
const { properties } = usePropsAsObjectProperties(props);
3239
33-
const source = computed(() => {
34-
const bingMaps = new BingMaps({
35-
...properties,
36-
key: properties.apiKey,
37-
});
38-
39-
eventGateway(emit, bingMaps, IMAGE_SOURCE_EVENTS);
40+
const source = computed(
41+
() =>
42+
new BingMaps({
43+
...properties,
44+
key: properties.apiKey,
45+
})
46+
);
4047
41-
return bingMaps;
42-
});
48+
useOpenLayersEvents(source, IMAGE_SOURCE_EVENTS);
4349
4450
watch(source, () => {
4551
layer?.value?.setSource(source.value);

src/components/sources/OlSourceCluster.vue

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,30 @@ import type Geometry from "ol/geom/Geometry";
1212
import type Feature from "ol/Feature";
1313
import type Point from "ol/geom/Point";
1414
import usePropsAsObjectProperties from "@/composables/usePropsAsObjectProperties";
15-
import eventGateway, { FEATURE_EVENTS } from "@/helpers/eventGateway";
15+
import {
16+
FEATURE_EVENTS,
17+
useOpenLayersEvents,
18+
} from "@/composables/useOpenLayersEvents";
19+
20+
// prevent warnings caused by event pass-through via useOpenLayersEvents composable
21+
defineOptions({
22+
inheritAttrs: false,
23+
});
1624
1725
const props = withDefaults(defineProps<Options>(), {
1826
distance: 20,
1927
geometryFunction: (feature: Feature<Geometry>): Point =>
2028
feature.getGeometry() as Point,
2129
wrapX: true,
2230
});
23-
const emit = defineEmits([]);
2431
2532
const layer = inject<Ref<Cluster> | null>("vectorLayer");
2633
2734
const { properties } = usePropsAsObjectProperties(props);
2835
29-
const source = computed(() => {
30-
const c = new Cluster(properties);
36+
const source = computed(() => new Cluster(properties));
3137
32-
eventGateway(emit, c, FEATURE_EVENTS);
33-
34-
return c;
35-
});
38+
useOpenLayersEvents(source, FEATURE_EVENTS);
3639
3740
const applySource = () => {
3841
layer?.value?.setSource(null);

src/components/sources/OlSourceImageStatic.vue

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,32 @@ import Static, { type Options } from "ol/source/ImageStatic";
66
import { inject, onMounted, onUnmounted, watch } from "vue";
77
import type ImageLayer from "ol/layer/Image";
88
import usePropsAsObjectProperties from "@/composables/usePropsAsObjectProperties";
9-
import eventGateway, { IMAGE_SOURCE_EVENTS } from "@/helpers/eventGateway";
109
import projectionFromProperties from "@/helpers/projection";
10+
import {
11+
IMAGE_SOURCE_EVENTS,
12+
useOpenLayersEvents,
13+
} from "@/composables/useOpenLayersEvents";
14+
15+
// prevent warnings caused by event pass-through via useOpenLayersEvents composable
16+
defineOptions({
17+
inheritAttrs: false,
18+
});
1119
1220
const props = withDefaults(defineProps<Options>(), {
1321
interpolate: true,
1422
});
15-
const emit = defineEmits([]);
1623
1724
const layer = inject<ImageLayer<Static> | null>("imageLayer");
1825
const { properties } = usePropsAsObjectProperties(props);
1926
20-
const createSource = () => {
21-
const s = new Static({
27+
const createSource = () =>
28+
new Static({
2229
...properties,
2330
projection: projectionFromProperties(properties.projection),
2431
});
2532
26-
eventGateway(emit, s, IMAGE_SOURCE_EVENTS);
27-
28-
return s;
29-
};
30-
3133
let source = createSource();
34+
useOpenLayersEvents(source, IMAGE_SOURCE_EVENTS);
3235
3336
watch(properties, () => {
3437
layer?.setSource(null);

src/components/sources/OlSourceImageWMS.vue

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,15 @@ import { inject, onMounted, onUnmounted, watch } from "vue";
77
import type ImageLayer from "ol/layer/Image";
88
import usePropsAsObjectProperties from "@/composables/usePropsAsObjectProperties";
99
import projectionFromProperties from "@/helpers/projection";
10-
import eventGateway, { IMAGE_SOURCE_EVENTS } from "@/helpers/eventGateway";
10+
import {
11+
IMAGE_SOURCE_EVENTS,
12+
useOpenLayersEvents,
13+
} from "@/composables/useOpenLayersEvents";
14+
15+
// prevent warnings caused by event pass-through via useOpenLayersEvents composable
16+
defineOptions({
17+
inheritAttrs: false,
18+
});
1119
1220
const props = withDefaults(
1321
defineProps<
@@ -28,13 +36,12 @@ const props = withDefaults(
2836
ratio: 1,
2937
}
3038
);
31-
const emit = defineEmits([]);
3239
3340
const layer = inject<ImageLayer<ImageWMS> | null>("imageLayer");
3441
const { properties } = usePropsAsObjectProperties(props);
3542
36-
const createSource = () => {
37-
const i = new ImageWMS({
43+
const createSource = () =>
44+
new ImageWMS({
3845
...properties,
3946
params: {
4047
...props.params,
@@ -45,12 +52,8 @@ const createSource = () => {
4552
projection: projectionFromProperties(properties.projection),
4653
});
4754
48-
eventGateway(emit, i, IMAGE_SOURCE_EVENTS);
49-
50-
return i;
51-
};
52-
5355
let source = createSource();
56+
useOpenLayersEvents(source, IMAGE_SOURCE_EVENTS);
5457
5558
watch(properties, () => {
5659
layer?.setSource(null);

src/components/sources/OlSourceOSM.vue

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,15 @@ import type { Ref } from "vue";
77
import { inject, watch, onMounted, onUnmounted, computed } from "vue";
88
import type TileLayer from "ol/layer/Tile";
99
import usePropsAsObjectProperties from "@/composables/usePropsAsObjectProperties";
10-
import eventGateway, { TILE_SOURCE_EVENTS } from "@/helpers/eventGateway";
10+
import {
11+
TILE_SOURCE_EVENTS,
12+
useOpenLayersEvents,
13+
} from "@/composables/useOpenLayersEvents";
14+
15+
// prevent warnings caused by event pass-through via useOpenLayersEvents composable
16+
defineOptions({
17+
inheritAttrs: false,
18+
});
1119
1220
const props = withDefaults(defineProps<Options>(), {
1321
crossOrigin: "anonymous",
@@ -21,19 +29,14 @@ const props = withDefaults(defineProps<Options>(), {
2129
wrapX: true,
2230
zDirection: 0,
2331
});
24-
const emit = defineEmits([]);
2532
2633
const layer = inject<Ref<TileLayer<OSM>> | null>("tileLayer");
2734
2835
const { properties } = usePropsAsObjectProperties(props);
2936
30-
const source = computed(() => {
31-
const o = new OSM(properties);
37+
const source = computed(() => new OSM(properties));
3238
33-
eventGateway(emit, o, TILE_SOURCE_EVENTS);
34-
35-
return o;
36-
});
39+
useOpenLayersEvents(source, TILE_SOURCE_EVENTS);
3740
3841
watch(source, () => {
3942
layer?.value?.setSource(source.value);

src/components/sources/OlSourceStamen.vue

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,15 @@ import Stamen, { type Options } from "ol/source/Stamen";
77
import { inject, onMounted, onUnmounted, watch, type Ref, computed } from "vue";
88
import usePropsAsObjectProperties from "@/composables/usePropsAsObjectProperties";
99
import type TileLayer from "ol/layer/Tile";
10-
import eventGateway, { TILE_SOURCE_EVENTS } from "@/helpers/eventGateway";
10+
import {
11+
TILE_SOURCE_EVENTS,
12+
useOpenLayersEvents,
13+
} from "@/composables/useOpenLayersEvents";
14+
15+
// prevent warnings caused by event pass-through via useOpenLayersEvents composable
16+
defineOptions({
17+
inheritAttrs: false,
18+
});
1119
1220
const props = withDefaults(defineProps<Options>(), {
1321
interpolate: true,
@@ -18,22 +26,18 @@ const props = withDefaults(defineProps<Options>(), {
1826
zDirection: 0,
1927
wrapX: true,
2028
});
21-
const emit = defineEmits([]);
2229
2330
const layer = inject<Ref<TileLayer<Stamen>> | null>("tileLayer");
2431
const { properties } = usePropsAsObjectProperties(props);
2532
26-
const source = computed(() => {
27-
const s = new Stamen(properties);
33+
const source = computed(() => new Stamen(properties));
2834
29-
eventGateway(emit, s, [
30-
...TILE_SOURCE_EVENTS,
31-
"propertychange",
32-
"removefeature",
33-
]);
35+
useOpenLayersEvents(source, [
36+
...TILE_SOURCE_EVENTS,
37+
"propertychange",
38+
"removefeature",
39+
]);
3440
35-
return s;
36-
});
3741
const applySource = () => {
3842
layer?.value?.setSource(null);
3943
layer?.value?.setSource(source.value);

src/components/sources/OlSourceTianDiTu.vue

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,16 @@ import { inject, watch, onMounted, onUnmounted, computed } from "vue";
77
import type TileLayer from "ol/layer/Tile";
88
import usePropsAsObjectProperties from "../../composables/usePropsAsObjectProperties";
99
import { Tianditu, type Options } from "@/components/sources/TiandituClass";
10-
import eventGateway, { TILE_SOURCE_EVENTS } from "@/helpers/eventGateway";
1110
import type { ImageTile } from "ol";
11+
import {
12+
TILE_SOURCE_EVENTS,
13+
useOpenLayersEvents,
14+
} from "@/composables/useOpenLayersEvents";
15+
16+
// prevent warnings caused by event pass-through via useOpenLayersEvents composable
17+
defineOptions({
18+
inheritAttrs: false,
19+
});
1220
1321
const props = withDefaults(defineProps<Options>(), {
1422
layerType: "img",
@@ -26,17 +34,12 @@ const props = withDefaults(defineProps<Options>(), {
2634
},
2735
wrapX: true,
2836
});
29-
const emit = defineEmits([]);
3037
3138
const layer = inject<Ref<TileLayer<Tianditu>> | null>("tileLayer");
3239
const { properties } = usePropsAsObjectProperties(props);
33-
const source = computed(() => {
34-
const t = new Tianditu(properties);
40+
const source = computed(() => new Tianditu(properties));
3541
36-
eventGateway(emit, t, TILE_SOURCE_EVENTS);
37-
38-
return t;
39-
});
42+
useOpenLayersEvents(source, TILE_SOURCE_EVENTS);
4043
4144
watch(source, () => {
4245
layer?.value?.setSource(source.value);

0 commit comments

Comments
 (0)