Skip to content

Commit 55f4c1f

Browse files
committed
feat: allow to define global options for child plugins and via provide
1 parent 11a9cd0 commit 55f4c1f

File tree

10 files changed

+97
-17
lines changed

10 files changed

+97
-17
lines changed

Diff for: docs/get-started.md

+25-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ import { Map, Layers, Sources } from 'vue3-openlayers'
8787

8888
You can activate the `debug` mode, to log events receiving from OpenLayers and props passed to OpenLayers on the console.
8989

90-
> Note: This is currently only possible when setting up vue3-openlayers globally.
90+
### Plugin Usage
9191

9292
```ts
9393
import OpenLayersMap, { type Vue3OpenlayersGlobalOptions } from "vue3-openlayers";
@@ -98,3 +98,27 @@ const options: Vue3OpenlayersGlobalOptions = {
9898
};
9999
app.use(OpenLayersMap, options);
100100
```
101+
102+
### Provide for components
103+
104+
```vue
105+
<script setup lang="ts">
106+
import { provide } from 'vue'
107+
import { Map, Layers, Sources, type Vue3OpenlayersGlobalOptions } from 'vue3-openlayers'
108+
109+
const options: Vue3OpenlayersGlobalOptions = {
110+
debug: true,
111+
};
112+
113+
provide("ol-options", options);
114+
</script>
115+
116+
<template>
117+
<Map.OlMap style="min-width: 400px; min-height: 400px">
118+
<Map.OlView :center="[40, 40]" :zoom="5" projection="EPSG:4326" />
119+
<Layers.OlTileLayer>
120+
<Sources.OlSourceOSM />
121+
</Layers.OlTileLayer>
122+
</Map.OlMap>
123+
</template>
124+
```

Diff for: src/components/animations/index.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,22 @@ import OlAnimationShake from "./OlAnimationShake.vue";
66
import OlAnimationSlide from "./OlAnimationSlide.vue";
77
import OlAnimationTeleport from "./OlAnimationTeleport.vue";
88
import OlAnimationZoom from "./OlAnimationZoom.vue";
9+
import type { Vue3OpenlayersGlobalOptions } from "@/types";
910

