Skip to content

Commit 30fbcf7

Browse files
committed
feat(ol-interaction-modify): make source injection optional
closes #368
1 parent a43bbe9 commit 30fbcf7

File tree

4 files changed

+108
-30
lines changed

4 files changed

+108
-30
lines changed

docs/componentsguide/interactions/modify/index.md

+25-6
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,10 @@
77
## Demo
88

99
<script setup>
10-
import SnapModifyDemo from "@demos/SnapModifyDemo.vue"
10+
import ModifySourceDemo from "@demos/ModifySourceDemo.vue";
11+
import SnapModifyDemo from "@demos/SnapModifyDemo.vue";
1112
</script>
1213

13-
<ClientOnly>
14-
<SnapModifyDemo/>
15-
</ClientOnly>
16-
1714
## Setup
1815

1916
<!--@include: ../../interactions.plugin.md-->
@@ -24,6 +21,24 @@ import SnapModifyDemo from "@demos/SnapModifyDemo.vue"
2421
| ------------------------- | :----------------------------------: |
2522
| `<ol-interaction-modify>` | `<Interactions.OlInteractionModify>` |
2623

24+
### Modify the source vector components features
25+
26+
<ClientOnly>
27+
<ModifySourceDemo/>
28+
</ClientOnly>
29+
30+
::: code-group
31+
32+
<<< ../../../../src/demos/ModifySourceDemo.vue
33+
34+
:::
35+
36+
### Modify passed `features`
37+
38+
<ClientOnly>
39+
<SnapModifyDemo/>
40+
</ClientOnly>
41+
2742
::: code-group
2843

2944
<<< ../../../../src/demos/SnapModifyDemo.vue
@@ -58,6 +73,10 @@ import SnapModifyDemo from "@demos/SnapModifyDemo.vue"
5873

5974
- **Type**: `Boolean`
6075

61-
### features
76+
### features
6277

6378
- **Type**: `[Collection,Object]`
79+
80+
### source
81+
82+
- **Type**: `VectorSource`

src/components/interaction/OlInteractionModify.vue