10-
function install(app: App) {
11+
function install(app: App, options?: Vue3OpenlayersGlobalOptions) {
1112
app.component("ol-animation-drop", OLAnimationDrop);
1213
app.component("ol-animation-fade", OlAnimationFade);
1314
app.component("ol-animation-path", OlAnimationPath);
1415
app.component("ol-animation-shake", OlAnimationShake);
1516
app.component("ol-animation-slide", OlAnimationSlide);
1617
app.component("ol-animation-teleport", OlAnimationTeleport);
1718
app.component("ol-animation-zoom", OlAnimationZoom);
19+
20+
app.provide("ol-options", options);
21+
}
22+
23+
declare module "@vue/runtime-core" {
24+
export function inject(key: "ol-options"): Vue3OpenlayersGlobalOptions;
1825
}
1926

2027
export default install;

Diff for: src/components/geometries/index.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,22 @@ import OlGeomMultiPoint from "./OlGeomMultiPoint.vue";
66
import OlGeomMultiPolygon from "./OlGeomMultiPolygon.vue";
77
import OlGeomPoint from "./OlGeomPoint.vue";
88
import OlGeomPolygon from "./OlGeomPolygon.vue";
9+
import type { Vue3OpenlayersGlobalOptions } from "@/types";
910

10-
function install(app: App) {
11+
function install(app: App, options?: Vue3OpenlayersGlobalOptions) {
1112
app.component("ol-geom-circle", OlGeomCircle);
1213
app.component("ol-geom-line-string", OlGeomLineString);
1314
app.component("ol-geom-multi-line-string", OlGeomMultiLineString);
1415
app.component("ol-geom-multi-point", OlGeomMultiPoint);
1516
app.component("ol-geom-multi-polygon", OlGeomMultiPolygon);
1617
app.component("ol-geom-point", OlGeomPoint);
1718
app.component("ol-geom-polygon", OlGeomPolygon);
19+
20+
app.provide("ol-options", options);
21+
}
22+
23+
declare module "@vue/runtime-core" {
24+
export function inject(key: "ol-options"): Vue3OpenlayersGlobalOptions;
1825
}
1926

2027
export default install;

Diff for: src/components/interaction/index.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ import OlDrawInteraction from "./OlDrawInteraction.vue";
99
import OlModifyInteraction from "./OlModifyInteraction.vue";
1010
import OlSnapInteraction from "./OlSnapInteraction.vue";
1111
import OlTransformInteraction from "./OlTransformInteraction.vue";
12+
import type { Vue3OpenlayersGlobalOptions } from "@/types";
1213

13-
function install(app: App) {
14+
function install(app: App, options?: Vue3OpenlayersGlobalOptions) {
1415
app.component("ol-interaction-clusterselect", OlClusterSelectInteraction);
1516
app.component("ol-interaction-dragbox", OlDragBoxInteraction);
1617
app.component("ol-interaction-dragrotate", OlDragRotateInteraction);
@@ -21,6 +22,12 @@ function install(app: App) {
2122
app.component("ol-interaction-modify", OlModifyInteraction);
2223
app.component("ol-interaction-snap", OlSnapInteraction);
2324
app.component("ol-interaction-transform", OlTransformInteraction);
25+
26+
app.provide("ol-options", options);
27+
}
28+
29+
declare module "@vue/runtime-core" {
30+
export function inject(key: "ol-options"): Vue3OpenlayersGlobalOptions;
2431
}
2532

2633
export default install;

Diff for: src/components/layers/index.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ import OlVectorTileLayer from "./OlVectorTileLayer.vue";
99
import OlVectorImageLayer from "./OlVectorImageLayer.vue";
1010
import OlWebglVectorLayer from "./OlWebglVectorLayer.vue";
1111
import OlWebglTileLayer from "./OlWebglTileLayer.vue";
12+
import type { Vue3OpenlayersGlobalOptions } from "@/types";
1213

13-
function install(app: App) {
14+
function install(app: App, options?: Vue3OpenlayersGlobalOptions) {
1415
app.component("ol-animated-clusterlayer", OlAnimatedClusterLayer);
1516
app.component("ol-heatmap-layer", OlHeatmapLayer);
1617
app.component("ol-image-layer", OlImageLayer);
@@ -21,6 +22,12 @@ function install(app: App) {
2122
app.component("ol-vector-tile-layer", OlVectorTileLayer);
2223
app.component("ol-webgl-vector-layer", OlWebglVectorLayer);
2324
app.component("ol-webgl-tile-layer", OlWebglTileLayer);
25+
26+
app.provide("ol-options", options);
27+
}
28+
29+
declare module "@vue/runtime-core" {
30+
export function inject(key: "ol-options"): Vue3OpenlayersGlobalOptions;
2431
}
2532

2633
export default install;

Diff for: src/components/map/index.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,21 @@ import OlMap from "./OlMap.vue";
55
import OlOverlay from "./OlOverlay.vue";
66
import OlProjectionRegister from "./OlProjectionRegister.vue";
77
import OlView from "./OlView.vue";
8+
import type { Vue3OpenlayersGlobalOptions } from "@/types";
89

9-
function install(app: App) {
10+
function install(app: App, options?: Vue3OpenlayersGlobalOptions) {
1011
app.component("ol-feature", OlFeature);
1112
app.component("ol-geolocation", OlGeoLocation);
1213
app.component("ol-map", OlMap);
1314
app.component("ol-overlay", OlOverlay);
1415
app.component("ol-projection-register", OlProjectionRegister);
1516
app.component("ol-view", OlView);
17+
18+
app.provide("ol-options", options);
19+
}
20+
21+
declare module "@vue/runtime-core" {
22+
export function inject(key: "ol-options"): Vue3OpenlayersGlobalOptions;
1623
}
1724

1825
export default install;

Diff for: src/components/mapControls/index.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ import OlZoneControl from "./OlZoneControl.vue";
2020
import OlZoomControl from "./OlZoomControl.vue";
2121
import OlZoomSliderControl from "./OlZoomSliderControl.vue";
2222
import OlZoomToExtentControl from "./OlZoomToExtentControl.vue";
23+
import type { Vue3OpenlayersGlobalOptions } from "@/types";
2324

24-
function install(app: App) {
25+
function install(app: App, options?: Vue3OpenlayersGlobalOptions) {
2526
app.component("ol-attribution-control", OlAttributionControl);
2627
app.component("ol-fullscreen-control", OlFullScreenControl);
2728
app.component("ol-mouseposition-control", OlMousePositionControl);
@@ -43,6 +44,12 @@ function install(app: App) {
4344
app.component("ol-layerswitcher-control", OlLayerSwitcherControl);
4445
app.component("ol-layerswitcherimage-control", OlLayerSwitcherImageControl);
4546
app.component("ol-zone-control", OlZoneControl);
47+
48+
app.provide("ol-options", options);
49+
}
50+
51+
declare module "@vue/runtime-core" {
52+
export function inject(key: "ol-options"): Vue3OpenlayersGlobalOptions;
4653
}
4754

4855
export default install;

Diff for: src/components/sources/index.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ import OlSourceVector from "./OlSourceVector.vue";
1515
import OlSourceVectorTile from "./OlSourceVectorTile.vue";
1616
import OlSourceXYZ from "./OlSourceXYZ.vue";
1717
import OlSourceWMTS from "./OlSourceWMTS.vue";
18+
import type { Vue3OpenlayersGlobalOptions } from "@/types";
1819

19-
function install(app: App) {
20+
function install(app: App, options?: Vue3OpenlayersGlobalOptions) {
2021
app.component("ol-source-bingmaps", OlSourceBingMaps);
2122
app.component("ol-source-cluster", OlSourceCluster);
2223
app.component("ol-source-image-static", OlSourceImageStatic);
@@ -33,6 +34,12 @@ function install(app: App) {
3334
app.component("ol-source-vector-tile", OlSourceVectorTile);
3435
app.component("ol-source-xyz", OlSourceXYZ);
3536
app.component("ol-source-wmts", OlSourceWMTS);
37+
38+
app.provide("ol-options", options);
39+
}
40+
41+
declare module "@vue/runtime-core" {
42+
export function inject(key: "ol-options"): Vue3OpenlayersGlobalOptions;
3643
}
3744

3845
export default install;

Diff for: src/components/styles/index.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,28 @@ import OlStyleText from "./OlStyleText.vue";
88
import OlStyleFlowline from "./OlStyleFlowline.vue";
99
import type { FeatureLike } from "ol/Feature";
1010
import type { Style } from "ol/style";
11+
import type { Vue3OpenlayersGlobalOptions } from "@/types";
1112

1213
type OverrideStyleFunction = (
1314
feature: FeatureLike,
1415
currentStyle: Style,
1516
resolution: number,
1617
) => Style | Style[] | void;
1718

18-
function install(app: App) {
19+
function install(app: App, options?: Vue3OpenlayersGlobalOptions) {
1920
app.component("ol-style", OlStyle);
2021
app.component("ol-style-circle", OlStyleCircle);
2122
app.component("ol-style-stroke", OlStyleStroke);
2223
app.component("ol-style-fill", OlStyleFill);
2324
app.component("ol-style-icon", OlStyleIcon);
2425
app.component("ol-style-text", OlStyleText);
2526
app.component("ol-style-flowline", OlStyleFlowline);
27+
28+
app.provide("ol-options", options);
29+
}
30+
31+
declare module "@vue/runtime-core" {
32+
export function inject(key: "ol-options"): Vue3OpenlayersGlobalOptions;
2633
}
2734

2835
export default install;

Diff for: src/index.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ import type { App, Plugin } from "vue";
1717
import type { Vue3OpenlayersGlobalOptions } from "./types";
1818

1919
const install: Plugin = (app: App, options?: Vue3OpenlayersGlobalOptions) => {
20-
app.use(Map.install);
21-
app.use(Layers.install);
22-
app.use(Sources.install);
23-
app.use(MapControls.install);
24-
app.use(Geometries.install);
25-
app.use(Styles.install);
26-
app.use(Interactions.install);
27-
app.use(Animations.install);
20+
app.use(Map.install, options);
21+
app.use(Layers.install, options);
22+
app.use(Sources.install, options);
23+
app.use(MapControls.install, options);
24+
app.use(Geometries.install, options);
25+
app.use(Styles.install, options);
26+
app.use(Interactions.install, options);
27+
app.use(Animations.install, options);
2828
app.use(Providers.install, options);
2929
};
3030

0 commit comments

Comments
 (0)