+14-8
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
<script setup lang="ts">
66
import type { Ref } from "vue";
77
import {
8-
provide,
98
inject,
10-
watch,
119
onMounted,
1210
onUnmounted,
13-
toRefs,
11+
provide,
1412
ref,
13+
toRefs,
14+
watch,
1515
} from "vue";
1616
import type Collection from "ol/Collection";
1717
import Modify from "ol/interaction/Modify";
@@ -36,6 +36,7 @@ const props = withDefaults(
3636
wrapX?: boolean;
3737
hitDetection?: boolean;
3838
features?: Collection<Feature<Geometry>>;
39+
source?: VectorSource;
3940
}>(),
4041
{
4142
pixelTolerance: 10,
@@ -44,9 +45,10 @@ const props = withDefaults(
4445
);
4546
4647
const map = inject<Map>("map");
47-
const source = inject<Ref<VectorSource> | null>("vectorSource");
48+
const vectorSource = inject<Ref<VectorSource> | null>("vectorSource", null);
4849
4950
const {
51+
source,
5052
features,
5153
condition,
5254
deleteCondition,
@@ -57,8 +59,14 @@ const {
5759
} = toRefs(props);
5860
5961
function createModify() {
60-
const m = new Modify({
61-
source: source?.value,
62+
if (!source?.value || !features.value) {
63+
console.error(
64+
`[Vue3-OpenLayers Error] OlInteractionModify: Modify interactions needs either a either a source or features to work.
65+
Please provide either the props 'source' or 'feature' or use the component with an '<OlSourceVector>' component.`,
66+
);
67+
}
68+
return new Modify({
69+
source: source?.value || vectorSource?.value,
6270
features: features?.value,
6371
condition: condition?.value,
6472
deleteCondition: deleteCondition?.value,
@@ -67,8 +75,6 @@ function createModify() {
6775
wrapX: wrapX.value,
6876
hitDetection: hitDetection.value,
6977
});
70-
71-
return m;
7278
}
7379
7480
let modify = createModify();

src/demos/ModifySourceDemo.vue

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<template>
2+
<ol-map style="height: 400px">
3+
<ol-view :center="center" :zoom="5" :projection="projection" />
4+
5+
<ol-tile-layer>
6+
<ol-source-osm />
7+
</ol-tile-layer>
8+
9+
<ol-vector-layer>
10+
<ol-source-vector :features="zones">
11+
<ol-style>
12+
<ol-style-stroke color="blue" :width="3" />
13+
<ol-style-fill color="rgba(0, 0, 255, 0.4)" />
14+
</ol-style>
15+
<ol-interaction-modify />
16+
</ol-source-vector>
17+
</ol-vector-layer>
18+
</ol-map>
19+
</template>
20+
21+
<script setup>
22+
import { ref } from "vue";
23+
import { GeoJSON } from "ol/format";
24+
25+
const center = ref([-102.13121, 40.2436]);
26+
const projection = ref("EPSG:4326");
27+
28+
const zones = ref([]);
29+
30+
zones.value = new GeoJSON().readFeatures({
31+
type: "FeatureCollection",
32+
crs: {
33+
type: "name",
34+
properties: {
35+
name: projection.value,
36+
},
37+
},
38+
features: [
39+
{
40+
type: "Feature",
41+
geometry: {
42+
type: "Polygon",
43+
coordinates: [
44+
[
45+
[-103.85636392303468, 38.10970692739486],
46+
[-103.86770698495866, 33.218572724914544],
47+
[-98.20654544301988, 33.6532810221672],
48+
[-98.4408283538437, 38.31894739375114],
49+
[-103.85636392303468, 38.10970692739486],
50+
],
51+
],
52+
},
53+
},
54+
],
55+
});
56+
</script>

src/demos/SnapModifyDemo.vue

+13-16
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,6 @@
2121

2222
<ol-vector-layer>
2323
<ol-source-vector :features="zones">
24-
<ol-interaction-modify
25-
v-if="modifyEnabled"
26-
:features="selectedFeatures"
27-
>
28-
<ol-style>
29-
<ol-style-circle :radius="5">
30-
<ol-style-fill color="#00dd11" />
31-
<ol-style-stroke color="blue" :width="2" />
32-
</ol-style-circle>
33-
</ol-style>
34-
</ol-interaction-modify>
3524
<ol-interaction-draw
3625
v-if="drawEnabled"
3726
:stopClick="true"
@@ -40,26 +29,34 @@
4029
/>
4130
<ol-interaction-snap v-if="modifyEnabled || drawEnabled" />
4231
<ol-style>
43-
<ol-style-stroke color="blue" :width="3"></ol-style-stroke>
44-
<ol-style-fill color="rgba(0, 0, 255, 0.4)"></ol-style-fill>
32+
<ol-style-stroke color="blue" :width="3" />
33+
<ol-style-fill color="rgba(0, 0, 255, 0.4)" />
4534
</ol-style>
4635
</ol-source-vector>
36+
<ol-interaction-modify v-if="modifyEnabled" :features="selectedFeatures">
37+
<ol-style>
38+
<ol-style-circle :radius="5">
39+
<ol-style-fill color="#00dd11" />
40+
<ol-style-stroke color="blue" :width="2" />
41+
</ol-style-circle>
42+
</ol-style>
43+
</ol-interaction-modify>
4744
</ol-vector-layer>
4845
<ol-interaction-select
4946
@select="featureSelected"
5047
:condition="selectCondition"
5148
:features="selectedFeatures"
5249
>
5350
<ol-style>
54-
<ol-style-stroke color="red" :width="2"></ol-style-stroke>
55-
<ol-style-fill color="rgba(255, 0, 0, 0.4)"></ol-style-fill>
51+
<ol-style-stroke color="red" :width="2" />
52+
<ol-style-fill color="rgba(255, 0, 0, 0.4)" />
5653
</ol-style>
5754
</ol-interaction-select>
5855
</ol-map>
5956
</template>
6057

6158
<script setup>
62-
import { ref, inject } from "vue";
59+
import { inject, ref } from "vue";
6360
import { GeoJSON } from "ol/format";
6461
import { Collection } from "ol";
6562

0 commit comments

Comments
 (0)